Debian Codegraph @

api/internal/builtins/ValueAddTransformer.go

raw
1 // Code generated by pluginator on ValueAddTransformer; DO NOT EDIT.
2 package builtins
3
4 import (
5 "fmt"
6 "path/filepath"
7 "strings"
8
9 "sigs.k8s.io/kustomize/api/filters/namespace"
10 "sigs.k8s.io/kustomize/api/filters/valueadd"
11 "sigs.k8s.io/kustomize/api/resmap"
12 "sigs.k8s.io/kustomize/api/resource"
13 "sigs.k8s.io/kustomize/api/types"
14 "sigs.k8s.io/yaml"
15 )
16
17 // An 'Add' transformer inspired by the IETF RFC 6902 JSON spec Add operation.
18 type ValueAddTransformerPlugin struct {
19 // Value is the value to add.
20 // Defaults to base name of encompassing kustomization root.
21 Value string `json:"value,omitempty" yaml:"value,omitempty"`
22
23 // Targets is a slice of targets that should have the value added.
24 Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
25
26 // TargetFilePath is a file path. If specified, the file will be parsed into
27 // a slice of Target, and appended to anything that was specified in the
28 // Targets field. This is just a means to share common target specifications.
29 TargetFilePath string `json:"targetFilePath,omitempty" yaml:"targetFilePath,omitempty"`
30 }
31
32 // Target describes where to put the value.
33 type Target struct {
34 // Selector selects the resources to modify.
35 Selector *types.Selector `json:"selector,omitempty" yaml:"selector,omitempty"`
36
37 // NotSelector selects the resources to exclude
38 // from those included by overly broad selectors.
39 // TODO: implement this?
40 // NotSelector *types.Selector `json:"notSelector,omitempty" yaml:"notSelector,omitempty"`
41
42 // FieldPath is a JSON-style path to the field intended to hold the value.
43 FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
44
45 // FilePathPosition is passed to the filter directly. Look there for doc.
46 FilePathPosition int `json:"filePathPosition,omitempty" yaml:"filePathPosition,omitempty"`
47 }
48
49 func (p *ValueAddTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error {
50 err := yaml.Unmarshal(c, p)
51 if err != nil {
52 return err
53 }
54 p.Value = strings.TrimSpace(p.Value)
55 if p.Value == "" {
56 p.Value = filepath.Base(h.Loader().Root())
57 }
58 if p.TargetFilePath != "" {
59 bytes, err := h.Loader().Load(p.TargetFilePath)
60 if err != nil {
61 return err
62 }
63 var targets struct {
64 Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
65 }
66 err = yaml.Unmarshal(bytes, &targets)
67 if err != nil {
68 return err
69 }
70 p.Targets = append(p.Targets, targets.Targets...)
71 }
72 if len(p.Targets) == 0 {
73 return fmt.Errorf("must specify at least one target")
74 }
75 for _, target := range p.Targets {
76 if err = validateSelector(target.Selector); err != nil {
77 return err
78 }
79 // TODO: call validateSelector(target.NotSelector) if field added.
80 if err = validateJsonFieldPath(target.FieldPath); err != nil {
81 return err
82 }
83 if target.FilePathPosition < 0 {
84 return fmt.Errorf(
85 "value of FilePathPosition (%d) cannot be negative",
86 target.FilePathPosition)
87 }
88 }
89 return nil
90 }
91
92 // TODO: implement
93 func validateSelector(_ *types.Selector) error {
94 return nil
95 }
96
97 // TODO: Enforce RFC 6902?
98 func validateJsonFieldPath(p string) error {
99 if len(p) == 0 {
100 return fmt.Errorf("fieldPath cannot be empty")
101 }
102 return nil
103 }
104
105 func (p *ValueAddTransformerPlugin) Transform(m resmap.ResMap) (err error) {
106 for _, t := range p.Targets {
107 var resources []*resource.Resource
108 if t.Selector == nil {
109 resources = m.Resources()
110 } else {
111 resources, err = m.Select(*t.Selector)
112 if err != nil {
113 return err
114 }
115 }
116 // TODO: consider t.NotSelector if implemented
117 for _, res := range resources {
118 if t.FieldPath == types.MetadataNamespacePath {
119 err = res.ApplyFilter(namespace.Filter{
120 Namespace: p.Value,
121 })
122 } else {
123 err = res.ApplyFilter(valueadd.Filter{
124 Value: p.Value,
125 FieldPath: t.FieldPath,
126 FilePathPosition: t.FilePathPosition,
127 })
128 }
129 if err != nil {
130 return err
131 }
132 }
133 }
134 return nil
135 }
136
137 func NewValueAddTransformerPlugin() resmap.TransformerPlugin {
138 return &ValueAddTransformerPlugin{}
139 }