diff --git a/pkg/health/health.go b/pkg/health/health.go index 572eca8577..b94636111c 100644 --- a/pkg/health/health.go +++ b/pkg/health/health.go @@ -11,6 +11,7 @@ import ( networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status" ) // Represents resource health status @@ -64,9 +65,22 @@ func (hc *healthChecker) Check(obj unstructured.Unstructured) (HealthStatus, err return checkService(obj) } - return HealthStatus{ - Status: HealthStatusUnknown, - }, nil + result, err := kstatus.Compute(&obj) + if err != nil { + err = fmt.Errorf("computing kstatus for resource: %w", err) + return HealthStatus{Status: HealthStatusUnknown, Message: err.Error()}, err + } + + status := HealthStatusUnknown + switch result.Status { + case kstatus.CurrentStatus: + status = HealthStatusHealthy + case kstatus.FailedStatus: + status = HealthStatusUnhealthy + case kstatus.InProgressStatus: + status = HealthStatusProgressing + } + return HealthStatus{Status: status, Message: result.Message}, nil } func checkDeployment(obj unstructured.Unstructured) (HealthStatus, error) { diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index 55188ac4da..aab5f5885e 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -121,6 +121,18 @@ func TestHealthCheck(t *testing.T) { data: "testdata/svc-progressing.yaml", healthStatus: HealthStatusProgressing, }, + { + data: "testdata/kstatus-healthy.yaml", + healthStatus: HealthStatusHealthy, + }, + { + data: "testdata/kstatus-progressing.yaml", + healthStatus: HealthStatusProgressing, + }, + { + data: "testdata/kstatus-unhealty.yaml", + healthStatus: HealthStatusUnhealthy, + }, } { t.Run(fmt.Sprintf("%s is %s", scenario.data, scenario.healthStatus), func(t *testing.T) { yamlBytes, err := os.ReadFile(scenario.data) diff --git a/pkg/health/testdata/kstatus-healthy.yaml b/pkg/health/testdata/kstatus-healthy.yaml new file mode 100644 index 0000000000..ebfefa2fc9 --- /dev/null +++ b/pkg/health/testdata/kstatus-healthy.yaml @@ -0,0 +1,9 @@ +apiVersion: apps.example.com/v1 +kind: Application +metadata: + generation: 1 + name: my-app +spec: + image: my-app:v1.2.3 +status: + observedGeneration: 1 diff --git a/pkg/health/testdata/kstatus-progressing.yaml b/pkg/health/testdata/kstatus-progressing.yaml new file mode 100644 index 0000000000..7e076a67e3 --- /dev/null +++ b/pkg/health/testdata/kstatus-progressing.yaml @@ -0,0 +1,14 @@ +apiVersion: apps.example.com/v1 +kind: Application +metadata: + generation: 1 + name: my-app +spec: + image: my-app:v1.2.3 +status: + conditions: + - message: "Available: 0/1" + reason: LessAvailable + status: "True" + type: Reconciling + observedGeneration: 1 diff --git a/pkg/health/testdata/kstatus-unhealty.yaml b/pkg/health/testdata/kstatus-unhealty.yaml new file mode 100644 index 0000000000..65329ffadb --- /dev/null +++ b/pkg/health/testdata/kstatus-unhealty.yaml @@ -0,0 +1,14 @@ +apiVersion: apps.example.com/v1 +kind: Application +metadata: + generation: 1 + name: my-app +spec: + image: my-app:v1.2.3 +status: + conditions: + - message: 'Error reconciling: Back-off pulling image "my-app:v1.2.3"' + reason: Failed + status: "True" + type: Stalled + observedGeneration: 1