Skip to content

Commit

Permalink
chore: funcs to create kustomize plugins (opendatahub-io#1062)
Browse files Browse the repository at this point in the history
The actual use with resMap is inlined and moved to the caller.

This way we can construct plugins on demand and use them as building blocks instead.
  • Loading branch information
bartoszmajsak authored Jun 18, 2024
1 parent 1b23c9f commit 7bf56a4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions pkg/plugins/addLabelsplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +39,4 @@ func ApplyAddLabelsPlugin(componentName string, resMap resmap.ResMap) error {
},
},
}

return nsplug.Transform(resMap)
}
10 changes: 4 additions & 6 deletions pkg/plugins/namespacePlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -64,6 +64,4 @@ func ApplyNamespacePlugin(manifestNamespace string, resMap resmap.ResMap) error
UnsetOnly: false,
SetRoleBindingSubjects: namespace.AllServiceAccountSubjects,
}

return nsplug.Transform(resMap)
}

0 comments on commit 7bf56a4

Please sign in to comment.