Skip to content

Commit

Permalink
feat: delete static resources when the canary is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Apr 3, 2024
1 parent 89e80ee commit f21071a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
9 changes: 5 additions & 4 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/flanksource/commons/duration"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/utils"
"github.com/flanksource/duty/types"

"github.com/flanksource/canary-checker/api/context"
Expand Down Expand Up @@ -105,20 +106,20 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne
// annotate the resource with the canary ID so we can easily clean it up later
// TODO: see if this is actually needed
resource.SetAnnotations(map[string]string{annotationkey: ctx.Canary.ID()})
if err := ctx.Kommons().ApplyUnstructured(ctx.Namespace, &resource); err != nil {
if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil {
return results.Failf("failed to apply static resource %s: %v", resource.GetName(), err)
}
}

for i := range check.Resources {
resource := check.Resources[i]
resource.SetAnnotations(map[string]string{annotationkey: ctx.Canary.ID()})
if err := ctx.Kommons().ApplyUnstructured(ctx.Namespace, &resource); err != nil {
if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil {
return results.Failf("failed to apply resource %s: %v", resource.GetName(), err)
}

defer func() {
if err := ctx.Kommons().DeleteUnstructured(ctx.Namespace, &resource); err != nil {
if err := ctx.Kommons().DeleteUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil {
logger.Errorf("failed to delete resource %s: %v", resource.GetName(), err)
results.ErrorMessage(fmt.Errorf("failed to delete resource %s: %v", resource.GetName(), err))
}
Expand Down Expand Up @@ -172,7 +173,7 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne
},
}
if err := templater.Walk(&virtualCanary); err != nil {
return results.Failf("error templating checks %v", err)
return results.Failf("error templating checks: %v", err)
}

checkCtx := context.New(ctx.Context, virtualCanary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: nginx-pod-creation-tests
name: ingress-test
namespace: default
labels:
"Expected-Fail": "false"
Expand All @@ -22,7 +22,7 @@ spec:
namespace: default
spec:
rules:
- host: 'httpbin.test.flanksource.com'
- host: "httpbin.test.flanksource.com"
http:
paths:
- pathType: Prefix
Expand All @@ -41,7 +41,7 @@ spec:
spec:
containers:
- name: httpbin
image: 'kennethreitz/httpbin:latest'
image: "kennethreitz/httpbin:latest"
ports:
- containerPort: 80
- apiVersion: v1
Expand All @@ -57,12 +57,8 @@ spec:
targetPort: 80
checks:
- http:
- url: "http://ingress-nginx.ingress-nginx.svc"
- name: Call httpbin via ingress
url: "http://ingress-nginx.ingress-nginx.svc"
headers:
- name: Host
value: "nginx.test.flanksource.com"
# - url: "http://localhost:1337"
# headers:
# - name: host
# value: "httpbin.test.flanksource.com"
# # value: "{{(index (index .staticResources 0).spec.rules 0).host}}"
value: "{{(index ((index .staticResources 0).Object.spec.rules) 0).host}}"
43 changes: 43 additions & 0 deletions fixtures/k8s/kubernetes_resource_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: pod-svc-test
namespace: default
labels:
"Expected-Fail": "false"
spec:
schedule: "@every 5m"
kubernetesResource:
- name: service accessibility test
namespace: default
description: "deploy httpbin & check that it's accessible via its service"
waitForReady: true
timeout: 10m
resources:
- apiVersion: v1
kind: Pod
metadata:
name: httpbin-pod-1
namespace: default
spec:
containers:
- name: httpbin
image: "kennethreitz/httpbin:latest"
ports:
- containerPort: 80
- apiVersion: v1
kind: Service
metadata:
name: httpbin-svc
namespace: default
spec:
selector:
name: httpbin-pod-1
ports:
- port: 80
targetPort: 80
checks:
- http:
- name: Call httpbin service
url: "http://httpbin-svc.default.svc"
3 changes: 2 additions & 1 deletion fixtures/k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ resources:
- pod_fail.yaml
- pod_pass.yaml
- kubernetes_bundle.yaml
- kuberenetes_resource.yaml
- kubernetes_resource_ingress.yaml
- kubernetes_resource_service.yaml
27 changes: 26 additions & 1 deletion pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ func RemoveTransformedChecks(ctx context.Context, ids []string) error {
Error
}

func deleteStaticKubernetesResources(ctx context.Context, id string) error {
var canary models.Canary
if err := ctx.DB().Where("id = ?", id).First(&canary).Error; err != nil {
return err
}

var spec v1.CanarySpec
if err := json.Unmarshal(canary.Spec, &spec); err != nil {
return err
}

for _, kr := range spec.KubernetesResource {
for _, sr := range kr.StaticResources {
ctx.Infof("Deleting static resource (kind=%s, namespace=%s, name=%s)", sr.GetKind(), sr.GetNamespace(), sr.GetName())
if err := ctx.Kommons().DeleteUnstructured(ctx.GetNamespace(), &sr); err != nil {
logger.Errorf("error deleting static resource (kind=%s, namespace=%s, name=%s): %v", sr.GetKind(), ctx.GetNamespace(), sr.GetName(), err)
}
}
}

return nil
}

func DeleteCanary(ctx context.Context, id string) error {
logger.Infof("Deleting canary[%s]", id)

Expand All @@ -220,7 +243,9 @@ func DeleteCanary(ctx context.Context, id string) error {
}
metrics.UnregisterGauge(ctx, checkIDs)

// TODO: delete all the static resources
if err := deleteStaticKubernetesResources(ctx, id); err != nil {
return fmt.Errorf("failed to delete static kubernetes resources: %w", err)
}

if err := DeleteCheckComponentRelationshipsForCanary(ctx.DB(), id); err != nil {
return err
Expand Down

0 comments on commit f21071a

Please sign in to comment.