| 1 |
// Code generated by pluginator on PrefixTransformer; DO NOT EDIT. |
| 2 |
package builtins |
| 3 |
|
| 4 |
import ( |
| 5 |
"errors" |
| 6 |
|
| 7 |
"sigs.k8s.io/kustomize/api/filters/prefix" |
| 8 |
"sigs.k8s.io/kustomize/api/resmap" |
| 9 |
"sigs.k8s.io/kustomize/api/types" |
| 10 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 11 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 12 |
) |
| 13 |
|
| 14 |
// Add the given prefix to the field |
| 15 |
type PrefixTransformerPlugin struct { |
| 16 |
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"` |
| 17 |
FieldSpecs types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` |
| 18 |
} |
| 19 |
|
| 20 |
// TODO: Make this gvk skip list part of the config. |
| 21 |
var prefixFieldSpecsToSkip = types.FsSlice{ |
| 22 |
{Gvk: resid.Gvk{Kind: "CustomResourceDefinition"}}, |
| 23 |
{Gvk: resid.Gvk{Group: "apiregistration.k8s.io", Kind: "APIService"}}, |
| 24 |
{Gvk: resid.Gvk{Kind: "Namespace"}}, |
| 25 |
} |
| 26 |
|
| 27 |
func (p *PrefixTransformerPlugin) Config( |
| 28 |
_ *resmap.PluginHelpers, c []byte) (err error) { |
| 29 |
p.Prefix = "" |
| 30 |
p.FieldSpecs = nil |
| 31 |
err = yaml.Unmarshal(c, p) |
| 32 |
if err != nil { |
| 33 |
return |
| 34 |
} |
| 35 |
if p.FieldSpecs == nil { |
| 36 |
return errors.New("fieldSpecs is not expected to be nil") |
| 37 |
} |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
func (p *PrefixTransformerPlugin) Transform(m resmap.ResMap) error { |
| 42 |
// Even if the Prefix is empty we want to proceed with the |
| 43 |
// transformation. This allows to add contextual information |
| 44 |
// to the resources (AddNamePrefix). |
| 45 |
for _, r := range m.Resources() { |
| 46 |
// TODO: move this test into the filter (i.e. make a better filter) |
| 47 |
if p.shouldSkip(r.OrgId()) { |
| 48 |
continue |
| 49 |
} |
| 50 |
id := r.OrgId() |
| 51 |
// current default configuration contains |
| 52 |
// only one entry: "metadata/name" with no GVK |
| 53 |
for _, fs := range p.FieldSpecs { |
| 54 |
// TODO: this is redundant to filter (but needed for now) |
| 55 |
if !id.IsSelected(&fs.Gvk) { |
| 56 |
continue |
| 57 |
} |
| 58 |
// TODO: move this test into the filter. |
| 59 |
if fs.Path == "metadata/name" { |
| 60 |
// "metadata/name" is the only field. |
| 61 |
// this will add a prefix to the resource |
| 62 |
// even if it is empty |
| 63 |
|
| 64 |
r.AddNamePrefix(p.Prefix) |
| 65 |
if p.Prefix != "" { |
| 66 |
// TODO: There are multiple transformers that can change a resource's name, and each makes a call to |
| 67 |
// StorePreviousID(). We should make it so that we only call StorePreviousID once per kustomization layer |
| 68 |
// to avoid storing intermediate names between transformations, to prevent intermediate name conflicts. |
| 69 |
r.StorePreviousId() |
| 70 |
} |
| 71 |
} |
| 72 |
if err := r.ApplyFilter(prefix.Filter{ |
| 73 |
Prefix: p.Prefix, |
| 74 |
FieldSpec: fs, |
| 75 |
}); err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
func (p *PrefixTransformerPlugin) shouldSkip(id resid.ResId) bool { |
| 84 |
for _, path := range prefixFieldSpecsToSkip { |
| 85 |
if id.IsSelected(&path.Gvk) { |
| 86 |
return true |
| 87 |
} |
| 88 |
} |
| 89 |
return false |
| 90 |
} |
| 91 |
|
| 92 |
func NewPrefixTransformerPlugin() resmap.TransformerPlugin { |
| 93 |
return &PrefixTransformerPlugin{} |
| 94 |
} |