Skip to content

Commit 196e120

Browse files
fix(reporter): reject non-positive CHECK_INTERVAL and HEARTBEAT_PERIOD (#367)
1 parent af1f57d commit 196e120

2 files changed

Lines changed: 62 additions & 25 deletions

File tree

cmd/readiness-condition-reporter/main.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ type HealthResponse struct {
5555
Message string `json:"message"`
5656
}
5757

58+
func parseDurationWithDefault(input string, defaultVal time.Duration, name string) time.Duration {
59+
if input == "" {
60+
return defaultVal
61+
}
62+
parsed, err := time.ParseDuration(input)
63+
if err != nil || parsed <= 0 {
64+
klog.ErrorS(err, "Invalid duration, using default",
65+
"setting", name,
66+
"input", input,
67+
"default", defaultVal)
68+
return defaultVal
69+
}
70+
return parsed
71+
}
72+
5873
func main() {
5974
klog.InitFlags(nil)
6075
flag.Parse()
@@ -82,31 +97,8 @@ func main() {
8297
os.Exit(1)
8398
}
8499

85-
checkInterval := os.Getenv(envCheckInterval)
86-
interval := defaultCheckInterval
87-
if checkInterval != "" {
88-
parsedInterval, err := time.ParseDuration(checkInterval)
89-
if err == nil {
90-
interval = parsedInterval
91-
} else {
92-
klog.ErrorS(err, "Failed to parse check interval, using default",
93-
"input", checkInterval,
94-
"default", defaultCheckInterval)
95-
}
96-
}
97-
98-
heartbeatPeriodStr := os.Getenv(envHeartbeatPeriod)
99-
heartbeatPeriod := defaultHeartbeatPeriod
100-
if heartbeatPeriodStr != "" {
101-
parsedPeriod, err := time.ParseDuration(heartbeatPeriodStr)
102-
if err == nil {
103-
heartbeatPeriod = parsedPeriod
104-
} else {
105-
klog.ErrorS(err, "Failed parse heartbeat period, using default",
106-
"input", heartbeatPeriodStr,
107-
"default", defaultHeartbeatPeriod)
108-
}
109-
}
100+
interval := parseDurationWithDefault(os.Getenv(envCheckInterval), defaultCheckInterval, "check interval")
101+
heartbeatPeriod := parseDurationWithDefault(os.Getenv(envHeartbeatPeriod), defaultHeartbeatPeriod, "heartbeat period")
110102

111103
// Create Kubernetes client
112104
config, err := rest.InClusterConfig()

cmd/readiness-condition-reporter/main_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,48 @@ func TestUpdateNodeCondition(t *testing.T) {
267267
})
268268
}
269269
}
270+
271+
func TestParseDurationWithDefault(t *testing.T) {
272+
defaultVal := 30 * time.Second
273+
274+
tests := []struct {
275+
name string
276+
input string
277+
expected time.Duration
278+
}{
279+
{
280+
name: "valid positive duration",
281+
input: "60s",
282+
expected: 60 * time.Second,
283+
},
284+
{
285+
name: "zero duration",
286+
input: "0s",
287+
expected: defaultVal,
288+
},
289+
{
290+
name: "negative duration",
291+
input: "-30s",
292+
expected: defaultVal,
293+
},
294+
{
295+
name: "unparseable duration",
296+
input: "abc",
297+
expected: defaultVal,
298+
},
299+
{
300+
name: "empty duration",
301+
input: "",
302+
expected: defaultVal,
303+
},
304+
}
305+
306+
for _, tt := range tests {
307+
t.Run(tt.name, func(t *testing.T) {
308+
result := parseDurationWithDefault(tt.input, defaultVal, "test duration")
309+
if result != tt.expected {
310+
t.Errorf("parseDurationWithDefault(%q) = %v, expected %v", tt.input, result, tt.expected)
311+
}
312+
})
313+
}
314+
}

0 commit comments

Comments
 (0)