@@ -43,6 +43,9 @@ const (
4343 headlessAgentEnv = "HEADLESS_AGENT"
4444 podNamespaceEnv = "POD_NAMESPACE"
4545 automationConfigEnv = "AUTOMATION_CONFIG_MAP"
46+
47+ automationconfFilePath = "/data/automation-mongod.conf"
48+ keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
4649)
4750
4851// MongoDBStatefulSetOwner is an interface which any resource which generates a MongoDB StatefulSet should implement.
@@ -112,6 +115,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
112115 statefulset .WithVolumeClaim (logVolumeName , logsPvc ()),
113116 statefulset .WithPodSpecTemplate (
114117 podtemplatespec .Apply (
118+ podtemplatespec .WithSecurityContext (podtemplatespec .DefaultPodSecurityContext ()),
115119 podtemplatespec .WithPodLabels (labels ),
116120 podtemplatespec .WithVolume (healthStatusVolume ),
117121 podtemplatespec .WithVolume (hooksVolume ),
@@ -128,23 +132,34 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
128132}
129133
130134func 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" }, " " )
131143 return container .Apply (
132144 container .WithName (AgentName ),
133145 container .WithImage (os .Getenv (AgentImageEnv )),
134146 container .WithImagePullPolicy (corev1 .PullAlways ),
135147 container .WithReadinessProbe (DefaultReadiness ()),
136148 container .WithResourceRequirements (resourcerequirements .Defaults ()),
137149 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 }),
148163 container .WithEnvs (
149164 corev1.EnvVar {
150165 Name : headlessAgentEnv ,
@@ -227,32 +242,37 @@ func getMongoDBImage(version string) string {
227242}
228243
229244func 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
235247/hooks/version-upgrade
236248
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+
239252
240253# 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 ,
243261 }
244262
245263 return container .Apply (
246264 container .WithName (MongodbName ),
247265 container .WithImage (getMongoDBImage (version )),
248266 container .WithResourceRequirements (resourcerequirements .Defaults ()),
249- container .WithCommand (mongoDbCommand ),
267+ container .WithCommand (containerCommand ),
250268 container .WithEnvs (
251269 corev1.EnvVar {
252270 Name : agentHealthStatusFilePathEnv ,
253271 Value : "/healthstatus/agent-health-status.json" ,
254272 },
255273 ),
256274 container .WithVolumeMounts (volumeMounts ),
275+
276+ container .WithSecurityContext (container .DefaultSecurityContext ()),
257277 )
258278}
0 commit comments