Skip to content

Commit 2137883

Browse files
author
Nikolas De Giorgis
authored
Added MANAGED_SECURITY_CONTEXT check and refactored env vars (#401)
1 parent aa5526d commit 2137883

File tree

9 files changed

+55
-59
lines changed

9 files changed

+55
-59
lines changed

cmd/manager/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
88
"github.com/mongodb/mongodb-kubernetes-operator/controllers"
9+
"github.com/mongodb/mongodb-kubernetes-operator/controllers/construct"
910
"go.uber.org/zap"
1011
"k8s.io/apimachinery/pkg/runtime"
1112
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -21,6 +22,10 @@ var (
2122
setupLog = ctrl.Log.WithName("setup")
2223
)
2324

25+
const (
26+
WatchNamespaceEnv = "WATCH_NAMESPACE"
27+
)
28+
2429
func init() {
2530
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
2631

@@ -52,12 +57,12 @@ func main() {
5257
os.Exit(1)
5358
}
5459

55-
if !hasRequiredVariables(log, "AGENT_IMAGE", "VERSION_UPGRADE_HOOK_IMAGE", "READINESS_PROBE_IMAGE") {
60+
if !hasRequiredVariables(log, construct.AgentImageEnv, construct.VersionUpgradeHookImageEnv, construct.ReadinessProbeImageEnv) {
5661
os.Exit(1)
5762
}
5863

5964
// Get watch namespace from environment variable.
60-
namespace, nsSpecified := os.LookupEnv("WATCH_NAMESPACE")
65+
namespace, nsSpecified := os.LookupEnv(WatchNamespaceEnv)
6166
if !nsSpecified {
6267
os.Exit(1)
6368
}

config/samples/mongodb.com_v1_mongodbcommunity_openshift_cr.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ spec:
2525
spec:
2626
serviceName: example-openshift-mongodb-svc
2727
selector: {}
28-
template:
29-
spec:
30-
containers:
31-
- name: "mongodb-agent"
32-
env:
33-
- name: MANAGED_SECURITY_CONTEXT
34-
value: "true"
35-
- name: "mongod"
36-
env:
37-
- name: MANAGED_SECURITY_CONTEXT
38-
value: "true"
3928

4029
# the user credentials will be generated from this secret
4130
# once the credentials are generated, this secret is no longer required

controllers/construct/build_statefulset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func init() {
20-
os.Setenv(versionUpgradeHookImageEnv, "version-upgrade-hook-image")
20+
os.Setenv(VersionUpgradeHookImageEnv, "version-upgrade-hook-image")
2121
}
2222

2323
func newTestReplicaSet() mdbv1.MongoDBCommunity {

controllers/construct/mongodbstatefulset.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/probes"
1313
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/resourcerequirements"
1414
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/statefulset"
15+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
1516
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/scale"
1617
appsv1 "k8s.io/api/apps/v1"
1718
"k8s.io/apimachinery/pkg/types"
@@ -23,7 +24,6 @@ const (
2324
AgentName = "mongodb-agent"
2425
MongodbName = "mongod"
2526

26-
AgentImageEnv = "AGENT_IMAGE"
2727
versionUpgradeHookName = "mongod-posthook"
2828
readinessProbeContainerName = "mongodb-agent-readinessprobe"
2929
dataVolumeName = "data-volume"
@@ -34,18 +34,20 @@ const (
3434
operatorServiceAccountName = "mongodb-kubernetes-operator"
3535
agentHealthStatusFilePathValue = "/var/log/mongodb-mms-automation/healthstatus/agent-health-status.json"
3636

37-
readinessProbeImageEnv = "READINESS_PROBE_IMAGE"
37+
MongodbRepoUrl = "MONGODB_REPO_URL"
3838

39-
MongodbImageEnv = "MONGODB_IMAGE"
40-
MongodbRepoUrl = "MONGODB_REPO_URL"
41-
42-
versionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
43-
headlessAgentEnv = "HEADLESS_AGENT"
44-
podNamespaceEnv = "POD_NAMESPACE"
45-
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
39+
headlessAgentEnv = "HEADLESS_AGENT"
40+
podNamespaceEnv = "POD_NAMESPACE"
41+
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
4642

4743
automationconfFilePath = "/data/automation-mongod.conf"
4844
keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
45+
46+
AgentImageEnv = "AGENT_IMAGE"
47+
MongodbImageEnv = "MONGODB_IMAGE"
48+
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
49+
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
50+
ManagedSecurityContextEnv = "MANAGED_SECURITY_CONTEXT"
4951
)
5052

5153
// MongoDBStatefulSetOwner is an interface which any resource which generates a MongoDB StatefulSet should implement.
@@ -124,6 +126,13 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
124126
singleModeVolumeClaim = statefulset.WithVolumeClaim(dataVolumeName, dataPvc())
125127
}
126128
}
129+
130+
podSecurityContext := podtemplatespec.NOOP()
131+
managedSecurityContext := envvar.ReadBool(ManagedSecurityContextEnv)
132+
if !managedSecurityContext {
133+
podSecurityContext = podtemplatespec.WithSecurityContext(podtemplatespec.DefaultPodSecurityContext())
134+
}
135+
127136
return statefulset.Apply(
128137
statefulset.WithName(mdb.GetName()),
129138
statefulset.WithNamespace(mdb.GetNamespace()),
@@ -137,7 +146,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
137146
singleModeVolumeClaim,
138147
statefulset.WithPodSpecTemplate(
139148
podtemplatespec.Apply(
140-
podtemplatespec.WithSecurityContext(podtemplatespec.DefaultPodSecurityContext()),
149+
podSecurityContext,
141150
podtemplatespec.WithPodLabels(labels),
142151
podtemplatespec.WithVolume(healthStatusVolume),
143152
podtemplatespec.WithVolume(hooksVolume),
@@ -162,14 +171,20 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
162171
"-healthCheckFilePath=" + agentHealthStatusFilePathValue,
163172
"-serveStatusPort=5000",
164173
"-useLocalMongoDbTools"}, " ")
174+
175+
securityContext := container.NOOP()
176+
managedSecurityContext := envvar.ReadBool(ManagedSecurityContextEnv)
177+
if !managedSecurityContext {
178+
securityContext = container.WithSecurityContext(container.DefaultSecurityContext())
179+
}
165180
return container.Apply(
166181
container.WithName(AgentName),
167182
container.WithImage(os.Getenv(AgentImageEnv)),
168183
container.WithImagePullPolicy(corev1.PullAlways),
169184
container.WithReadinessProbe(DefaultReadiness()),
170185
container.WithResourceRequirements(resourcerequirements.Defaults()),
171186
container.WithVolumeMounts(volumeMounts),
172-
container.WithSecurityContext(container.DefaultSecurityContext()),
187+
securityContext,
173188
container.WithCommand([]string{"/bin/bash", "-c", `current_uid=$(id -u)
174189
echo $current_uid
175190
declare -r current_uid
@@ -212,7 +227,7 @@ func versionUpgradeHookInit(volumeMount []corev1.VolumeMount) container.Modifica
212227
return container.Apply(
213228
container.WithName(versionUpgradeHookName),
214229
container.WithCommand([]string{"cp", "version-upgrade-hook", "/hooks/version-upgrade"}),
215-
container.WithImage(os.Getenv(versionUpgradeHookImageEnv)),
230+
container.WithImage(os.Getenv(VersionUpgradeHookImageEnv)),
216231
container.WithImagePullPolicy(corev1.PullAlways),
217232
container.WithVolumeMounts(volumeMount),
218233
)
@@ -248,7 +263,7 @@ func readinessProbeInit(volumeMount []corev1.VolumeMount) container.Modification
248263
return container.Apply(
249264
container.WithName(readinessProbeContainerName),
250265
container.WithCommand([]string{"cp", "/probes/readinessprobe", "/opt/scripts/readinessprobe"}),
251-
container.WithImage(os.Getenv(readinessProbeImageEnv)),
266+
container.WithImage(os.Getenv(ReadinessProbeImageEnv)),
252267
container.WithImagePullPolicy(corev1.PullAlways),
253268
container.WithVolumeMounts(volumeMount),
254269
)
@@ -282,6 +297,12 @@ exec mongod -f %s;
282297
mongoDbCommand,
283298
}
284299

300+
securityContext := container.NOOP()
301+
managedSecurityContext := envvar.ReadBool(ManagedSecurityContextEnv)
302+
if !managedSecurityContext {
303+
securityContext = container.WithSecurityContext(container.DefaultSecurityContext())
304+
}
305+
285306
return container.Apply(
286307
container.WithName(MongodbName),
287308
container.WithImage(getMongoDBImage(version)),
@@ -295,6 +316,6 @@ exec mongod -f %s;
295316
),
296317
container.WithVolumeMounts(volumeMounts),
297318

298-
container.WithSecurityContext(container.DefaultSecurityContext()),
319+
securityContext,
299320
)
300321
}

controllers/replicaset_controller_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
)
4141

4242
func init() {
43-
os.Setenv("AGENT_IMAGE", "agent-image")
43+
os.Setenv(construct.AgentImageEnv, "agent-image")
4444
}
4545

4646
func newTestReplicaSet() mdbv1.MongoDBCommunity {
@@ -561,12 +561,6 @@ func TestReplicaSet_IsScaledUpToDesiredMembers_WhenFirstCreated(t *testing.T) {
561561
assert.Equal(t, 3, mdb.Status.CurrentMongoDBMembers)
562562
}
563563

564-
func TestOpenshift_Configuration(t *testing.T) {
565-
sts := performReconciliationAndGetStatefulSet(t, "openshift_mdb.yaml")
566-
assert.Equal(t, "MANAGED_SECURITY_CONTEXT", sts.Spec.Template.Spec.Containers[1].Env[3].Name)
567-
assert.Equal(t, "MANAGED_SECURITY_CONTEXT", sts.Spec.Template.Spec.Containers[0].Env[1].Name)
568-
}
569-
570564
func TestVolumeClaimTemplates_Configuration(t *testing.T) {
571565
sts := performReconciliationAndGetStatefulSet(t, "volume_claim_templates_mdb.yaml")
572566

controllers/testdata/openshift_mdb.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,3 @@ spec:
1919
db: admin
2020
- name: userAdminAnyDatabase
2121
db: admin
22-
statefulSet:
23-
spec:
24-
template:
25-
spec:
26-
containers:
27-
- name: "mongodb-agent"
28-
env:
29-
- name: MANAGED_SECURITY_CONTEXT
30-
value: "true"
31-
- name: "mongod"
32-
env:
33-
- name: MANAGED_SECURITY_CONTEXT
34-
value: "true"

docs/deploy-configure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ To upgrade this resource from `4.0.6` to `4.2.7`:
128128

129129
## Deploy Replica Sets on OpenShift
130130

131-
To deploy the operator on OpenShift you will have to provide the environment variable `MANAGED_SECURITY_CONTEXT` set to `true` for both the `mongodb` and `mongodb-agent` containers, as well as the operator deployment.
131+
To deploy the operator on OpenShift you will have to provide the environment variable `MANAGED_SECURITY_CONTEXT` set to `true` for the operator deployment.
132132

133133
See [here](/config/samples/mongodb.com_v1_mongodbcommunity_openshift_cr.yaml) for
134134
an example of how to provide the required configuration for a MongoDB

test/e2e/setup/setup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path"
1010
"testing"
1111

12+
"github.com/mongodb/mongodb-kubernetes-operator/controllers/construct"
1213
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/generate"
1314
"github.com/pkg/errors"
1415
appsv1 "k8s.io/api/apps/v1"
@@ -145,7 +146,7 @@ func deployOperator() error {
145146
withOperatorImage(testConfig.operatorImage),
146147
withVersionUpgradeHookImage(testConfig.versionUpgradeHookImage),
147148
withEnvVar("WATCH_NAMESPACE", watchNamespace),
148-
withEnvVar("AGENT_IMAGE", testConfig.agentImage),
149+
withEnvVar(construct.AgentImageEnv, testConfig.agentImage),
149150
); err != nil {
150151
return errors.Errorf("error building operator deployment: %s", err)
151152
}
@@ -240,7 +241,7 @@ func withVersionUpgradeHookImage(image string) func(runtime.Object) {
240241
return func(obj runtime.Object) {
241242
if dep, ok := obj.(*appsv1.Deployment); ok {
242243
versionUpgradeHookEnv := corev1.EnvVar{
243-
Name: "VERSION_UPGRADE_HOOK_IMAGE",
244+
Name: construct.VersionUpgradeHookImageEnv,
244245
Value: image,
245246
}
246247
found := false

test/e2e/setup/test_config.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package setup
22

33
import (
4+
"github.com/mongodb/mongodb-kubernetes-operator/controllers/construct"
45
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
56
)
67

78
const (
8-
testNamespaceEnvName = "TEST_NAMESPACE"
9-
operatorImageEnvName = "OPERATOR_IMAGE"
10-
agentImage = "AGENT_IMAGE"
11-
clusterWideEnvName = "CLUSTER_WIDE"
12-
versionUpgradeHookEnvName = "VERSION_UPGRADE_HOOK_IMAGE"
13-
performCleanupEnvName = "PERFORM_CLEANUP"
9+
testNamespaceEnvName = "TEST_NAMESPACE"
10+
operatorImageEnvName = "OPERATOR_IMAGE"
11+
clusterWideEnvName = "CLUSTER_WIDE"
12+
performCleanupEnvName = "PERFORM_CLEANUP"
1413
)
1514

1615
type testConfig struct {
@@ -26,8 +25,8 @@ func loadTestConfigFromEnv() testConfig {
2625
return testConfig{
2726
namespace: envvar.GetEnvOrDefault(testNamespaceEnvName, "default"),
2827
operatorImage: envvar.GetEnvOrDefault(operatorImageEnvName, "quay.io/mongodb/community-operator-dev:latest"),
29-
versionUpgradeHookImage: envvar.GetEnvOrDefault(versionUpgradeHookEnvName, "quay.io/mongodb/mongodb-kubernetes-operator-version-upgrade-post-start-hook:1.0.2"),
30-
agentImage: envvar.GetEnvOrDefault(agentImage, "quay.io/mongodb/mongodb-agent:10.27.0.6772-1"), // TODO: better way to decide default agent image.
28+
versionUpgradeHookImage: envvar.GetEnvOrDefault(construct.VersionUpgradeHookImageEnv, "quay.io/mongodb/mongodb-kubernetes-operator-version-upgrade-post-start-hook:1.0.2"),
29+
agentImage: envvar.GetEnvOrDefault(construct.AgentImageEnv, "quay.io/mongodb/mongodb-agent:10.27.0.6772-1"), // TODO: better way to decide default agent image.
3130
clusterWide: envvar.ReadBool(clusterWideEnvName),
3231
performCleanup: envvar.ReadBool(performCleanupEnvName),
3332
}

0 commit comments

Comments
 (0)