Skip to content

Commit ffb658e

Browse files
authored
Preparation to make the operator static (#1488)
1 parent 240be3b commit ffb658e

File tree

10 files changed

+93
-75
lines changed

10 files changed

+93
-75
lines changed

api/v1/mongodbcommunity_types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1111,15 +1111,15 @@ func (m *MongoDBCommunity) CurrentArbiters() int {
11111111
return m.Status.CurrentStatefulSetArbitersReplicas
11121112
}
11131113

1114-
func (m *MongoDBCommunity) GetMongoDBVersion() string {
1114+
func (m *MongoDBCommunity) GetMongoDBVersion(map[string]string) string {
11151115
return m.Spec.Version
11161116
}
11171117

11181118
// GetMongoDBVersionForAnnotation returns the MDB version used to annotate the object.
11191119
// Here it's the same as GetMongoDBVersion, but a different name is used in order to make
11201120
// the usage clearer in enterprise (where it's a method of OpsManager but is used for the AppDB)
11211121
func (m *MongoDBCommunity) GetMongoDBVersionForAnnotation() string {
1122-
return m.GetMongoDBVersion()
1122+
return m.GetMongoDBVersion(nil)
11231123
}
11241124

11251125
func (m *MongoDBCommunity) StatefulSetReplicasThisReconciliation() int {

controllers/construct/build_statefulset_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
4545
t.Setenv(AgentImageEnv, "agent-image")
4646

4747
mdb := newTestReplicaSet()
48-
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb)
48+
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)
4949
sts := &appsv1.StatefulSet{}
5050

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

7171
mdb := newTestReplicaSet()
72-
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb)
72+
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)
7373

7474
sts := &appsv1.StatefulSet{}
7575
stsFunc(sts)
@@ -177,7 +177,7 @@ func TestMongod_Container(t *testing.T) {
177177
}
178178

179179
func TestMongoDBAgentLogging_Container(t *testing.T) {
180-
c := container.New(mongodbAgentContainer("test-mongodb-automation-config", []corev1.VolumeMount{}, "INFO", "/var/log/mongodb-mms-automation/automation-agent.log", 24))
180+
c := container.New(mongodbAgentContainer("test-mongodb-automation-config", []corev1.VolumeMount{}, "INFO", "/var/log/mongodb-mms-automation/automation-agent.log", 24, "image"))
181181

182182
t.Run("Has correct Env vars", func(t *testing.T) {
183183
assert.Len(t, c.Env, 7)

controllers/construct/mongodbstatefulset.go

+40-19
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type MongoDBStatefulSetOwner interface {
9696
// GetNamespace returns the namespace the resource is defined in.
9797
GetNamespace() string
9898
// GetMongoDBVersion returns the version of MongoDB to be used for this resource.
99-
GetMongoDBVersion() string
99+
GetMongoDBVersion(annotations map[string]string) string
100100
// AutomationConfigSecretName returns the name of the secret which will contain the automation config.
101101
AutomationConfigSecretName() string
102102
// GetUpdateStrategyType returns the UpdateStrategyType of the statefulset.
@@ -126,7 +126,7 @@ type MongoDBStatefulSetOwner interface {
126126
// BuildMongoDBReplicaSetStatefulSetModificationFunction builds the parts of the replica set that are common between every resource that implements
127127
// MongoDBStatefulSetOwner.
128128
// It doesn't configure TLS or additional containers/env vars that the statefulset might need.
129-
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler) statefulset.Modification {
129+
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler, agentImage string, withInitContainers bool) statefulset.Modification {
130130
labels := map[string]string{
131131
"app": mdb.ServiceName(),
132132
}
@@ -138,13 +138,10 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
138138
agentHealthStatusVolumeMount := statefulset.CreateVolumeMount(healthStatusVolume.Name, "/var/log/mongodb-mms-automation/healthstatus")
139139
mongodHealthStatusVolumeMount := statefulset.CreateVolumeMount(healthStatusVolume.Name, "/healthstatus")
140140

141-
// hooks volume is only required on the mongod pod.
142-
hooksVolume := statefulset.CreateVolumeFromEmptyDir("hooks")
143-
hooksVolumeMount := statefulset.CreateVolumeMount(hooksVolume.Name, "/hooks", statefulset.WithReadOnly(false))
144-
145-
// scripts volume is only required on the mongodb-agent pod.
146-
scriptsVolume := statefulset.CreateVolumeFromEmptyDir("agent-scripts")
147-
scriptsVolumeMount := statefulset.CreateVolumeMount(scriptsVolume.Name, "/opt/scripts", statefulset.WithReadOnly(false))
141+
hooksVolume := corev1.Volume{}
142+
scriptsVolume := corev1.Volume{}
143+
upgradeInitContainer := podtemplatespec.NOOP()
144+
readinessInitContainer := podtemplatespec.NOOP()
148145

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

158-
mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, scriptsVolumeMount, keyFileVolumeVolumeMount, tmpVolumeMount}
155+
mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, keyFileVolumeVolumeMount, tmpVolumeMount}
159156

160157
automationConfigVolumeFunc := podtemplatespec.NOOP()
161158
if mdb.NeedsAutomationConfigVolume() {
@@ -164,7 +161,31 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
164161
automationConfigVolumeMount := statefulset.CreateVolumeMount(automationConfigVolume.Name, "/var/lib/automation/config", statefulset.WithReadOnly(true))
165162
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, automationConfigVolumeMount)
166163
}
167-
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, hooksVolumeMount, keyFileVolumeVolumeMountMongod, tmpVolumeMount}
164+
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, keyFileVolumeVolumeMountMongod, tmpVolumeMount}
165+
166+
hooksVolumeMod := podtemplatespec.NOOP()
167+
scriptsVolumeMod := podtemplatespec.NOOP()
168+
169+
// This is temporary code;
170+
// once we make the operator fully deploy static workloads, we will remove those init containers.
171+
if withInitContainers {
172+
// hooks volume is only required on the mongod pod.
173+
hooksVolume = statefulset.CreateVolumeFromEmptyDir("hooks")
174+
hooksVolumeMount := statefulset.CreateVolumeMount(hooksVolume.Name, "/hooks", statefulset.WithReadOnly(false))
175+
176+
// scripts volume is only required on the mongodb-agent pod.
177+
scriptsVolume = statefulset.CreateVolumeFromEmptyDir("agent-scripts")
178+
scriptsVolumeMount := statefulset.CreateVolumeMount(scriptsVolume.Name, "/opt/scripts", statefulset.WithReadOnly(false))
179+
180+
upgradeInitContainer = podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount}))
181+
readinessInitContainer = podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount}))
182+
scriptsVolumeMod = podtemplatespec.WithVolume(scriptsVolume)
183+
hooksVolumeMod = podtemplatespec.WithVolume(hooksVolume)
184+
185+
mongodVolumeMounts = append(mongodVolumeMounts, hooksVolumeMount)
186+
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, scriptsVolumeMount)
187+
}
188+
168189
dataVolumeClaim := statefulset.NOOP()
169190
logVolumeClaim := statefulset.NOOP()
170191
singleModeVolumeClaim := func(s *appsv1.StatefulSet) {}
@@ -218,16 +239,16 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
218239
podSecurityContext,
219240
podtemplatespec.WithPodLabels(labels),
220241
podtemplatespec.WithVolume(healthStatusVolume),
221-
podtemplatespec.WithVolume(hooksVolume),
222242
automationConfigVolumeFunc,
223-
podtemplatespec.WithVolume(scriptsVolume),
243+
hooksVolumeMod,
244+
scriptsVolumeMod,
224245
podtemplatespec.WithVolume(tmpVolume),
225246
podtemplatespec.WithVolume(keyFileVolume),
226247
podtemplatespec.WithServiceAccount(mongodbDatabaseServiceAccountName),
227-
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentLogFile, agentMaxLogFileDurationHours)),
228-
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(), mongodVolumeMounts, mdb.GetMongodConfiguration())),
229-
podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount})),
230-
podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount})),
248+
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentLogFile, agentMaxLogFileDurationHours, agentImage)),
249+
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(nil), mongodVolumeMounts, mdb.GetMongodConfiguration())),
250+
upgradeInitContainer,
251+
readinessInitContainer,
231252
),
232253
))
233254
}
@@ -245,11 +266,11 @@ func AutomationAgentCommand(withAgentAPIKeyExport bool) []string {
245266
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
246267
}
247268

248-
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, logFile string, maxLogFileDurationHours int) container.Modification {
269+
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, logFile string, maxLogFileDurationHours int, agentImage string) container.Modification {
249270
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
250271
return container.Apply(
251272
container.WithName(AgentName),
252-
container.WithImage(os.Getenv(AgentImageEnv)),
273+
container.WithImage(agentImage),
253274
container.WithImagePullPolicy(corev1.PullAlways),
254275
container.WithReadinessProbe(DefaultReadiness()),
255276
container.WithResourceRequirements(resourcerequirements.Defaults()),

controllers/replica_set_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (r ReplicaSetReconciler) Reconcile(ctx context.Context, request reconcile.R
236236
withMongoDBArbiters(mdb.AutomationConfigArbitersThisReconciliation()).
237237
withMessage(None, "").
238238
withRunningPhase().
239-
withVersion(mdb.GetMongoDBVersion()),
239+
withVersion(mdb.GetMongoDBVersion(nil)),
240240
)
241241
if err != nil {
242242
r.log.Errorf("Error updating the status of the MongoDB resource: %s", err)
@@ -754,7 +754,7 @@ func buildStatefulSet(mdb mdbv1.MongoDBCommunity) (appsv1.StatefulSet, error) {
754754
}
755755

756756
func buildStatefulSetModificationFunction(mdb mdbv1.MongoDBCommunity) statefulset.Modification {
757-
commonModification := construct.BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb)
757+
commonModification := construct.BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(construct.AgentImageEnv), true)
758758
return statefulset.Apply(
759759
commonModification,
760760
statefulset.WithOwnerReference(mdb.GetOwnerReferences()),

0 commit comments

Comments
 (0)