Skip to content

Commit d76eda3

Browse files
committed
refactor db construction
1 parent 6d50a2b commit d76eda3

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

controllers/operator/construct/construction_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package construct
22

33
import (
4+
"github.com/google/go-cmp/cmp"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -261,10 +262,7 @@ func checkPvClaims(t *testing.T, set appsv1.StatefulSet, expectedClaims []corev1
261262

262263
func checkMounts(t *testing.T, set appsv1.StatefulSet, expectedMounts []corev1.VolumeMount) {
263264
assert.Len(t, set.Spec.Template.Spec.Containers[0].VolumeMounts, len(expectedMounts))
264-
265-
for i, c := range expectedMounts {
266-
assert.Equal(t, c, set.Spec.Template.Spec.Containers[0].VolumeMounts[i])
267-
}
265+
cmp.Equal(expectedMounts, set.Spec.Template.Spec.Containers[0].VolumeMounts)
268266
}
269267

270268
func pvClaim(pvName, size string, storageClass *string, labels map[string]string) corev1.PersistentVolumeClaim {

controllers/operator/construct/database_construction.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
421421
}
422422

423423
secretsToInject := buildVaultDatabaseSecretsToInject(mdb, opts)
424-
volumes, _, pvcFuncs := getVolumesAndPVCs(mdb, opts, secretsToInject, log)
424+
volumes, volumeMounts, pvcFuncs := getVolumesAndPVCs(mdb, opts, secretsToInject, log)
425425

426426
volumesFunc := func(spec *corev1.PodTemplateSpec) {
427427
for _, v := range volumes {
@@ -492,6 +492,7 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
492492
volumesFunc,
493493
configureImagePullSecrets,
494494
podTemplateSpecFunc,
495+
podtemplatespec.WithAllVolumeMounts(volumeMounts...),
495496
)),
496497
)
497498
}
@@ -543,7 +544,7 @@ func buildPersistentVolumeClaimsFuncs(opts DatabaseStatefulSetOptions) (map[stri
543544
return claims, mounts
544545
}
545546

546-
func sharedDatabaseContainerFunc(databaseImage string, podSpecWrapper mdbv1.PodSpecWrapper, volumeMounts []corev1.VolumeMount, configureContainerSecurityContext container.Modification, port int32) container.Modification {
547+
func sharedDatabaseContainerFunc(databaseImage string, podSpecWrapper mdbv1.PodSpecWrapper, configureContainerSecurityContext container.Modification, port int32) container.Modification {
547548
return container.Apply(
548549
container.WithResourceRequirements(buildRequirementsFromPodSpec(podSpecWrapper)),
549550
container.WithPorts([]corev1.ContainerPort{{ContainerPort: port}}),
@@ -665,9 +666,6 @@ func buildMongoDBPodTemplateSpec(opts DatabaseStatefulSetOptions, mdb databaseSt
665666
scriptsVolume := statefulset.CreateVolumeFromEmptyDir("database-scripts")
666667
volumes := []corev1.Volume{scriptsVolume}
667668

668-
secretsToInject := buildVaultDatabaseSecretsToInject(mdb, opts)
669-
_, volumeMounts, _ := getVolumesAndPVCs(mdb, opts, secretsToInject, log)
670-
671669
pullSecretsConfigurationFunc := podtemplatespec.NOOP()
672670
if pullSecrets, ok := env.Read(util.ImagePullSecrets); ok { // nolint:forbidigo
673671
pullSecretsConfigurationFunc = podtemplatespec.WithImagePullSecrets(pullSecrets)
@@ -682,49 +680,41 @@ func buildMongoDBPodTemplateSpec(opts DatabaseStatefulSetOptions, mdb databaseSt
682680
podtemplatespec.WithTopologyKey(opts.PodSpec.GetTopologyKeyOrDefault(), 0),
683681
podtemplatespec.WithServiceAccount(serviceAccountName),
684682
podtemplatespec.WithVolumes(volumes),
685-
buildContainers(opts, mdb, volumeMounts),
683+
buildContainers(opts, mdb),
686684
)
687685
}
688686

689687
// buildContainers directly creates and configures all containers based on architecture
690-
func buildContainers(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource, volumeMounts []corev1.VolumeMount) func(*corev1.PodTemplateSpec) {
688+
func buildContainers(opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) func(*corev1.PodTemplateSpec) {
691689
return func(podTemplateSpec *corev1.PodTemplateSpec) {
692690
isStaticArchitecture := architectures.IsRunningStaticArchitecture(mdb.GetAnnotations())
693691

694692
if isStaticArchitecture {
695-
buildStaticArchitectureContainers(podTemplateSpec, opts, mdb, volumeMounts)
693+
buildStaticArchitectureContainers(podTemplateSpec, opts, mdb)
696694
} else {
697-
buildNonStaticArchitectureContainers(podTemplateSpec, opts, volumeMounts)
698-
}
699-
700-
// Apply hostname override volume mounts if specified
701-
if opts.HostNameOverrideConfigmapName != "" {
702-
applyHostnameOverrideVolumeMounts(podTemplateSpec, opts.HostNameOverrideConfigmapName)
695+
buildNonStaticArchitectureContainers(podTemplateSpec, opts)
703696
}
704697
}
705698
}
706699

707700
// buildStaticArchitectureContainers creates containers for static architecture
708-
func buildStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource, volumeMounts []corev1.VolumeMount) {
701+
func buildStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions, mdb databaseStatefulSetSource) {
709702
podTemplateSpec.Spec.Containers = make([]corev1.Container, 3)
710703
podTemplateSpec.Spec.Containers[0] = createAgentContainer(opts, mdb)
711704
podTemplateSpec.Spec.Containers[1] = createMongodBinaryHolderContainer(opts)
712705
podTemplateSpec.Spec.Containers[2] = createAgentUtilitiesHolderContainer()
713-
container.WithVolumeMounts(volumeMounts)(&podTemplateSpec.Spec.Containers[0])
714-
container.WithVolumeMounts(volumeMounts)(&podTemplateSpec.Spec.Containers[1])
715-
container.WithVolumeMounts(volumeMounts)(&podTemplateSpec.Spec.Containers[2])
716706

717707
// Apply common configurations to all containers
718708
applyCommonStaticConfigurations(podTemplateSpec.Spec.Containers, opts)
719709
}
720710

721711
// buildNonStaticArchitectureContainers creates containers for non-static architecture
722-
func buildNonStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions, volumeMounts []corev1.VolumeMount) {
712+
func buildNonStaticArchitectureContainers(podTemplateSpec *corev1.PodTemplateSpec, opts DatabaseStatefulSetOptions) {
723713
podTemplateSpec.Spec.Containers = make([]corev1.Container, 1)
724714
podTemplateSpec.Spec.InitContainers = make([]corev1.Container, 1)
725715

726716
podTemplateSpec.Spec.InitContainers[0] = createDatabaseInitContainer(opts)
727-
podTemplateSpec.Spec.Containers[0] = createDatabaseContainer(opts, volumeMounts)
717+
podTemplateSpec.Spec.Containers[0] = createDatabaseContainer(opts)
728718
}
729719

730720
// createAgentContainer creates the agent container for static architecture
@@ -786,7 +776,7 @@ func createDatabaseInitContainer(opts DatabaseStatefulSetOptions) corev1.Contain
786776
}
787777

788778
// createDatabaseContainer creates the database container for non-static architecture
789-
func createDatabaseContainer(opts DatabaseStatefulSetOptions, volumeMounts []corev1.VolumeMount) corev1.Container {
779+
func createDatabaseContainer(opts DatabaseStatefulSetOptions) corev1.Container {
790780
// scripts volume is shared by the init container and the AppDB, so the startup
791781
// script can be copied over
792782

@@ -795,7 +785,7 @@ func createDatabaseContainer(opts DatabaseStatefulSetOptions, volumeMounts []cor
795785
Image: opts.DatabaseNonStaticImage,
796786
Command: []string{"/opt/scripts/agent-launcher.sh"},
797787
Env: databaseEnvVars(opts),
798-
VolumeMounts: append(volumeMounts, databaseScriptsVolumeMount(true)),
788+
VolumeMounts: []corev1.VolumeMount{databaseScriptsVolumeMount(true)},
799789
Resources: buildRequirementsFromPodSpec(*opts.PodSpec),
800790
Ports: []corev1.ContainerPort{{ContainerPort: opts.ServicePort}},
801791
ImagePullPolicy: corev1.PullPolicy(env.ReadOrPanic(util.AutomationAgentImagePullPolicy)),
@@ -810,7 +800,7 @@ func createDatabaseContainer(opts DatabaseStatefulSetOptions, volumeMounts []cor
810800

811801
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
812802

813-
sharedDatabaseContainerFunc(opts.DatabaseNonStaticImage, *opts.PodSpec, volumeMounts, containerSecurityContext, opts.ServicePort)(&c)
803+
sharedDatabaseContainerFunc(opts.DatabaseNonStaticImage, *opts.PodSpec, containerSecurityContext, opts.ServicePort)(&c)
814804

815805
return c
816806
}

mongodb-community-operator/pkg/kube/podtemplatespec/podspec_template.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ func WithVolumeMounts(containerName string, volumeMounts ...corev1.VolumeMount)
271271
}
272272
}
273273

274+
// WithAllVolumeMounts will add volume mounts to all containers
275+
func WithAllVolumeMounts(volumeMounts ...corev1.VolumeMount) Modification {
276+
return func(podTemplateSpec *corev1.PodTemplateSpec) {
277+
for _, volumeMount := range volumeMounts {
278+
// Apply to all containers
279+
for i := range podTemplateSpec.Spec.Containers {
280+
podTemplateSpec.Spec.Containers[i].VolumeMounts = append(
281+
podTemplateSpec.Spec.Containers[i].VolumeMounts, volumeMount)
282+
}
283+
}
284+
}
285+
}
286+
274287
func RemoveVolumeMount(containerName string, volumeMount string) Modification {
275288
return func(podTemplateSpec *corev1.PodTemplateSpec) {
276289
c := FindContainerByName(containerName, podTemplateSpec)

0 commit comments

Comments
 (0)