@@ -366,7 +366,7 @@ func DatabaseStatefulSetHelper(mdb databaseStatefulSetSource, stsOpts *DatabaseS
366
366
extraEnvs = append (extraEnvs , ReadDatabaseProxyVarsFromEnv ()... )
367
367
stsOpts .ExtraEnvs = extraEnvs
368
368
369
- templateFunc := buildMongoDBPodTemplateSpec (* stsOpts , mdb )
369
+ templateFunc := buildMongoDBPodTemplateSpec (* stsOpts , mdb , log )
370
370
return statefulset .New (buildDatabaseStatefulSetConfigurationFunction (mdb , templateFunc , * stsOpts , log ))
371
371
}
372
372
@@ -414,7 +414,7 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
414
414
PodAntiAffinityLabelKey : opts .Name ,
415
415
}
416
416
417
- configurePodSpecSecurityContext , configureContainerSecurityContext := podtemplatespec .WithDefaultSecurityContextsModifications ()
417
+ configurePodSpecSecurityContext , _ := podtemplatespec .WithDefaultSecurityContextsModifications ()
418
418
419
419
configureImagePullSecrets := podtemplatespec .NOOP ()
420
420
name , found := env .Read (util .ImagePullSecrets ) // nolint:forbidigo
@@ -423,24 +423,7 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
423
423
}
424
424
425
425
secretsToInject := buildVaultDatabaseSecretsToInject (mdb , opts )
426
- volumes , volumeMounts := getVolumesAndVolumeMounts (mdb , opts , secretsToInject .AgentCerts , secretsToInject .InternalClusterAuth )
427
-
428
- allSources := getAllMongoDBVolumeSources (mdb , opts , log )
429
- for _ , source := range allSources {
430
- if source .ShouldBeAdded () {
431
- volumes = append (volumes , source .GetVolumes ()... )
432
- volumeMounts = append (volumeMounts , source .GetVolumeMounts ()... )
433
- }
434
- }
435
-
436
- var mounts []corev1.VolumeMount
437
- var pvcFuncs map [string ]persistentvolumeclaim.Modification
438
- if opts .Persistent == nil || * opts .Persistent {
439
- pvcFuncs , mounts = buildPersistentVolumeClaimsFuncs (opts )
440
- volumeMounts = append (volumeMounts , mounts ... )
441
- } else {
442
- volumes , volumeMounts = GetNonPersistentMongoDBVolumeMounts (volumes , volumeMounts )
443
- }
426
+ volumes , _ , pvcFuncs := getVolumesAndPVCs (mdb , opts , secretsToInject , log )
444
427
445
428
volumesFunc := func (spec * corev1.PodTemplateSpec ) {
446
429
for _ , v := range volumes {
@@ -490,17 +473,8 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
490
473
491
474
if architectures .IsRunningStaticArchitecture (mdb .GetAnnotations ()) {
492
475
shareProcessNs = func (sts * appsv1.StatefulSet ) {
493
- a := true
494
- sts .Spec .Template .Spec .ShareProcessNamespace = & a
476
+ sts .Spec .Template .Spec .ShareProcessNamespace = ptr .To (true )
495
477
}
496
- secondContainerModification = podtemplatespec .WithContainerByIndex (1 , container .WithVolumeMounts (volumeMounts ))
497
- }
498
-
499
- var databaseImage string
500
- if architectures .IsRunningStaticArchitecture (mdb .GetAnnotations ()) {
501
- databaseImage = opts .AgentImage
502
- } else {
503
- databaseImage = opts .DatabaseNonStaticImage
504
478
}
505
479
506
480
return statefulset .Apply (
@@ -519,7 +493,6 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
519
493
podtemplatespec .WithAffinity (podAffinity , PodAntiAffinityLabelKey , 100 ),
520
494
podtemplatespec .WithTerminationGracePeriodSeconds (util .DefaultPodTerminationPeriodSeconds ),
521
495
podtemplatespec .WithPodLabels (podLabels ),
522
- podtemplatespec .WithContainerByIndex (0 , sharedDatabaseContainerFunc (databaseImage , * opts .PodSpec , volumeMounts , configureContainerSecurityContext , opts .ServicePort )),
523
496
secondContainerModification ,
524
497
volumesFunc ,
525
498
configurePodSpecSecurityContext ,
@@ -529,6 +502,28 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
529
502
)
530
503
}
531
504
505
+ func getVolumesAndPVCs (mdb databaseStatefulSetSource , opts DatabaseStatefulSetOptions , secretsToInject vault.DatabaseSecretsToInject , log * zap.SugaredLogger ) ([]corev1.Volume , []corev1.VolumeMount , map [string ]persistentvolumeclaim.Modification ) {
506
+ volumes , volumeMounts := getVolumesAndVolumeMounts (mdb , opts , secretsToInject .AgentCerts , secretsToInject .InternalClusterAuth )
507
+
508
+ allSources := getAllMongoDBVolumeSources (mdb , opts , log )
509
+ for _ , source := range allSources {
510
+ if source .ShouldBeAdded () {
511
+ volumes = append (volumes , source .GetVolumes ()... )
512
+ volumeMounts = append (volumeMounts , source .GetVolumeMounts ()... )
513
+ }
514
+ }
515
+
516
+ var mounts []corev1.VolumeMount
517
+ var pvcFuncs map [string ]persistentvolumeclaim.Modification
518
+ if opts .Persistent == nil || * opts .Persistent {
519
+ pvcFuncs , mounts = buildPersistentVolumeClaimsFuncs (opts )
520
+ volumeMounts = append (volumeMounts , mounts ... )
521
+ } else {
522
+ volumes , volumeMounts = GetNonPersistentMongoDBVolumeMounts (volumes , volumeMounts )
523
+ }
524
+ return volumes , volumeMounts , pvcFuncs
525
+ }
526
+
532
527
func buildPersistentVolumeClaimsFuncs (opts DatabaseStatefulSetOptions ) (map [string ]persistentvolumeclaim.Modification , []corev1.VolumeMount ) {
533
528
var claims map [string ]persistentvolumeclaim.Modification
534
529
var mounts []corev1.VolumeMount
@@ -559,7 +554,6 @@ func sharedDatabaseContainerFunc(databaseImage string, podSpecWrapper mdbv1.PodS
559
554
container .WithResourceRequirements (buildRequirementsFromPodSpec (podSpecWrapper )),
560
555
container .WithPorts ([]corev1.ContainerPort {{ContainerPort : port }}),
561
556
container .WithImagePullPolicy (corev1 .PullPolicy (env .ReadOrPanic (util .AutomationAgentImagePullPolicy ))), // nolint:forbidigo
562
- container .WithVolumeMounts (volumeMounts ),
563
557
container .WithImage (databaseImage ),
564
558
container .WithLivenessProbe (DatabaseLivenessProbe ()),
565
559
container .WithReadinessProbe (DatabaseReadinessProbe ()),
@@ -655,27 +649,29 @@ func getVolumesAndVolumeMounts(mdb databaseStatefulSetSource, databaseOpts Datab
655
649
volumesToAdd = append (volumesToAdd , internalClusterAuthVolume )
656
650
}
657
651
652
+ // Add agent volume mounts first (to match test expectations)
653
+ volumesToAdd , volumeMounts = GetNonPersistentAgentVolumeMounts (volumesToAdd , volumeMounts )
654
+
655
+ // Then add agent-api-key volume mount
658
656
if ! vault .IsVaultSecretBackend () {
659
657
volumesToAdd = append (volumesToAdd , statefulset .CreateVolumeFromSecret (AgentAPIKeyVolumeName , agents .ApiKeySecretName (databaseOpts .PodVars .ProjectID )))
660
658
volumeMounts = append (volumeMounts , statefulset .CreateVolumeMount (AgentAPIKeyVolumeName , AgentAPIKeySecretPath ))
661
659
}
662
660
663
- volumesToAdd , volumeMounts = GetNonPersistentAgentVolumeMounts (volumesToAdd , volumeMounts )
664
-
665
661
return volumesToAdd , volumeMounts
666
662
}
667
663
668
664
// buildMongoDBPodTemplateSpec constructs the podTemplateSpec for the MongoDB resource
669
- func buildMongoDBPodTemplateSpec (opts DatabaseStatefulSetOptions , mdb databaseStatefulSetSource ) podtemplatespec.Modification {
665
+ func buildMongoDBPodTemplateSpec (opts DatabaseStatefulSetOptions , mdb databaseStatefulSetSource , log * zap. SugaredLogger ) podtemplatespec.Modification {
670
666
serviceAccountName := getServiceAccountName (opts )
671
667
672
668
// scripts volume is shared by the init container and the AppDB, so the startup
673
669
// script can be copied over
674
670
scriptsVolume := statefulset .CreateVolumeFromEmptyDir ("database-scripts" )
675
- databaseScriptsVolumeMount := databaseScriptsVolumeMount (true )
676
671
677
672
volumes := []corev1.Volume {scriptsVolume }
678
- volumeMounts := []corev1.VolumeMount {databaseScriptsVolumeMount }
673
+ secretsToInject := buildVaultDatabaseSecretsToInject (mdb , opts )
674
+ _ , volumeMounts , _ := getVolumesAndPVCs (mdb , opts , secretsToInject , log )
679
675
680
676
// Add hostname override volume if specified
681
677
if opts .HostNameOverrideConfigmapName != "" {
@@ -707,7 +703,7 @@ func buildContainers(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSou
707
703
isStaticArchitecture := architectures .IsRunningStaticArchitecture (mdb .GetAnnotations ())
708
704
709
705
if isStaticArchitecture {
710
- buildStaticArchitectureContainers (podTemplateSpec , opts , mdb )
706
+ buildStaticArchitectureContainers (podTemplateSpec , opts , mdb , volumeMounts )
711
707
} else {
712
708
buildNonStaticArchitectureContainers (podTemplateSpec , opts , volumeMounts )
713
709
}
@@ -720,11 +716,14 @@ func buildContainers(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSou
720
716
}
721
717
722
718
// buildStaticArchitectureContainers creates containers for static architecture
723
- func buildStaticArchitectureContainers (podTemplateSpec * corev1.PodTemplateSpec , opts DatabaseStatefulSetOptions , mdb databaseStatefulSetSource ) {
719
+ func buildStaticArchitectureContainers (podTemplateSpec * corev1.PodTemplateSpec , opts DatabaseStatefulSetOptions , mdb databaseStatefulSetSource , volumeMounts []corev1. VolumeMount ) {
724
720
podTemplateSpec .Spec .Containers = make ([]corev1.Container , 3 )
725
721
podTemplateSpec .Spec .Containers [0 ] = createAgentContainer (opts , mdb )
726
722
podTemplateSpec .Spec .Containers [1 ] = createMongodBinaryHolderContainer (opts )
727
723
podTemplateSpec .Spec .Containers [2 ] = createAgentUtilitiesHolderContainer ()
724
+ container .WithVolumeMounts (volumeMounts )(& podTemplateSpec .Spec .Containers [0 ])
725
+ container .WithVolumeMounts (volumeMounts )(& podTemplateSpec .Spec .Containers [1 ])
726
+ container .WithVolumeMounts (volumeMounts )(& podTemplateSpec .Spec .Containers [2 ])
728
727
729
728
// Apply common configurations to all containers
730
729
applyCommonStaticConfigurations (podTemplateSpec .Spec .Containers , opts )
@@ -818,7 +817,8 @@ func createDatabaseContainer(opts DatabaseStatefulSetOptions, volumeMounts []cor
818
817
DatabaseLivenessProbe ()(c .LivenessProbe )
819
818
820
819
_ , containerSecurityContext := podtemplatespec .WithDefaultSecurityContextsModifications ()
821
- containerSecurityContext (& c )
820
+
821
+ sharedDatabaseContainerFunc ( opts .DatabaseNonStaticImage , * opts .PodSpec , volumeMounts , containerSecurityContext , opts .ServicePort )(& c )
822
822
823
823
return c
824
824
}
@@ -1135,13 +1135,14 @@ func GetNonPersistentMongoDBVolumeMounts(volumes []corev1.Volume, volumeMounts [
1135
1135
func GetNonPersistentAgentVolumeMounts (volumes []corev1.Volume , volumeMounts []corev1.VolumeMount ) ([]corev1.Volume , []corev1.VolumeMount ) {
1136
1136
volumes = append (volumes , statefulset .CreateVolumeFromEmptyDir (util .PvMms ))
1137
1137
1138
- // The agent reads and writes into its own directory. It also contains a subdirectory called downloads.
1139
- // This one is published by the Dockerfile
1140
- volumeMounts = append (volumeMounts , statefulset .CreateVolumeMount (util .PvMms , util .PvcMmsMountPath , statefulset .WithSubPath (util .PvcMms )))
1141
-
1142
1138
// Runtime data for MMS
1143
1139
volumeMounts = append (volumeMounts , statefulset .CreateVolumeMount (util .PvMms , util .PvcMmsHomeMountPath , statefulset .WithSubPath (util .PvcMmsHome )))
1144
1140
1145
1141
volumeMounts = append (volumeMounts , statefulset .CreateVolumeMount (util .PvMms , util .PvcMountPathTmp , statefulset .WithSubPath (util .PvcNameTmp )))
1142
+
1143
+ // The agent reads and writes into its own directory. It also contains a subdirectory called downloads.
1144
+ // This one is published by the Dockerfile
1145
+ volumeMounts = append (volumeMounts , statefulset .CreateVolumeMount (util .PvMms , util .PvcMmsMountPath , statefulset .WithSubPath (util .PvcMms )))
1146
+
1146
1147
return volumes , volumeMounts
1147
1148
}
0 commit comments