Debian Codegraph @

plugin/builtin/valueaddtransformer/ValueAddTransformer.go

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