Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit a07365e

Browse files
authored
fix red herring in the readinessProbe (#1504)
* fix red herring * refactor logging
1 parent 2b18764 commit a07365e

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

cmd/readiness/main.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,22 @@ func main() {
221221
panic(err)
222222
}
223223

224-
cfg, err := config.BuildFromEnvVariables(clientSet, isHeadlessMode())
224+
initLogger(config.GetLogger())
225+
226+
healthStatusFilePath := config.GetEnvOrDefault(config.AgentHealthStatusFilePathEnv, config.DefaultAgentHealthStatusFilePath)
227+
file, err := os.Open(healthStatusFilePath)
228+
// The agent might be slow in creating the health status file.
229+
// In that case, we don't want to panic to show the message
230+
// in the kubernetes description. That would be a red herring, since that will solve itself with enough time.
225231
if err != nil {
226-
panic(err)
232+
logger.Errorf("health status file not avaible yet: %s ", err)
233+
os.Exit(1)
227234
}
228235

229-
initLogger(cfg.Logger)
236+
cfg, err := config.BuildFromEnvVariables(clientSet, isHeadlessMode(), file)
237+
if err != nil {
238+
panic(err)
239+
}
230240

231241
ready, err := isPodReady(cfg)
232242
if err != nil {

pkg/readiness/config/config.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313
)
1414

1515
const (
16-
defaultAgentHealthStatusFilePath = "/var/log/mongodb-mms-automation/agent-health-status.json"
17-
defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log"
18-
podNamespaceEnv = "POD_NAMESPACE"
19-
automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint
20-
agentHealthStatusFilePathEnv = "AGENT_STATUS_FILEPATH"
21-
logPathEnv = "LOG_FILE_PATH"
22-
hostNameEnv = "HOSTNAME"
23-
readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS"
24-
readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE"
25-
readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE"
16+
DefaultAgentHealthStatusFilePath = "/var/log/mongodb-mms-automation/agent-health-status.json"
17+
AgentHealthStatusFilePathEnv = "AGENT_STATUS_FILEPATH"
18+
19+
defaultLogPath = "/var/log/mongodb-mms-automation/readiness.log"
20+
podNamespaceEnv = "POD_NAMESPACE"
21+
automationConfigSecretEnv = "AUTOMATION_CONFIG_MAP" //nolint
22+
logPathEnv = "LOG_FILE_PATH"
23+
hostNameEnv = "HOSTNAME"
24+
readinessProbeLoggerBackups = "READINESS_PROBE_LOGGER_BACKUPS"
25+
readinessProbeLoggerMaxSize = "READINESS_PROBE_LOGGER_MAX_SIZE"
26+
readinessProbeLoggerMaxAge = "READINESS_PROBE_LOGGER_MAX_AGE"
2627
)
2728

2829
type Config struct {
@@ -32,12 +33,10 @@ type Config struct {
3233
AutomationConfigSecretName string
3334
HealthStatusReader io.Reader
3435
LogFilePath string
35-
Logger *lumberjack.Logger
3636
}
3737

38-
func BuildFromEnvVariables(clientSet kubernetes.Interface, isHeadless bool) (Config, error) {
39-
healthStatusFilePath := getEnvOrDefault(agentHealthStatusFilePathEnv, defaultAgentHealthStatusFilePath)
40-
logFilePath := getEnvOrDefault(logPathEnv, defaultLogPath)
38+
func BuildFromEnvVariables(clientSet kubernetes.Interface, isHeadless bool, file *os.File) (Config, error) {
39+
logFilePath := GetEnvOrDefault(logPathEnv, defaultLogPath)
4140

4241
var namespace, automationConfigName, hostname string
4342
if isHeadless {
@@ -56,35 +55,33 @@ func BuildFromEnvVariables(clientSet kubernetes.Interface, isHeadless bool) (Con
5655
}
5756
}
5857

59-
logger := &lumberjack.Logger{
60-
Filename: readinessProbeLogFilePath(),
61-
MaxBackups: readIntOrDefault(readinessProbeLoggerBackups, 5),
62-
MaxSize: readInt(readinessProbeLoggerMaxSize),
63-
MaxAge: readInt(readinessProbeLoggerMaxAge),
64-
}
65-
6658
// Note, that we shouldn't close the file here - it will be closed very soon by the 'ioutil.ReadAll'
6759
// in main.go
68-
file, err := os.Open(healthStatusFilePath)
69-
if err != nil {
70-
return Config{}, err
71-
}
7260
return Config{
7361
ClientSet: clientSet,
7462
Namespace: namespace,
7563
AutomationConfigSecretName: automationConfigName,
7664
Hostname: hostname,
7765
HealthStatusReader: file,
7866
LogFilePath: logFilePath,
79-
Logger: logger,
8067
}, nil
8168
}
8269

70+
func GetLogger() *lumberjack.Logger {
71+
logger := &lumberjack.Logger{
72+
Filename: readinessProbeLogFilePath(),
73+
MaxBackups: readIntOrDefault(readinessProbeLoggerBackups, 5),
74+
MaxSize: readInt(readinessProbeLoggerMaxSize),
75+
MaxAge: readInt(readinessProbeLoggerMaxAge),
76+
}
77+
return logger
78+
}
79+
8380
func readinessProbeLogFilePath() string {
84-
return getEnvOrDefault(logPathEnv, defaultLogPath)
81+
return GetEnvOrDefault(logPathEnv, defaultLogPath)
8582
}
8683

87-
func getEnvOrDefault(envVar, defaultValue string) string {
84+
func GetEnvOrDefault(envVar, defaultValue string) string {
8885
value := strings.TrimSpace(os.Getenv(envVar))
8986
if value == "" {
9087
return defaultValue
@@ -101,7 +98,7 @@ func readInt(envVarName string) int {
10198
// readIntOrDefault returns the int value of an envvar of the given name.
10299
// defaults to the given value if not specified.
103100
func readIntOrDefault(envVarName string, defaultValue int) int {
104-
envVar := getEnvOrDefault(envVarName, strconv.Itoa(defaultValue))
101+
envVar := GetEnvOrDefault(envVarName, strconv.Itoa(defaultValue))
105102
intValue, err := strconv.Atoi(envVar)
106103
if err != nil {
107104
return defaultValue

release.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"golang-builder-image": "golang:1.21",
33
"operator": "0.9.0",
44
"version-upgrade-hook": "1.0.8",
5-
"readiness-probe": "1.0.17",
5+
"readiness-probe": "1.0.18",
66
"agent": "107.0.1.8507-1",
77
"agent-tools-version": "100.9.4"
88
}

0 commit comments

Comments
 (0)