Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only set the canary in the ownerReferences if the namespace is known & matches the resources's namespace #2343

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,40 @@ type KubernetesResourceCheck struct {
WaitFor KubernetesResourceCheckWaitFor `json:"waitFor,omitempty"`
}

func (c *KubernetesResourceCheck) SetCanaryOwnerReference(id, name string) {
// SetMissingNamespace will set the parent canaries name to resources whose namespace
// is not explicitly specified.
func (c *KubernetesResourceCheck) SetMissingNamespace(parent Canary) {
adityathebe marked this conversation as resolved.
Show resolved Hide resolved
for i, r := range c.StaticResources {
if r.GetNamespace() == "" {
c.StaticResources[i].SetNamespace(parent.GetNamespace())
}
}

for i, r := range c.Resources {
if r.GetNamespace() == "" {
c.Resources[i].SetNamespace(parent.GetNamespace())
}
}
}

func (c *KubernetesResourceCheck) SetCanaryOwnerReference(parent Canary) {
var (
id = parent.GetPersistedID()
name = parent.GetName()
namespace = parent.GetNamespace()
)

if id == "" || name == "" {
// if the canary isn't persisted
return
}

if namespace == "" {
// we don't know the canaries namespace
// so we can't set it in the owner references.
return
}

canaryOwnerRef := metav1.OwnerReference{
APIVersion: "canaries.flanksource.com/v1",
Kind: "Canary",
Expand All @@ -993,12 +1021,22 @@ func (c *KubernetesResourceCheck) SetCanaryOwnerReference(id, name string) {
}

for i, resource := range c.StaticResources {
if resource.GetNamespace() != namespace {
// the canary and the resource to be created are in different namespaces.
// ownerRef enforces the owner to be in the same repo.
continue
}

ownerRefs := resource.GetOwnerReferences()
ownerRefs = append(ownerRefs, canaryOwnerRef)
c.StaticResources[i].SetOwnerReferences(ownerRefs)
}

for i, resource := range c.Resources {
if resource.GetNamespace() != namespace {
continue
}

ownerRefs := resource.GetOwnerReferences()
ownerRefs = append(ownerRefs, canaryOwnerRef)
c.Resources[i].SetOwnerReferences(ownerRefs)
Expand Down
7 changes: 4 additions & 3 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +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)
check.SetMissingNamespace(ctx.Canary)
check.SetCanaryOwnerReference(ctx.Canary)

if check.Kubeconfig != nil {
var err error
Expand All @@ -86,7 +87,7 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet

for i := range check.StaticResources {
resource := check.StaticResources[i]
if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil {
if err := ctx.Kommons().ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
return results.Failf("failed to apply static resource %s: %v", resource.GetName(), err)
}
}
Expand All @@ -105,7 +106,7 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet

for i := range check.Resources {
resource := check.Resources[i]
if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil {
if err := ctx.Kommons().ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
return results.Failf("failed to apply resource (%s/%s/%s): %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err)
}
}
Expand Down
Loading