Skip to content

Commit 64b5e44

Browse files
erikgbcasibbald
andauthored
Ensure health status for custom resources implementing kstatus (#4192)
Co-authored-by: Charles Sibbald <[email protected]>
1 parent ba3c2c4 commit 64b5e44

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

pkg/health/health.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
networkingv1 "k8s.io/api/networking/v1"
1212
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1313
"k8s.io/apimachinery/pkg/runtime"
14+
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
1415
)
1516

1617
// Represents resource health status
@@ -64,9 +65,22 @@ func (hc *healthChecker) Check(obj unstructured.Unstructured) (HealthStatus, err
6465
return checkService(obj)
6566
}
6667

67-
return HealthStatus{
68-
Status: HealthStatusUnknown,
69-
}, nil
68+
result, err := kstatus.Compute(&obj)
69+
if err != nil {
70+
err = fmt.Errorf("computing kstatus for resource: %w", err)
71+
return HealthStatus{Status: HealthStatusUnknown, Message: err.Error()}, err
72+
}
73+
74+
status := HealthStatusUnknown
75+
switch result.Status {
76+
case kstatus.CurrentStatus:
77+
status = HealthStatusHealthy
78+
case kstatus.FailedStatus:
79+
status = HealthStatusUnhealthy
80+
case kstatus.InProgressStatus:
81+
status = HealthStatusProgressing
82+
}
83+
return HealthStatus{Status: status, Message: result.Message}, nil
7084
}
7185

7286
func checkDeployment(obj unstructured.Unstructured) (HealthStatus, error) {

pkg/health/health_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ func TestHealthCheck(t *testing.T) {
121121
data: "testdata/svc-progressing.yaml",
122122
healthStatus: HealthStatusProgressing,
123123
},
124+
{
125+
data: "testdata/kstatus-healthy.yaml",
126+
healthStatus: HealthStatusHealthy,
127+
},
128+
{
129+
data: "testdata/kstatus-progressing.yaml",
130+
healthStatus: HealthStatusProgressing,
131+
},
132+
{
133+
data: "testdata/kstatus-unhealty.yaml",
134+
healthStatus: HealthStatusUnhealthy,
135+
},
124136
} {
125137
t.Run(fmt.Sprintf("%s is %s", scenario.data, scenario.healthStatus), func(t *testing.T) {
126138
yamlBytes, err := os.ReadFile(scenario.data)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: apps.example.com/v1
2+
kind: Application
3+
metadata:
4+
generation: 1
5+
name: my-app
6+
spec:
7+
image: my-app:v1.2.3
8+
status:
9+
observedGeneration: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: apps.example.com/v1
2+
kind: Application
3+
metadata:
4+
generation: 1
5+
name: my-app
6+
spec:
7+
image: my-app:v1.2.3
8+
status:
9+
conditions:
10+
- message: "Available: 0/1"
11+
reason: LessAvailable
12+
status: "True"
13+
type: Reconciling
14+
observedGeneration: 1
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: apps.example.com/v1
2+
kind: Application
3+
metadata:
4+
generation: 1
5+
name: my-app
6+
spec:
7+
image: my-app:v1.2.3
8+
status:
9+
conditions:
10+
- message: 'Error reconciling: Back-off pulling image "my-app:v1.2.3"'
11+
reason: Failed
12+
status: "True"
13+
type: Stalled
14+
observedGeneration: 1

0 commit comments

Comments
 (0)