diff --git a/api/v1/checks.go b/api/v1/checks.go index 153692817..324314593 100644 --- a/api/v1/checks.go +++ b/api/v1/checks.go @@ -14,7 +14,9 @@ import ( "github.com/flanksource/duty/models" "github.com/flanksource/duty/types" "github.com/samber/lo" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + k8sTypes "k8s.io/apimachinery/pkg/types" ) // List of additional check label keys that should be included in the check metrics. @@ -976,6 +978,28 @@ type KubernetesResourceCheck struct { WaitFor KubernetesResourceCheckWaitFor `json:"waitFor,omitempty"` } +func (c *KubernetesResourceCheck) SetCanaryOwnerReference(id, name string) { + canaryOwnerRef := metav1.OwnerReference{ + APIVersion: "canaries.flanksource.com/v1", + Kind: "Canary", + Name: name, + UID: k8sTypes.UID(id), + Controller: lo.ToPtr(true), + } + + for i, resource := range c.StaticResources { + ownerRefs := resource.GetOwnerReferences() + ownerRefs = append(ownerRefs, canaryOwnerRef) + c.StaticResources[i].SetOwnerReferences(ownerRefs) + } + + for i, resource := range c.Resources { + ownerRefs := resource.GetOwnerReferences() + ownerRefs = append(ownerRefs, canaryOwnerRef) + c.Resources[i].SetOwnerReferences(ownerRefs) + } +} + func (c KubernetesResourceCheck) GetDisplayTemplate() Template { if !c.Templatable.Display.IsEmpty() { return c.Templatable.Display diff --git a/checks/kubernetes_resource.go b/checks/kubernetes_resource.go index 658977040..e1556f41c 100644 --- a/checks/kubernetes_resource.go +++ b/checks/kubernetes_resource.go @@ -70,6 +70,8 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet return results.Failf("validation: %v", err) } + check.SetCanaryOwnerReference(string(ctx.Canary.GetPersistedID()), ctx.Canary.Name) + if check.Kubeconfig != nil { var err error ctx, err = ctx.WithKubeconfig(*check.Kubeconfig)