@@ -442,17 +442,20 @@ func (r *ReplicaSetReconciler) deployMongoDBReplicaSet(mdb mdbv1.MongoDBCommunit
442
442
// The Service definition is built from the `mdb` resource. If `isArbiter` is set to true, the Service
443
443
// will be created for the arbiters Statefulset.
444
444
func (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
+ }
446
449
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 }}
448
451
op , err := controllerutil .CreateOrUpdate (context .TODO (), r .client , svc , func () error {
449
452
resourceVersion := svc .ResourceVersion // Save resourceVersion for later
450
- * svc = buildService (mdb )
453
+ * svc = r . buildService (mdb , processPortManager )
451
454
svc .ResourceVersion = resourceVersion
452
455
return nil
453
456
})
454
457
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 )
456
459
return nil
457
460
}
458
461
@@ -461,12 +464,29 @@ func (r *ReplicaSetReconciler) ensureService(mdb mdbv1.MongoDBCommunity) error {
461
464
return err
462
465
}
463
466
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
+
464
484
func (r * ReplicaSetReconciler ) createOrUpdateStatefulSet (mdb mdbv1.MongoDBCommunity , isArbiter bool ) error {
465
485
set := appsv1.StatefulSet {}
466
486
467
487
name := mdb .NamespacedName ()
468
488
if isArbiter {
469
- name . Name = name . Name + "-arb"
489
+ name = mdb . ArbiterNamespacedName ()
470
490
}
471
491
472
492
err := r .client .Get (context .TODO (), name , & set )
@@ -528,41 +548,37 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
528
548
SetOptions (automationconfig.Options {DownloadBase : "/var/lib/mongodb-mms-automation" }).
529
549
SetAuth (auth ).
530
550
SetDataDir (mdb .GetMongodConfiguration ().GetDBDataDir ()).
531
- SetPort (mdb .GetMongodConfiguration ().GetDBPort ()).
532
551
AddModifications (getMongodConfigModification (mdb )).
533
552
AddModifications (modifications ... ).
534
553
Build ()
535
554
}
536
555
537
556
// buildService creates a Service that will be used for the Replica Set StatefulSet
538
557
// 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 {
540
559
label := make (map [string ]string )
541
560
name := mdb .ServiceName ()
542
561
543
562
label ["app" ] = name
544
563
545
- return service .Builder ().
564
+ serviceBuilder := service .Builder ().
546
565
SetName (name ).
547
566
SetNamespace (mdb .Namespace ).
548
567
SetSelector (label ).
549
568
SetLabels (label ).
550
569
SetServiceType (corev1 .ServiceTypeClusterIP ).
551
570
SetClusterIP ("None" ).
552
571
SetPublishNotReadyAddresses (true ).
553
- SetOwnerReferences (mdb .GetOwnerReferences ()).
554
- AddPort (mongoDBPort (mdb )).
555
- AddPort (prometheusPort (mdb )).
556
- Build ()
557
- }
572
+ SetOwnerReferences (mdb .GetOwnerReferences ())
558
573
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 )
565
577
}
578
+
579
+ serviceBuilder .AddPort (prometheusPort (mdb ))
580
+
581
+ return serviceBuilder .Build ()
566
582
}
567
583
568
584
// validateSpec checks if the MongoDB resource Spec is valid.
@@ -627,14 +643,21 @@ func (r ReplicaSetReconciler) buildAutomationConfig(mdb mdbv1.MongoDBCommunity)
627
643
}
628
644
}
629
645
646
+ processPortManager , err := r .createProcessPortManager (mdb )
647
+ if err != nil {
648
+ return automationconfig.AutomationConfig {}, err
649
+ }
650
+
630
651
automationConfig , err := buildAutomationConfig (
631
652
mdb ,
632
653
auth ,
633
654
currentAC ,
634
655
tlsModification ,
635
656
customRolesModification ,
636
657
prometheusModification ,
658
+ processPortManager .GetPortsModification (),
637
659
)
660
+
638
661
if err != nil {
639
662
return automationconfig.AutomationConfig {}, errors .Errorf ("could not create an automation config: %s" , err )
640
663
}
@@ -646,7 +669,7 @@ func (r ReplicaSetReconciler) buildAutomationConfig(mdb mdbv1.MongoDBCommunity)
646
669
return automationConfig , nil
647
670
}
648
671
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
650
673
// which can be used to merge.
651
674
func overrideToAutomationConfig (override mdbv1.AutomationConfigOverride ) automationconfig.AutomationConfig {
652
675
var processes []automationconfig.Process
@@ -703,7 +726,7 @@ func buildArbitersModificationFunction(mdb mdbv1.MongoDBCommunity) statefulset.M
703
726
return statefulset .Apply (
704
727
statefulset .WithReplicas (mdb .StatefulSetArbitersThisReconciliation ()),
705
728
statefulset .WithServiceName (mdb .ServiceName ()),
706
- statefulset .WithName (mdb .Name + "-arb" ),
729
+ statefulset .WithName (mdb .ArbiterNamespacedName (). Name ),
707
730
)
708
731
}
709
732
0 commit comments