Skip to content

Commit e89c0f7

Browse files
committedFeb 20, 2025··
Refactoring: propagate env vars via parameters
1 parent 423b6eb commit e89c0f7

File tree

9 files changed

+178
-201
lines changed

9 files changed

+178
-201
lines changed
 

‎cmd/manager/main.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package main
33
import (
44
"fmt"
55
"os"
6+
67
"sigs.k8s.io/controller-runtime/pkg/cache"
78

89
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
910
"github.com/mongodb/mongodb-kubernetes-operator/controllers"
1011
"github.com/mongodb/mongodb-kubernetes-operator/controllers/construct"
12+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
1113
"go.uber.org/zap"
1214
"k8s.io/apimachinery/pkg/runtime"
1315
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -56,7 +58,14 @@ func main() {
5658
log.Sugar().Fatalf("Failed to configure logger: %v", err)
5759
}
5860

59-
if !hasRequiredVariables(log, construct.AgentImageEnv, construct.VersionUpgradeHookImageEnv, construct.ReadinessProbeImageEnv) {
61+
if !hasRequiredVariables(
62+
log,
63+
construct.MongodbRepoUrlEnv,
64+
construct.MongodbImageEnv,
65+
construct.AgentImageEnv,
66+
construct.VersionUpgradeHookImageEnv,
67+
construct.ReadinessProbeImageEnv,
68+
) {
6069
os.Exit(1)
6170
}
6271

@@ -99,7 +108,15 @@ func main() {
99108
}
100109

101110
// Setup Controller.
102-
if err = controllers.NewReconciler(mgr).SetupWithManager(mgr); err != nil {
111+
if err = controllers.NewReconciler(
112+
mgr,
113+
os.Getenv(construct.MongodbRepoUrlEnv),
114+
os.Getenv(construct.MongodbImageEnv),
115+
envvar.GetEnvOrDefault(construct.MongoDBImageTypeEnv, construct.DefaultImageType),
116+
os.Getenv(construct.AgentImageEnv),
117+
os.Getenv(construct.VersionUpgradeHookImageEnv),
118+
os.Getenv(construct.ReadinessProbeImageEnv),
119+
).SetupWithManager(mgr); err != nil {
103120
log.Sugar().Fatalf("Unable to create controller: %v", err)
104121
}
105122
// +kubebuilder:scaffold:builder

‎controllers/construct/build_statefulset_test.go

+48-70
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package construct
22

33
import (
4-
"os"
54
"reflect"
65
"testing"
76

@@ -21,10 +20,6 @@ import (
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221
)
2322

24-
func init() {
25-
os.Setenv(VersionUpgradeHookImageEnv, "version-upgrade-hook-image")
26-
}
27-
2823
func newTestReplicaSet() mdbv1.MongoDBCommunity {
2924
return mdbv1.MongoDBCommunity{
3025
ObjectMeta: metav1.ObjectMeta{
@@ -40,12 +35,8 @@ func newTestReplicaSet() mdbv1.MongoDBCommunity {
4035
}
4136

4237
func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
43-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
44-
t.Setenv(MongodbImageEnv, "mongodb-community-server")
45-
t.Setenv(AgentImageEnv, "agent-image")
46-
4738
mdb := newTestReplicaSet()
48-
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)
39+
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, "docker.io/mongodb", "mongodb-community-server", "ubi8", "agent-image", "version-upgrade-hook-image", "fake-readinessProbeImage", true)
4940
sts := &appsv1.StatefulSet{}
5041

5142
t.Run("1st Call", func(t *testing.T) {
@@ -63,13 +54,10 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
6354
}
6455

6556
func TestManagedSecurityContext(t *testing.T) {
66-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
67-
t.Setenv(MongodbImageEnv, "mongodb-community-server")
68-
t.Setenv(AgentImageEnv, "agent-image")
6957
t.Setenv(podtemplatespec.ManagedSecurityContextEnv, "true")
7058

7159
mdb := newTestReplicaSet()
72-
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, os.Getenv(AgentImageEnv), true)
60+
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, &mdb, "docker.io/mongodb", "mongodb-community-server", "ubi8", "agent-image", "version-upgrade-hook-image", "fake-readinessProbeImage", true)
7361

7462
sts := &appsv1.StatefulSet{}
7563
stsFunc(sts)
@@ -79,87 +67,77 @@ func TestManagedSecurityContext(t *testing.T) {
7967

8068
func TestGetMongoDBImage(t *testing.T) {
8169
type testConfig struct {
82-
setArgs func(t *testing.T)
83-
version string
84-
expectedImage string
70+
mongodbRepoUrl string
71+
mongodbImage string
72+
mongodbImageType string
73+
version string
74+
expectedImage string
8575
}
8676
tests := map[string]testConfig{
8777
"Default UBI8 Community image": {
88-
setArgs: func(t *testing.T) {
89-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
90-
t.Setenv(MongodbImageEnv, "mongodb-community-server")
91-
},
92-
version: "6.0.5",
93-
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8",
78+
mongodbRepoUrl: "docker.io/mongodb",
79+
mongodbImage: "mongodb-community-server",
80+
mongodbImageType: "ubi8",
81+
version: "6.0.5",
82+
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8",
9483
},
9584
"Overridden UBI8 Enterprise image": {
96-
setArgs: func(t *testing.T) {
97-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
98-
t.Setenv(MongodbImageEnv, "mongodb-enterprise-server")
99-
},
100-
version: "6.0.5",
101-
expectedImage: "docker.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8",
85+
mongodbRepoUrl: "docker.io/mongodb",
86+
mongodbImage: "mongodb-enterprise-server",
87+
mongodbImageType: "ubi8",
88+
version: "6.0.5",
89+
expectedImage: "docker.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8",
10290
},
10391
"Overridden UBI8 Enterprise image from Quay": {
104-
setArgs: func(t *testing.T) {
105-
t.Setenv(MongodbRepoUrl, "quay.io/mongodb")
106-
t.Setenv(MongodbImageEnv, "mongodb-enterprise-server")
107-
},
108-
version: "6.0.5",
109-
expectedImage: "quay.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8",
92+
mongodbRepoUrl: "quay.io/mongodb",
93+
mongodbImage: "mongodb-enterprise-server",
94+
mongodbImageType: "ubi8",
95+
version: "6.0.5",
96+
expectedImage: "quay.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8",
11097
},
11198
"Overridden Ubuntu Community image": {
112-
setArgs: func(t *testing.T) {
113-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
114-
t.Setenv(MongodbImageEnv, "mongodb-community-server")
115-
t.Setenv(MongoDBImageType, "ubuntu2204")
116-
},
117-
version: "6.0.5",
118-
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubuntu2204",
99+
mongodbRepoUrl: "docker.io/mongodb",
100+
mongodbImage: "mongodb-community-server",
101+
mongodbImageType: "ubuntu2204",
102+
version: "6.0.5",
103+
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubuntu2204",
119104
},
120105
"Overridden UBI Community image": {
121-
setArgs: func(t *testing.T) {
122-
t.Setenv(MongodbRepoUrl, "docker.io/mongodb")
123-
t.Setenv(MongodbImageEnv, "mongodb-community-server")
124-
t.Setenv(MongoDBImageType, "ubi8")
125-
},
126-
version: "6.0.5",
127-
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8",
106+
mongodbRepoUrl: "docker.io/mongodb",
107+
mongodbImage: "mongodb-community-server",
108+
mongodbImageType: "ubi8",
109+
version: "6.0.5",
110+
expectedImage: "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8",
128111
},
129112
"Docker Inc images": {
130-
setArgs: func(t *testing.T) {
131-
t.Setenv(MongodbRepoUrl, "docker.io")
132-
t.Setenv(MongodbImageEnv, "mongo")
133-
},
134-
version: "6.0.5",
135-
expectedImage: "docker.io/mongo:6.0.5",
113+
mongodbRepoUrl: "docker.io",
114+
mongodbImage: "mongo",
115+
mongodbImageType: "ubi8",
116+
version: "6.0.5",
117+
expectedImage: "docker.io/mongo:6.0.5",
136118
},
137119
"Deprecated AppDB images defined the old way": {
138-
setArgs: func(t *testing.T) {
139-
t.Setenv(MongodbRepoUrl, "quay.io")
140-
t.Setenv(MongodbImageEnv, "mongodb/mongodb-enterprise-appdb-database-ubi")
141-
// In this example, we intentionally don't use the suffix from the env. variable and let users
142-
// define it in the version instead. There are some known customers who do this.
143-
// This is a backwards compatibility case.
144-
t.Setenv(MongoDBImageType, "will-be-ignored")
145-
},
146-
147-
version: "5.0.14-ent",
148-
expectedImage: "quay.io/mongodb/mongodb-enterprise-appdb-database-ubi:5.0.14-ent",
120+
mongodbRepoUrl: "quay.io",
121+
mongodbImage: "mongodb/mongodb-enterprise-appdb-database-ubi",
122+
// In this example, we intentionally don't use the suffix from the env. variable and let users
123+
// define it in the version instead. There are some known customers who do this.
124+
// This is a backwards compatibility case.
125+
mongodbImageType: "will-be-ignored",
126+
version: "5.0.14-ent",
127+
expectedImage: "quay.io/mongodb/mongodb-enterprise-appdb-database-ubi:5.0.14-ent",
149128
},
150129
}
151130
for testName := range tests {
152131
t.Run(testName, func(t *testing.T) {
153132
testConfig := tests[testName]
154-
testConfig.setArgs(t)
155-
image := getMongoDBImage(testConfig.version)
133+
image := getMongoDBImage(testConfig.mongodbRepoUrl, testConfig.mongodbImage, testConfig.mongodbImageType, testConfig.version)
156134
assert.Equal(t, testConfig.expectedImage, image)
157135
})
158136
}
159137
}
160138

161139
func TestMongod_Container(t *testing.T) {
162-
c := container.New(mongodbContainer("4.2", []corev1.VolumeMount{}, mdbv1.NewMongodConfiguration()))
140+
c := container.New(mongodbContainer("fake-repo-url", "fake-image", "ubi8", "4.2", []corev1.VolumeMount{}, mdbv1.NewMongodConfiguration()))
163141

164142
t.Run("Has correct Env vars", func(t *testing.T) {
165143
assert.Len(t, c.Env, 1)
@@ -168,7 +146,7 @@ func TestMongod_Container(t *testing.T) {
168146
})
169147

170148
t.Run("Image is correct", func(t *testing.T) {
171-
assert.Equal(t, getMongoDBImage("4.2"), c.Image)
149+
assert.Equal(t, getMongoDBImage("fake-repo-url", "fake-image", "ubi8", "4.2"), c.Image)
172150
})
173151

174152
t.Run("Resource requirements are correct", func(t *testing.T) {

‎controllers/construct/mongodbstatefulset.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package construct
22

33
import (
44
"fmt"
5-
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config"
65
"os"
76
"strconv"
87
"strings"
98

10-
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
9+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config"
1110

1211
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
1312
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/container"
@@ -28,6 +27,16 @@ var (
2827
OfficialMongodbRepoUrls = []string{"docker.io/mongodb", "quay.io/mongodb"}
2928
)
3029

30+
// Environment variables used to configure the MongoDB StatefulSet.
31+
const (
32+
MongodbRepoUrlEnv = "MONGODB_REPO_URL"
33+
MongodbImageEnv = "MONGODB_IMAGE"
34+
MongoDBImageTypeEnv = "MDB_IMAGE_TYPE"
35+
AgentImageEnv = "AGENT_IMAGE"
36+
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
37+
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
38+
)
39+
3140
const (
3241
AgentName = "mongodb-agent"
3342
MongodbName = "mongod"
@@ -42,18 +51,12 @@ const (
4251
mongodbDatabaseServiceAccountName = "mongodb-database"
4352
agentHealthStatusFilePathValue = "/var/log/mongodb-mms-automation/healthstatus/agent-health-status.json"
4453

45-
MongodbRepoUrl = "MONGODB_REPO_URL"
4654
OfficialMongodbEnterpriseServerImageName = "mongodb-enterprise-server"
4755

4856
headlessAgentEnv = "HEADLESS_AGENT"
4957
podNamespaceEnv = "POD_NAMESPACE"
5058
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
51-
AgentImageEnv = "AGENT_IMAGE"
52-
MongodbImageEnv = "MONGODB_IMAGE"
53-
MongoDBImageType = "MDB_IMAGE_TYPE"
5459
MongoDBAssumeEnterpriseEnv = "MDB_ASSUME_ENTERPRISE"
55-
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
56-
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
5760

5861
automationMongodConfFileName = "automation-mongod.conf"
5962
keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
@@ -123,7 +126,7 @@ type MongoDBStatefulSetOwner interface {
123126
// BuildMongoDBReplicaSetStatefulSetModificationFunction builds the parts of the replica set that are common between every resource that implements
124127
// MongoDBStatefulSetOwner.
125128
// It doesn't configure TLS or additional containers/env vars that the statefulset might need.
126-
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler, agentImage string, withInitContainers bool) statefulset.Modification {
129+
func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSetOwner, scaler scale.ReplicaSetScaler, mongodbRepoUrl, mongodbImage, mongodbImageType, agentImage, versionUpgradeHookImage, readinessProbeImage string, withInitContainers bool) statefulset.Modification {
127130
labels := map[string]string{
128131
"app": mdb.ServiceName(),
129132
}
@@ -174,8 +177,8 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
174177
scriptsVolume = statefulset.CreateVolumeFromEmptyDir("agent-scripts")
175178
scriptsVolumeMount := statefulset.CreateVolumeMount(scriptsVolume.Name, "/opt/scripts", statefulset.WithReadOnly(false))
176179

177-
upgradeInitContainer = podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount}))
178-
readinessInitContainer = podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount}))
180+
upgradeInitContainer = podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount}, versionUpgradeHookImage))
181+
readinessInitContainer = podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount}, readinessProbeImage))
179182
scriptsVolumeMod = podtemplatespec.WithVolume(scriptsVolume)
180183
hooksVolumeMod = podtemplatespec.WithVolume(hooksVolume)
181184

@@ -243,7 +246,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
243246
podtemplatespec.WithVolume(keyFileVolume),
244247
podtemplatespec.WithServiceAccount(mongodbDatabaseServiceAccountName),
245248
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentLogFile, agentMaxLogFileDurationHours, agentImage)),
246-
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(nil), mongodVolumeMounts, mdb.GetMongodConfiguration())),
249+
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mongodbRepoUrl, mongodbImage, mongodbImageType, mdb.GetMongoDBVersion(nil), mongodVolumeMounts, mdb.GetMongodConfiguration())),
247250
upgradeInitContainer,
248251
readinessInitContainer,
249252
),
@@ -312,12 +315,12 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
312315
)
313316
}
314317

315-
func versionUpgradeHookInit(volumeMount []corev1.VolumeMount) container.Modification {
318+
func versionUpgradeHookInit(volumeMount []corev1.VolumeMount, versionUpgradeHookImage string) container.Modification {
316319
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
317320
return container.Apply(
318321
container.WithName(versionUpgradeHookName),
319322
container.WithCommand([]string{"cp", "version-upgrade-hook", "/hooks/version-upgrade"}),
320-
container.WithImage(os.Getenv(VersionUpgradeHookImageEnv)),
323+
container.WithImage(versionUpgradeHookImage),
321324
container.WithResourceRequirements(resourcerequirements.Defaults()),
322325
container.WithImagePullPolicy(corev1.PullAlways),
323326
container.WithVolumeMounts(volumeMount),
@@ -351,38 +354,35 @@ func logsPvc(logsVolumeName string) persistentvolumeclaim.Modification {
351354

352355
// readinessProbeInit returns a modification function which will add the readiness probe container.
353356
// this container will copy the readiness probe binary into the /opt/scripts directory.
354-
func readinessProbeInit(volumeMount []corev1.VolumeMount) container.Modification {
357+
func readinessProbeInit(volumeMount []corev1.VolumeMount, readinessProbeImage string) container.Modification {
355358
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
356359
return container.Apply(
357360
container.WithName(ReadinessProbeContainerName),
358361
container.WithCommand([]string{"cp", "/probes/readinessprobe", "/opt/scripts/readinessprobe"}),
359-
container.WithImage(os.Getenv(ReadinessProbeImageEnv)),
362+
container.WithImage(readinessProbeImage),
360363
container.WithImagePullPolicy(corev1.PullAlways),
361364
container.WithVolumeMounts(volumeMount),
362365
container.WithResourceRequirements(resourcerequirements.Defaults()),
363366
containerSecurityContext,
364367
)
365368
}
366369

367-
func getMongoDBImage(version string) string {
368-
repoUrl := os.Getenv(MongodbRepoUrl)
369-
imageType := envvar.GetEnvOrDefault(MongoDBImageType, DefaultImageType)
370-
370+
func getMongoDBImage(repoUrl, mongodbImage, mongodbImageType, version string) string {
371371
if strings.HasSuffix(repoUrl, "/") {
372372
repoUrl = strings.TrimRight(repoUrl, "/")
373373
}
374-
mongoImageName := os.Getenv(MongodbImageEnv)
374+
mongoImageName := mongodbImage
375375
for _, officialUrl := range OfficialMongodbRepoUrls {
376376
if repoUrl == officialUrl {
377-
return fmt.Sprintf("%s/%s:%s-%s", repoUrl, mongoImageName, version, imageType)
377+
return fmt.Sprintf("%s/%s:%s-%s", repoUrl, mongoImageName, version, mongodbImageType)
378378
}
379379
}
380380

381381
// This is the old images backwards compatibility code path.
382382
return fmt.Sprintf("%s/%s:%s", repoUrl, mongoImageName, version)
383383
}
384384

385-
func mongodbContainer(version string, volumeMounts []corev1.VolumeMount, additionalMongoDBConfig mdbv1.MongodConfiguration) container.Modification {
385+
func mongodbContainer(mongodbRepoUrl, mongodbImage, mongodbImageType, version string, volumeMounts []corev1.VolumeMount, additionalMongoDBConfig mdbv1.MongodConfiguration) container.Modification {
386386
filePath := additionalMongoDBConfig.GetDBDataDir() + "/" + automationMongodConfFileName
387387
mongoDbCommand := fmt.Sprintf(`
388388
if [ -e "/hooks/version-upgrade" ]; then
@@ -408,7 +408,7 @@ exec mongod -f %s;
408408

409409
return container.Apply(
410410
container.WithName(MongodbName),
411-
container.WithImage(getMongoDBImage(version)),
411+
container.WithImage(getMongoDBImage(mongodbRepoUrl, mongodbImage, mongodbImageType, version)),
412412
container.WithResourceRequirements(resourcerequirements.Defaults()),
413413
container.WithCommand(containerCommand),
414414
// The official image provides both CMD and ENTRYPOINT. We're reusing the former and need to replace

0 commit comments

Comments
 (0)
Please sign in to comment.