Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure health status for custom resources implementing kstatus #4192

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading