From 6252bf7c4cd829eeb410e6dc5147e2bd1acebe81 Mon Sep 17 00:00:00 2001 From: Catalin Codreanu <6771910+ccodreanu@users.noreply.github.com> Date: Mon, 18 Sep 2023 10:04:12 +0200 Subject: [PATCH] Make agent log file configurable (#1360) --- api/v1/mongodbcommunity_types.go | 6 ++++++ ...ommunity.mongodb.com_mongodbcommunity.yaml | 2 ++ .../construct/build_statefulset_test.go | 16 +++++++++++++- controllers/construct/mongodbstatefulset.go | 21 +++++++++++++++---- pkg/automationconfig/automation_config.go | 1 + 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/api/v1/mongodbcommunity_types.go b/api/v1/mongodbcommunity_types.go index d99cc311c..0153e22e6 100644 --- a/api/v1/mongodbcommunity_types.go +++ b/api/v1/mongodbcommunity_types.go @@ -355,6 +355,8 @@ type AgentConfiguration struct { // +optional LogLevel LogLevel `json:"logLevel"` // +optional + LogFile string `json:"logFile"` + // +optional MaxLogFileDurationHours int `json:"maxLogFileDurationHours"` // +optional // LogRotate if enabled, will enable LogRotate for all processes. @@ -1154,6 +1156,10 @@ func (m MongoDBCommunity) GetAgentLogLevel() LogLevel { return m.Spec.AgentConfiguration.LogLevel } +func (m MongoDBCommunity) GetAgentLogFile() string { + return m.Spec.AgentConfiguration.LogFile +} + func (m MongoDBCommunity) GetAgentMaxLogFileDurationHours() int { return m.Spec.AgentConfiguration.MaxLogFileDurationHours } diff --git a/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml b/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml index 353188339..f903a1b53 100644 --- a/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml +++ b/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml @@ -69,6 +69,8 @@ spec: description: AgentConfiguration sets options for the MongoDB automation agent properties: + logFile: + type: string logLevel: type: string logRotate: diff --git a/controllers/construct/build_statefulset_test.go b/controllers/construct/build_statefulset_test.go index cd265782f..a369464a1 100644 --- a/controllers/construct/build_statefulset_test.go +++ b/controllers/construct/build_statefulset_test.go @@ -176,6 +176,20 @@ 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)) + + 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) + }) +} + func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity, sts *appsv1.StatefulSet) { assert.Len(t, sts.Spec.Template.Spec.Containers, 2) assert.Len(t, sts.Spec.Template.Spec.InitContainers, 2) @@ -183,7 +197,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, 6) + assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 7) assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 1) managedSecurityContext := envvar.ReadBool(podtemplatespec.ManagedSecurityContextEnv) diff --git a/controllers/construct/mongodbstatefulset.go b/controllers/construct/mongodbstatefulset.go index 35e495637..876d61505 100644 --- a/controllers/construct/mongodbstatefulset.go +++ b/controllers/construct/mongodbstatefulset.go @@ -2,11 +2,12 @@ package construct import ( "fmt" - "github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar" "os" "strconv" "strings" + "github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar" + "github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig" "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/container" "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/persistentvolumeclaim" @@ -53,13 +54,14 @@ const ( VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE" ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE" agentLogLevelEnv = "AGENT_LOG_LEVEL" + agentLogFileEnv = "AGENT_LOG_FILE" agentMaxLogFileDurationHoursEnv = "AGENT_MAX_LOG_FILE_DURATION_HOURS" automationMongodConfFileName = "automation-mongod.conf" keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile" automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools" - automationAgentLogOptions = " -logFile /var/log/mongodb-mms-automation/automation-agent.log -maxLogFileDurationHrs ${AGENT_MAX_LOG_FILE_DURATION_HOURS} -logLevel ${AGENT_LOG_LEVEL}" + automationAgentLogOptions = " -logFile ${AGENT_LOG_FILE} -maxLogFileDurationHrs ${AGENT_MAX_LOG_FILE_DURATION_HOURS} -logLevel ${AGENT_LOG_LEVEL}" MongodbUserCommand = `current_uid=$(id -u) AGENT_API_KEY="$(cat /mongodb-automation/agent-api-key/agentApiKey)" @@ -98,6 +100,8 @@ type MongoDBStatefulSetOwner interface { LogsVolumeName() string // GetAgentLogLevel returns the log level for the MongoDB automation agent. GetAgentLogLevel() mdbv1.LogLevel + // GetAgentLogFile returns the log file for the MongoDB automation agent. + GetAgentLogFile() string // GetAgentMaxLogFileDurationHours returns the number of hours after which the log file should be rolled. GetAgentMaxLogFileDurationHours() int @@ -177,6 +181,11 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe agentLogLevel = string(mdb.GetAgentLogLevel()) } + agentLogFile := automationconfig.DefaultAgentLogFile + if mdb.GetAgentLogFile() != "" { + agentLogFile = mdb.GetAgentLogFile() + } + agentMaxLogFileDurationHours := automationconfig.DefaultAgentMaxLogFileDurationHours if mdb.GetAgentMaxLogFileDurationHours() != 0 { agentMaxLogFileDurationHours = mdb.GetAgentMaxLogFileDurationHours() @@ -204,7 +213,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe podtemplatespec.WithVolume(tmpVolume), podtemplatespec.WithVolume(keyFileVolume), podtemplatespec.WithServiceAccount(mongodbDatabaseServiceAccountName), - podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentMaxLogFileDurationHours)), + podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentLogFile, agentMaxLogFileDurationHours)), podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(), mongodVolumeMounts, mdb.GetMongodConfiguration())), podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount})), podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount})), @@ -220,7 +229,7 @@ func AutomationAgentCommand() []string { return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions} } -func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, maxLogFileDurationHours int) container.Modification { +func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, logFile string, maxLogFileDurationHours int) container.Modification { _, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications() return container.Apply( container.WithName(AgentName), @@ -257,6 +266,10 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor Name: agentLogLevelEnv, Value: logLevel, }, + corev1.EnvVar{ + Name: agentLogFileEnv, + Value: logFile, + }, corev1.EnvVar{ Name: agentMaxLogFileDurationHoursEnv, Value: strconv.Itoa(maxLogFileDurationHours), diff --git a/pkg/automationconfig/automation_config.go b/pkg/automationconfig/automation_config.go index d1cad9fa9..dac98a16b 100644 --- a/pkg/automationconfig/automation_config.go +++ b/pkg/automationconfig/automation_config.go @@ -14,6 +14,7 @@ const ( DefaultMongoDBDataDir string = "/data" DefaultDBPort int = 27017 DefaultAgentLogPath string = "/var/log/mongodb-mms-automation" + DefaultAgentLogFile string = "/var/log/mongodb-mms-automation/automation-agent.log" DefaultAgentMaxLogFileDurationHours int = 24 )