From e895391d10596efb7c44e2ed45d2dfbe640eb2d6 Mon Sep 17 00:00:00 2001 From: Moshe Immermam Date: Wed, 20 Nov 2024 21:57:49 +0200 Subject: [PATCH] chore: job fixes --- pkg/health/health_pod.go | 13 +++++++++---- pkg/health/health_test.go | 8 -------- .../Job/healthy.yaml} | 0 .../testdata/{ => Kubernetes/Job}/job-failed.yaml | 3 +++ .../testdata/{ => Kubernetes/Job}/job-running.yaml | 4 ++++ .../{ => Kubernetes/Job}/job-suspended.yaml | 0 .../Kubernetes/Pod/pod-crashloop-pending.yaml | 4 ++-- 7 files changed, 18 insertions(+), 14 deletions(-) rename pkg/health/testdata/{job-succeeded.yaml => Kubernetes/Job/healthy.yaml} (100%) rename pkg/health/testdata/{ => Kubernetes/Job}/job-failed.yaml (93%) rename pkg/health/testdata/{ => Kubernetes/Job}/job-running.yaml (92%) rename pkg/health/testdata/{ => Kubernetes/Job}/job-suspended.yaml (100%) diff --git a/pkg/health/health_pod.go b/pkg/health/health_pod.go index e10bb4c..9197207 100644 --- a/pkg/health/health_pod.go +++ b/pkg/health/health_pod.go @@ -55,8 +55,12 @@ func isErrorStatus(s string) bool { func getContainerStatus(containerStatus corev1.ContainerStatus) (waiting *HealthStatus, terminated *HealthStatus) { if state := containerStatus.State.Waiting; state != nil { waiting = &HealthStatus{ - Status: HealthStatusCode(state.Reason), - Health: lo.Ternary(isErrorStatus(state.Reason) || containerStatus.RestartCount > 0, HealthUnhealthy, HealthUnknown), + Status: HealthStatusCode(state.Reason), + Health: lo.Ternary( + isErrorStatus(state.Reason) || containerStatus.RestartCount > 0, + HealthUnhealthy, + HealthUnknown, + ), Message: state.Message, } } @@ -85,7 +89,7 @@ func getCorev1PodHealth(pod *corev1.Pod) (*HealthStatus, error) { deadline := GetStartDeadline(append(pod.Spec.InitContainers, pod.Spec.Containers...)...) age := time.Since(pod.CreationTimestamp.Time).Truncate(time.Minute).Abs() isStarting := age < deadline - var hr = HealthStatus{ + hr := HealthStatus{ Health: lo.Ternary(isReady, HealthHealthy, HealthUnhealthy), } @@ -145,7 +149,8 @@ func getCorev1PodHealth(pod *corev1.Pod) (*HealthStatus, error) { case corev1.PodRunning, corev1.PodPending: hr = hr.Merge(terminated, waiting) - if terminated != nil && terminated.Health.IsWorseThan(HealthWarning) && hr.Status == HealthStatusCrashLoopBackoff { + if terminated != nil && terminated.Health.IsWorseThan(HealthWarning) && + hr.Status == HealthStatusCrashLoopBackoff { hr.Status = terminated.Status hr.Health = hr.Health.Worst(terminated.Health) } diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index 0776f15..1139539 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -81,7 +81,6 @@ func TestHealthCompare(t *testing.T) { assert.True(t, health.HealthUnhealthy.IsWorseThan(health.HealthWarning)) assert.Equal(t, health.HealthHealthy, health.HealthHealthy.Worst(health.HealthUnknown)) assert.Equal(t, health.HealthUnhealthy, health.HealthHealthy.Worst(health.HealthUnhealthy)) - } func assertAppHealthMsg( @@ -502,13 +501,6 @@ func TestCnrmPubSub(t *testing.T) { assertAppHealthMsg(t, b+"update_in_progress.yaml", "Progressing", health.HealthUnknown, false) } -func TestJob(t *testing.T) { - assertAppHealthMsg(t, "./testdata/job-running.yaml", health.HealthStatusRunning, health.HealthHealthy, false) - assertAppHealthMsg(t, "./testdata/job-failed.yaml", health.HealthStatusError, health.HealthUnhealthy, true) - assertAppHealthMsg(t, "./testdata/job-succeeded.yaml", health.HealthStatusCompleted, health.HealthHealthy, true) - assertAppHealthMsg(t, "./testdata/job-suspended.yaml", health.HealthStatusSuspended, health.HealthUnknown, false) -} - func TestHPA(t *testing.T) { assertAppHealthMsg(t, "./testdata/hpa-v2-healthy.yaml", health.HealthStatusHealthy, health.HealthHealthy, true) assertAppHealthMsg(t, "./testdata/hpa-v2-degraded.yaml", health.HealthStatusDegraded, health.HealthUnhealthy, false) diff --git a/pkg/health/testdata/job-succeeded.yaml b/pkg/health/testdata/Kubernetes/Job/healthy.yaml similarity index 100% rename from pkg/health/testdata/job-succeeded.yaml rename to pkg/health/testdata/Kubernetes/Job/healthy.yaml diff --git a/pkg/health/testdata/job-failed.yaml b/pkg/health/testdata/Kubernetes/Job/job-failed.yaml similarity index 93% rename from pkg/health/testdata/job-failed.yaml rename to pkg/health/testdata/Kubernetes/Job/job-failed.yaml index 1deadf9..c8ee5ef 100644 --- a/pkg/health/testdata/job-failed.yaml +++ b/pkg/health/testdata/Kubernetes/Job/job-failed.yaml @@ -7,6 +7,9 @@ metadata: job-name: fail name: fail namespace: argoci-workflows + annotations: + expected-status: BackoffLimitExceeded + expected-health: unhealthy resourceVersion: "46534173" selfLink: /apis/batch/v1/namespaces/argoci-workflows/jobs/fail uid: 95052288-f609-11e8-aa53-42010a80021b diff --git a/pkg/health/testdata/job-running.yaml b/pkg/health/testdata/Kubernetes/Job/job-running.yaml similarity index 92% rename from pkg/health/testdata/job-running.yaml rename to pkg/health/testdata/Kubernetes/Job/job-running.yaml index c845274..6d6b9d4 100644 --- a/pkg/health/testdata/job-running.yaml +++ b/pkg/health/testdata/Kubernetes/Job/job-running.yaml @@ -7,6 +7,10 @@ metadata: job-name: succeed name: succeed namespace: argoci-workflows + annotations: + expected-status: Running + expected-health: healthy + expected-ready: "false" resourceVersion: "46535911" selfLink: /apis/batch/v1/namespaces/argoci-workflows/jobs/succeed uid: f3fe3a46-f60a-11e8-aa53-42010a80021b diff --git a/pkg/health/testdata/job-suspended.yaml b/pkg/health/testdata/Kubernetes/Job/job-suspended.yaml similarity index 100% rename from pkg/health/testdata/job-suspended.yaml rename to pkg/health/testdata/Kubernetes/Job/job-suspended.yaml diff --git a/pkg/health/testdata/Kubernetes/Pod/pod-crashloop-pending.yaml b/pkg/health/testdata/Kubernetes/Pod/pod-crashloop-pending.yaml index 918b86b..c43d3e2 100644 --- a/pkg/health/testdata/Kubernetes/Pod/pod-crashloop-pending.yaml +++ b/pkg/health/testdata/Kubernetes/Pod/pod-crashloop-pending.yaml @@ -7,8 +7,8 @@ metadata: app: crashloop pod-template-hash: 6bf9b7b858 namespace: default - annotations: - expected-status: CrashloopBackoff + annotations: + expected-status: PodInitializing expected-health: unhealthy generateName: crashloop-deployment-6bf9b7b858- ownerReferences: