Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 238e094

Browse files
authored
Fix logging to stdout (#1489)
* Fix logging to stdout Allow for setting logFile to /dev/stdout for logging to stdout. Log settings are set directly in the mongodb-agent command instead of passing through the environment.
1 parent 353fb62 commit 238e094

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

controllers/construct/build_statefulset_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,19 @@ func TestMongod_Container(t *testing.T) {
176176
})
177177
}
178178

179-
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, "image"))
179+
func TestMongoDBAgentCommand(t *testing.T) {
180+
cmd := AutomationAgentCommand(false, "INFO", "testfile", 24)
181+
baseCmd := MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions
182+
assert.Len(t, cmd, 3)
183+
assert.Equal(t, cmd[0], "/bin/bash")
184+
assert.Equal(t, cmd[1], "-c")
185+
assert.Equal(t, cmd[2], baseCmd+" -logFile testfile -logLevel INFO -maxLogFileDurationHrs 24")
181186

182-
t.Run("Has correct Env vars", func(t *testing.T) {
183-
assert.Len(t, c.Env, 7)
184-
assert.Equal(t, agentLogFileEnv, c.Env[0].Name)
185-
assert.Equal(t, "/var/log/mongodb-mms-automation/automation-agent.log", c.Env[0].Value)
186-
assert.Equal(t, agentLogLevelEnv, c.Env[1].Name)
187-
assert.Equal(t, "INFO", c.Env[1].Value)
188-
assert.Equal(t, agentMaxLogFileDurationHoursEnv, c.Env[2].Name)
189-
assert.Equal(t, "24", c.Env[2].Value)
190-
})
187+
cmd = AutomationAgentCommand(false, "INFO", "/dev/stdout", 24)
188+
assert.Len(t, cmd, 3)
189+
assert.Equal(t, cmd[0], "/bin/bash")
190+
assert.Equal(t, cmd[1], "-c")
191+
assert.Equal(t, cmd[2], baseCmd+" -logLevel INFO")
191192
}
192193

193194
func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity, sts *appsv1.StatefulSet) {
@@ -197,7 +198,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
197198
assert.Equal(t, mdb.Name, sts.Name)
198199
assert.Equal(t, mdb.Namespace, sts.Namespace)
199200
assert.Equal(t, mongodbDatabaseServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName)
200-
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 7)
201+
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 4)
201202
assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 1)
202203

203204
managedSecurityContext := envvar.ReadBool(podtemplatespec.ManagedSecurityContextEnv)

controllers/construct/mongodbstatefulset.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,20 @@ const (
4444
MongodbRepoUrl = "MONGODB_REPO_URL"
4545
OfficialMongodbEnterpriseServerImageName = "mongodb-enterprise-server"
4646

47-
headlessAgentEnv = "HEADLESS_AGENT"
48-
podNamespaceEnv = "POD_NAMESPACE"
49-
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
50-
AgentImageEnv = "AGENT_IMAGE"
51-
MongodbImageEnv = "MONGODB_IMAGE"
52-
MongoDBImageType = "MDB_IMAGE_TYPE"
53-
MongoDBAssumeEnterpriseEnv = "MDB_ASSUME_ENTERPRISE"
54-
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
55-
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
56-
agentLogLevelEnv = "AGENT_LOG_LEVEL"
57-
agentLogFileEnv = "AGENT_LOG_FILE"
58-
agentMaxLogFileDurationHoursEnv = "AGENT_MAX_LOG_FILE_DURATION_HOURS"
47+
headlessAgentEnv = "HEADLESS_AGENT"
48+
podNamespaceEnv = "POD_NAMESPACE"
49+
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
50+
AgentImageEnv = "AGENT_IMAGE"
51+
MongodbImageEnv = "MONGODB_IMAGE"
52+
MongoDBImageType = "MDB_IMAGE_TYPE"
53+
MongoDBAssumeEnterpriseEnv = "MDB_ASSUME_ENTERPRISE"
54+
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
55+
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
5956

6057
automationMongodConfFileName = "automation-mongod.conf"
6158
keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
6259

63-
automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"
64-
automationAgentLogOptions = " -logFile ${AGENT_LOG_FILE} -maxLogFileDurationHrs ${AGENT_MAX_LOG_FILE_DURATION_HOURS} -logLevel ${AGENT_LOG_LEVEL}"
60+
automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"
6561

6662
MongodbUserCommand = `current_uid=$(id -u)
6763
declare -r current_uid
@@ -259,11 +255,23 @@ func BaseAgentCommand() string {
259255

260256
// AutomationAgentCommand withAgentAPIKeyExport detects whether we want to deploy this agent with the agent api key exported
261257
// it can be used to register the agent with OM.
262-
func AutomationAgentCommand(withAgentAPIKeyExport bool) []string {
258+
func AutomationAgentCommand(withAgentAPIKeyExport bool, logLevel string, logFile string, maxLogFileDurationHours int) []string {
259+
// This is somewhat undocumented at https://www.mongodb.com/docs/ops-manager/current/reference/mongodb-agent-settings/
260+
// Not setting the -logFile option make the mongodb-agent log to stdout. Setting -logFile /dev/stdout will result in
261+
// an error by the agent trying to open /dev/stdout-verbose and still trying to do log rotation.
262+
// To keep consistent with old behavior not setting the logFile in the config does not log to stdout but keeps
263+
// the default logFile as defined by DefaultAgentLogFile. Setting the logFile explictly to "/dev/stdout" will log to stdout.
264+
agentLogOptions := ""
265+
if logFile != "/dev/stdout" {
266+
agentLogOptions += " -logFile " + logFile + " -logLevel " + logLevel + " -maxLogFileDurationHrs " + strconv.Itoa(maxLogFileDurationHours)
267+
} else {
268+
agentLogOptions += " -logLevel " + logLevel
269+
}
270+
263271
if withAgentAPIKeyExport {
264-
return []string{"/bin/bash", "-c", MongodbUserCommandWithAPIKeyExport + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
272+
return []string{"/bin/bash", "-c", MongodbUserCommandWithAPIKeyExport + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + agentLogOptions}
265273
}
266-
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
274+
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + agentLogOptions}
267275
}
268276

269277
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, logFile string, maxLogFileDurationHours int, agentImage string) container.Modification {
@@ -275,7 +283,7 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
275283
container.WithReadinessProbe(DefaultReadiness()),
276284
container.WithResourceRequirements(resourcerequirements.Defaults()),
277285
container.WithVolumeMounts(volumeMounts),
278-
container.WithCommand(AutomationAgentCommand(false)),
286+
container.WithCommand(AutomationAgentCommand(false, logFile, logLevel, maxLogFileDurationHours)),
279287
containerSecurityContext,
280288
container.WithEnvs(
281289
corev1.EnvVar{
@@ -299,18 +307,6 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
299307
Name: agentHealthStatusFilePathEnv,
300308
Value: agentHealthStatusFilePathValue,
301309
},
302-
corev1.EnvVar{
303-
Name: agentLogLevelEnv,
304-
Value: logLevel,
305-
},
306-
corev1.EnvVar{
307-
Name: agentLogFileEnv,
308-
Value: logFile,
309-
},
310-
corev1.EnvVar{
311-
Name: agentMaxLogFileDurationHoursEnv,
312-
Value: strconv.Itoa(maxLogFileDurationHours),
313-
},
314310
),
315311
)
316312
}

0 commit comments

Comments
 (0)