Debian Codegraph @

api/krusty/customconfig_test.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package krusty_test
5
6 import (
7 "testing"
8
9 kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
10 )
11
12 func makeBaseReferencingCustomConfig(th kusttest_test.Harness) {
13 th.WriteK("base", `
14 namePrefix: x-
15 commonLabels:
16 app: myApp
17 vars:
18 - name: APRIL_DIET
19 objref:
20 group: foo
21 version: v1
22 kind: Giraffe
23 name: april
24 fieldref:
25 fieldpath: spec.diet
26 - name: KOKO_DIET
27 objref:
28 group: foo
29 version: v1
30 kind: Gorilla
31 name: koko
32 fieldref:
33 fieldpath: spec.diet
34 resources:
35 - animalPark.yaml
36 - giraffes.yaml
37 - gorilla.yaml
38 configurations:
39 - config/defaults.yaml
40 - config/custom.yaml
41 `)
42 th.WriteF("base/giraffes.yaml", `
43 apiVersion: foo/v1
44 kind: Giraffe
45 metadata:
46 name: april
47 spec:
48 diet: mimosa
49 location: NE
50 ---
51 apiVersion: foo/v1
52 kind: Giraffe
53 metadata:
54 name: may
55 spec:
56 diet: acacia
57 location: SE
58 `)
59 th.WriteF("base/gorilla.yaml", `
60 apiVersion: foo/v1
61 kind: Gorilla
62 metadata:
63 name: koko
64 spec:
65 diet: bambooshoots
66 location: SW
67 `)
68 th.WriteF("base/animalPark.yaml", `
69 apiVersion: foo/v1
70 kind: AnimalPark
71 metadata:
72 name: sandiego
73 spec:
74 gorillaRef:
75 name: koko
76 giraffeRef:
77 name: april
78 food:
79 - "$(APRIL_DIET)"
80 - "$(KOKO_DIET)"
81 `)
82 }
83
84 func TestCustomConfig(t *testing.T) {
85 th := kusttest_test.MakeHarness(t)
86 makeBaseReferencingCustomConfig(th)
87 th.WriteLegacyConfigs("base/config/defaults.yaml")
88 th.WriteF("base/config/custom.yaml", `
89 nameReference:
90 - kind: Gorilla
91 fieldSpecs:
92 - kind: AnimalPark
93 path: spec/gorillaRef/name
94 - kind: Giraffe
95 fieldSpecs:
96 - kind: AnimalPark
97 path: spec/giraffeRef/name
98 varReference:
99 - path: spec/food
100 kind: AnimalPark
101 `)
102 m := th.Run("base", th.MakeDefaultOptions())
103 th.AssertActualEqualsExpected(m, `
104 apiVersion: foo/v1
105 kind: AnimalPark
106 metadata:
107 labels:
108 app: myApp
109 name: x-sandiego
110 spec:
111 food:
112 - mimosa
113 - bambooshoots
114 giraffeRef:
115 name: x-april
116 gorillaRef:
117 name: x-koko
118 ---
119 apiVersion: foo/v1
120 kind: Giraffe
121 metadata:
122 labels:
123 app: myApp
124 name: x-april
125 spec:
126 diet: mimosa
127 location: NE
128 ---
129 apiVersion: foo/v1
130 kind: Giraffe
131 metadata:
132 labels:
133 app: myApp
134 name: x-may
135 spec:
136 diet: acacia
137 location: SE
138 ---
139 apiVersion: foo/v1
140 kind: Gorilla
141 metadata:
142 labels:
143 app: myApp
144 name: x-koko
145 spec:
146 diet: bambooshoots
147 location: SW
148 `)
149 }
150
151 func TestCustomConfigWithDefaultOverspecification(t *testing.T) {
152 th := kusttest_test.MakeHarness(t)
153 makeBaseReferencingCustomConfig(th)
154 th.WriteLegacyConfigs("base/config/defaults.yaml")
155 // Specifying namePrefix here conflicts with (is the same as)
156 // the defaults written above. This is intentional in the
157 // test to assure duplicate config doesn't cause problems.
158 th.WriteF("base/config/custom.yaml", `
159 namePrefix:
160 - path: metadata/name
161 nameReference:
162 - kind: Gorilla
163 fieldSpecs:
164 - kind: AnimalPark
165 path: spec/gorillaRef/name
166 - kind: Giraffe
167 fieldSpecs:
168 - kind: AnimalPark
169 path: spec/giraffeRef/name
170 varReference:
171 - path: spec/food
172 kind: AnimalPark
173 `)
174 m := th.Run("base", th.MakeDefaultOptions())
175 th.AssertActualEqualsExpected(m, `
176 apiVersion: foo/v1
177 kind: AnimalPark
178 metadata:
179 labels:
180 app: myApp
181 name: x-sandiego
182 spec:
183 food:
184 - mimosa
185 - bambooshoots
186 giraffeRef:
187 name: x-april
188 gorillaRef:
189 name: x-koko
190 ---
191 apiVersion: foo/v1
192 kind: Giraffe
193 metadata:
194 labels:
195 app: myApp
196 name: x-april
197 spec:
198 diet: mimosa
199 location: NE
200 ---
201 apiVersion: foo/v1
202 kind: Giraffe
203 metadata:
204 labels:
205 app: myApp
206 name: x-may
207 spec:
208 diet: acacia
209 location: SE
210 ---
211 apiVersion: foo/v1
212 kind: Gorilla
213 metadata:
214 labels:
215 app: myApp
216 name: x-koko
217 spec:
218 diet: bambooshoots
219 location: SW
220 `)
221 }
222
223 func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) {
224 th := kusttest_test.MakeHarness(t)
225 makeBaseReferencingCustomConfig(th)
226 th.WriteLegacyConfigs("base/config/defaults.yaml")
227 th.WriteF("base/config/custom.yaml", `
228 nameReference:
229 - kind: Gorilla
230 fieldSpecs:
231 - group: foo
232 version: v1
233 kind: AnimalPark
234 path: spec/gorillaRef/name
235 - kind: Giraffe
236 fieldSpecs:
237 - group: foo
238 version: v1
239 kind: AnimalPark
240 path: spec/giraffeRef/name
241 varReference:
242 - path: spec/food
243 group: foo
244 version: v1
245 kind: AnimalPark
246 `)
247 th.WriteK("overlay", `
248 namePrefix: o-
249 commonLabels:
250 movie: planetOfTheApes
251 patchesStrategicMerge:
252 - animalPark.yaml
253 resources:
254 - ../base
255 - ursus.yaml
256 `)
257 th.WriteF("overlay/ursus.yaml", `
258 apiVersion: foo/v1
259 kind: Gorilla
260 metadata:
261 name: ursus
262 spec:
263 diet: heston
264 location: Arizona
265 `)
266 // The following replaces the gorillaRef in the AnimalPark.
267 th.WriteF("overlay/animalPark.yaml", `
268 apiVersion: foo/v1
269 kind: AnimalPark
270 metadata:
271 name: sandiego
272 spec:
273 gorillaRef:
274 name: ursus
275 `)
276 m := th.Run("overlay", th.MakeDefaultOptions())
277 th.AssertActualEqualsExpected(m, `
278 apiVersion: foo/v1
279 kind: AnimalPark
280 metadata:
281 labels:
282 app: myApp
283 movie: planetOfTheApes
284 name: o-x-sandiego
285 spec:
286 food:
287 - mimosa
288 - bambooshoots
289 giraffeRef:
290 name: o-x-april
291 gorillaRef:
292 name: o-ursus
293 ---
294 apiVersion: foo/v1
295 kind: Giraffe
296 metadata:
297 labels:
298 app: myApp
299 movie: planetOfTheApes
300 name: o-x-april
301 spec:
302 diet: mimosa
303 location: NE
304 ---
305 apiVersion: foo/v1
306 kind: Giraffe
307 metadata:
308 labels:
309 app: myApp
310 movie: planetOfTheApes
311 name: o-x-may
312 spec:
313 diet: acacia
314 location: SE
315 ---
316 apiVersion: foo/v1
317 kind: Gorilla
318 metadata:
319 labels:
320 app: myApp
321 movie: planetOfTheApes
322 name: o-x-koko
323 spec:
324 diet: bambooshoots
325 location: SW
326 ---
327 apiVersion: foo/v1
328 kind: Gorilla
329 metadata:
330 labels:
331 movie: planetOfTheApes
332 name: o-ursus
333 spec:
334 diet: heston
335 location: Arizona
336 `)
337 }
338
339 func TestLabelTransformerConfig(t *testing.T) {
340 testCases := []struct {
341 name string
342 kustomization string
343 transformerConfig string
344 expectedResult string
345 }{
346 {
347 name: "includeSelectors=false, includeTemplates=false, include template via transformerConfig",
348 kustomization: `configurations:
349 - config/configurations.yaml
350
351 labels:
352 - includeSelectors: false
353 includeTemplates: false
354 pairs:
355 location: planet-earth
356 environment: dev
357
358 resources:
359 - resources/deployment.yaml
360 `,
361 transformerConfig: `labels:
362 - path: spec/template/metadata/labels
363 create: true
364 kind: Deployment
365 `,
366 expectedResult: `apiVersion: apps/v1
367 kind: Deployment
368 metadata:
369 creationTimestamp: null
370 labels:
371 app: sample-deploy
372 environment: dev
373 location: planet-earth
374 name: sample-deploy
375 spec:
376 replicas: 1
377 selector:
378 matchLabels:
379 app: sample-deploy
380 strategy: {}
381 template:
382 metadata:
383 creationTimestamp: null
384 labels:
385 app: sample-deploy
386 environment: dev
387 location: planet-earth
388 spec:
389 containers:
390 - image: hello-world:latest
391 name: hello-world
392 `,
393 },
394 {
395 name: "includeSelectors=true, includeTemplates=false, include template via transformerConfig",
396 kustomization: `configurations:
397 - config/configurations.yaml
398
399 labels:
400 - includeSelectors: true
401 includeTemplates: false
402 pairs:
403 location: planet-earth
404 environment: dev
405
406 resources:
407 - resources/deployment.yaml
408 `,
409 transformerConfig: `labels:
410 - path: spec/template/metadata/labels
411 create: true
412 kind: Deployment
413 `,
414 expectedResult: `apiVersion: apps/v1
415 kind: Deployment
416 metadata:
417 creationTimestamp: null
418 labels:
419 app: sample-deploy
420 environment: dev
421 location: planet-earth
422 name: sample-deploy
423 spec:
424 replicas: 1
425 selector:
426 matchLabels:
427 app: sample-deploy
428 environment: dev
429 location: planet-earth
430 strategy: {}
431 template:
432 metadata:
433 creationTimestamp: null
434 labels:
435 app: sample-deploy
436 environment: dev
437 location: planet-earth
438 spec:
439 containers:
440 - image: hello-world:latest
441 name: hello-world
442 `,
443 },
444 {
445 name: "includeSelectors=false, includeTemplates=true, no transformerConfig",
446 kustomization: `labels:
447 - includeSelectors: false
448 includeTemplates: true
449 pairs:
450 location: planet-earth
451 environment: dev
452
453 resources:
454 - resources/deployment.yaml
455 `,
456 expectedResult: `apiVersion: apps/v1
457 kind: Deployment
458 metadata:
459 creationTimestamp: null
460 labels:
461 app: sample-deploy
462 environment: dev
463 location: planet-earth
464 name: sample-deploy
465 spec:
466 replicas: 1
467 selector:
468 matchLabels:
469 app: sample-deploy
470 strategy: {}
471 template:
472 metadata:
473 creationTimestamp: null
474 labels:
475 app: sample-deploy
476 environment: dev
477 location: planet-earth
478 spec:
479 containers:
480 - image: hello-world:latest
481 name: hello-world
482 `,
483 },
484 {
485 name: "includeSelectors=false, includeTemplates=false, no transformerConfig",
486 kustomization: `labels:
487 - includeSelectors: false
488 includeTemplates: false
489 pairs:
490 location: planet-earth
491 environment: dev
492
493 resources:
494 - resources/deployment.yaml
495 `,
496 expectedResult: `apiVersion: apps/v1
497 kind: Deployment
498 metadata:
499 creationTimestamp: null
500 labels:
501 app: sample-deploy
502 environment: dev
503 location: planet-earth
504 name: sample-deploy
505 spec:
506 replicas: 1
507 selector:
508 matchLabels:
509 app: sample-deploy
510 strategy: {}
511 template:
512 metadata:
513 creationTimestamp: null
514 labels:
515 app: sample-deploy
516 spec:
517 containers:
518 - image: hello-world:latest
519 name: hello-world
520 `,
521 },
522 }
523
524 for _, tc := range testCases {
525 t.Run(tc.name, func(t *testing.T) {
526 th := kusttest_test.MakeHarness(t)
527 th.WriteK(".", tc.kustomization)
528 th.WriteF("resources/deployment.yaml",
529 `apiVersion: apps/v1
530 kind: Deployment
531 metadata:
532 creationTimestamp: null
533 labels:
534 app: sample-deploy
535 name: sample-deploy
536 spec:
537 replicas: 1
538 selector:
539 matchLabels:
540 app: sample-deploy
541
542 strategy: {}
543 template:
544 metadata:
545 creationTimestamp: null
546 labels:
547 app: sample-deploy
548 spec:
549 containers:
550 - image: hello-world:latest
551 name: hello-world
552 `)
553 if tc.transformerConfig != "" {
554 th.WriteF("config/configurations.yaml", tc.transformerConfig)
555 }
556
557 output := th.Run(".", th.MakeDefaultOptions())
558
559 th.AssertActualEqualsExpected(output, tc.expectedResult)
560 })
561 }
562 }
563
564 func TestLabelTransformerConfigWithCustomResources(t *testing.T) {
565 testCases := []struct {
566 name string
567 kustomization string
568 transformerConfig string
569 expectedResult string
570 }{
571 {
572 name: "include template via transformerConfig",
573 kustomization: `configurations:
574 - config/configurations.yaml
575
576 labels:
577 - includeSelectors: false
578 includeTemplates: false
579 pairs:
580 location: planet-earth
581 environment: dev
582
583 resources:
584 - resources/custom-resource.yaml
585 `,
586 transformerConfig: `labels:
587 - path: spec/template/metadata/labels
588 create: true
589 kind: SampleResource
590 `,
591 expectedResult: `apiVersion: custom.example.org/v1
592 kind: SampleResource
593 metadata:
594 labels:
595 environment: dev
596 location: planet-earth
597 name: sample-resource
598 namespace: sample-namespace
599 spec:
600 template:
601 metadata:
602 labels:
603 environment: dev
604 location: planet-earth
605 spec:
606 containers:
607 - env:
608 - name: VARIABLE
609 value: value
610 image: index.docker.io/library/hello-world
611 `,
612 },
613 {
614 name: "include selector via transformerConfig",
615 kustomization: `configurations:
616 - config/configurations.yaml
617
618 labels:
619 - includeSelectors: false
620 includeTemplates: false
621 pairs:
622 location: planet-earth
623 environment: dev
624
625 resources:
626 - resources/custom-resource.yaml
627 `,
628 transformerConfig: `labels:
629 - path: spec/selectors/labels
630 create: true
631 kind: SampleResource
632 `,
633 expectedResult: `apiVersion: custom.example.org/v1
634 kind: SampleResource
635 metadata:
636 labels:
637 environment: dev
638 location: planet-earth
639 name: sample-resource
640 namespace: sample-namespace
641 spec:
642 selectors:
643 labels:
644 environment: dev
645 location: planet-earth
646 template:
647 spec:
648 containers:
649 - env:
650 - name: VARIABLE
651 value: value
652 image: index.docker.io/library/hello-world
653 `,
654 },
655 {
656 name: "include selectors and labels via transformerConfig",
657 kustomization: `configurations:
658 - config/configurations.yaml
659
660 labels:
661 - includeSelectors: false
662 includeTemplates: false
663 pairs:
664 location: planet-earth
665 environment: dev
666
667 resources:
668 - resources/custom-resource.yaml
669 `,
670 transformerConfig: `
671 labels:
672 - path: spec/selectors/labels
673 create: true
674 kind: SampleResource
675 - path: spec/template/metadata/labels
676 create: true
677 kind: SampleResource
678 `,
679 expectedResult: `apiVersion: custom.example.org/v1
680 kind: SampleResource
681 metadata:
682 labels:
683 environment: dev
684 location: planet-earth
685 name: sample-resource
686 namespace: sample-namespace
687 spec:
688 selectors:
689 labels:
690 environment: dev
691 location: planet-earth
692 template:
693 metadata:
694 labels:
695 environment: dev
696 location: planet-earth
697 spec:
698 containers:
699 - env:
700 - name: VARIABLE
701 value: value
702 image: index.docker.io/library/hello-world
703 `,
704 },
705 }
706
707 for _, tc := range testCases {
708 t.Run(tc.name, func(t *testing.T) {
709 th := kusttest_test.MakeHarness(t)
710 th.WriteK(".", tc.kustomization)
711 th.WriteF("resources/custom-resource.yaml",
712 `apiVersion: custom.example.org/v1
713 kind: SampleResource
714 metadata:
715 name: sample-resource
716 namespace: sample-namespace
717 spec:
718 template:
719 spec:
720 containers:
721 - image: index.docker.io/library/hello-world
722 env:
723 - name: VARIABLE
724 value: value
725 `)
726
727 th.WriteF("config/configurations.yaml", tc.transformerConfig)
728
729 output := th.Run(".", th.MakeDefaultOptions())
730
731 th.AssertActualEqualsExpected(output, tc.expectedResult)
732 })
733 }
734 }