@@ -12,6 +12,7 @@ import (
1212 "github.com/mongodb/mongodb-kubernetes-operator/controllers/predicates"
1313 "sigs.k8s.io/controller-runtime/pkg/builder"
1414 "sigs.k8s.io/controller-runtime/pkg/controller"
15+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1516 "sigs.k8s.io/controller-runtime/pkg/source"
1617
1718 "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/container"
@@ -46,6 +47,7 @@ import (
4647 appsv1 "k8s.io/api/apps/v1"
4748 corev1 "k8s.io/api/core/v1"
4849 apiErrors "k8s.io/apimachinery/pkg/api/errors"
50+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4951 "k8s.io/apimachinery/pkg/runtime"
5052 "k8s.io/apimachinery/pkg/types"
5153 ctrl "sigs.k8s.io/controller-runtime"
@@ -453,13 +455,25 @@ func (r *ReplicaSetReconciler) deployMongoDBReplicaSet(mdb mdbv1.MongoDBCommunit
453455// The Service definition is built from the `mdb` resource. If `isArbiter` is set to true, the Service
454456// will be created for the arbiters Statefulset.
455457func (r * ReplicaSetReconciler ) ensureService (mdb mdbv1.MongoDBCommunity , isArbiter bool ) error {
456- svc := buildService (mdb , isArbiter )
457- err := r .client .Create (context .TODO (), & svc )
458- if err != nil && apiErrors .IsAlreadyExists (err ) {
459- r .log .Infof ("The service already exists... moving forward: %s" , err )
458+ name := mdb .ServiceName ()
459+ if isArbiter {
460+ name = mdb .ArbiterServiceName ()
461+ }
462+
463+ svc := & corev1.Service {ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : mdb .Namespace }}
464+ op , err := controllerutil .CreateOrUpdate (context .TODO (), r .client , svc , func () error {
465+ resourceVersion := svc .ResourceVersion // Save resourceVersion for later
466+ * svc = buildService (mdb , isArbiter )
467+ svc .ResourceVersion = resourceVersion
468+ return nil
469+ })
470+ if err != nil {
471+ r .log .Infof ("Cloud not create or patch the service: %s" , err )
460472 return nil
461473 }
462474
475+ r .log .Infow ("Create/Update operation succeeded" , "operation" , op )
476+
463477 return err
464478}
465479
0 commit comments