From 0071f7f8b86e909cad32487b838ea793df407fb5 Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Wed, 7 May 2025 21:48:32 +0000 Subject: [PATCH] fix(health): handle nil lastTransitionTime (#22897) Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- .../k8s.keycloak.org/Keycloak/health.lua | 12 +++++++++++- .../k8s.keycloak.org/Keycloak/health_test.yaml | 6 +++++- .../Keycloak/testdata/nil_last_transition_time.yaml | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 resource_customizations/k8s.keycloak.org/Keycloak/testdata/nil_last_transition_time.yaml diff --git a/resource_customizations/k8s.keycloak.org/Keycloak/health.lua b/resource_customizations/k8s.keycloak.org/Keycloak/health.lua index a82135af0a119..2245b1a930c94 100644 --- a/resource_customizations/k8s.keycloak.org/Keycloak/health.lua +++ b/resource_customizations/k8s.keycloak.org/Keycloak/health.lua @@ -7,8 +7,18 @@ if obj.status == nil or obj.status.conditions == nil then end -- Sort conditions by lastTransitionTime, from old to new. +-- Ensure that conditions with nil lastTransitionTime are always sorted after those with non-nil values. table.sort(obj.status.conditions, function(a, b) - return a.lastTransitionTime < b.lastTransitionTime + -- Nil values are considered "less than" non-nil values. + -- This means that conditions with nil lastTransitionTime will be sorted to the end. + if a.lastTransitionTime == nil then + return false + elseif b.lastTransitionTime == nil then + return true + else + -- If both have non-nil lastTransitionTime, compare them normally. + return a.lastTransitionTime < b.lastTransitionTime + end end) for _, condition in ipairs(obj.status.conditions) do diff --git a/resource_customizations/k8s.keycloak.org/Keycloak/health_test.yaml b/resource_customizations/k8s.keycloak.org/Keycloak/health_test.yaml index 8560f67890517..6f56c68783647 100644 --- a/resource_customizations/k8s.keycloak.org/Keycloak/health_test.yaml +++ b/resource_customizations/k8s.keycloak.org/Keycloak/health_test.yaml @@ -14,4 +14,8 @@ tests: - healthStatus: status: Degraded message: "Has Errors: Waiting for foo/keycloak-1 due to CrashLoopBackOff: back-off 10s" - inputPath: testdata/degraded.yaml \ No newline at end of file + inputPath: testdata/degraded.yaml +- healthStatus: + status: Healthy + message: "" + inputPath: testdata/nil_last_transition_time.yaml \ No newline at end of file diff --git a/resource_customizations/k8s.keycloak.org/Keycloak/testdata/nil_last_transition_time.yaml b/resource_customizations/k8s.keycloak.org/Keycloak/testdata/nil_last_transition_time.yaml new file mode 100644 index 0000000000000..7a5df8e9a9c24 --- /dev/null +++ b/resource_customizations/k8s.keycloak.org/Keycloak/testdata/nil_last_transition_time.yaml @@ -0,0 +1,13 @@ +apiVersion: k8s.keycloak.org/v1alpha1 +kind: Keycloak +metadata: + name: keycloak-23 + namespace: keycloak +status: + conditions: + - type: Ready + status: "True" + lastTransitionTime: "2025-05-06T12:00:00Z" # Non-nil lastTransitionTime + - type: HasErrors + status: "False" + lastTransitionTime: null # Nil lastTransitionTime \ No newline at end of file