Skip to content

Commit

Permalink
feat: use new is healthy
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Apr 19, 2024
1 parent 632727b commit dd05a1b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 9 additions & 3 deletions kubernetes/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand Down
19 changes: 12 additions & 7 deletions kubernetes/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
})
}

Expand All @@ -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: ""},
Expand Down

0 comments on commit dd05a1b

Please sign in to comment.