From ce2ef23f6282a33bfb718511339b126ef420ab8f Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 28 Jul 2023 15:45:10 -0400 Subject: [PATCH] Fix cluster scope resources (#1510) * Add the owner references to the namespaced-scoped resources * Inject the owner only to the namespace-scoped resources --- pkg/reconciler/common/transformers.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/reconciler/common/transformers.go b/pkg/reconciler/common/transformers.go index 0b15e979e6..00ff77d592 100644 --- a/pkg/reconciler/common/transformers.go +++ b/pkg/reconciler/common/transformers.go @@ -17,6 +17,8 @@ import ( "context" mf "github.com/manifestival/manifestival" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "knative.dev/pkg/logging" "knative.dev/operator/pkg/apis/operator/base" @@ -26,7 +28,7 @@ import ( func transformers(ctx context.Context, obj base.KComponent) []mf.Transformer { logger := logging.FromContext(ctx) return []mf.Transformer{ - mf.InjectOwner(obj), + injectOwner(obj), mf.InjectNamespace(obj.GetNamespace()), JobTransform(obj), HighAvailabilityTransform(obj, logger), @@ -39,6 +41,15 @@ func transformers(ctx context.Context, obj base.KComponent) []mf.Transformer { } } +func injectOwner(owner mf.Owner) mf.Transformer { + return func(u *unstructured.Unstructured) error { + if u.GetNamespace() != "" { + u.SetOwnerReferences([]v1.OwnerReference{*v1.NewControllerRef(owner, owner.GroupVersionKind())}) + } + return nil + } +} + // Transform will mutate the passed-by-reference manifest with one // transformed by platform, common, and any extra passed in func Transform(ctx context.Context, manifest *mf.Manifest, instance base.KComponent, extra ...mf.Transformer) error {