Skip to content

Commit

Permalink
✨ Added RemoveTransformer
Browse files Browse the repository at this point in the history
Uses the selectors used for patches.
  • Loading branch information
antoinemartin committed Jan 26, 2023
1 parent ce9701f commit 04dc173
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
46 changes: 46 additions & 0 deletions pkg/extras/RemoveTransformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package extras

import (
"fmt"

"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/yaml"
)

type RemoveTransformerPlugin struct {
Targets []*types.Selector `json:"targets,omitempty" yaml:"targets,omitempty"`
}

func (p *RemoveTransformerPlugin) Config(
h *resmap.PluginHelpers, c []byte) (err error) {
err = yaml.Unmarshal(c, p)
if err != nil {
return err
}
return err
}

func (p *RemoveTransformerPlugin) Transform(m resmap.ResMap) error {
if p.Targets == nil {
return fmt.Errorf("must specify at least one target")
}
for _, t := range p.Targets {
resources, err := m.Select(*t)
if err != nil {
return errors.WrapPrefixf(err, "while selecting target %s", t.String())
}
for _, r := range resources {
err = m.Remove(r.CurId())
if err != nil {
return errors.WrapPrefixf(err, "while removing resource %s", r.CurId().String())
}
}
}
return nil
}

func NewRemoveTransformerPlugin() resmap.TransformerPlugin {
return &RemoveTransformerPlugin{}
}
5 changes: 3 additions & 2 deletions pkg/plugins/builtinplugintype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/plugins/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
HelmChartInflationGenerator
ReplacementTransformer
GitConfigMapGenerator
RemoveTransformer
)

var stringToBuiltinPluginTypeMap map[string]BuiltinPluginType
Expand Down Expand Up @@ -109,6 +110,7 @@ var TransformerFactories = map[BuiltinPluginType]func() resmap.TransformerPlugin
ReplacementTransformer: extras.NewExtendedReplacementTransformerPlugin,
ReplicaCountTransformer: builtins.NewReplicaCountTransformerPlugin,
ValueAddTransformer: builtins.NewValueAddTransformerPlugin,
RemoveTransformer: extras.NewRemoveTransformerPlugin,
// Do not wired SortOrderTransformer as a builtin plugin.
// We only want it to be available in the top-level kustomization.
// See: https://github.com/kubernetes-sigs/kustomize/issues/3913
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: ReplacementTransformer
metadata:
name: replacement-transformer
annotations:
config.kubernetes.io/prune-local: "true"
## config.kaweezle.com/prune-local: "true"
config.kubernetes.io/function: |
exec:
path: ../../krmfnbuiltin
Expand Down
10 changes: 10 additions & 0 deletions tests/multi-replacement/functions/03_multi-transformation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: builtin
kind: RemoveTransformer
metadata:
name: replacement-transformer
annotations:
config.kubernetes.io/function: |
exec:
path: ../../krmfnbuiltin
targets:
- annotationSelector: config.kaweezle.com/local-config

0 comments on commit 04dc173

Please sign in to comment.