Skip to content

Commit

Permalink
Remove duplicated setControllerReference method (#1548)
Browse files Browse the repository at this point in the history
Summary:
`controller-runtime` already does the [namespace checking](https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/controller/controllerutil/controllerutil.go#L144) which was presumably not always the case (and why we had to implement our own)

Signed-off-by: Aleksey Dukhovniy <alex.dukhovniy@googlemail.com>
  • Loading branch information
Aleksey Dukhovniy committed Jun 4, 2020
1 parent f8d96bd commit 8841203
Showing 1 changed file with 1 addition and 28 deletions.
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
}

0 comments on commit 8841203

Please sign in to comment.