Skip to content

Commit

Permalink
feat: kubernetes check add healthy flag (#1795)
Browse files Browse the repository at this point in the history
* feat: kubernetes check add healthy flag

* chore: change comment
  • Loading branch information
adityathebe authored Apr 18, 2024
1 parent d361b6e commit 161e1be
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,12 @@ type KubernetesCheck struct {
// Ignore the specified resources from the fetched resources. Can be a glob pattern.
Ignore []string `yaml:"ignore,omitempty" json:"ignore,omitempty"`
Kind string `yaml:"kind" json:"kind"`
Ready *bool `yaml:"ready,omitempty" json:"ready,omitempty"`

// Fail the check if any resources are unhealthy
Healthy bool `yaml:"healthy,omitempty" json:"healthy,omitempty"`

// Fail the check if any resources are not ready
Ready bool `yaml:"ready,omitempty" json:"ready,omitempty"`
}

func (c KubernetesCheck) GetType() string {
Expand Down
5 changes: 0 additions & 5 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions checks/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,28 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check
}
}

ctx.Tracef("Found %d %s in namespace %s with label=%s field=%s", len(resources), check.Kind, namespace, check.Resource.LabelSelector, check.Resource.FieldSelector)
ctx.Tracef("Found %d %s in namespace %s with label=%s field=%s", len(resources), check.Kind, namespace, check.Resource.LabelSelector, check.Resource.FieldSelector)
for _, resource := range resources {
_resource := resource
resourceHealth, err := health.GetResourceHealth(&_resource, nil)
if err == nil {
if err != nil {
results.Failf("error getting resource health (%s/%s/%s): %v",
resource.GetKind(), resource.GetNamespace(), resource.GetName(), err)
} else {
resource.Object["healthStatus"] = resourceHealth
}
if check.Ready != nil && *check.Ready && resourceHealth.Status != health.HealthStatusHealthy {
results.Failf("%s/%s/%s is %s: %s", resource.GetKind(), resource.GetNamespace(), resource.GetName(), resourceHealth.Status, resourceHealth.Message)

if check.Healthy && resourceHealth.Health != health.HealthHealthy {
results.Failf("%s/%s/%s is not healthy (health: %s, status: %s): %s",
resource.GetKind(), resource.GetNamespace(), resource.GetName(), resourceHealth.Health, resourceHealth.Status, resourceHealth.Message)
}

if check.Ready && !resourceHealth.Ready {
results.Failf("%s/%s/%s is not ready (status: %s): %s", resource.GetKind(),
resource.GetNamespace(), resource.GetName(), resourceHealth.Status, resourceHealth.Message)
}
}
}

allResources = append(allResources, resources...)
}

Expand Down
4 changes: 4 additions & 0 deletions config/deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4844,6 +4844,9 @@ spec:
template:
type: string
type: object
healthy:
description: Fail the check if any resources are unhealthy
type: boolean
icon:
description: Icon for overwriting default icon on the dashboard
type: string
Expand Down Expand Up @@ -4942,6 +4945,7 @@ spec:
type: string
type: object
ready:
description: Fail the check if any resources are not ready
type: boolean
resource:
properties:
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,9 @@ spec:
template:
type: string
type: object
healthy:
description: Fail the check if any resources are unhealthy
type: boolean
icon:
description: Icon for overwriting default icon on the dashboard
type: string
Expand Down Expand Up @@ -4941,6 +4944,7 @@ spec:
type: string
type: object
ready:
description: Fail the check if any resources are not ready
type: boolean
resource:
properties:
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/canary.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,9 @@
"kind": {
"type": "string"
},
"healthy": {
"type": "boolean"
},
"ready": {
"type": "boolean"
}
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/component.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,9 @@
"kind": {
"type": "string"
},
"healthy": {
"type": "boolean"
},
"ready": {
"type": "boolean"
}
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/health_kubernetes.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
"kind": {
"type": "string"
},
"healthy": {
"type": "boolean"
},
"ready": {
"type": "boolean"
}
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/topology.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,9 @@
"kind": {
"type": "string"
},
"healthy": {
"type": "boolean"
},
"ready": {
"type": "boolean"
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/flanksource/commons v1.22.1
github.com/flanksource/duty v1.0.419
github.com/flanksource/gomplate/v3 v3.24.3
github.com/flanksource/is-healthy v1.0.2
github.com/flanksource/is-healthy v1.0.4
github.com/flanksource/kommons v0.31.4
github.com/friendsofgo/errors v0.9.2
github.com/go-git/go-git/v5 v5.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9
github.com/flanksource/gomplate/v3 v3.24.3 h1:vBvwlBqLT+8u5xrIcAXEHCeQ5OZPNM89rGTmeMHTMvs=
github.com/flanksource/gomplate/v3 v3.24.3/go.mod h1:94BxYobZqouGdVezuz6LNto5C+yLMG0LnNnM9CUPyoo=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
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/kommons v0.31.4 h1:zksAgYjZuwPgS8XTejDIWEYB0nPSU1i3Jxcavm/vovI=
github.com/flanksource/kommons v0.31.4/go.mod h1:70BPMzjTvejsqRyVyAm/ZCeZ176toCvauaZjU03svnE=
github.com/flanksource/kubectl-neat v1.0.4 h1:t5/9CqgE84oEtB0KitgJ2+WIeLfD+RhXSxYrqb4X8yI=
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/exaring/otelpgx v0.5.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flanksource/duty v1.0.419 // indirect
github.com/flanksource/is-healthy v1.0.2 // indirect
github.com/flanksource/is-healthy v1.0.4 // indirect
github.com/flanksource/kommons v0.31.4 // indirect
github.com/flanksource/kubectl-neat v1.0.4 // indirect
github.com/flanksource/postq v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,8 @@ github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9
github.com/flanksource/gomplate/v3 v3.24.3 h1:vBvwlBqLT+8u5xrIcAXEHCeQ5OZPNM89rGTmeMHTMvs=
github.com/flanksource/gomplate/v3 v3.24.3/go.mod h1:94BxYobZqouGdVezuz6LNto5C+yLMG0LnNnM9CUPyoo=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
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/kommons v0.31.4 h1:zksAgYjZuwPgS8XTejDIWEYB0nPSU1i3Jxcavm/vovI=
github.com/flanksource/kommons v0.31.4/go.mod h1:70BPMzjTvejsqRyVyAm/ZCeZ176toCvauaZjU03svnE=
github.com/flanksource/kubectl-neat v1.0.4 h1:t5/9CqgE84oEtB0KitgJ2+WIeLfD+RhXSxYrqb4X8yI=
Expand Down

0 comments on commit 161e1be

Please sign in to comment.