@@ -442,17 +442,20 @@ func (r *ReplicaSetReconciler) deployMongoDBReplicaSet(mdb mdbv1.MongoDBCommunit
442442// The Service definition is built from the `mdb` resource. If `isArbiter` is set to true, the Service
443443// will be created for the arbiters Statefulset.
444444func (r * ReplicaSetReconciler ) ensureService (mdb mdbv1.MongoDBCommunity ) error {
445- name := mdb .ServiceName ()
445+ processPortManager , err := r .createProcessPortManager (mdb )
446+ if err != nil {
447+ return err
448+ }
446449
447- svc := & corev1.Service {ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : mdb .Namespace }}
450+ svc := & corev1.Service {ObjectMeta : metav1.ObjectMeta {Name : mdb . ServiceName () , Namespace : mdb .Namespace }}
448451 op , err := controllerutil .CreateOrUpdate (context .TODO (), r .client , svc , func () error {
449452 resourceVersion := svc .ResourceVersion // Save resourceVersion for later
450- * svc = buildService (mdb )
453+ * svc = r . buildService (mdb , processPortManager )
451454 svc .ResourceVersion = resourceVersion
452455 return nil
453456 })
454457 if err != nil {
455- r .log .Infof ( "Cloud not create or patch the service: %s" , err )
458+ r .log .Errorf ( "Could not create or patch the service: %s" , err )
456459 return nil
457460 }
458461
@@ -461,12 +464,29 @@ func (r *ReplicaSetReconciler) ensureService(mdb mdbv1.MongoDBCommunity) error {
461464 return err
462465}
463466
467+ // createProcessPortManager is a helper method for creating new ReplicaSetPortManager.
468+ // ReplicaSetPortManager needs current automation config and current pod state and the code for getting them
469+ // was extracted here as it is used in ensureService and buildAutomationConfig.
470+ func (r * ReplicaSetReconciler ) createProcessPortManager (mdb mdbv1.MongoDBCommunity ) (* agent.ReplicaSetPortManager , error ) {
471+ currentAC , err := automationconfig .ReadFromSecret (r .client , types.NamespacedName {Name : mdb .AutomationConfigSecretName (), Namespace : mdb .Namespace })
472+ if err != nil {
473+ return nil , errors .Errorf ("could not read existing automation config: %s" , err )
474+ }
475+
476+ currentPodStates , err := agent .GetAllDesiredMembersAndArbitersPodState (mdb .NamespacedName (), r .client , mdb .StatefulSetReplicasThisReconciliation (), mdb .StatefulSetArbitersThisReconciliation (), currentAC .Version , r .log )
477+ if err != nil {
478+ return nil , fmt .Errorf ("cannot get all pods goal state: %w" , err )
479+ }
480+
481+ return agent .NewReplicaSetPortManager (r .log , mdb .Spec .AdditionalMongodConfig .GetDBPort (), currentPodStates , currentAC .Processes ), nil
482+ }
483+
464484func (r * ReplicaSetReconciler ) createOrUpdateStatefulSet (mdb mdbv1.MongoDBCommunity , isArbiter bool ) error {
465485 set := appsv1.StatefulSet {}
466486
467487 name := mdb .NamespacedName ()
468488 if isArbiter {
469- name . Name = name . Name + "-arb"
489+ name = mdb . ArbiterNamespacedName ()
470490 }
471491
472492 err := r .client .Get (context .TODO (), name , & set )
@@ -528,41 +548,37 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
528548 SetOptions (automationconfig.Options {DownloadBase : "/var/lib/mongodb-mms-automation" }).
529549 SetAuth (auth ).
530550 SetDataDir (mdb .GetMongodConfiguration ().GetDBDataDir ()).
531- SetPort (mdb .GetMongodConfiguration ().GetDBPort ()).
532551 AddModifications (getMongodConfigModification (mdb )).
533552 AddModifications (modifications ... ).
534553 Build ()
535554}
536555
537556// buildService creates a Service that will be used for the Replica Set StatefulSet
538557// that allows all the members of the STS to see each other.
539- func buildService (mdb mdbv1.MongoDBCommunity ) corev1.Service {
558+ func ( r * ReplicaSetReconciler ) buildService (mdb mdbv1.MongoDBCommunity , portManager * agent. ReplicaSetPortManager ) corev1.Service {
540559 label := make (map [string ]string )
541560 name := mdb .ServiceName ()
542561
543562 label ["app" ] = name
544563
545- return service .Builder ().
564+ serviceBuilder := service .Builder ().
546565 SetName (name ).
547566 SetNamespace (mdb .Namespace ).
548567 SetSelector (label ).
549568 SetLabels (label ).
550569 SetServiceType (corev1 .ServiceTypeClusterIP ).
551570 SetClusterIP ("None" ).
552571 SetPublishNotReadyAddresses (true ).
553- SetOwnerReferences (mdb .GetOwnerReferences ()).
554- AddPort (mongoDBPort (mdb )).
555- AddPort (prometheusPort (mdb )).
556- Build ()
557- }
572+ SetOwnerReferences (mdb .GetOwnerReferences ())
558573
559- // mongoDBPort returns a `corev1.ServicePort` to be configured in the StatefulSet
560- // for this MongoDB resource.
561- func mongoDBPort (mdb mdbv1.MongoDBCommunity ) * corev1.ServicePort {
562- return & corev1.ServicePort {
563- Port : int32 (mdb .GetMongodConfiguration ().GetDBPort ()),
564- Name : "mongodb" ,
574+ for _ , servicePort := range portManager .GetServicePorts () {
575+ tmpServicePort := servicePort
576+ serviceBuilder .AddPort (& tmpServicePort )
565577 }
578+
579+ serviceBuilder .AddPort (prometheusPort (mdb ))
580+
581+ return serviceBuilder .Build ()
566582}
567583
568584// validateSpec checks if the MongoDB resource Spec is valid.
@@ -627,14 +643,21 @@ func (r ReplicaSetReconciler) buildAutomationConfig(mdb mdbv1.MongoDBCommunity)
627643 }
628644 }
629645
646+ processPortManager , err := r .createProcessPortManager (mdb )
647+ if err != nil {
648+ return automationconfig.AutomationConfig {}, err
649+ }
650+
630651 automationConfig , err := buildAutomationConfig (
631652 mdb ,
632653 auth ,
633654 currentAC ,
634655 tlsModification ,
635656 customRolesModification ,
636657 prometheusModification ,
658+ processPortManager .GetPortsModification (),
637659 )
660+
638661 if err != nil {
639662 return automationconfig.AutomationConfig {}, errors .Errorf ("could not create an automation config: %s" , err )
640663 }
@@ -646,7 +669,7 @@ func (r ReplicaSetReconciler) buildAutomationConfig(mdb mdbv1.MongoDBCommunity)
646669 return automationConfig , nil
647670}
648671
649- // overrideToAutomationConfig turns an automation config ovverride from the resource spec into an automation config
672+ // overrideToAutomationConfig turns an automation config override from the resource spec into an automation config
650673// which can be used to merge.
651674func overrideToAutomationConfig (override mdbv1.AutomationConfigOverride ) automationconfig.AutomationConfig {
652675 var processes []automationconfig.Process
@@ -703,7 +726,7 @@ func buildArbitersModificationFunction(mdb mdbv1.MongoDBCommunity) statefulset.M
703726 return statefulset .Apply (
704727 statefulset .WithReplicas (mdb .StatefulSetArbitersThisReconciliation ()),
705728 statefulset .WithServiceName (mdb .ServiceName ()),
706- statefulset .WithName (mdb .Name + "-arb" ),
729+ statefulset .WithName (mdb .ArbiterNamespacedName (). Name ),
707730 )
708731}
709732
0 commit comments