Skip to content

Commit

Permalink
feat: set canary as the controller owner of all created resources
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 15, 2024
1 parent 386598b commit 94f19fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet
return results.Failf("validation: %v", err)
}

check.SetCanaryOwnerReference(ctx.Canary.GetPersistedID(), ctx.Canary.Name)

if check.Kubeconfig != nil {
var err error
ctx, err = ctx.WithKubeconfig(*check.Kubeconfig)
Expand Down

0 comments on commit 94f19fa

Please sign in to comment.