Skip to content

Commit

Permalink
Make agent log file configurable (#1360)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccodreanu authored Sep 18, 2023
1 parent 424bb8a commit 6252bf7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
6 changes: 6 additions & 0 deletions api/v1/mongodbcommunity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ spec:
description: AgentConfiguration sets options for the MongoDB automation
agent
properties:
logFile:
type: string
logLevel:
type: string
logRotate:
Expand Down
16 changes: 15 additions & 1 deletion controllers/construct/build_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,28 @@ 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)
assert.Equal(t, mdb.ServiceName(), sts.Spec.ServiceName)
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)
Expand Down
21 changes: 17 additions & 4 deletions controllers/construct/mongodbstatefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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})),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions pkg/automationconfig/automation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit 6252bf7

Please sign in to comment.