Skip to content

Commit

Permalink
Ensure health status for custom resources implementing kstatus (#4192)
Browse files Browse the repository at this point in the history
Co-authored-by: Charles Sibbald <123247+casibbald@users.noreply.github.com>
  • Loading branch information
erikgb and casibbald authored Nov 14, 2024
1 parent ba3c2c4 commit 64b5e44
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/health/testdata/kstatus-healthy.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions pkg/health/testdata/kstatus-progressing.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions pkg/health/testdata/kstatus-unhealty.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64b5e44

Please sign in to comment.