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

Do not set ownership for x-namespaced resources #1548

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Changes from all commits
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
29 changes: 1 addition & 28 deletions pkg/engine/renderer/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package renderer

import (
"fmt"
"log"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -67,7 +65,7 @@ func (de *DefaultEnhancer) Apply(templates map[string]string, metadata Metadata)
// More: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/
if isNamespaced {
objUnstructured.SetNamespace(metadata.InstanceNamespace)
if err = setControllerReference(metadata.ResourcesOwner, objUnstructured, de.Scheme); err != nil {
if err := controllerutil.SetControllerReference(metadata.ResourcesOwner, objUnstructured, de.Scheme); err != nil {
return nil, fmt.Errorf("%wsetting controller reference on parsed object %s: %v", engine.ErrFatalExecution, obj.GetObjectKind(), err)
}
}
Expand Down Expand Up @@ -225,28 +223,3 @@ func addMapValues(obj map[string]interface{}, fieldsToAdd map[string]string, pat
}
return unstructured.SetNestedStringMap(obj, stringMap, path...)
}

func setControllerReference(owner metav1.Object, object metav1.Object, scheme *runtime.Scheme) error {
ownerNs := owner.GetNamespace()
if ownerNs != "" {
objNs := object.GetNamespace()
if objNs == "" {
// we're trying to create cluster-scoped resource from and bind Instance as owner of that
// that is disallowed by design, see https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
// for now solve by not adding the owner
log.Printf("Not adding owner to resource %s because it's cluster-scoped and cannot be owned by namespace-scoped instance %s/%s", object.GetName(), owner.GetNamespace(), owner.GetName())
return nil
}
if ownerNs != objNs {
// we're trying to create resource in another namespace as is Instance's namespace, Instance cannot be owner of such resource
// that is disallowed by design, see https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
// for now solve by not adding the owner
log.Printf("Not adding owner to resource %s/%s because it's in different namespace than instance %s/%s and thus cannot be owned by that instance", object.GetNamespace(), object.GetName(), owner.GetNamespace(), owner.GetName())
return nil
}
}
if err := controllerutil.SetControllerReference(owner, object, scheme); err != nil {
return err
}
return nil
}