From dd05a1b3da0fbe7b1733e2d271401a0cb7aa04f1 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Fri, 19 Apr 2024 12:09:23 +0545 Subject: [PATCH] feat: use new is healthy --- go.mod | 2 +- go.sum | 4 ++-- kubernetes/health.go | 12 +++++++++--- kubernetes/health_test.go | 19 ++++++++++++------- tests/cel_test.go | 7 ++++--- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 2c43e2d9a..098bc8e1f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/Masterminds/goutils v1.1.1 github.com/Masterminds/semver/v3 v3.2.1 - github.com/flanksource/is-healthy v1.0.2 + github.com/flanksource/is-healthy v1.0.4 github.com/flanksource/kubectl-neat v1.0.4 github.com/google/cel-go v0.18.2 github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index df2ed8f4d..927de2650 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0= github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= -github.com/flanksource/is-healthy v1.0.2 h1:QJvtwIFoz4k8D2atHOciaXCsaFepUXz8laP4c0RBc4E= -github.com/flanksource/is-healthy v1.0.2/go.mod h1:cFejm0MapnJzgeoG3iizMv+tCIPthe0XqO+3nrhM79c= +github.com/flanksource/is-healthy v1.0.4 h1:r/dVsi7keR5NGLCTpgO/M6Hah2jRtYNfMh/XJNnxacc= +github.com/flanksource/is-healthy v1.0.4/go.mod h1:ijdyDDpdRzDtIt1UVZv5WNpAnb8V4hGUz75rnr3Ubhk= github.com/flanksource/kubectl-neat v1.0.4 h1:t5/9CqgE84oEtB0KitgJ2+WIeLfD+RhXSxYrqb4X8yI= github.com/flanksource/kubectl-neat v1.0.4/go.mod h1:Un/Voyh3cmiZNKQrW/TkAl28nAA7vwnwDGVjRErKjOw= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= diff --git a/kubernetes/health.go b/kubernetes/health.go index 318742ac4..e517fd2e7 100644 --- a/kubernetes/health.go +++ b/kubernetes/health.go @@ -17,7 +17,11 @@ import ( type HealthStatus struct { Status string `json:"status"` Message string `json:"message"` - OK bool `json:"ok"` + Health string `json:"health"` + Ready bool `json:"ready"` + + // Deprecated: Use Health. + OK bool `json:"ok"` } func GetUnstructuredMap(in interface{}) []byte { @@ -60,7 +64,7 @@ func GetUnstructured(in interface{}) *unstructured.Unstructured { } func IsHealthy(in interface{}) bool { - return GetHealth(in).OK + return GetHealth(in).Health == string(health.HealthHealthy) } func GetStatus(in interface{}) string { @@ -98,7 +102,9 @@ func GetHealth(in interface{}) HealthStatus { } return HealthStatus{ - OK: _health.Status == health.HealthStatusHealthy || _health.Status == health.HealthStatusProgressing, + OK: _health.Health == health.HealthHealthy, + Health: string(_health.Health), + Ready: _health.Ready, Status: string(_health.Status), Message: _health.Message, } diff --git a/kubernetes/health_test.go b/kubernetes/health_test.go index 16a6e47a2..ad002e91a 100644 --- a/kubernetes/health_test.go +++ b/kubernetes/health_test.go @@ -10,34 +10,39 @@ import ( func TestIsHealthySvc(t *testing.T) { r := GetHealth(TestHealthySvc) assert.Equal(t, true, r.OK) - assert.Equal(t, "Healthy", r.Status) + assert.Equal(t, "Running", r.Status) + assert.Equal(t, "healthy", r.Health) } func TestIsHealthyPod(t *testing.T) { r := GetHealth(TestUnhealthy) assert.Equal(t, false, r.OK) - assert.Equal(t, "Degraded", r.Status) + assert.Equal(t, "CrashLoopBackOff", r.Status) + assert.Equal(t, "unhealthy", r.Health) } func TestIsHealthyCertificate(t *testing.T) { r := GetHealth(TestHealthyCertificate) assert.Equal(t, true, r.OK) - assert.Equal(t, "Healthy", r.Status) + assert.Equal(t, "Issued", r.Status) + assert.Equal(t, "healthy", r.Health) r = GetHealth(TestDegradedCertificate) assert.Equal(t, false, r.OK) - assert.Equal(t, "Degraded", r.Status) + assert.Equal(t, "Issuing", r.Status) + assert.Equal(t, "unknown", r.Health) } func TestIsHealthyAppset(t *testing.T) { r := GetHealth(TestLuaStatus) assert.Equal(t, false, r.OK) - assert.Equal(t, "Degraded", r.Status) + assert.Equal(t, "unhealthy", r.Health) assert.Equal(t, "found less than two generators, Merge requires two or more", r.Message) } func TestIsHealthySvcPending(t *testing.T) { r := GetHealth(TestProgressing) - assert.Equal(t, true, r.OK) - assert.Equal(t, "Progressing", r.Status) + assert.Equal(t, false, r.OK) + assert.Equal(t, "Creating", r.Status) + assert.Equal(t, "unknown", r.Health) } diff --git a/tests/cel_test.go b/tests/cel_test.go index f5f3746ce..d86b5caf3 100644 --- a/tests/cel_test.go +++ b/tests/cel_test.go @@ -436,8 +436,8 @@ func TestCelSliceReturn(t *testing.T) { func TestCelK8sResources(t *testing.T) { runTests(t, []Test{ {map[string]interface{}{"healthySvc": kubernetes.GetUnstructuredMap(kubernetes.TestHealthySvc)}, "k8s.isHealthy(healthySvc)", "true"}, - {map[string]interface{}{"healthySvc": kubernetes.GetUnstructuredMap(kubernetes.TestLuaStatus)}, "k8s.getStatus(healthySvc)", "Degraded: found less than two generators, Merge requires two or more"}, - {map[string]interface{}{"healthySvc": kubernetes.GetUnstructuredMap(kubernetes.TestHealthySvc)}, "k8s.getHealth(healthySvc).status", "Healthy"}, + {map[string]interface{}{"healthySvc": kubernetes.GetUnstructuredMap(kubernetes.TestLuaStatus)}, "k8s.getStatus(healthySvc)", ": found less than two generators, Merge requires two or more"}, + {map[string]interface{}{"healthySvc": kubernetes.GetUnstructuredMap(kubernetes.TestHealthySvc)}, "k8s.getHealth(healthySvc).health", "healthy"}, }) } @@ -447,9 +447,10 @@ func TestCelK8s(t *testing.T) { Output any jsonCompare bool }{ + {Input: `k8s.getHealth(healthy_obj).health`, Output: "healthy"}, {Input: `k8s.isHealthy(healthy_obj)`, Output: true}, {Input: `k8s.isHealthy(unhealthy_obj)`, Output: false}, - {Input: `k8s.getHealth(healthy_obj).status`, Output: "Healthy"}, + {Input: `k8s.getHealth(healthy_obj).status`, Output: "Running"}, {Input: `k8s.getHealth(unhealthy_obj).message`, Output: "Back-off 40s restarting failed container=main pod=my-pod_argocd(63674389-f613-11e8-a057-fe5f49266390)"}, {Input: `k8s.getHealth(unhealthy_obj).ok`, Output: false}, {Input: `k8s.getHealth(healthy_obj).message`, Output: ""},