Skip to content

Commit 760d962

Browse files
committed
ad wasm functionality to apply-setters and rewrite to new go kpt fn sdk
1 parent 1e5d4a1 commit 760d962

File tree

9 files changed

+386
-299
lines changed

9 files changed

+386
-299
lines changed

functions/go/apply-setters/applysetters/apply_setters.go

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,14 @@ import (
66
"strings"
77

88
"sigs.k8s.io/kustomize/kyaml/errors"
9-
"sigs.k8s.io/kustomize/kyaml/kio"
109
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
1110
"sigs.k8s.io/kustomize/kyaml/yaml"
1211
)
1312

1413
const SetterCommentIdentifier = "# kpt-set: "
1514

16-
var _ kio.Filter = &ApplySetters{}
17-
18-
// ApplySetters applies the setter values to the resource fields which are tagged
19-
// by the setter reference comments
20-
type ApplySetters struct {
21-
// Setters holds the user provided values for all the setters
22-
Setters []Setter
23-
24-
// Results are the results of applying setter values
25-
Results []*Result
26-
27-
// filePath file path of resource
28-
filePath string
29-
}
30-
31-
type Setter struct {
32-
// Name is the name of the setter
33-
Name string
34-
35-
// Value is the input value for setter
36-
Value string
37-
}
38-
39-
// Result holds result of search and replace operation
40-
type Result struct {
41-
// FilePath is the file path of the matching field
42-
FilePath string
43-
44-
// FieldPath is field path of the matching field
45-
FieldPath string
46-
47-
// Value of the matching field
48-
Value string
49-
}
50-
5115
// Filter implements Set as a yaml.Filter
52-
func (as *ApplySetters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
16+
func (as *Setters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
5317
for i := range nodes {
5418
filePath, _, err := kioutil.GetFileAnnotations(nodes[i])
5519
if err != nil {
@@ -77,14 +41,13 @@ environments: # kpt-set: ${env}
7741
- dev
7842
- stage
7943
80-
For input ApplySetters [name: env, value: "[stage, prod]"], qthe yaml node is transformed to
44+
For input Setters [name: env, value: "[stage, prod]"], qthe yaml node is transformed to
8145
8246
environments: # kpt-set: ${env}
8347
- stage
8448
- prod
85-
8649
*/
87-
func (as *ApplySetters) visitMapping(object *yaml.RNode, path string) error {
50+
func (as *Setters) visitMapping(object *yaml.RNode, path string) error {
8851
return object.VisitFields(func(node *yaml.MapNode) error {
8952
if node == nil || node.Key.IsNil() || node.Value.IsNil() {
9053
// don't do IsNilOrEmpty check as empty sequences are allowed
@@ -180,17 +143,18 @@ e.g.for input of scalar node 'nginx:1.7.1 # kpt-set: ${image}:${tag}' in the yam
180143
181144
apiVersion: v1
182145
...
183-
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
184146
185-
and for input ApplySetters [[name: image, value: ubuntu], [name: tag, value: 1.8.0]]
147+
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
148+
149+
and for input Setters [[name: image, value: ubuntu], [name: tag, value: 1.8.0]]
186150
The yaml node is transformed to
187151
188152
apiVersion: v1
189153
...
190-
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
191154
155+
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
192156
*/
193-
func (as *ApplySetters) visitScalar(object *yaml.RNode, path string) error {
157+
func (as *Setters) visitScalar(object *yaml.RNode, path string) error {
194158
if object.IsNil() {
195159
return nil
196160
}
@@ -358,7 +322,7 @@ func clean(input string) string {
358322
}
359323

360324
// Decode decodes the input yaml node into Set struct
361-
func Decode(rn *yaml.RNode, fcd *ApplySetters) {
325+
func Decode(rn *yaml.RNode, fcd *Setters) {
362326
for k, v := range rn.GetDataMap() {
363327
fcd.Setters = append(fcd.Setters, Setter{Name: k, Value: v})
364328
}

functions/go/apply-setters/applysetters/apply_setters_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ spec:
9696
template:
9797
spec:
9898
containers:
99-
- name: nginx
100-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
99+
- name: nginx
100+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
101101
`,
102102
expectedResources: `apiVersion: apps/v1
103103
kind: Deployment
@@ -108,8 +108,8 @@ spec:
108108
template:
109109
spec:
110110
containers:
111-
- name: nginx
112-
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
111+
- name: nginx
112+
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
113113
`,
114114
},
115115
{
@@ -127,8 +127,8 @@ spec:
127127
template:
128128
spec:
129129
containers:
130-
- name: nginx
131-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}`,
130+
- name: nginx
131+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}`,
132132
expectedResources: `apiVersion: apps/v1
133133
kind: Deployment
134134
metadata:
@@ -138,8 +138,8 @@ spec:
138138
template:
139139
spec:
140140
containers:
141-
- name: nginx
142-
image: ubuntu:1.7.9 # kpt-set: ${image}:${tag}
141+
- name: nginx
142+
image: ubuntu:1.7.9 # kpt-set: ${image}:${tag}
143143
`,
144144
},
145145
{
@@ -157,8 +157,8 @@ spec:
157157
template:
158158
spec:
159159
containers:
160-
- name: nginx
161-
image: nginx:1.7.9 # kpt-set: ${image-~!@#$%^&*()<>?"|}:${tag}`,
160+
- name: nginx
161+
image: nginx:1.7.9 # kpt-set: ${image-~!@#$%^&*()<>?"|}:${tag}`,
162162
expectedResources: `apiVersion: apps/v1
163163
kind: Deployment
164164
metadata:
@@ -168,8 +168,8 @@ spec:
168168
template:
169169
spec:
170170
containers:
171-
- name: nginx
172-
image: ubuntu-~!@#$%^&*()<>?"|:1.7.9 # kpt-set: ${image-~!@#$%^&*()<>?"|}:${tag}
171+
- name: nginx
172+
image: ubuntu-~!@#$%^&*()<>?"|:1.7.9 # kpt-set: ${image-~!@#$%^&*()<>?"|}:${tag}
173173
`,
174174
},
175175
{
@@ -187,8 +187,8 @@ spec:
187187
template:
188188
spec:
189189
containers:
190-
- name: nginx
191-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
190+
- name: nginx
191+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
192192
`,
193193
expectedResources: `apiVersion: apps/v1
194194
kind: Deployment
@@ -199,8 +199,8 @@ spec:
199199
template:
200200
spec:
201201
containers:
202-
- name: nginx
203-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
202+
- name: nginx
203+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
204204
`,
205205
},
206206
{
@@ -218,8 +218,8 @@ spec:
218218
template:
219219
spec:
220220
containers:
221-
- image: irrelevant_value # kpt-set: ${image}:${tag}
222-
name: nginx
221+
- image: irrelevant_value # kpt-set: ${image}:${tag}
222+
name: nginx
223223
`,
224224
expectedResources: `apiVersion: apps/v1
225225
kind: Deployment
@@ -230,8 +230,8 @@ spec:
230230
template:
231231
spec:
232232
containers:
233-
- image: irrelevant_value # kpt-set: ${image}:${tag}
234-
name: nginx
233+
- image: irrelevant_value # kpt-set: ${image}:${tag}
234+
name: nginx
235235
`,
236236
errMsg: `values for setters [${tag}] must be provided`,
237237
},
@@ -249,17 +249,17 @@ metadata:
249249
name: nginx-deployment
250250
spec:
251251
images: # kpt-set: ${images}
252-
- nginx
253-
- ubuntu
252+
- nginx
253+
- ubuntu
254254
`,
255255
expectedResources: `apiVersion: apps/v1
256256
kind: Deployment
257257
metadata:
258258
name: nginx-deployment
259259
spec:
260260
images: # kpt-set: ${images}
261-
- ubuntu
262-
- hbase
261+
- ubuntu
262+
- hbase
263263
`,
264264
},
265265
{
@@ -351,8 +351,8 @@ spec:
351351
template:
352352
spec:
353353
containers:
354-
- name: nginx
355-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
354+
- name: nginx
355+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
356356
`,
357357
expectedResources: `apiVersion: apps/v1
358358
kind: Deployment
@@ -363,8 +363,8 @@ spec:
363363
template:
364364
spec:
365365
containers:
366-
- name: nginx
367-
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
366+
- name: nginx
367+
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
368368
`,
369369
},
370370
{
@@ -427,11 +427,11 @@ metadata:
427427
namespace: "foo" # kpt-set: ${ns}
428428
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
429429
env: # kpt-set: ${env}
430-
- foo
431-
- bar
430+
- foo
431+
- bar
432432
roles: # kpt-set: ${roles}
433-
- dev
434-
- prod
433+
- dev
434+
- prod
435435
`,
436436
},
437437
}
@@ -454,7 +454,7 @@ roles: # kpt-set: ${roles}
454454
t.FailNow()
455455
}
456456

457-
s := &ApplySetters{}
457+
s := &Setters{}
458458
node, err := kyaml.Parse(test.config)
459459
if !assert.NoError(t, err) {
460460
t.FailNow()
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package applysetters
2+
3+
import (
4+
"fmt"
5+
"sigs.k8s.io/kustomize/kyaml/yaml"
6+
7+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
8+
)
9+
10+
const fnConfigKind = "ApplySetters"
11+
const fnConfigApiVersion = "fn.kpt.dev/v1alpha1"
12+
13+
func ApplySetters(rl *fn.ResourceList) (bool, error) {
14+
r := Setters{}
15+
return r.Process(rl)
16+
}
17+
18+
type Setters struct {
19+
// Setters holds the user provided values for all the setters
20+
Setters []Setter `yaml:"setters,omitempty" json:"setters,omitempty"`
21+
22+
// Results are the results of applying setter values
23+
Results []*Result
24+
25+
// filePath file path of resource
26+
filePath string
27+
}
28+
29+
type Setter struct {
30+
// Name is the name of the setter
31+
Name string `yaml:"name" json:"name"`
32+
33+
// Value is the input value for setter
34+
Value string `yaml:"value" json:"value"`
35+
}
36+
37+
// Result holds result of search and replace operation
38+
type Result struct {
39+
// FilePath is the file path of the matching field
40+
FilePath string
41+
42+
// FieldPath is field path of the matching field
43+
FieldPath string
44+
45+
// Value of the matching field
46+
Value string
47+
}
48+
49+
// Config initializes Setters from a functionConfig fn.KubeObject
50+
func (r *Setters) Config(functionConfig *fn.KubeObject) error {
51+
if functionConfig.IsEmpty() {
52+
return fmt.Errorf("FunctionConfig is missing. Expect `ApplySetters`")
53+
}
54+
if functionConfig.GetKind() != fnConfigKind || functionConfig.GetAPIVersion() != fnConfigApiVersion {
55+
return fmt.Errorf("received functionConfig of kind %s and apiVersion %s, "+
56+
"only functionConfig of kind %s and apiVersion %s is supported",
57+
functionConfig.GetKind(), functionConfig.GetAPIVersion(), fnConfigKind, fnConfigApiVersion)
58+
}
59+
r.Setters = []Setter{}
60+
if err := functionConfig.As(r); err != nil {
61+
return fmt.Errorf("unable to convert functionConfig to %s:\n%w",
62+
"setters", err)
63+
}
64+
return nil
65+
}
66+
67+
// Process configures the setters and transforms them.
68+
func (r *Setters) Process(rl *fn.ResourceList) (bool, error) {
69+
if err := r.Config(rl.FunctionConfig); err != nil {
70+
rl.LogResult(err)
71+
return false, nil
72+
}
73+
transformedItems, err := r.Transform(rl.Items)
74+
if err != nil {
75+
rl.LogResult(err)
76+
return false, nil
77+
}
78+
rl.Items = transformedItems
79+
return true, nil
80+
}
81+
82+
// Transform runs the setters filter in order to apply the setters - this
83+
// does the actual work.
84+
func (r *Setters) Transform(items []*fn.KubeObject) ([]*fn.KubeObject, error) {
85+
var transformedItems []*fn.KubeObject
86+
var nodes []*yaml.RNode
87+
88+
for _, obj := range items {
89+
objRN, err := yaml.Parse(obj.String())
90+
if err != nil {
91+
return nil, err
92+
}
93+
nodes = append(nodes, objRN)
94+
}
95+
96+
transformedNodes, err := r.Filter(nodes)
97+
if err != nil {
98+
return nil, err
99+
}
100+
for _, n := range transformedNodes {
101+
obj, err := fn.ParseKubeObject([]byte(n.MustString()))
102+
if err != nil {
103+
return nil, err
104+
}
105+
transformedItems = append(transformedItems, obj)
106+
}
107+
return transformedItems, nil
108+
}

functions/go/apply-setters/generated/docs.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)