Skip to content

Commit

Permalink
feat: add health check for namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Apr 4, 2024
1 parent f6117cf commit df8c662
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
return getPVCHealth
case PodKind:
return getPodHealth
case NamespaceKind:
return getNamespaceHealth
}
case "batch":
switch gvk.Kind {
Expand Down
26 changes: 26 additions & 0 deletions pkg/health/health_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package health

import (
"fmt"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func getNamespaceHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
var node v1.Namespace
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &node); err != nil {
return nil, fmt.Errorf("failed to convert unstructured Node to typed: %v", err)
}

if node.Status.Phase == v1.NamespaceActive {
return &HealthStatus{
Status: HealthStatusHealthy,
}, nil
}

return &HealthStatus{
Status: HealthStatusDeleting,
}, nil
}
5 changes: 5 additions & 0 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func getHealthStatus(yamlPath string, t *testing.T) *health.HealthStatus {
return health
}

func TestNamespace(t *testing.T) {
assertAppHealth(t, "./testdata/namespace.yaml", health.HealthStatusHealthy)
assertAppHealth(t, "./testdata/namespace-terminating.yaml", health.HealthStatusDeleting)
}

func TestCertificate(t *testing.T) {
assertAppHealth(t, "./testdata/certificate-healthy.yaml", health.HealthStatusHealthy)
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/health/testdata/namespace-terminating.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: '2024-02-10T10:27:15Z'
labels:
kubernetes.io/metadata.name: media
kustomize.toolkit.fluxcd.io/name: flux-system
kustomize.toolkit.fluxcd.io/namespace: flux-system
kustomize.toolkit.fluxcd.io/prune: disabled
name: media
resourceVersion: '4518738'
uid: c1b2dc7f-7070-4ee5-9ae0-e43df5523cd8
spec:
finalizers:
- kubernetes
status:
phase: Terminating
18 changes: 18 additions & 0 deletions pkg/health/testdata/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: '2024-02-10T10:27:15Z'
labels:
kubernetes.io/metadata.name: media
kustomize.toolkit.fluxcd.io/name: flux-system
kustomize.toolkit.fluxcd.io/namespace: flux-system
kustomize.toolkit.fluxcd.io/prune: disabled
name: media
resourceVersion: '4518738'
uid: c1b2dc7f-7070-4ee5-9ae0-e43df5523cd8
spec:
finalizers:
- kubernetes
status:
phase: Active

0 comments on commit df8c662

Please sign in to comment.