From 2ab4281cc83a3b8664bb4e4d631176f026381d86 Mon Sep 17 00:00:00 2001 From: jasonz Date: Wed, 27 Nov 2024 21:28:19 +0800 Subject: [PATCH] add health check support for ArgoCD Application Signed-off-by: jasonz --- pkg/health/health.go | 2 ++ pkg/health/health_argo.go | 18 ++++++++++++++++++ pkg/health/health_test.go | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/pkg/health/health.go b/pkg/health/health.go index b93d8c967..ef32c82e1 100644 --- a/pkg/health/health.go +++ b/pkg/health/health.go @@ -120,6 +120,8 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst switch gvk.Kind { case "Workflow": return getArgoWorkflowHealth + case "Application": + return getArgoApplicationHealth } case "apiregistration.k8s.io": switch gvk.Kind { diff --git a/pkg/health/health_argo.go b/pkg/health/health_argo.go index 478d94228..985b4d4b3 100644 --- a/pkg/health/health_argo.go +++ b/pkg/health/health_argo.go @@ -26,6 +26,15 @@ type argoWorkflow struct { } } +type argoApplication struct { + Status struct { + Health struct { + Status HealthStatusCode + Message string + } + } +} + func getArgoWorkflowHealth(obj *unstructured.Unstructured) (*HealthStatus, error) { var wf argoWorkflow err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &wf) @@ -42,3 +51,12 @@ func getArgoWorkflowHealth(obj *unstructured.Unstructured) (*HealthStatus, error } return &HealthStatus{Status: HealthStatusUnknown, Message: wf.Status.Message}, nil } + +func getArgoApplicationHealth(obj *unstructured.Unstructured) (*HealthStatus, error) { + var app argoApplication + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &app) + if err != nil { + return nil, err + } + return &HealthStatus{Status: app.Status.Health.Status, Message: app.Status.Health.Message}, nil +} diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index 45ff74941..e01baa771 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -167,3 +167,8 @@ func TestGetArgoWorkflowHealth(t *testing.T) { assert.Equal(t, "", health.Message) } + +func TestGetArgoApplicationHealth(t *testing.T) { + assertAppHealth(t, "./testdata/application-healthy.yaml", HealthStatusHealthy) + assertAppHealth(t, "./testdata/application-degraded.yaml", HealthStatusDegraded) +}