Skip to content

Commit

Permalink
fix: templating of kubernetes resource checks
Browse files Browse the repository at this point in the history
* fix how results of checks are handled in kubernetes resouce checks
  • Loading branch information
adityathebe committed Apr 1, 2024
1 parent a44c39f commit 9e4b75f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 5 additions & 1 deletion api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ func (c ConfigDBCheck) GetEndpoint() string {
return c.Query
}

// KubernetesResourceChecks is the canary spec.
// NOTE: It's only created to make crd generation possible.
// embedding CanarySpec into KubernetesResourceCheck.checks
// directly generates an invalid crd.
type KubernetesResourceChecks struct {
CanarySpec `yaml:",inline" json:",inline"`
}
Expand Down Expand Up @@ -813,7 +817,7 @@ func (c KubernetesResourceCheck) GetType() string {
}

func (c KubernetesResourceCheck) GetEndpoint() string {
return "" // TODO:
return c.Name
}

type ResourceSelector struct {
Expand Down
25 changes: 21 additions & 4 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/flanksource/gomplate/v3"
"github.com/flanksource/is-healthy/pkg/health"
"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -150,23 +151,39 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne
}
}

// run the actual check now. We need to run this in a canary-checker pod as well
logger.Debugf("found %d checks to run", len(check.Checks))
for _, c := range check.Checks {
virtualCanary := v1.Canary{
ObjectMeta: ctx.Canary.ObjectMeta,
Spec: c.CanarySpec,
}

logger.Infof("running check inside kubernetes resource")
templater := gomplate.StructTemplater{
Values: map[string]any{
"staticResource": check.StaticResources,
"resources": check.Resources,
},
ValueFunctions: true,
DelimSets: []gomplate.Delims{
{Left: "{{", Right: "}}"},
{Left: "$(", Right: ")"},
},
}
if err := templater.Walk(&virtualCanary); err != nil {
return results.Failf("error templating checks %v", err)
}

checkCtx := context.New(ctx.Context, virtualCanary)
res, err := Exec(checkCtx)
if err != nil {
return results.Failf("%v", err)
} else {
for _, r := range res {
if r.Error != "" {
results.Failf("check (name:%s) failed with error: %v", r.GetName(), r.Error)
}
}
}

results = append(results, res...)
}

return results
Expand Down
7 changes: 1 addition & 6 deletions fixtures/k8s/ingress_accessibility.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
schedule: '@every 5m'
kubernetesResource:
- name: ingress-accessibility-check
description: "deploy a pod and check that it is accessible via ingress"
description: "deploy an nginx pod"
namespace: default
resources:
- apiVersion: v1
Expand All @@ -23,8 +23,3 @@ spec:
- containerPort: 80
waitForReady: true
timeout: 10m
podChecks:
checks:
- http:
- name: adityathebe.com.np
url: "https://adityathebe.com.np"

0 comments on commit 9e4b75f

Please sign in to comment.