Skip to content

Commit 6897171

Browse files
authored
Make automation agent log level configurable (#1238)
1 parent ad7348a commit 6897171

File tree

6 files changed

+99
-20
lines changed

6 files changed

+99
-20
lines changed

api/v1/mongodbcommunity_types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ type MongoDBCommunitySpec struct {
9898
// +optional
9999
StatefulSetConfiguration StatefulSetConfiguration `json:"statefulSet,omitempty"`
100100

101+
// AgentConfiguration sets options for the MongoDB automation agent
102+
// +optional
103+
AgentConfiguration AgentConfiguration `json:"agent,omitempty"`
104+
101105
// AdditionalMongodConfig is additional configuration that can be passed to
102106
// each data-bearing mongod at runtime. Uses the same structure as the mongod
103107
// configuration file: https://www.mongodb.com/docs/manual/reference/configuration-options/
@@ -268,6 +272,23 @@ type StatefulSetConfiguration struct {
268272
SpecWrapper StatefulSetSpecWrapper `json:"spec"`
269273
}
270274

275+
type LogLevel string
276+
277+
const (
278+
LogLevelDebug LogLevel = "DEBUG"
279+
LogLevelInfo = "INFO"
280+
LogLevelWarn = "WARN"
281+
LogLevelError = "ERROR"
282+
LogLevelFatal = "FATAL"
283+
)
284+
285+
type AgentConfiguration struct {
286+
// +optional
287+
LogLevel LogLevel `json:"logLevel"`
288+
// +optional
289+
MaxLogFileDurationHours int `json:"maxLogFileDurationHours"`
290+
}
291+
271292
// StatefulSetSpecWrapper is a wrapper around StatefulSetSpec with a custom implementation
272293
// of MarshalJSON and UnmarshalJSON which delegate to the underlying Spec to avoid CRD pollution.
273294

@@ -920,6 +941,14 @@ func (m MongoDBCommunity) NeedsAutomationConfigVolume() bool {
920941
return true
921942
}
922943

944+
func (m MongoDBCommunity) GetAgentLogLevel() LogLevel {
945+
return m.Spec.AgentConfiguration.LogLevel
946+
}
947+
948+
func (m MongoDBCommunity) GetAgentMaxLogFileDurationHours() int {
949+
return m.Spec.AgentConfiguration.MaxLogFileDurationHours
950+
}
951+
923952
type automationConfigReplicasScaler struct {
924953
current, desired int
925954
forceIndividualScaling bool

api/v1/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ spec:
6060
nullable: true
6161
type: object
6262
x-kubernetes-preserve-unknown-fields: true
63+
agent:
64+
description: AgentConfiguration sets options for the MongoDB automation
65+
agent
66+
properties:
67+
logLevel:
68+
type: string
69+
maxLogFileDurationHours:
70+
type: integer
71+
type: object
6372
arbiters:
6473
description: 'Arbiters is the number of arbiters to add to the Replica
6574
Set. It is not recommended to have more than one arbiter per Replica

controllers/construct/build_statefulset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
102102
assert.Equal(t, mdb.Name, sts.Name)
103103
assert.Equal(t, mdb.Namespace, sts.Namespace)
104104
assert.Equal(t, mongodbDatabaseServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName)
105-
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 4)
105+
assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 6)
106106
assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 1)
107107

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

controllers/construct/mongodbstatefulset.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package construct
33
import (
44
"fmt"
55
"os"
6+
"strconv"
67
"strings"
78

89
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
@@ -34,20 +35,21 @@ const (
3435

3536
MongodbRepoUrl = "MONGODB_REPO_URL"
3637

37-
headlessAgentEnv = "HEADLESS_AGENT"
38-
podNamespaceEnv = "POD_NAMESPACE"
39-
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
40-
AgentImageEnv = "AGENT_IMAGE"
41-
MongodbImageEnv = "MONGODB_IMAGE"
42-
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
43-
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
38+
headlessAgentEnv = "HEADLESS_AGENT"
39+
podNamespaceEnv = "POD_NAMESPACE"
40+
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
41+
AgentImageEnv = "AGENT_IMAGE"
42+
MongodbImageEnv = "MONGODB_IMAGE"
43+
VersionUpgradeHookImageEnv = "VERSION_UPGRADE_HOOK_IMAGE"
44+
ReadinessProbeImageEnv = "READINESS_PROBE_IMAGE"
45+
agentLogLevelEnv = "AGENT_LOG_LEVEL"
46+
agentMaxLogFileDurationHoursEnv = "AGENT_MAX_LOG_FILE_DURATION_HOURS"
4447

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

48-
automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"
49-
50-
automationAgentLogOptions = " -logFile /var/log/mongodb-mms-automation/automation-agent.log -maxLogFileDurationHrs 24 -logLevel DEBUG"
51+
automationAgentOptions = " -skipMongoStart -noDaemonize -useLocalMongoDbTools"
52+
automationAgentLogOptions = " -logFile /var/log/mongodb-mms-automation/automation-agent.log -maxLogFileDurationHrs ${AGENT_MAX_LOG_FILE_DURATION_HOURS} -logLevel ${AGENT_LOG_LEVEL}"
5153

5254
MongodbUserCommand = `current_uid=$(id -u)
5355
AGENT_API_KEY="$(cat /mongodb-automation/agent-api-key/agentApiKey)"
@@ -70,7 +72,7 @@ type MongoDBStatefulSetOwner interface {
7072
GetName() string
7173
// GetNamespace returns the namespace the resource is defined in.
7274
GetNamespace() string
73-
// GetMongoDBVersion returns the version of MongoDB to be used for this resource
75+
// GetMongoDBVersion returns the version of MongoDB to be used for this resource.
7476
GetMongoDBVersion() string
7577
// AutomationConfigSecretName returns the name of the secret which will contain the automation config.
7678
AutomationConfigSecretName() string
@@ -80,10 +82,14 @@ type MongoDBStatefulSetOwner interface {
8082
HasSeparateDataAndLogsVolumes() bool
8183
// GetAgentKeyfileSecretNamespacedName returns the NamespacedName of the secret which stores the keyfile for the agent.
8284
GetAgentKeyfileSecretNamespacedName() types.NamespacedName
83-
// DataVolumeName returns the name that the data volume should have
85+
// DataVolumeName returns the name that the data volume should have.
8486
DataVolumeName() string
85-
// LogsVolumeName returns the name that the data volume should have
87+
// LogsVolumeName returns the name that the data volume should have.
8688
LogsVolumeName() string
89+
// GetAgentLogLevel returns the log level for the MongoDB automation agent.
90+
GetAgentLogLevel() mdbv1.LogLevel
91+
// GetAgentMaxLogFileDurationHours returns the number of hours after which the log file should be rolled.
92+
GetAgentMaxLogFileDurationHours() int
8793

8894
// GetMongodConfiguration returns the MongoDB configuration for each member.
8995
GetMongodConfiguration() mdbv1.MongodConfiguration
@@ -156,6 +162,16 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
156162

157163
podSecurityContext, _ := podtemplatespec.WithDefaultSecurityContextsModifications()
158164

165+
agentLogLevel := mdbv1.LogLevelInfo
166+
if mdb.GetAgentLogLevel() != "" {
167+
agentLogLevel = string(mdb.GetAgentLogLevel())
168+
}
169+
170+
agentMaxLogFileDurationHours := automationconfig.DefaultAgentMaxLogFileDurationHours
171+
if mdb.GetAgentMaxLogFileDurationHours() != 0 {
172+
agentMaxLogFileDurationHours = mdb.GetAgentMaxLogFileDurationHours()
173+
}
174+
159175
return statefulset.Apply(
160176
statefulset.WithName(mdb.GetName()),
161177
statefulset.WithNamespace(mdb.GetNamespace()),
@@ -178,7 +194,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
178194
podtemplatespec.WithVolume(tmpVolume),
179195
podtemplatespec.WithVolume(keyFileVolume),
180196
podtemplatespec.WithServiceAccount(mongodbDatabaseServiceAccountName),
181-
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts)),
197+
podtemplatespec.WithContainer(AgentName, mongodbAgentContainer(mdb.AutomationConfigSecretName(), mongodbAgentVolumeMounts, agentLogLevel, agentMaxLogFileDurationHours)),
182198
podtemplatespec.WithContainer(MongodbName, mongodbContainer(mdb.GetMongoDBVersion(), mongodVolumeMounts, mdb.GetMongodConfiguration())),
183199
podtemplatespec.WithInitContainer(versionUpgradeHookName, versionUpgradeHookInit([]corev1.VolumeMount{hooksVolumeMount})),
184200
podtemplatespec.WithInitContainer(ReadinessProbeContainerName, readinessProbeInit([]corev1.VolumeMount{scriptsVolumeMount})),
@@ -194,7 +210,7 @@ func AutomationAgentCommand() []string {
194210
return []string{"/bin/bash", "-c", MongodbUserCommand + BaseAgentCommand() + " -cluster=" + clusterFilePath + automationAgentOptions + automationAgentLogOptions}
195211
}
196212

197-
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount) container.Modification {
213+
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount, logLevel string, maxLogFileDurationHours int) container.Modification {
198214
_, containerSecurityContext := podtemplatespec.WithDefaultSecurityContextsModifications()
199215
return container.Apply(
200216
container.WithName(AgentName),
@@ -227,6 +243,14 @@ func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []cor
227243
Name: agentHealthStatusFilePathEnv,
228244
Value: agentHealthStatusFilePathValue,
229245
},
246+
corev1.EnvVar{
247+
Name: agentLogLevelEnv,
248+
Value: logLevel,
249+
},
250+
corev1.EnvVar{
251+
Name: agentMaxLogFileDurationHoursEnv,
252+
Value: strconv.Itoa(maxLogFileDurationHours),
253+
},
230254
),
231255
)
232256
}

pkg/automationconfig/automation_config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
)
1010

1111
const (
12-
Mongod ProcessType = "mongod"
13-
DefaultMongoDBDataDir string = "/data"
14-
DefaultDBPort int = 27017
15-
DefaultAgentLogPath string = "/var/log/mongodb-mms-automation"
12+
Mongod ProcessType = "mongod"
13+
DefaultMongoDBDataDir string = "/data"
14+
DefaultDBPort int = 27017
15+
DefaultAgentLogPath string = "/var/log/mongodb-mms-automation"
16+
DefaultAgentMaxLogFileDurationHours int = 24
1617
)
1718

1819
type AutomationConfig struct {

0 commit comments

Comments
 (0)