Skip to content

Commit cb5d6f5

Browse files
fix(health): handle nil lastTransitionTime (#22897) (cherry-pick #22900) (#22908)
Signed-off-by: Michael Crenshaw <[email protected]> Co-authored-by: Michael Crenshaw <[email protected]>
1 parent 2913d5f commit cb5d6f5

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

resource_customizations/k8s.keycloak.org/Keycloak/health.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ if obj.status == nil or obj.status.conditions == nil then
77
end
88

99
-- Sort conditions by lastTransitionTime, from old to new.
10+
-- Ensure that conditions with nil lastTransitionTime are always sorted after those with non-nil values.
1011
table.sort(obj.status.conditions, function(a, b)
11-
return a.lastTransitionTime < b.lastTransitionTime
12+
-- Nil values are considered "less than" non-nil values.
13+
-- This means that conditions with nil lastTransitionTime will be sorted to the end.
14+
if a.lastTransitionTime == nil then
15+
return false
16+
elseif b.lastTransitionTime == nil then
17+
return true
18+
else
19+
-- If both have non-nil lastTransitionTime, compare them normally.
20+
return a.lastTransitionTime < b.lastTransitionTime
21+
end
1222
end)
1323

1424
for _, condition in ipairs(obj.status.conditions) do

resource_customizations/k8s.keycloak.org/Keycloak/health_test.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ tests:
1414
- healthStatus:
1515
status: Degraded
1616
message: "Has Errors: Waiting for foo/keycloak-1 due to CrashLoopBackOff: back-off 10s"
17-
inputPath: testdata/degraded.yaml
17+
inputPath: testdata/degraded.yaml
18+
- healthStatus:
19+
status: Healthy
20+
message: ""
21+
inputPath: testdata/nil_last_transition_time.yaml
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.keycloak.org/v1alpha1
2+
kind: Keycloak
3+
metadata:
4+
name: keycloak-23
5+
namespace: keycloak
6+
status:
7+
conditions:
8+
- type: Ready
9+
status: "True"
10+
lastTransitionTime: "2025-05-06T12:00:00Z" # Non-nil lastTransitionTime
11+
- type: HasErrors
12+
status: "False"
13+
lastTransitionTime: null # Nil lastTransitionTime

0 commit comments

Comments
 (0)