Skip to content

Commit

Permalink
Fix logging to stdout (#1489)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
schans authored Feb 29, 2024
1 parent 353fb62 commit 238e094
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
25 changes: 13 additions & 12 deletions controllers/construct/build_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,19 @@ func TestMongod_Container(t *testing.T) {
})
}

func TestMongoDBAgentLogging_Container(t *testing.T) {
c := container.New(mongodbAgentContainer("test-mongodb-automation-config", []corev1.VolumeMount{}, "INFO", "/var/log/mongodb-mms-automation/automation-agent.log", 24, "image"))
func TestMongoDBAgentCommand(t *testing.T) {
cmd := AutomationAgentCommand(false, "INFO", "testfile", 24)
baseCmd := MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions
assert.Len(t, cmd, 3)
assert.Equal(t, cmd[0], "/bin/bash")
assert.Equal(t, cmd[1], "-c")
assert.Equal(t, cmd[2], baseCmd+" -logFile testfile -logLevel INFO -maxLogFileDurationHrs 24")

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

func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity, sts *appsv1.StatefulSet) {
Expand All @@ -197,7 +198,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
assert.Equal(t, mdb.Name, sts.Name)
assert.Equal(t, mdb.Namespace, sts.Namespace)
assert.Equal(t, mongodbDatabaseServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName)
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 7)
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 4)
assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 1)

managedSecurityContext := envvar.ReadBool(podtemplatespec.ManagedSecurityContextEnv)
Expand Down
56 changes: 26 additions & 30 deletions controllers/construct/mongodbstatefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,20 @@ const (
MongodbRepoUrl = "MONGODB_REPO_URL"
OfficialMongodbEnterpriseServerImageName = "mongodb-enterprise-server"

headlessAgentEnv = "HEADLESS_AGENT"
podNamespaceEnv = "POD_NAMESPACE"
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
AgentImageEnv = "AGENT_IMAGE"
MongodbImageEnv = "MONGODB_IMAGE"
MongoDBImageType = "MDB_IMAGE_TYPE"
MongoDBAssumeEnterpriseEnv = "MDB_ASSUME_ENTERPRISE"
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
agentLogLevelEnv = "AGENT_LOG_LEVEL"
agentLogFileEnv = "AGENT_LOG_FILE"
agentMaxLogFileDurationHoursEnv = "AGENT_MAX_LOG_FILE_DURATION_HOURS"
headlessAgentEnv = "HEADLESS_AGENT"
podNamespaceEnv = "POD_NAMESPACE"
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
AgentImageEnv = "AGENT_IMAGE"
MongodbImageEnv = "MONGODB_IMAGE"
MongoDBImageType = "MDB_IMAGE_TYPE"
MongoDBAssumeEnterpriseEnv = "MDB_ASSUME_ENTERPRISE"
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"

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

automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"
automationAgentLogOptions = " -logFile ${AGENT_LOG_FILE} -maxLogFileDurationHrs ${AGENT_MAX_LOG_FILE_DURATION_HOURS} -logLevel ${AGENT_LOG_LEVEL}"
automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"

MongodbUserCommand = `current_uid=$(id -u)
declare -r current_uid
Expand Down Expand Up @@ -259,11 +255,23 @@ func BaseAgentCommand() string {

// AutomationAgentCommand withAgentAPIKeyExport detects whether we want to deploy this agent with the agent api key exported
// it can be used to register the agent with OM.
func AutomationAgentCommand(withAgentAPIKeyExport bool) []string {
func AutomationAgentCommand(withAgentAPIKeyExport bool, logLevel string, logFile string, maxLogFileDurationHours int) []string {
// This is somewhat undocumented at https://www.mongodb.com/docs/ops-manager/current/reference/mongodb-agent-settings/
// Not setting the -logFile option make the mongodb-agent log to stdout. Setting -logFile /dev/stdout will result in
// an error by the agent trying to open /dev/stdout-verbose and still trying to do log rotation.
// To keep consistent with old behavior not setting the logFile in the config does not log to stdout but keeps
// the default logFile as defined by DefaultAgentLogFile. Setting the logFile explictly to "/dev/stdout" will log to stdout.
agentLogOptions := ""
if logFile != "/dev/stdout" {
agentLogOptions += " -logFile " + logFile + " -logLevel " + logLevel + " -maxLogFileDurationHrs " + strconv.Itoa(maxLogFileDurationHours)
} else {
agentLogOptions += " -logLevel " + logLevel
}

if withAgentAPIKeyExport {
return []string{"/bin/bash", "-c", MongodbUserCommandWithAPIKeyExport + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
return []string{"/bin/bash", "-c", MongodbUserCommandWithAPIKeyExport + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + agentLogOptions}
}
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + agentLogOptions}
}

func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, logFile string, maxLogFileDurationHours int, agentImage string) container.Modification {
Expand All @@ -275,7 +283,7 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
container.WithReadinessProbe(DefaultReadiness()),
container.WithResourceRequirements(resourcerequirements.Defaults()),
container.WithVolumeMounts(volumeMounts),
container.WithCommand(AutomationAgentCommand(false)),
container.WithCommand(AutomationAgentCommand(false, logFile, logLevel, maxLogFileDurationHours)),
containerSecurityContext,
container.WithEnvs(
corev1.EnvVar{
Expand All @@ -299,18 +307,6 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
Name: agentHealthStatusFilePathEnv,
Value: agentHealthStatusFilePathValue,
},
corev1.EnvVar{
Name: agentLogLevelEnv,
Value: logLevel,
},
corev1.EnvVar{
Name: agentLogFileEnv,
Value: logFile,
},
corev1.EnvVar{
Name: agentMaxLogFileDurationHoursEnv,
Value: strconv.Itoa(maxLogFileDurationHours),
},
),
)
}
Expand Down

0 comments on commit 238e094

Please sign in to comment.