...
1
2
3
4 package starlark_test
5
6 import (
7 "bytes"
8 "fmt"
9 "log"
10 "os"
11 "path/filepath"
12
13 "sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
14 "sigs.k8s.io/kustomize/kyaml/fn/runtime/starlark"
15 "sigs.k8s.io/kustomize/kyaml/kio"
16 "sigs.k8s.io/kustomize/kyaml/kio/kioutil"
17 "sigs.k8s.io/kustomize/kyaml/yaml"
18 )
19
20 func ExampleFilter_Filter() {
21
22 input := bytes.NewBufferString(`
23 apiVersion: apps/v1
24 kind: Deployment
25 metadata:
26 name: deployment-1
27 spec:
28 template:
29 spec:
30 containers:
31 - name: nginx
32 image: nginx:1.8.1 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-1"}
33 ---
34 apiVersion: apps/v1
35 kind: Deployment
36 metadata:
37 name: deployment-2
38 spec:
39 template:
40 spec:
41 containers:
42 - name: nginx
43 image: nginx:1.7.9 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-2"}
44 `)
45
46
47 fltr := &starlark.Filter{
48 Name: "annotate",
49 Program: `
50 def run(items):
51 for item in items:
52 item["metadata"]["annotations"]["foo"] = "bar"
53
54 run(ctx.resource_list["items"])
55 `,
56 }
57
58
59 output := &bytes.Buffer{}
60
61
62 err := kio.Pipeline{
63 Inputs: []kio.Reader{&kio.ByteReader{Reader: input}},
64 Filters: []kio.Filter{fltr},
65 Outputs: []kio.Writer{&kio.ByteWriter{Writer: output}}}.Execute()
66 if err != nil {
67 log.Println(err)
68 }
69
70 fmt.Println(output.String())
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 }
103
104 func ExampleFilter_Filter_functionConfig() {
105
106 input := bytes.NewBufferString(`
107 apiVersion: apps/v1
108 kind: Deployment
109 metadata:
110 name: deployment-1
111 spec:
112 template:
113 spec:
114 containers:
115 - name: nginx
116 image: nginx:1.8.1 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-1"}
117 ---
118 apiVersion: apps/v1
119 kind: Deployment
120 metadata:
121 name: deployment-2
122 spec:
123 template:
124 spec:
125 containers:
126 - name: nginx
127 image: nginx:1.7.9 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-2"}
128 `)
129
130 fc, err := yaml.Parse(`
131 kind: AnnotationSetter
132 spec:
133 value: "hello world"
134 `)
135 if err != nil {
136 log.Println(err)
137 }
138
139
140 fltr := &starlark.Filter{
141 Name: "annotate",
142 Program: `
143 def run(items, value):
144 for item in items:
145 item["metadata"]["annotations"]["foo"] = value
146
147 run(ctx.resource_list["items"], ctx.resource_list["functionConfig"]["spec"]["value"])
148 `,
149 FunctionFilter: runtimeutil.FunctionFilter{FunctionConfig: fc},
150 }
151
152
153 output := &bytes.Buffer{}
154
155
156 err = kio.Pipeline{
157 Inputs: []kio.Reader{&kio.ByteReader{Reader: input}},
158 Filters: []kio.Filter{fltr},
159 Outputs: []kio.Writer{&kio.ByteWriter{Writer: output}}}.Execute()
160 if err != nil {
161 log.Println(err)
162 }
163
164 fmt.Println(output.String())
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 }
197
198
199
200 func ExampleFilter_Filter_file() {
201
202 d, err := os.MkdirTemp("", "")
203 if err != nil {
204 log.Println(err)
205 }
206 defer os.RemoveAll(d)
207
208 err = os.WriteFile(filepath.Join(d, "deploy1.yaml"), []byte(`
209 apiVersion: apps/v1
210 kind: Deployment
211 metadata:
212 name: deployment-1
213 spec:
214 template:
215 spec:
216 containers:
217 - name: nginx
218 image: nginx:1.8.1 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-1"}
219 `), 0600)
220 if err != nil {
221 log.Println(err)
222 }
223
224 err = os.WriteFile(filepath.Join(d, "deploy2.yaml"), []byte(`
225 apiVersion: apps/v1
226 kind: Deployment
227 metadata:
228 name: deployment-2
229 spec:
230 template:
231 spec:
232 containers:
233 - name: nginx
234 image: nginx:1.7.9 # {"$ref": "#/definitions/io.k8s.cli.substitutions.image-2"}
235 `), 0600)
236 if err != nil {
237 log.Println(err)
238 }
239
240 err = os.WriteFile(filepath.Join(d, "annotate.star"), []byte(`
241 def run(items):
242 for item in items:
243 item["metadata"]["annotations"]["foo"] = "bar"
244
245 run(ctx.resource_list["items"])
246 `), 0600)
247 if err != nil {
248 log.Println(err)
249 }
250
251 fltr := &starlark.Filter{
252 Name: "annotate",
253 Path: filepath.Join(d, "annotate.star"),
254 }
255
256
257 output := &bytes.Buffer{}
258
259
260 err = kio.Pipeline{
261 Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: d}},
262 Filters: []kio.Filter{fltr},
263 Outputs: []kio.Writer{&kio.ByteWriter{
264 Writer: output,
265 ClearAnnotations: []string{
266 kioutil.PathAnnotation,
267 kioutil.LegacyPathAnnotation,
268 },
269 }}}.Execute()
270 if err != nil {
271 log.Println(err)
272 }
273
274 fmt.Println(output.String())
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 }
303
View as plain text