Skip to content

Commit 83a7e14

Browse files
authored
CLOUDP-242559 - expose readinessProbe settings, add documentation (#1566)
* expose readinessProbe settings, add documentation * update documentation
1 parent 08428a4 commit 83a7e14

File tree

7 files changed

+175
-16
lines changed

7 files changed

+175
-16
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ See the [documentation](docs) to learn how to:
3333

3434
1. [Install or upgrade](docs/install-upgrade.md) the Operator.
3535
1. [Deploy and configure](docs/deploy-configure.md) MongoDB resources.
36+
1. [Configure Logging](docs/logging.md) of the MongoDB resource components.
3637
1. [Create a database user](docs/users.md) with SCRAM authentication.
3738
1. [Secure MongoDB resource connections](docs/secure.md) using TLS.
3839

controllers/construct/mongodbstatefulset.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package construct
22

33
import (
44
"fmt"
5+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config"
56
"os"
67
"strconv"
78
"strings"
@@ -262,10 +263,10 @@ func AutomationAgentCommand(withAgentAPIKeyExport bool, logLevel string, logFile
262263
// To keep consistent with old behavior not setting the logFile in the config does not log to stdout but keeps
263264
// the default logFile as defined by DefaultAgentLogFile. Setting the logFile explictly to "/dev/stdout" will log to stdout.
264265
agentLogOptions := ""
265-
if logFile != "/dev/stdout" {
266-
agentLogOptions += " -logFile " + logFile + " -logLevel " + logLevel + " -maxLogFileDurationHrs " + strconv.Itoa(maxLogFileDurationHours)
267-
} else {
266+
if logFile == "/dev/stdout" {
268267
agentLogOptions += " -logLevel " + logLevel
268+
} else {
269+
agentLogOptions += " -logFile " + logFile + " -logLevel " + logLevel + " -maxLogFileDurationHrs " + strconv.Itoa(maxLogFileDurationHours)
269270
}
270271

271272
if withAgentAPIKeyExport {
@@ -415,11 +416,37 @@ exec mongod -f %s;
415416
container.WithArgs([]string{""}),
416417
containerSecurityContext,
417418
container.WithEnvs(
418-
corev1.EnvVar{
419-
Name: agentHealthStatusFilePathEnv,
420-
Value: "/healthstatus/agent-health-status.json",
421-
},
419+
collectEnvVars()...,
422420
),
423421
container.WithVolumeMounts(volumeMounts),
424422
)
425423
}
424+
425+
// Function to collect and return the environment variables to be used in the
426+
// MongoDB container.
427+
func collectEnvVars() []corev1.EnvVar {
428+
var envVars []corev1.EnvVar
429+
430+
envVars = append(envVars, corev1.EnvVar{
431+
Name: agentHealthStatusFilePathEnv,
432+
Value: "/healthstatus/agent-health-status.json",
433+
})
434+
435+
addEnvVarIfSet := func(name string) {
436+
value := os.Getenv(name)
437+
if value != "" {
438+
envVars = append(envVars, corev1.EnvVar{
439+
Name: name,
440+
Value: value,
441+
})
442+
}
443+
}
444+
445+
addEnvVarIfSet(config.ReadinessProbeLoggerBackups)
446+
addEnvVarIfSet(config.ReadinessProbeLoggerMaxSize)
447+
addEnvVarIfSet(config.ReadinessProbeLoggerMaxAge)
448+
addEnvVarIfSet(config.ReadinessProbeLoggerCompress)
449+
addEnvVarIfSet(config.WithAgentFileLogging)
450+
451+
return envVars
452+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package construct
2+
3+
import (
4+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config"
5+
"github.com/stretchr/testify/assert"
6+
corev1 "k8s.io/api/core/v1"
7+
"testing"
8+
)
9+
10+
func TestCollectEnvVars(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
envSetup map[string]string
14+
expectedEnv []corev1.EnvVar
15+
}{
16+
{
17+
name: "Basic env vars set",
18+
envSetup: map[string]string{
19+
config.ReadinessProbeLoggerBackups: "3",
20+
config.ReadinessProbeLoggerMaxSize: "10M",
21+
config.ReadinessProbeLoggerMaxAge: "7",
22+
config.WithAgentFileLogging: "enabled",
23+
},
24+
expectedEnv: []corev1.EnvVar{
25+
{
26+
Name: config.AgentHealthStatusFilePathEnv,
27+
Value: "/healthstatus/agent-health-status.json",
28+
},
29+
{
30+
Name: config.ReadinessProbeLoggerBackups,
31+
Value: "3",
32+
},
33+
{
34+
Name: config.ReadinessProbeLoggerMaxSize,
35+
Value: "10M",
36+
},
37+
{
38+
Name: config.ReadinessProbeLoggerMaxAge,
39+
Value: "7",
40+
},
41+
{
42+
Name: config.WithAgentFileLogging,
43+
Value: "enabled",
44+
},
45+
},
46+
},
47+
{
48+
name: "Additional env var set",
49+
envSetup: map[string]string{
50+
config.ReadinessProbeLoggerBackups: "3",
51+
config.ReadinessProbeLoggerMaxSize: "10M",
52+
config.ReadinessProbeLoggerMaxAge: "7",
53+
config.ReadinessProbeLoggerCompress: "true",
54+
config.WithAgentFileLogging: "enabled",
55+
},
56+
expectedEnv: []corev1.EnvVar{
57+
{
58+
Name: config.AgentHealthStatusFilePathEnv,
59+
Value: "/healthstatus/agent-health-status.json",
60+
},
61+
{
62+
Name: config.ReadinessProbeLoggerBackups,
63+
Value: "3",
64+
},
65+
{
66+
Name: config.ReadinessProbeLoggerMaxSize,
67+
Value: "10M",
68+
},
69+
{
70+
Name: config.ReadinessProbeLoggerMaxAge,
71+
Value: "7",
72+
},
73+
{
74+
Name: config.ReadinessProbeLoggerCompress,
75+
Value: "true",
76+
},
77+
{
78+
Name: config.WithAgentFileLogging,
79+
Value: "enabled",
80+
},
81+
},
82+
},
83+
}
84+
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
// Setup environment variables
88+
for key, value := range tt.envSetup {
89+
t.Setenv(key, value)
90+
}
91+
92+
actualEnvVars := collectEnvVars()
93+
94+
assert.EqualValues(t, tt.expectedEnv, actualEnvVars)
95+
})
96+
}
97+
}

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
- [MongoDB Community Kubernetes Operator Architecture](architecture.md)
77
- [Install and Upgrade the Community Kubernetes Operator](install-upgrade.md)
88
- [Deploy and Configure MongoDBCommunity Resources](deploy-configure.md)
9+
- [Configure Logging of the MongoDB components](logging.md)
910
- [Create Database Users](users.md)
1011
- [Secure MongoDBCommunity Resources](secure.md)

docs/contributing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ kubectl get pods
140140

141141
#### (Optional) Create a MongoDBCommunity Resource
142142

143-
Follow the steps outlined [here](./deploy-configure.md) to deploy some resource.
143+
Follow the steps outlined [here](./deploy-configure.md) to deploy some resources.
144144

145145
#### Cleanup
146146
To remove the operator and any created resources you can run

docs/logging.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Configure Logging in MongoDB Community
2+
3+
This section describes the components which are logging either to a file or stdout,
4+
how to configure them and what their defaults are.
5+
6+
## MongoDB Processes
7+
### Configuration
8+
The exposed CRD options can be seen [in the crd yaml](https://github.com/mongodb/mongodb-kubernetes-operator/blob/74d13f189566574b862e5670b366b61ec5b65923/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml#L105-L117).
9+
Additionally, more information regarding configuring systemLog can be found [in the official documentation of systemLog](https://www.mongodb.com/docs/manual/reference/configuration-options/#core-options)].
10+
`spec.agent.systemLog.destination` configures the logging destination of the mongod process.
11+
### Default Values
12+
By default, MongoDB sends all log output to standard output.
13+
14+
## MongoDB Agent
15+
### Configuration
16+
`spec.agent.logFile` can be used to configure the output file of the mongoDB agent logging.
17+
The agent will log to standard output with the following setting: `/dev/stdout`.
18+
### Default Values
19+
By default, the MongoDB agent logs to `/var/log/mongodb-mms-automation/automation-agent.log`
20+
21+
## ReadinessProbe
22+
### Configuration & Default Values
23+
The readinessProbe can be configured via Environment variables.
24+
Below is a table with each environment variable, its explanation and its default value.
25+
26+
| Environment Variable | Explanation | Default Value |
27+
|---------------------------------|-------------------------------------------------------------------------|-----------------------------------------------|
28+
| READINESS_PROBE_LOGGER_BACKUPS | maximum number of old log files to retain | 5 |
29+
| READINESS_PROBE_LOGGER_MAX_SIZE | maximum size in megabytes | 5 |
30+
| READINESS_PROBE_LOGGER_MAX_AGE | maximum number of days to retain old log files | none |
31+
| READINESS_PROBE_LOGGER_COMPRESS | if the rotated log files should be compressed | false |
32+
| MDB_WITH_AGENT_FILE_LOGGING | whether we should also log to stdout (which shows in kubectl describe) | true |
33+
| LOG_FILE_PATH | path of the logfile of the readinessProbe. | /var/log/mongodb-mms-automation/readiness.log |

pkg/readiness/config/config.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ const (
2222
automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint
2323
logPathEnv = "LOG_FILE_PATH"
2424
hostNameEnv = "HOSTNAME"
25-
readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS"
26-
readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE"
27-
readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE"
28-
readinessProbeLoggerCompress = "READINESS_PROBE_LOGGER_COMPRESS"
25+
ReadinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS"
26+
ReadinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE"
27+
ReadinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE"
28+
ReadinessProbeLoggerCompress = "READINESS_PROBE_LOGGER_COMPRESS"
2929
)
3030

3131
type Config struct {
@@ -72,10 +72,10 @@ func BuildFromEnvVariables(clientSet kubernetes.Interface, isHeadless bool, file
7272
func GetLogger() *lumberjack.Logger {
7373
logger := &lumberjack.Logger{
7474
Filename: readinessProbeLogFilePath(),
75-
MaxBackups: readIntOrDefault(readinessProbeLoggerBackups, 5),
76-
MaxSize: readIntOrDefault(readinessProbeLoggerMaxSize, 5),
77-
MaxAge: readInt(readinessProbeLoggerMaxAge),
78-
Compress: ReadBoolWitDefault(readinessProbeLoggerCompress, "false"),
75+
MaxBackups: readIntOrDefault(ReadinessProbeLoggerBackups, 5),
76+
MaxSize: readIntOrDefault(ReadinessProbeLoggerMaxSize, 5),
77+
MaxAge: readInt(ReadinessProbeLoggerMaxAge),
78+
Compress: ReadBoolWitDefault(ReadinessProbeLoggerCompress, "false"),
7979
}
8080
return logger
8181
}

0 commit comments

Comments
 (0)