diff --git a/pkg/deploy/deploy.go b/pkg/deploy/deploy.go index 3de42f92bc3..74597df213b 100644 --- a/pkg/deploy/deploy.go +++ b/pkg/deploy/deploy.go @@ -165,14 +165,14 @@ func DeployManifestsFromPath(cli client.Client, owner metav1.Object, manifestPat return err } - // Apply NamespaceTransformer Plugin - if err := plugins.ApplyNamespacePlugin(namespace, resMap); err != nil { - return err + nsPlugin := plugins.CreateNamespaceApplierPlugin(namespace) + if err := nsPlugin.Transform(resMap); err != nil { + return fmt.Errorf("failed applying namespace plugin when preparing Kustomize resources. %w", err) } - // Apply LabelTransformer Plugin - if err := plugins.ApplyAddLabelsPlugin(componentName, resMap); err != nil { - return err + labelsPlugin := plugins.CreateAddLabelsPlugin(componentName) + if err := labelsPlugin.Transform(resMap); err != nil { + return fmt.Errorf("failed applying labels plugin when preparing Kustomize resources. %w", err) } objs, err := GetResources(resMap) diff --git a/pkg/plugins/addLabelsplugin.go b/pkg/plugins/addLabelsplugin.go index ca09980d49d..573a19f2ad2 100644 --- a/pkg/plugins/addLabelsplugin.go +++ b/pkg/plugins/addLabelsplugin.go @@ -2,15 +2,21 @@ package plugins import ( "sigs.k8s.io/kustomize/api/builtins" //nolint:staticcheck //Remove after package update - "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/resid" "github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels" ) -func ApplyAddLabelsPlugin(componentName string, resMap resmap.ResMap) error { - nsplug := builtins.LabelTransformerPlugin{ +// CreateAddLabelsPlugin creates a label transformer plugin that ensures resources +// to which this plugin is applied will have the Open Data Hub common labels included. +// +// It has a following characteristics: +// - It adds labels to the "metadata/labels" path for all resource kinds. +// - It adds labels to the "spec/template/metadata/labels" and "spec/selector/matchLabels" paths +// for resources of kind "Deployment". +func CreateAddLabelsPlugin(componentName string) builtins.LabelTransformerPlugin { + return builtins.LabelTransformerPlugin{ Labels: map[string]string{ labels.ODH.Component(componentName): "true", labels.K8SCommon.PartOf: componentName, @@ -33,6 +39,4 @@ func ApplyAddLabelsPlugin(componentName string, resMap resmap.ResMap) error { }, }, } - - return nsplug.Transform(resMap) } diff --git a/pkg/plugins/namespacePlugin.go b/pkg/plugins/namespacePlugin.go index 3b04601096e..7060b6330c4 100644 --- a/pkg/plugins/namespacePlugin.go +++ b/pkg/plugins/namespacePlugin.go @@ -3,16 +3,16 @@ package plugins import ( "sigs.k8s.io/kustomize/api/builtins" //nolint:staticcheck // Remove after package update "sigs.k8s.io/kustomize/api/filters/namespace" - "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/resid" ) -func ApplyNamespacePlugin(manifestNamespace string, resMap resmap.ResMap) error { - nsplug := builtins.NamespaceTransformerPlugin{ +// CreateNamespaceApplierPlugin creates a plugin to ensure resources have the specified target namespace. +func CreateNamespaceApplierPlugin(targetNamespace string) builtins.NamespaceTransformerPlugin { + return builtins.NamespaceTransformerPlugin{ ObjectMeta: types.ObjectMeta{ Name: "odh-namespace-plugin", - Namespace: manifestNamespace, + Namespace: targetNamespace, }, FieldSpecs: []types.FieldSpec{ { @@ -64,6 +64,4 @@ func ApplyNamespacePlugin(manifestNamespace string, resMap resmap.ResMap) error UnsetOnly: false, SetRoleBindingSubjects: namespace.AllServiceAccountSubjects, } - - return nsplug.Transform(resMap) }