Skip to content

Commit

Permalink
make initContainers optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nammn committed Feb 13, 2024
1 parent 3a25b1b commit c41fd3f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions controllers/construct/build_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
t.Setenv(AgentImageEnv, "agent-image")

mdb := newTestReplicaSet()
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv))
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)
sts := &appsv1.StatefulSet{}

t.Run("1st Call", func(t *testing.T) {
Expand All @@ -69,7 +69,7 @@ func TestManagedSecurityContext(t *testing.T) {
t.Setenv(podtemplatespec.ManagedSecurityContextEnv, "true")

mdb := newTestReplicaSet()
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv))
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)

sts := &appsv1.StatefulSet{}
stsFunc(sts)
Expand Down
39 changes: 27 additions & 12 deletions controllers/construct/mongodbstatefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type MongoDBStatefulSetOwner interface {
// BuildMongoDBReplicaSetStatefulSetModificationFunction builds the parts of the replica set that are common between every resource that implements
// MongoDBStatefulSetOwner.
// It doesn't configure TLS or additional containers/env vars that the statefulset might need.
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler, agentImage string) statefulset.Modification {
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler, agentImage string, withInitContainers bool) statefulset.Modification {
labels := map[string]string{
"app": mdb.ServiceName(),
}
Expand All @@ -138,13 +138,10 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
agentHealthStatusVolumeMount := statefulset.CreateVolumeMount(healthStatusVolume.Name, "/var/log/mongodb-mms-automation/healthstatus")
mongodHealthStatusVolumeMount := statefulset.CreateVolumeMount(healthStatusVolume.Name, "/healthstatus")

// hooks volume is only required on the mongod pod.
hooksVolume := statefulset.CreateVolumeFromEmptyDir("hooks")
hooksVolumeMount := statefulset.CreateVolumeMount(hooksVolume.Name, "/hooks", statefulset.WithReadOnly(false))

// scripts volume is only required on the mongodb-agent pod.
scriptsVolume := statefulset.CreateVolumeFromEmptyDir("agent-scripts")
scriptsVolumeMount := statefulset.CreateVolumeMount(scriptsVolume.Name, "/opt/scripts", statefulset.WithReadOnly(false))
hooksVolume := corev1.Volume{}
scriptsVolume := corev1.Volume{}
upgradeInitContainer := podtemplatespec.NOOP()
readinessInitContainer := podtemplatespec.NOOP()

// tmp volume is required by the mongodb-agent and mongod
tmpVolume := statefulset.CreateVolumeFromEmptyDir("tmp")
Expand All @@ -155,7 +152,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
keyFileVolumeVolumeMount := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false))
keyFileVolumeVolumeMountMongod := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false))

mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, scriptsVolumeMount, keyFileVolumeVolumeMount, tmpVolumeMount}
mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, keyFileVolumeVolumeMount, tmpVolumeMount}

automationConfigVolumeFunc := podtemplatespec.NOOP()
if mdb.NeedsAutomationConfigVolume() {
Expand All @@ -164,7 +161,25 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
automationConfigVolumeMount := statefulset.CreateVolumeMount(automationConfigVolume.Name, "/var/lib/automation/config", statefulset.WithReadOnly(true))
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, automationConfigVolumeMount)
}
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, hooksVolumeMount, keyFileVolumeVolumeMountMongod, tmpVolumeMount}
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, keyFileVolumeVolumeMountMongod, tmpVolumeMount}

if withInitContainers {
// hooks volume is only required on the mongod pod.
hooksVolume = statefulset.CreateVolumeFromEmptyDir("hooks")
hooksVolumeMount := statefulset.CreateVolumeMount(hooksVolume.Name, "/hooks", statefulset.WithReadOnly(false))

// scripts volume is only required on the mongodb-agent pod.
scriptsVolume = statefulset.CreateVolumeFromEmptyDir("agent-scripts")
scriptsVolumeMount := statefulset.CreateVolumeMount(scriptsVolume.Name, "/opt/scripts", statefulset.WithReadOnly(false))

upgradeInitContainer = podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount}))
readinessInitContainer = podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount}))

mongodVolumeMounts = append(mongodVolumeMounts, hooksVolumeMount)
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, scriptsVolumeMount)

}

dataVolumeClaim := statefulset.NOOP()
logVolumeClaim := statefulset.NOOP()
singleModeVolumeClaim := func(s *appsv1.StatefulSet) {}
Expand Down Expand Up @@ -226,8 +241,8 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
podtemplatespec.WithServiceAccount(mongodbDatabaseServiceAccountName),
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentLogFile, agentMaxLogFileDurationHours, agentImage)),
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(), mongodVolumeMounts, mdb.GetMongodConfiguration())),
podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount})),
podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount})),
upgradeInitContainer,
readinessInitContainer,
),
))
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/replica_set_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func buildStatefulSet(mdb mdbv1.MongoDBCommunity) (appsv1.StatefulSet, error) {
}

func buildStatefulSetModificationFunction(mdb mdbv1.MongoDBCommunity) statefulset.Modification {
commonModification := construct.BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(construct.AgentImageEnv))
commonModification := construct.BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(construct.AgentImageEnv), true)
return statefulset.Apply(
commonModification,
statefulset.WithOwnerReference(mdb.GetOwnerReferences()),
Expand Down
3 changes: 3 additions & 0 deletions pkg/kube/podtemplatespec/podspec_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func WithVolumes(volumes []corev1.Volume) Modification {
// WithVolume ensures given volume is present in the PodTemplateSpec. It merges the volume if it already exists.
func WithVolume(volume corev1.Volume) Modification {
return func(template *corev1.PodTemplateSpec) {
if template == nil {
return
}
for i := range template.Spec.Volumes {
if template.Spec.Volumes[i].Name == volume.Name {
template.Spec.Volumes[i] = merge.Volume(template.Spec.Volumes[i], volume)
Expand Down

0 comments on commit c41fd3f

Please sign in to comment.