@@ -43,6 +43,9 @@ const (
43
43
headlessAgentEnv = "HEADLESS_AGENT"
44
44
podNamespaceEnv = "POD_NAMESPACE"
45
45
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
46
+
47
+ automationconfFilePath = "/data/automation-mongod.conf"
48
+ keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
46
49
)
47
50
48
51
// MongoDBStatefulSetOwner is an interface which any resource which generates a MongoDB StatefulSet should implement.
@@ -112,6 +115,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
112
115
statefulset .WithVolumeClaim (logVolumeName , logsPvc ()),
113
116
statefulset .WithPodSpecTemplate (
114
117
podtemplatespec .Apply (
118
+ podtemplatespec .WithSecurityContext (podtemplatespec .DefaultPodSecurityContext ()),
115
119
podtemplatespec .WithPodLabels (labels ),
116
120
podtemplatespec .WithVolume (healthStatusVolume ),
117
121
podtemplatespec .WithVolume (hooksVolume ),
@@ -128,23 +132,34 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
128
132
}
129
133
130
134
func mongodbAgentContainer (automationConfigSecretName string , volumeMounts []corev1.VolumeMount ) container.Modification {
135
+ agentCommand := strings .Join ([]string {
136
+ "agent/mongodb-agent" ,
137
+ "-cluster=" + clusterFilePath ,
138
+ "-skipMongoStart" ,
139
+ "-noDaemonize" ,
140
+ "-healthCheckFilePath=" + agentHealthStatusFilePathValue ,
141
+ "-serveStatusPort=5000" ,
142
+ "-useLocalMongoDbTools" }, " " )
131
143
return container .Apply (
132
144
container .WithName (AgentName ),
133
145
container .WithImage (os .Getenv (AgentImageEnv )),
134
146
container .WithImagePullPolicy (corev1 .PullAlways ),
135
147
container .WithReadinessProbe (DefaultReadiness ()),
136
148
container .WithResourceRequirements (resourcerequirements .Defaults ()),
137
149
container .WithVolumeMounts (volumeMounts ),
138
- container .WithCommand ([]string {
139
- "agent/mongodb-agent" ,
140
- "-cluster=" + clusterFilePath ,
141
- "-skipMongoStart" ,
142
- "-noDaemonize" ,
143
- "-healthCheckFilePath=" + agentHealthStatusFilePathValue ,
144
- "-serveStatusPort=5000" ,
145
- "-useLocalMongoDbTools" ,
146
- },
147
- ),
150
+ container .WithSecurityContext (container .DefaultSecurityContext ()),
151
+ container .WithCommand ([]string {"/bin/bash" , "-c" , `current_uid=$(id -u)
152
+ echo $current_uid
153
+ declare -r current_uid
154
+ if ! grep -q "${current_uid}" /etc/passwd ; then
155
+ sed -e "s/^mongodb:/builder:/" /etc/passwd > /tmp/passwd
156
+ echo "mongodb:x:$(id -u):$(id -g):,,,:/:/bin/bash" >> /tmp/passwd
157
+ cat /tmp/passwd
158
+ export NSS_WRAPPER_PASSWD=/tmp/passwd
159
+ export LD_PRELOAD=libnss_wrapper.so
160
+ export NSS_WRAPPER_GROUP=/etc/group
161
+ fi
162
+ ` + agentCommand }),
148
163
container .WithEnvs (
149
164
corev1.EnvVar {
150
165
Name : headlessAgentEnv ,
@@ -227,32 +242,37 @@ func getMongoDBImage(version string) string {
227
242
}
228
243
229
244
func mongodbContainer (version string , volumeMounts []corev1.VolumeMount ) container.Modification {
230
- mongoDbCommand := []string {
231
- "/bin/sh" ,
232
- "-c" ,
233
- `
234
- # run post-start hook to handle version changes
245
+ mongoDbCommand := fmt .Sprintf (`
246
+ #run post-start hook to handle version changes
235
247
/hooks/version-upgrade
236
248
237
- # wait for config to be created by the agent
238
- while [ ! -f /data/automation-mongod.conf ]; do sleep 3 ; done ; sleep 2 ;
249
+ # wait for config and keyfile to be created by the agent
250
+ while ! [ -f %s -a -f %s ]; do sleep 3 ; done ; sleep 2 ;
251
+
239
252
240
253
# start mongod with this configuration
241
- exec mongod -f /data/automation-mongod.conf ;
242
- ` ,
254
+ exec mongod -f %s;
255
+ ` , automationconfFilePath , keyfileFilePath , automationconfFilePath )
256
+
257
+ containerCommand := []string {
258
+ "/bin/sh" ,
259
+ "-c" ,
260
+ mongoDbCommand ,
243
261
}
244
262
245
263
return container .Apply (
246
264
container .WithName (MongodbName ),
247
265
container .WithImage (getMongoDBImage (version )),
248
266
container .WithResourceRequirements (resourcerequirements .Defaults ()),
249
- container .WithCommand (mongoDbCommand ),
267
+ container .WithCommand (containerCommand ),
250
268
container .WithEnvs (
251
269
corev1.EnvVar {
252
270
Name : agentHealthStatusFilePathEnv ,
253
271
Value : "/healthstatus/agent-health-status.json" ,
254
272
},
255
273
),
256
274
container .WithVolumeMounts (volumeMounts ),
275
+
276
+ container .WithSecurityContext (container .DefaultSecurityContext ()),
257
277
)
258
278
}
0 commit comments