blob: 5473093821ca66d2ebdd543c126f31d34020a216 [file] [log] [blame]
garciadeblas83775ba2025-07-23 18:35:24 +02001#!/usr/bin/env -S nu --stdin
2#######################################################################################
3# Copyright ETSI Contributors and Others.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#######################################################################################
18
19
20use std assert
21use ../../krm/patch.nu *
22use ../../krm/overlaypatch.nu *
23
24
25
26# --- add patch tests ---
27
28export def "test overlaypatch add patch to kustomization" []: [
29 nothing -> nothing
30] {
31 let resourcelist: record = {
32 apiVersion: "config.kubernetes.io/v1"
33 kind: "ResourceList"
34 items: [
35 {
36 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
37 kind: "Kustomization"
38 metadata: { name: "example-kustomization", namespace: "default" }
39 # spec: { patches: [] }
40 spec: {}
41 }
42 ]
43 }
44
45 let kustomization_name: string = "example-kustomization"
46 let ks_namespace: string = "default"
47 let target: record = {
48 kind: "Deployment"
49 name: "example-deployment"
50 }
51 let patch_value: record = {
52 op: "replace"
53 path: "/spec/replicas"
54 value: 3
55 }
56
57 let actual: record = $resourcelist | add patch --ks-namespace $ks_namespace $kustomization_name $target $patch_value
58 let expected_patch_content: record = {
59 target: $target
60 patch: ($patch_value | to yaml)
61 }
62 let expected_resourcelist: record = {
63 apiVersion: "config.kubernetes.io/v1"
64 kind: "ResourceList"
65 items: [
66 {
67 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
68 kind: "Kustomization"
69 metadata: { name: $kustomization_name, namespace: $ks_namespace }
70 spec: { patches: [$expected_patch_content] }
71 }
72 ]
73 }
74
75 assert equal $actual $expected_resourcelist
76}
77
78
79export def "test overlaypatch add patch to kustomization with existing patches" []: [
80 nothing -> nothing
81] {
82 let resourcelist: record = {
83 apiVersion: "config.kubernetes.io/v1"
84 kind: "ResourceList"
85 items: [
86 {
87 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
88 kind: "Kustomization"
89 metadata: { name: "example-kustomization", namespace: "default" }
90 spec: { patches: [{ target: { kind: "Service", name: "example-service" }, patch: "op:\n replace\npath:\n /spec/type\nvalue:\n NodePort" }] }
91 }
92 ]
93 }
94
95 let kustomization_name: string = "example-kustomization"
96 let ks_namespace: string = "default"
97 let target: record = {
98 kind: "Deployment"
99 name: "example-deployment"
100 }
101 let patch_value: record = {
102 op: "replace"
103 path: "/spec/replicas"
104 value: 3
105 }
106
107 let actual: record = $resourcelist | add patch --ks-namespace $ks_namespace $kustomization_name $target $patch_value
108 let expected_patch_content_1st_patch: record = {
109 target: { kind: "Service", name: "example-service" }
110 patch: "op:\n replace\npath:\n /spec/type\nvalue:\n NodePort"
111 }
112 let expected_patch_content_2nd_patch: record = {
113 target: $target
114 patch: ($patch_value | to yaml)
115 }
116 let expected_resourcelist: record = {
117 apiVersion: "config.kubernetes.io/v1"
118 kind: "ResourceList"
119 items: [
120 {
121 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
122 kind: "Kustomization"
123 metadata: { name: $kustomization_name, namespace: $ks_namespace }
124 spec: { patches: [$expected_patch_content_1st_patch, $expected_patch_content_2nd_patch] }
125 }
126 ]
127 }
128
129 assert equal $actual $expected_resourcelist
130}
131
132
133# --- add jsonpatch tests ---
134
135export def "test overlaypatch add jsonpatch add operation" []: [
136 nothing -> nothing
137] {
138 let resourcelist: record = {
139 apiVersion: "config.kubernetes.io/v1"
140 kind: "ResourceList"
141 items: [
142 {
143 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
144 kind: "Kustomization"
145 metadata: { name: "example-kustomization", namespace: "default" }
146 # spec: { patches: [] }
147 spec: {}
148 }
149 ]
150 }
151
152 let kustomization_name: string = "example-kustomization"
153 let ks_namespace: string = "default"
154 let target: record = {
155 kind: "Deployment"
156 name: "example-deployment"
157 }
158 let path: string = "/spec/replicas"
159 let value: any = 3
160
161 let actual: record = $resourcelist | add jsonpatch --ks-namespace $ks_namespace $kustomization_name $target $path $value
162 let expected_patch_content: record = {
163 target: $target
164 patch: (
165 [{ op: "add", path: $path, value: $value }] | to yaml
166 )
167 }
168 let expected_resourcelist: record = {
169 apiVersion: "config.kubernetes.io/v1"
170 kind: "ResourceList"
171 items: [
172 {
173 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
174 kind: "Kustomization"
175 metadata: { name: $kustomization_name, namespace: $ks_namespace }
176 spec: { patches: [$expected_patch_content] }
177 }
178 ]
179 }
180
181 assert equal $actual $expected_resourcelist
182}
183
184
185export def "test overlaypatch add jsonpatch replace operation" []: [
186 nothing -> nothing
187] {
188 let resourcelist: record = {
189 apiVersion: "config.kubernetes.io/v1"
190 kind: "ResourceList"
191 items: [
192 {
193 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
194 kind: "Kustomization"
195 metadata: { name: "example-kustomization", namespace: "default" }
196 # spec: { patches: [] }
197 spec: {}
198 }
199 ]
200 }
201
202 let kustomization_name: string = "example-kustomization"
203 let ks_namespace: string = "default"
204 let target: record = {
205 kind: "Deployment"
206 name: "example-deployment"
207 }
208 let path: string = "/spec/replicas"
209 let value: any = 3
210
211 let actual: record = $resourcelist | (
212 add jsonpatch
213 --ks-namespace $ks_namespace
214 --operation "replace"
215 $kustomization_name
216 $target
217 $path
218 $value
219 )
220 let expected_patch_content: record = {
221 target: $target
222 patch: (
223 [{ op: "replace", path: $path, value: $value }]
224 | to yaml
225 )
226 }
227 let expected_resourcelist: record = {
228 apiVersion: "config.kubernetes.io/v1"
229 kind: "ResourceList"
230 items: [
231 {
232 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
233 kind: "Kustomization"
234 metadata: { name: $kustomization_name, namespace: $ks_namespace }
235 spec: { patches: [$expected_patch_content] }
236 }
237 ]
238 }
239
240 assert equal $actual $expected_resourcelist
241}
242
243
244export def "test overlaypatch add jsonpatch remove operation" []: [
245 nothing -> nothing
246] {
247 let resourcelist: record = {
248 apiVersion: "config.kubernetes.io/v1"
249 kind: "ResourceList"
250 items: [
251 {
252 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
253 kind: "Kustomization"
254 metadata: { name: "example-kustomization", namespace: "default" }
255 spec: { patches: [] }
256 }
257 ]
258 }
259
260 let kustomization_name: string = "example-kustomization"
261 let ks_namespace: string = "default"
262 let target: record = {
263 kind: "Deployment"
264 name: "example-deployment"
265 }
266 let path: string = "/spec/replicas"
267
268 let actual: record = $resourcelist | (
269 add jsonpatch
270 --ks-namespace $ks_namespace
271 --operation "remove"
272 $kustomization_name
273 $target
274 $path
275 )
276 let expected_patch_content: record = {
277 target: $target
278 patch: (
279 [{ op: "remove", path: $path}]
280 | to yaml
281 )
282 }
283 let expected_resourcelist: record = {
284 apiVersion: "config.kubernetes.io/v1"
285 kind: "ResourceList"
286 items: [
287 {
288 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
289 kind: "Kustomization"
290 metadata: { name: $kustomization_name, namespace: $ks_namespace }
291 spec: { patches: [$expected_patch_content] }
292 }
293 ]
294 }
295
296 assert equal $actual $expected_resourcelist
297}
298
299
300export def "test overlaypatch add jsonpatch move operation" []: [
301 nothing -> nothing
302] {
303 let resourcelist: record = {
304 apiVersion: "config.kubernetes.io/v1"
305 kind: "ResourceList"
306 items: [
307 {
308 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
309 kind: "Kustomization"
310 metadata: { name: "example-kustomization", namespace: "default" }
311 # spec: { patches: [] }
312 spec: {}
313 }
314 ]
315 }
316
317 let kustomization_name: string = "example-kustomization"
318 let ks_namespace: string = "default"
319 let target: record = {
320 kind: "Deployment"
321 name: "example-deployment"
322 }
323 let path: string = "/spec/new-replicas"
324 let from: string = "/spec/replicas"
325
326 let actual: record = (
327 $resourcelist
328 | add jsonpatch
329 --ks-namespace $ks_namespace
330 --operation "move"
331 $kustomization_name
332 $target
333 $path
334 ''
335 $from
336 )
337 let expected_patch_content: record = {
338 target: $target
339 patch: (
340 [{ op: "move", from: $from, path: $path }]
341 | to yaml
342 )
343 }
344 let expected_resourcelist: record = {
345 apiVersion: "config.kubernetes.io/v1"
346 kind: "ResourceList"
347 items: [
348 {
349 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
350 kind: "Kustomization"
351 metadata: { name: $kustomization_name, namespace: $ks_namespace }
352 spec: { patches: [$expected_patch_content] }
353 }
354 ]
355 }
356
357 assert equal $actual $expected_resourcelist
358}
359
360
361export def "test overlaypatch add jsonpatch copy operation" []: [
362 nothing -> nothing
363] {
364 let resourcelist: record = {
365 apiVersion: "config.kubernetes.io/v1"
366 kind: "ResourceList"
367 items: [
368 {
369 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
370 kind: "Kustomization"
371 metadata: { name: "example-kustomization", namespace: "default" }
372 spec: { patches: [] }
373 }
374 ]
375 }
376
377 let kustomization_name: string = "example-kustomization"
378 let ks_namespace: string = "default"
379 let target: record = {
380 kind: "Deployment"
381 name: "example-deployment"
382 }
383 let path: string = "/spec/new-replicas"
384 let from: string = "/spec/replicas"
385
386 let actual: record = (
387 $resourcelist
388 | add jsonpatch
389 --ks-namespace $ks_namespace
390 --operation "copy"
391 $kustomization_name
392 $target
393 $path
394 ''
395 $from
396 )
397 let expected_patch_content: record = {
398 target: $target
399 patch: (
400 [{ op: "copy", from: $from, path: $path }]
401 | to yaml
402 )
403 }
404 let expected_resourcelist: record = {
405 apiVersion: "config.kubernetes.io/v1"
406 kind: "ResourceList"
407 items: [
408 {
409 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
410 kind: "Kustomization"
411 metadata: { name: $kustomization_name, namespace: $ks_namespace }
412 spec: { patches: [$expected_patch_content] }
413 }
414 ]
415 }
416
417 assert equal $actual $expected_resourcelist
418}
419
420
421export def "test overlaypatch add jsonpatch invalid operation" []: [
422 nothing -> nothing
423] {
424 let resourcelist: record = {
425 apiVersion: "config.kubernetes.io/v1"
426 kind: "ResourceList"
427 items: [
428 {
429 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
430 kind: "Kustomization"
431 metadata: { name: "example-kustomization", namespace: "default" }
432 # spec: { patches: [] }
433 spec: {}
434 }
435 ]
436 }
437
438 let kustomization_name: string = "example-kustomization"
439 let ks_namespace: string = "default"
440 let target: record = {
441 kind: "Deployment"
442 name: "example-deployment"
443 }
444 let path: string = "/spec/replicas"
445
446 let error_occurred: any = try {
447 $resourcelist | (
448 add jsonpatch
449 --ks-namespace $ks_namespace
450 --operation "invalid"
451 $kustomization_name
452 $target
453 $path
454 )
455 } catch {
456 |err| $err.msg
457 }
458
459 assert equal $error_occurred "Invalid operation type. Supported values are 'add', 'remove', 'replace', 'move', 'copy'. See RFC6902 for details"
460}
461
462
463# --- helmrelease add inline values tests ---
464
465export def "test overlaypatch helmrelease add inline values with add operation" []: [
466 nothing -> nothing
467] {
468 let resourcelist: record = {
469 apiVersion: "config.kubernetes.io/v1"
470 kind: "ResourceList"
471 items: [
472 {
473 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
474 kind: "Kustomization"
475 metadata: { name: "example-kustomization", namespace: "default" }
476 # spec: { patches: [] }
477 spec: {}
478 }
479 ]
480 }
481
482 let ks_namespace: string = "default"
483 let hr_namespace: string = ""
484 let kustomization_name: string = "example-kustomization"
485 let helmrelease_name: string = "example-helmrelease"
486 let values: record = { key1: "value1", key2: "value2" }
487
488 let actual: record = (
489 $resourcelist
490 | (
491 helmrelease add inline values
492 --ks-namespace $ks_namespace
493 $kustomization_name
494 $helmrelease_name
495 $values
496 )
497 )
498 let expected_patch_content: record = {
499 target: { kind: "HelmRelease", name: $helmrelease_name }
500 patch: (
501 [{ op: "add", path: "/spec/values", value: $values }]
502 | to yaml
503 )
504 }
505 let expected_resourcelist: record = {
506 apiVersion: "config.kubernetes.io/v1"
507 kind: "ResourceList"
508 items: [
509 {
510 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
511 kind: "Kustomization"
512 metadata: { name: $kustomization_name, namespace: $ks_namespace }
513 spec: { patches: [$expected_patch_content] }
514 }
515 ]
516 }
517
518 assert equal $actual $expected_resourcelist
519}
520
521
522export def "test overlaypatch helmrelease add inline values with replace operation" []: [
523 nothing -> nothing
524] {
525 let resourcelist: record = {
526 apiVersion: "config.kubernetes.io/v1"
527 kind: "ResourceList"
528 items: [
529 {
530 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
531 kind: "Kustomization"
532 metadata: { name: "example-kustomization", namespace: "default" }
533 # spec: { patches: [] }
534 spec: {}
535 }
536 ]
537 }
538
539 let ks_namespace: string = "default"
540 let hr_namespace: string = ""
541 let kustomization_name: string = "example-kustomization"
542 let helmrelease_name: string = "example-helmrelease"
543 let values: record = { key1: "value1", key2: "value2" }
544
545 let actual: record = (
546 $resourcelist
547 | (
548 helmrelease add inline values
549 --ks-namespace $ks_namespace
550 --operation replace
551 $kustomization_name
552 $helmrelease_name
553 $values
554 )
555 )
556 let expected_patch_content: record = {
557 target: { kind: "HelmRelease", name: $helmrelease_name }
558 patch: (
559 [{ op: "replace", path: "/spec/values", value: $values }]
560 | to yaml
561 )
562 }
563 let expected_resourcelist: record = {
564 apiVersion: "config.kubernetes.io/v1"
565 kind: "ResourceList"
566 items: [
567 {
568 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
569 kind: "Kustomization"
570 metadata: { name: $kustomization_name, namespace: $ks_namespace }
571 spec: { patches: [$expected_patch_content] }
572 }
573 ]
574 }
575
576 assert equal $actual $expected_resourcelist
577}
578
579
580export def "test overlaypatch helmrelease add inline values with existing patches" []: [
581 nothing -> nothing
582] {
583 let resourcelist: record = {
584 apiVersion: "config.kubernetes.io/v1"
585 kind: "ResourceList"
586 items: [
587 {
588 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
589 kind: "Kustomization"
590 metadata: { name: "example-kustomization", namespace: "default" }
591 spec: { patches: [
592 {
593 target: { kind: "HelmRelease", name: "existing-helmrelease" }
594 patch: (
595 [{ op: "replace", path: "/spec/values/replicaCount", value: 2 }]
596 | to yaml
597 )
598 }
599 ] }
600 }
601 ]
602 }
603
604 let ks_namespace: string = "default"
605 let hr_namespace: string = ""
606 let kustomization_name: string = "example-kustomization"
607 let helmrelease_name: string = "example-helmrelease"
608 let values: record = { key1: "value1", key2: "value2" }
609
610 let actual: record = $resourcelist | helmrelease add inline values --ks-namespace $ks_namespace $kustomization_name $helmrelease_name $values
611 let expected_patch_content_new: record = {
612 target: { kind: "HelmRelease", name: $helmrelease_name }
613 patch: (
614 [{ op: "add", path: "/spec/values", value: $values }]
615 | to yaml
616 )
617 }
618 let expected_resourcelist: record = {
619 apiVersion: "config.kubernetes.io/v1"
620 kind: "ResourceList"
621 items: [
622 {
623 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
624 kind: "Kustomization"
625 metadata: { name: $kustomization_name, namespace: $ks_namespace }
626 spec: { patches: [
627 {
628 target: { kind: "HelmRelease", name: "existing-helmrelease" }
629 patch: (
630 [{ op: "replace", path: "/spec/values/replicaCount", value: 2 }]
631 | to yaml
632 )
633 },
634 $expected_patch_content_new
635 ] }
636 }
637 ]
638 }
639
640 assert equal $actual $expected_resourcelist
641}
642
643
644# --- helmrelease add values from configmap tests ---
645
646export def "test overlaypatch helmrelease add values from configmap basic" []: [
647 nothing -> nothing
648] {
649 let resourcelist: record = {
650 apiVersion: "config.kubernetes.io/v1"
651 kind: "ResourceList"
652 items: [
653 {
654 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
655 kind: "Kustomization"
656 metadata: { name: "example-kustomization", namespace: "default" }
657 # spec: { patches: [] }
658 spec: {}
659 }
660 ]
661 }
662
663 let ks_namespace: string = "default"
664 let hr_namespace: string = ""
665 let kustomization_name: string = "example-kustomization"
666 let helmrelease_name: string = "example-helmrelease"
667 let cm_name: string = "example-configmap"
668 let cm_key: string = "values.yaml"
669
670 let actual: record = (
671 $resourcelist
672 | (
673 helmrelease add values from configmap
674 --ks-namespace $ks_namespace
675 $kustomization_name
676 $helmrelease_name
677 $cm_name
678 $cm_key
679 )
680 )
681 let expected_patch_content: record = {
682 target: { kind: "HelmRelease", name: $helmrelease_name }
683 patch: (
684 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key } }]
685 | to yaml
686 )
687 }
688 let expected_resourcelist: record = {
689 apiVersion: "config.kubernetes.io/v1"
690 kind: "ResourceList"
691 items: [
692 {
693 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
694 kind: "Kustomization"
695 metadata: { name: $kustomization_name, namespace: $ks_namespace }
696 spec: { patches: [$expected_patch_content] }
697 }
698 ]
699 }
700
701 assert equal $actual $expected_resourcelist
702}
703
704
705export def "test overlaypatch helmrelease add values from configmap with target path" []: [
706 nothing -> nothing
707] {
708 let resourcelist: record = {
709 apiVersion: "config.kubernetes.io/v1"
710 kind: "ResourceList"
711 items: [
712 {
713 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
714 kind: "Kustomization"
715 metadata: { name: "example-kustomization", namespace: "default" }
716 # spec: { patches: [] }
717 spec: {}
718 }
719 ]
720 }
721
722 let ks_namespace: string = "default"
723 let hr_namespace: string = ""
724 let kustomization_name: string = "example-kustomization"
725 let helmrelease_name: string = "example-helmrelease"
726 let cm_name: string = "example-configmap"
727 let cm_key: string = "values.yaml"
728 let target_path: string = "/custom/path"
729
730 let actual: record = (
731 $resourcelist
732 | (
733 helmrelease add values from configmap
734 --ks-namespace $ks_namespace
735 --target-path $target_path
736 $kustomization_name
737 $helmrelease_name
738 $cm_name
739 $cm_key
740 )
741 )
742 let expected_patch_content: record = {
743 target: { kind: "HelmRelease", name: $helmrelease_name }
744 patch: (
745 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key, targetPath: $target_path } }]
746 | to yaml
747 )
748 }
749 let expected_resourcelist: record = {
750 apiVersion: "config.kubernetes.io/v1"
751 kind: "ResourceList"
752 items: [
753 {
754 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
755 kind: "Kustomization"
756 metadata: { name: $kustomization_name, namespace: $ks_namespace }
757 spec: { patches: [$expected_patch_content] }
758 }
759 ]
760 }
761
762 assert equal $actual $expected_resourcelist
763}
764
765
766export def "test overlaypatch helmrelease add values from configmap with existing patches" []: [
767 nothing -> nothing
768] {
769 let resourcelist: record = {
770 apiVersion: "config.kubernetes.io/v1"
771 kind: "ResourceList"
772 items: [
773 {
774 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
775 kind: "Kustomization"
776 metadata: { name: "example-kustomization", namespace: "default" }
777 spec: { patches: [
778 {
779 target: { kind: "HelmRelease", name: "existing-helmrelease" }
780 patch: (
781 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: "existing-configmap", key: "existing-values.yaml" } }]
782 | to yaml
783 )
784 }
785 ] }
786 }
787 ]
788 }
789
790 let ks_namespace: string = "default"
791 let hr_namespace: string = ""
792 let kustomization_name: string = "example-kustomization"
793 let helmrelease_name: string = "example-helmrelease"
794 let cm_name: string = "example-configmap"
795 let cm_key: string = "values.yaml"
796
797 let actual: record = (
798 $resourcelist
799 | (
800 helmrelease add values from configmap
801 --ks-namespace $ks_namespace
802 $kustomization_name
803 $helmrelease_name
804 $cm_name
805 $cm_key
806 )
807 )
808 let expected_patch_content_existing: record = {
809 target: { kind: "HelmRelease", name: "existing-helmrelease" }
810 patch: (
811 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: "existing-configmap", key: "existing-values.yaml" } }]
812 | to yaml
813 )
814 }
815 let expected_patch_content_new: record = {
816 target: { kind: "HelmRelease", name: $helmrelease_name }
817 patch: (
818 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key } }]
819 | to yaml
820 )
821 }
822 let expected_resourcelist: record = {
823 apiVersion: "config.kubernetes.io/v1"
824 kind: "ResourceList"
825 items: [
826 {
827 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
828 kind: "Kustomization"
829 metadata: { name: $kustomization_name, namespace: $ks_namespace }
830 spec: { patches: [$expected_patch_content_existing, $expected_patch_content_new] }
831 }
832 ]
833 }
834
835 assert equal $actual $expected_resourcelist
836}
837
838
839
840# --- helmrelease add values from secret tests ---
841
842export def "test overlaypatch helmrelease add values from secret basic" []: [
843 nothing -> nothing
844] {
845 let resourcelist: record = {
846 apiVersion: "config.kubernetes.io/v1"
847 kind: "ResourceList"
848 items: [
849 {
850 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
851 kind: "Kustomization"
852 metadata: { name: "example-kustomization", namespace: "default" }
853 # spec: { patches: [] }
854 spec: {}
855 }
856 ]
857 }
858
859 let ks_namespace: string = "default"
860 let hr_namespace: string = ""
861 let kustomization_name: string = "example-kustomization"
862 let helmrelease_name: string = "example-helmrelease"
863 let secret_name: string = "example-secret"
864 let secret_key: string = "values.yaml"
865
866 let actual: record = (
867 $resourcelist
868 | (
869 helmrelease add values from secret
870 --ks-namespace $ks_namespace
871 $kustomization_name
872 $helmrelease_name
873 $secret_name
874 $secret_key
875 )
876 )
877 let expected_patch_content: record = {
878 target: { kind: "HelmRelease", name: $helmrelease_name }
879 patch: (
880 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
881 | to yaml
882 )
883 }
884 let expected_resourcelist: record = {
885 apiVersion: "config.kubernetes.io/v1"
886 kind: "ResourceList"
887 items: [
888 {
889 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
890 kind: "Kustomization"
891 metadata: { name: $kustomization_name, namespace: $ks_namespace }
892 spec: { patches: [$expected_patch_content] }
893 }
894 ]
895 }
896
897 assert equal $actual $expected_resourcelist
898}
899
900
901export def "test overlaypatch helmrelease add values from secret with target path" []: [
902 nothing -> nothing
903] {
904 let resourcelist: record = {
905 apiVersion: "config.kubernetes.io/v1"
906 kind: "ResourceList"
907 items: [
908 {
909 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
910 kind: "Kustomization"
911 metadata: { name: "example-kustomization", namespace: "default" }
912 # spec: { patches: [] }
913 spec: {}
914 }
915 ]
916 }
917
918 let ks_namespace: string = "default"
919 let hr_namespace: string = ""
920 let kustomization_name: string = "example-kustomization"
921 let helmrelease_name: string = "example-helmrelease"
922 let secret_name: string = "example-secret"
923 let secret_key: string = "values.yaml"
924 let target_path: string = "/custom/path"
925
926 let actual: record = (
927 $resourcelist
928 | (
929 helmrelease add values from secret
930 --ks-namespace $ks_namespace
931 --target-path $target_path
932 $kustomization_name
933 $helmrelease_name
934 $secret_name
935 $secret_key
936 )
937 )
938 let expected_patch_content: record = {
939 target: { kind: "HelmRelease", name: $helmrelease_name }
940 patch: (
941 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key, targetPath: $target_path } }]
942 | to yaml
943 )
944 }
945 let expected_resourcelist: record = {
946 apiVersion: "config.kubernetes.io/v1"
947 kind: "ResourceList"
948 items: [
949 {
950 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
951 kind: "Kustomization"
952 metadata: { name: $kustomization_name, namespace: $ks_namespace }
953 spec: { patches: [$expected_patch_content] }
954 }
955 ]
956 }
957
958 assert equal $actual $expected_resourcelist
959}
960
961
962export def "test overlaypatch helmrelease add values from secret with optional flag" []: [
963 nothing -> nothing
964] {
965 let resourcelist: record = {
966 apiVersion: "config.kubernetes.io/v1"
967 kind: "ResourceList"
968 items: [
969 {
970 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
971 kind: "Kustomization"
972 metadata: { name: "example-kustomization", namespace: "default" }
973 # spec: { patches: [] }
974 spec: {}
975 }
976 ]
977 }
978
979 let ks_namespace: string = "default"
980 let hr_namespace: string = ""
981 let kustomization_name: string = "example-kustomization"
982 let helmrelease_name: string = "example-helmrelease"
983 let secret_name: string = "example-secret"
984 let secret_key: string = "values.yaml"
985
986 let actual: record = (
987 $resourcelist
988 | (
989 helmrelease add values from secret
990 --ks-namespace $ks_namespace
991 --optional $kustomization_name
992 $helmrelease_name
993 $secret_name
994 $secret_key
995 )
996 )
997 let expected_patch_content: record = {
998 target: { kind: "HelmRelease", name: $helmrelease_name }
999 patch: (
1000 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key, optional: true } }]
1001 | to yaml
1002 )
1003 }
1004 let expected_resourcelist: record = {
1005 apiVersion: "config.kubernetes.io/v1"
1006 kind: "ResourceList"
1007 items: [
1008 {
1009 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1010 kind: "Kustomization"
1011 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1012 spec: { patches: [$expected_patch_content] }
1013 }
1014 ]
1015 }
1016
1017 assert equal $actual $expected_resourcelist
1018}
1019
1020
1021export def "test overlaypatch helmrelease add values from secret with hr namespace" []: [
1022 nothing -> nothing
1023] {
1024 let resourcelist: record = {
1025 apiVersion: "config.kubernetes.io/v1"
1026 kind: "ResourceList"
1027 items: [
1028 {
1029 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1030 kind: "Kustomization"
1031 metadata: { name: "example-kustomization", namespace: "default" }
1032 # spec: { patches: [] }
1033 spec: {}
1034 }
1035 ]
1036 }
1037
1038 let ks_namespace: string = "default"
1039 let hr_namespace: string = "example-namespace"
1040 let kustomization_name: string = "example-kustomization"
1041 let helmrelease_name: string = "example-helmrelease"
1042 let secret_name: string = "example-secret"
1043 let secret_key: string = "values.yaml"
1044
1045 let actual: record = (
1046 $resourcelist
1047 | (
1048 helmrelease add values from secret
1049 --ks-namespace $ks_namespace
1050 --hr-namespace $hr_namespace
1051 $kustomization_name
1052 $helmrelease_name
1053 $secret_name
1054 $secret_key
1055 )
1056 )
1057 let expected_patch_content: record = {
1058 target: { kind: "HelmRelease", name: $helmrelease_name, namespace: $hr_namespace }
1059 patch: (
1060 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
1061 | to yaml
1062 )
1063 }
1064 let expected_resourcelist: record = {
1065 apiVersion: "config.kubernetes.io/v1"
1066 kind: "ResourceList"
1067 items: [
1068 {
1069 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1070 kind: "Kustomization"
1071 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1072 spec: { patches: [$expected_patch_content] }
1073 }
1074 ]
1075 }
1076
1077 assert equal $actual $expected_resourcelist
1078}
1079
1080
1081export def "test overlaypatch helmrelease add values from secret with existing patches" []: [
1082 nothing -> nothing
1083] {
1084 let resourcelist: record = {
1085 apiVersion: "config.kubernetes.io/v1"
1086 kind: "ResourceList"
1087 items: [
1088 {
1089 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1090 kind: "Kustomization"
1091 metadata: { name: "example-kustomization", namespace: "default" }
1092 spec: { patches: [
1093 {
1094 target: { kind: "HelmRelease", name: "existing-helmrelease" }
1095 patch: (
1096 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: "existing-secret", key: "existing-values.yaml" } }]
1097 | to yaml
1098 )
1099 }
1100 ] }
1101 }
1102 ]
1103 }
1104
1105 let ks_namespace: string = "default"
1106 let hr_namespace: string = ""
1107 let kustomization_name: string = "example-kustomization"
1108 let helmrelease_name: string = "example-helmrelease"
1109 let secret_name: string = "example-secret"
1110 let secret_key: string = "values.yaml"
1111
1112 let actual: record = (
1113 $resourcelist
1114 | (
1115 helmrelease add values from secret
1116 --ks-namespace $ks_namespace
1117 $kustomization_name
1118 $helmrelease_name
1119 $secret_name
1120 $secret_key
1121 )
1122 )
1123 let expected_patch_content_existing: record = {
1124 target: { kind: "HelmRelease", name: "existing-helmrelease" }
1125 patch: (
1126 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: "existing-secret", key: "existing-values.yaml" } }]
1127 | to yaml
1128 )
1129 }
1130 let expected_patch_content_new: record = {
1131 target: { kind: "HelmRelease", name: $helmrelease_name }
1132 patch: (
1133 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
1134 | to yaml
1135 )
1136 }
1137 let expected_resourcelist: record = {
1138 apiVersion: "config.kubernetes.io/v1"
1139 kind: "ResourceList"
1140 items: [
1141 {
1142 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1143 kind: "Kustomization"
1144 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1145 spec: { patches: [$expected_patch_content_existing, $expected_patch_content_new] }
1146 }
1147 ]
1148 }
1149
1150 assert equal $actual $expected_resourcelist
1151}
1152
1153
1154# TODO:
1155
1156# --- helmrelease set values ---
1157
1158## Inline values only
1159export def "test overlaypatch helmrelease set values with inline values only" []: [
1160 nothing -> nothing
1161] {
1162 let resourcelist: record = {
1163 apiVersion: "config.kubernetes.io/v1"
1164 kind: "ResourceList"
1165 items: [
1166 {
1167 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1168 kind: "Kustomization"
1169 metadata: { name: "example-kustomization", namespace: "default" }
1170 # spec: { patches: [] }
1171 spec: {}
1172 }
1173 ]
1174 }
1175
1176 let ks_namespace: string = "default"
1177 let kustomization_name: string = "example-kustomization"
1178 let helmrelease_name: string = "example-helmrelease"
1179 let inline_values: record = { key1: "value1", key2: "value2" }
1180
1181 let actual: record = (
1182 $resourcelist
1183 | (
1184 helmrelease set values
1185 --ks-namespace $ks_namespace
1186 $kustomization_name
1187 $helmrelease_name
1188 $inline_values
1189 )
1190 )
1191 let expected_patch_content: record = {
1192 target: { kind: "HelmRelease", name: $helmrelease_name }
1193 patch: (
1194 [{ op: "add", path: "/spec/values", value: $inline_values }]
1195 | to yaml
1196 )
1197 }
1198 let expected_resourcelist: record = {
1199 apiVersion: "config.kubernetes.io/v1"
1200 kind: "ResourceList"
1201 items: [
1202 {
1203 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1204 kind: "Kustomization"
1205 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1206 spec: { patches: [$expected_patch_content] }
1207 }
1208 ]
1209 }
1210
1211 assert equal $actual $expected_resourcelist
1212}
1213
1214
1215## Values from ConfigMap only
1216export def "test overlaypatch helmrelease set values with configmap only" []: [
1217 nothing -> nothing
1218] {
1219 let resourcelist: record = {
1220 apiVersion: "config.kubernetes.io/v1"
1221 kind: "ResourceList"
1222 items: [
1223 {
1224 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1225 kind: "Kustomization"
1226 metadata: { name: "example-kustomization", namespace: "default" }
1227 # spec: { patches: [] }
1228 spec: {}
1229 }
1230 ]
1231 }
1232
1233 let ks_namespace: string = "default"
1234 let kustomization_name: string = "example-kustomization"
1235 let helmrelease_name: string = "example-helmrelease"
1236 let cm_name: string = "example-configmap"
1237
1238 let actual: record = (
1239 $resourcelist
1240 | (
1241 helmrelease set values
1242 --ks-namespace $ks_namespace
1243 $kustomization_name
1244 $helmrelease_name
1245 {}
1246 $cm_name
1247 )
1248 )
1249 let expected_patch_content: record = {
1250 target: { kind: "HelmRelease", name: $helmrelease_name }
1251 patch: (
1252 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
1253 | to yaml
1254 )
1255 }
1256 let expected_resourcelist: record = {
1257 apiVersion: "config.kubernetes.io/v1"
1258 kind: "ResourceList"
1259 items: [
1260 {
1261 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1262 kind: "Kustomization"
1263 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1264 spec: { patches: [$expected_patch_content] }
1265 }
1266 ]
1267 }
1268
1269 assert equal $actual $expected_resourcelist
1270}
1271
1272
1273## Values from Secret only
1274export def "test overlaypatch helmrelease set values with secret only" []: [
1275 nothing -> nothing
1276] {
1277 let resourcelist: record = {
1278 apiVersion: "config.kubernetes.io/v1"
1279 kind: "ResourceList"
1280 items: [
1281 {
1282 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1283 kind: "Kustomization"
1284 metadata: { name: "example-kustomization", namespace: "default" }
1285 # spec: { patches: [] }
1286 spec: {}
1287 }
1288 ]
1289 }
1290
1291 let ks_namespace: string = "default"
1292 let kustomization_name: string = "example-kustomization"
1293 let helmrelease_name: string = "example-helmrelease"
1294 let secret_name: string = "example-secret"
1295
1296 let actual: record = (
1297 $resourcelist
1298 | (
1299 helmrelease set values
1300 --ks-namespace $ks_namespace
1301 $kustomization_name
1302 $helmrelease_name
1303 {}
1304 ''
1305 $secret_name
1306 )
1307 )
1308 let expected_patch_content: record = {
1309 target: { kind: "HelmRelease", name: $helmrelease_name }
1310 patch: (
1311 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
1312 | to yaml
1313 )
1314 }
1315 let expected_resourcelist: record = {
1316 apiVersion: "config.kubernetes.io/v1"
1317 kind: "ResourceList"
1318 items: [
1319 {
1320 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1321 kind: "Kustomization"
1322 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1323 spec: { patches: [$expected_patch_content] }
1324 }
1325 ]
1326 }
1327
1328 assert equal $actual $expected_resourcelist
1329}
1330
1331
1332## Inline values and values from ConfigMap
1333export def "test overlaypatch helmrelease set values with inline and configmap" []: [
1334 nothing -> nothing
1335] {
1336 let resourcelist: record = {
1337 apiVersion: "config.kubernetes.io/v1"
1338 kind: "ResourceList"
1339 items: [
1340 {
1341 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1342 kind: "Kustomization"
1343 metadata: { name: "example-kustomization", namespace: "default" }
1344 # spec: { patches: [] }
1345 spec: {}
1346 }
1347 ]
1348 }
1349
1350 let ks_namespace: string = "default"
1351 let kustomization_name: string = "example-kustomization"
1352 let helmrelease_name: string = "example-helmrelease"
1353 let inline_values: record = { key1: "value1", key2: "value2" }
1354 let cm_name: string = "example-configmap"
1355
1356 let actual: record = (
1357 $resourcelist
1358 | (
1359 helmrelease set values
1360 --ks-namespace $ks_namespace
1361 $kustomization_name
1362 $helmrelease_name
1363 $inline_values
1364 $cm_name
1365 )
1366 )
1367 let expected_patch_content_inline_values: record = {
1368 target: { kind: "HelmRelease", name: $helmrelease_name }
1369 patch: (
1370 [{ op: "add", path: "/spec/values", value: $inline_values }]
1371 | to yaml
1372 )
1373 }
1374 let expected_patch_content_cm_values: record = {
1375 target: { kind: "HelmRelease", name: $helmrelease_name }
1376 patch: (
1377 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
1378 | to yaml
1379 )
1380 }
1381 let expected_resourcelist: record = {
1382 apiVersion: "config.kubernetes.io/v1"
1383 kind: "ResourceList"
1384 items: [
1385 {
1386 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1387 kind: "Kustomization"
1388 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1389 spec: { patches: [$expected_patch_content_inline_values, $expected_patch_content_cm_values] }
1390 }
1391 ]
1392 }
1393
1394 assert equal $actual $expected_resourcelist
1395}
1396
1397
1398## Inline values and values from secret
1399export def "test overlaypatch helmrelease set values with inline and secret" []: [
1400 nothing -> nothing
1401] {
1402 let resourcelist: record = {
1403 apiVersion: "config.kubernetes.io/v1"
1404 kind: "ResourceList"
1405 items: [
1406 {
1407 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1408 kind: "Kustomization"
1409 metadata: { name: "example-kustomization", namespace: "default" }
1410 # spec: { patches: [] }
1411 spec: {}
1412 }
1413 ]
1414 }
1415
1416 let ks_namespace: string = "default"
1417 let kustomization_name: string = "example-kustomization"
1418 let helmrelease_name: string = "example-helmrelease"
1419 let inline_values: record = { key1: "value1", key2: "value2" }
1420 let secret_name: string = "example-secret"
1421
1422 let actual: record = (
1423 $resourcelist
1424 | (
1425 helmrelease set values
1426 --ks-namespace $ks_namespace
1427 $kustomization_name
1428 $helmrelease_name
1429 $inline_values
1430 ''
1431 $secret_name
1432 )
1433 )
1434 let expected_patch_content_inline_values: record = {
1435 target: { kind: "HelmRelease", name: $helmrelease_name }
1436 patch: (
1437 [{ op: "add", path: "/spec/values", value: $inline_values }]
1438 | to yaml
1439 )
1440 }
1441 let expected_patch_content_secret_values: record = {
1442 target: { kind: "HelmRelease", name: $helmrelease_name }
1443 patch: (
1444 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
1445 | to yaml
1446 )
1447 }
1448 let expected_resourcelist: record = {
1449 apiVersion: "config.kubernetes.io/v1"
1450 kind: "ResourceList"
1451 items: [
1452 {
1453 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1454 kind: "Kustomization"
1455 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1456 spec: { patches: [$expected_patch_content_inline_values, $expected_patch_content_secret_values] }
1457 }
1458 ]
1459 }
1460
1461 assert equal $actual $expected_resourcelist
1462}
1463
1464
1465## Values from cm and values from secret
1466export def "test overlaypatch helmrelease set values with configmap and secret" []: [
1467 nothing -> nothing
1468] {
1469 let resourcelist: record = {
1470 apiVersion: "config.kubernetes.io/v1"
1471 kind: "ResourceList"
1472 items: [
1473 {
1474 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1475 kind: "Kustomization"
1476 metadata: { name: "example-kustomization", namespace: "default" }
1477 # spec: { patches: [] }
1478 spec: {}
1479 }
1480 ]
1481 }
1482
1483 let ks_namespace: string = "default"
1484 let kustomization_name: string = "example-kustomization"
1485 let helmrelease_name: string = "example-helmrelease"
1486 let cm_name: string = "example-configmap"
1487 let secret_name: string = "example-secret"
1488
1489 let actual: record = (
1490 $resourcelist
1491 | (
1492 helmrelease set values
1493 --ks-namespace $ks_namespace
1494 $kustomization_name
1495 $helmrelease_name
1496 {}
1497 $cm_name
1498 $secret_name
1499 )
1500 )
1501 let expected_patch_content_cm_values: record = {
1502 target: { kind: "HelmRelease", name: $helmrelease_name }
1503 patch: (
1504 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
1505 | to yaml
1506 )
1507 }
1508 let expected_patch_content_secret_values: record = {
1509 target: { kind: "HelmRelease", name: $helmrelease_name }
1510 patch: (
1511 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
1512 | to yaml
1513 )
1514 }
1515 let expected_resourcelist: record = {
1516 apiVersion: "config.kubernetes.io/v1"
1517 kind: "ResourceList"
1518 items: [
1519 {
1520 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1521 kind: "Kustomization"
1522 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1523 spec: { patches: [$expected_patch_content_cm_values, $expected_patch_content_secret_values] }
1524 }
1525 ]
1526 }
1527
1528 assert equal $actual $expected_resourcelist
1529}
1530
1531
1532## Inline values, values from cm and values from secret
1533export def "test overlaypatch helmrelease set values with inline, configmap, and secret" []: [
1534 nothing -> nothing
1535] {
1536 let resourcelist: record = {
1537 apiVersion: "config.kubernetes.io/v1"
1538 kind: "ResourceList"
1539 items: [
1540 {
1541 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1542 kind: "Kustomization"
1543 metadata: { name: "example-kustomization", namespace: "default" }
1544 # spec: { patches: [] }
1545 spec: {}
1546 }
1547 ]
1548 }
1549
1550 let ks_namespace: string = "default"
1551 let kustomization_name: string = "example-kustomization"
1552 let helmrelease_name: string = "example-helmrelease"
1553 let inline_values: record = { key1: "value1", key2: "value2" }
1554 let cm_name: string = "example-configmap"
1555 let secret_name: string = "example-secret"
1556
1557 let actual_resourcelist: record = (
1558 $resourcelist
1559 | (
1560 helmrelease set values
1561 --ks-namespace $ks_namespace
1562 $kustomization_name
1563 $helmrelease_name
1564 $inline_values
1565 $cm_name
1566 $secret_name
1567 )
1568 )
1569
1570 # Expected patches for inline values, ConfigMap, and Secret
1571 let expected_patch_inline_values: record = {
1572 target: { kind: "HelmRelease", name: $helmrelease_name }
1573 patch: (
1574 [{ op: "add", path: "/spec/values", value: $inline_values }]
1575 | to yaml
1576 )
1577 }
1578 let expected_patch_cm_values: record = {
1579 target: { kind: "HelmRelease", name: $helmrelease_name }
1580 patch: (
1581 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
1582 | to yaml
1583 )
1584 }
1585 let expected_patch_secret_values: record = {
1586 target: { kind: "HelmRelease", name: $helmrelease_name }
1587 patch: (
1588 [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
1589 | to yaml
1590 )
1591 }
1592 let expected_resourcelist: record = {
1593 apiVersion: "config.kubernetes.io/v1"
1594 kind: "ResourceList"
1595 items: [
1596 {
1597 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1598 kind: "Kustomization"
1599 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1600 spec: { patches: [$expected_patch_inline_values, $expected_patch_cm_values, $expected_patch_secret_values] }
1601 }
1602 ]
1603 }
1604
1605 assert equal $actual_resourcelist $expected_resourcelist
1606}
1607
1608
1609export def "test helmrelease set values with create configmap" []: [
1610 nothing -> nothing
1611] {
1612 let resourcelist: record = {
1613 apiVersion: "config.kubernetes.io/v1"
1614 kind: "ResourceList"
1615 items: [
1616 {
1617 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1618 kind: "Kustomization"
1619 metadata: { name: "example-kustomization", namespace: "default" }
1620 # spec: { patches: [] }
1621 spec: {}
1622 }
1623 ]
1624 }
1625
1626 let ks_namespace: string = "default"
1627 let kustomization_name: string = "example-kustomization"
1628 let helmrelease_name: string = "example-helmrelease"
1629 let cm_name: string = "example-configmap"
1630 let create_cm_with_values: record = { key1: "value1", key2: "value2" }
1631
1632 let actual: record = (
1633 $resourcelist |
1634 (
1635 helmrelease set values
1636 --ks-namespace $ks_namespace
1637 --create-cm-with-values $create_cm_with_values
1638 $kustomization_name
1639 $helmrelease_name
1640 {}
1641 $cm_name
1642 )
1643 )
1644 let expected_cm_manifest: record = {
1645 apiVersion: "v1"
1646 kind: "ConfigMap"
1647 metadata: {
1648 name: $cm_name,
1649 namespace: "default"
1650 annotations: {
1651 # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml",
1652 # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml"
1653 "config.kubernetes.io/path": "example-configmap.yaml",
1654 "internal.config.kubernetes.io/path": "example-configmap.yaml"
1655 }
1656 }
1657 data: {
1658 "values.yaml": ($create_cm_with_values | to yaml | str trim)
1659 }
1660 }
1661 let expected_patch_content: record = {
1662 target: { kind: "HelmRelease", name: $helmrelease_name }
1663 patch: (
1664 [
1665 {
1666 op: "add",
1667 path: "/spec/valuesFrom/-",
1668 value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" }
1669 }
1670 ] | to yaml
1671 )
1672 }
1673 let expected_resourcelist: record = {
1674 apiVersion: "config.kubernetes.io/v1"
1675 kind: "ResourceList"
1676 items: [
1677 {
1678 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1679 kind: "Kustomization"
1680 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1681 spec: { patches: [$expected_patch_content] }
1682 },
1683 $expected_cm_manifest
1684 ]
1685 }
1686
1687 assert equal $actual $expected_resourcelist
1688}
1689
1690
1691export def "test helmrelease set values with create secret" []: [
1692 nothing -> nothing
1693] {
1694 let resourcelist: record = {
1695 apiVersion: "config.kubernetes.io/v1"
1696 kind: "ResourceList"
1697 items: [
1698 {
1699 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1700 kind: "Kustomization"
1701 metadata: { name: "example-kustomization", namespace: "default" }
1702 # spec: { patches: [] }
1703 spec: {}
1704 }
1705 ]
1706 }
1707
1708 let ks_namespace: string = "default"
1709 let kustomization_name: string = "example-kustomization"
1710 let helmrelease_name: string = "example-helmrelease"
1711 let secret_name: string = "example-secret"
1712 let create_secret_with_values: record = {key1: "value1", key2: "value2"}
1713
1714 let actual: record = $resourcelist | (
1715 helmrelease set values
1716 --ks-namespace $ks_namespace
1717 --create-secret-with-values $create_secret_with_values
1718 $kustomization_name
1719 $helmrelease_name
1720 {}
1721 ''
1722 $secret_name
1723 )
1724 let expected_secret_manifest: record = {
1725 apiVersion: "v1"
1726 kind: "Secret"
1727 metadata: {
1728 name: $secret_name,
1729 namespace: "default"
1730 annotations: {
1731 # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
1732 # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
1733 "config.kubernetes.io/path": "example-secret.yaml",
1734 "internal.config.kubernetes.io/path": "example-secret.yaml"
1735 }
1736 }
1737 data: {
1738 "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
1739 }
1740 }
1741 let expected_patch_content: record = {
1742 target: { kind: "HelmRelease", name: $helmrelease_name }
1743 patch: (
1744 [
1745 {
1746 op: "add",
1747 path: "/spec/valuesFrom/-",
1748 value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
1749 }
1750 ] | to yaml
1751 )
1752 }
1753 let expected_resourcelist: record = {
1754 apiVersion: "config.kubernetes.io/v1"
1755 kind: "ResourceList"
1756 items: [
1757 {
1758 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1759 kind: "Kustomization"
1760 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1761 spec: { patches: [$expected_patch_content] }
1762 },
1763 $expected_secret_manifest
1764 ]
1765 }
1766
1767 assert equal $actual $expected_resourcelist
1768}
1769
1770
1771export def "test helmrelease set values with create configmap and secret" []: [
1772 nothing -> nothing
1773] {
1774 let resourcelist: record = {
1775 apiVersion: "config.kubernetes.io/v1"
1776 kind: "ResourceList"
1777 items: [
1778 {
1779 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1780 kind: "Kustomization"
1781 metadata: { name: "example-kustomization", namespace: "default" }
1782 # spec: { patches: [] }
1783 spec: {}
1784 }
1785 ]
1786 }
1787
1788 let ks_namespace: string = "default"
1789 let kustomization_name: string = "example-kustomization"
1790 let helmrelease_name: string = "example-helmrelease"
1791 let cm_name: string = "example-configmap"
1792 let secret_name: string = "example-secret"
1793 let create_cm_with_values: record = { key1: "value1", key2: "value2" }
1794 let create_secret_with_values: record = { key3: "value3", key4: "value4" }
1795
1796 let actual: record = $resourcelist | (
1797 helmrelease set values
1798 --ks-namespace $ks_namespace
1799 --create-cm-with-values $create_cm_with_values
1800 --create-secret-with-values $create_secret_with_values
1801 $kustomization_name
1802 $helmrelease_name
1803 {}
1804 $cm_name
1805 $secret_name
1806 )
1807 let expected_cm_manifest: record = {
1808 apiVersion: "v1"
1809 kind: "ConfigMap"
1810 metadata: {
1811 name: $cm_name,
1812 namespace: "default"
1813 annotations: {
1814 # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml",
1815 # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml"
1816 "config.kubernetes.io/path": "example-configmap.yaml",
1817 "internal.config.kubernetes.io/path": "example-configmap.yaml"
1818 }
1819 }
1820 data: {
1821 "values.yaml": ($create_cm_with_values | to yaml | str trim)
1822 }
1823 }
1824 let expected_secret_manifest: record = {
1825 apiVersion: "v1"
1826 kind: "Secret"
1827 metadata: {
1828 name: $secret_name,
1829 namespace: "default",
1830 annotations: {
1831 # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
1832 # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
1833 "config.kubernetes.io/path": "example-secret.yaml",
1834 "internal.config.kubernetes.io/path": "example-secret.yaml"
1835 }
1836 }
1837 data: {
1838 "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
1839 }
1840 }
1841 let expected_patch_content_cm: record = {
1842 target: { kind: "HelmRelease", name: $helmrelease_name }
1843 patch: (
1844 [
1845 {
1846 op: "add",
1847 path: "/spec/valuesFrom/-",
1848 value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" }
1849 }
1850 ] | to yaml
1851 )
1852 }
1853 let expected_patch_content_secret: record = {
1854 target: { kind: "HelmRelease", name: $helmrelease_name }
1855 patch: (
1856 [
1857 {
1858 op: "add",
1859 path: "/spec/valuesFrom/-",
1860 value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
1861 }
1862 ] | to yaml
1863 )
1864 }
1865 let expected_resourcelist: record = {
1866 apiVersion: "config.kubernetes.io/v1"
1867 kind: "ResourceList"
1868 items: [
1869 {
1870 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1871 kind: "Kustomization"
1872 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1873 spec: { patches: [$expected_patch_content_cm, $expected_patch_content_secret] }
1874 },
1875 $expected_cm_manifest,
1876 $expected_secret_manifest
1877 ]
1878 }
1879
1880 assert equal $actual $expected_resourcelist
1881}
1882
1883
1884export def "test helmrelease set values with create secret and age encryption" []: [
1885 nothing -> nothing
1886] {
1887 let resourcelist: record = {
1888 apiVersion: "config.kubernetes.io/v1"
1889 kind: "ResourceList"
1890 items: [
1891 {
1892 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1893 kind: "Kustomization"
1894 metadata: { name: "example-kustomization", namespace: "default" }
1895 # spec: { patches: [] }
1896 spec: {}
1897 }
1898 ]
1899 }
1900
1901 let ks_namespace: string = "default"
1902 let kustomization_name: string = "example-kustomization"
1903 let helmrelease_name: string = "example-helmrelease"
1904 let secret_name: string = "example-secret"
1905 let create_secret_with_values: record = {
1906 key1: "value1",
1907 key2: "value2"
1908 }
1909 let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
1910 let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
1911
1912 let actual: record = $resourcelist | (
1913 helmrelease set values
1914 --ks-namespace $ks_namespace
1915 --create-secret-with-values $create_secret_with_values
1916 --public-age-key $test_public_key
1917 $kustomization_name
1918 $helmrelease_name
1919 {}
1920 ''
1921 $secret_name
1922 )
1923
1924 let expected_secret_manifest: record = {
1925 apiVersion: "v1"
1926 kind: "Secret"
1927 metadata: {
1928 name: $secret_name,
1929 namespace: "default"
1930 annotations: {
1931 # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
1932 # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
1933 "config.kubernetes.io/path": "example-secret.yaml",
1934 "internal.config.kubernetes.io/path": "example-secret.yaml"
1935 }
1936 }
1937 data: {
1938 "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
1939 }
1940 }
1941
1942 # NOTE: Here the secret is kept decrypted intentionally, since the same secret encrypted twice is never equal and we will need to decrypt them anyway to check they are equal
1943 let expected_patch_content: record = {
1944 target: { kind: "HelmRelease", name: $helmrelease_name }
1945 patch: (
1946 [
1947 {
1948 op: "add",
1949 path: "/spec/valuesFrom/-",
1950 value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
1951 }
1952 ] | to yaml
1953 )
1954 }
1955 let expected_resourcelist: record = {
1956 apiVersion: "config.kubernetes.io/v1"
1957 kind: "ResourceList"
1958 items: [
1959 $expected_secret_manifest,
1960 {
1961 apiVersion: "kustomize.toolkit.fluxcd.io/v1"
1962 kind: "Kustomization"
1963 metadata: { name: $kustomization_name, namespace: $ks_namespace }
1964 spec: { patches: [$expected_patch_content] }
1965 }
1966 ]
1967 }
1968
1969 # Check that everything except the encrypted secret is equal
1970 (assert equal
1971 ($actual | patch resource delete '' 'Secret')
1972 ($expected_resourcelist | patch resource delete '' 'Secret')
1973 )
1974
1975 # Check that both secrets, once decrypted, are equal
1976 let actual_secret_manifest: record = (
1977 # First, extracts the manifest of the encrypted Secret
1978 $actual
1979 | patch resource keep '' 'Secret'
1980 | get items.0
1981 # Removes the filename annotations, since they are excluded from encryption
1982 | reject $.metadata.annotations
1983 # Then, decrypts the manifest using the private key
1984 | to yaml
1985 | keypair decrypt secret manifest $test_private_key
1986 | from yaml
1987 )
1988 (assert equal
1989 $actual_secret_manifest
1990 ($expected_secret_manifest | reject $.metadata.annotations)
1991 )
1992}