@@ -10,27 +10,26 @@ import (
10
10
"reflect"
11
11
"strings"
12
12
13
- goyaml "gopkg.in/yaml.v3"
14
- rbacv1 "k8s.io/api/rbac/v1"
15
- v12 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
16
- "k8s.io/utils/pointer"
17
-
18
13
. "github.com/onsi/ginkgo/v2"
19
14
. "github.com/onsi/gomega"
20
- . "github.com/solo-io/skv2/codegen"
21
- "github.com/solo-io/skv2/codegen/model"
22
- . "github.com/solo-io/skv2/codegen/model"
23
- "github.com/solo-io/skv2/codegen/skv2_anyvendor"
24
- "github.com/solo-io/skv2/codegen/util"
25
- "github.com/solo-io/skv2/contrib"
15
+ goyaml "gopkg.in/yaml.v3"
26
16
appsv1 "k8s.io/api/apps/v1"
27
17
v1 "k8s.io/api/core/v1"
18
+ rbacv1 "k8s.io/api/rbac/v1"
19
+ v12 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
28
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
21
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30
22
"k8s.io/apimachinery/pkg/runtime/schema"
31
23
"k8s.io/apimachinery/pkg/util/intstr"
32
24
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
25
+ "k8s.io/utils/ptr"
33
26
"sigs.k8s.io/yaml"
27
+
28
+ . "github.com/solo-io/skv2/codegen"
29
+ . "github.com/solo-io/skv2/codegen/model"
30
+ "github.com/solo-io/skv2/codegen/skv2_anyvendor"
31
+ "github.com/solo-io/skv2/codegen/util"
32
+ "github.com/solo-io/skv2/contrib"
34
33
)
35
34
36
35
var _ = Describe ("Cmd" , func () {
@@ -42,6 +41,66 @@ var _ = Describe("Cmd", func() {
42
41
skv2Imports .External ["github.com/solo-io/cue" ] = []string {
43
42
"encoding/protobuf/cue/cue.proto" ,
44
43
}
44
+ It ("env variable priority" , func () {
45
+ cmd := & Command {
46
+ Chart : & Chart {
47
+ Data : Data {
48
+ ApiVersion : "v1" ,
49
+ Description : "" ,
50
+ Name : "Painting Operator" ,
51
+ Version : "v0.0.1" ,
52
+ Home : "https://docs.solo.io/skv2/latest" ,
53
+ Sources : []string {
54
+ "https://github.com/solo-io/skv2" ,
55
+ },
56
+ },
57
+ Operators : []Operator {{
58
+ Name : "painter" ,
59
+ Deployment : Deployment {
60
+ Container : Container {
61
+ Image : Image {Repository : "painter" , Tag : "v0.0.1" },
62
+ Env : []v1.EnvVar {{Name : "ENV_VAR" , Value : "default" }},
63
+ TemplateEnvVars : []TemplateEnvVar {
64
+ {
65
+ Condition : "$.Values.secret" ,
66
+ Name : "ENV_VAR" ,
67
+ Value : "templated" ,
68
+ },
69
+ },
70
+ },
71
+ },
72
+ }},
73
+ },
74
+ ManifestRoot : "codegen/test/chart/env-priority" ,
75
+ }
76
+ Expect (cmd .Execute ()).NotTo (HaveOccurred (), "failed to execute command" )
77
+
78
+ manifests := helmTemplate ("./test/chart/env-priority" , map [string ]any {"painter" : map [string ]any {"enabled" : true }, "secret" : true })
79
+ var renderedDeployment * appsv1.Deployment
80
+ decoder := kubeyaml .NewYAMLOrJSONDecoder (bytes .NewBuffer (manifests ), 4096 )
81
+ for {
82
+ obj := & unstructured.Unstructured {}
83
+ err := decoder .Decode (obj )
84
+ if err != nil {
85
+ break
86
+ }
87
+ if obj .GetName () != "painter" || obj .GetKind () != "Deployment" {
88
+ continue
89
+ }
90
+
91
+ bytes , err := obj .MarshalJSON ()
92
+ Expect (err ).NotTo (HaveOccurred ())
93
+ renderedDeployment = & appsv1.Deployment {}
94
+ err = json .Unmarshal (bytes , renderedDeployment )
95
+ Expect (err ).NotTo (HaveOccurred ())
96
+ }
97
+ Expect (renderedDeployment ).NotTo (BeNil ())
98
+
99
+ Expect (renderedDeployment .Spec .Template .Spec .Containers [0 ].Env ).To (HaveLen (2 ))
100
+ Expect (renderedDeployment .Spec .Template .Spec .Containers [0 ].Env [0 ]).To (Equal (v1.EnvVar {Name : "ENV_VAR" , Value : "templated" }))
101
+ Expect (renderedDeployment .Spec .Template .Spec .Containers [0 ].Env [1 ]).To (Equal (v1.EnvVar {Name : "ENV_VAR" , Value : "default" }))
102
+ })
103
+
45
104
It ("install conditional sidecars" , func () {
46
105
agentConditional := "and ($.Values.glooAgent.enabled) ($.Values.glooAgent.runAsSidecar)"
47
106
@@ -111,6 +170,30 @@ var _ = Describe("Cmd", func() {
111
170
Repository : "gloo-mesh-mgmt-server" ,
112
171
Tag : "0.0.1" ,
113
172
},
173
+ TemplateEnvVars : []TemplateEnvVar {
174
+ {
175
+ Name : "USERNAME" ,
176
+ ValueFrom : v1.EnvVarSource {
177
+ SecretKeyRef : & v1.SecretKeySelector {
178
+ LocalObjectReference : v1.LocalObjectReference {
179
+ Name : "{{ $.Values.someSecret }}" ,
180
+ },
181
+ Key : "{{ $.Values.usernameKey }}" ,
182
+ },
183
+ },
184
+ },
185
+ {
186
+ Name : "PASSWORD" ,
187
+ ValueFrom : v1.EnvVarSource {
188
+ ConfigMapKeyRef : & v1.ConfigMapKeySelector {
189
+ LocalObjectReference : v1.LocalObjectReference {
190
+ Name : "{{ $.Values.someConfigMap }}" ,
191
+ },
192
+ Key : "{{ $.Values.passwordKey }}" ,
193
+ },
194
+ },
195
+ },
196
+ },
114
197
ContainerPorts : []ContainerPort {{
115
198
Name : "stats" ,
116
199
Port : "{{ $Values.glooMgmtServer.statsPort }}" ,
@@ -155,6 +238,11 @@ var _ = Describe("Cmd", func() {
155
238
Expect (deployment ).To (ContainSubstring ("name: agent-volume" ))
156
239
Expect (deployment ).To (ContainSubstring (`{{ index $glooAgent "ports" "grpc" }}` ))
157
240
Expect (deployment ).To (ContainSubstring ("{{ $Values.glooMgmtServer.statsPort }}" ))
241
+
242
+ Expect (deployment ).To (ContainSubstring ("{{ $.Values.usernameKey }}" ))
243
+ Expect (deployment ).To (ContainSubstring ("{{ $.Values.passwordKey }}" ))
244
+ Expect (deployment ).To (ContainSubstring ("{{ $.Values.someSecret }}" ))
245
+ Expect (deployment ).To (ContainSubstring ("{{ $.Values.someConfigMap }}" ))
158
246
})
159
247
It ("generates conditional crds" , func () {
160
248
cmd := & Command {
@@ -772,13 +860,11 @@ var _ = Describe("Cmd", func() {
772
860
}
773
861
Expect (renderedDeployment ).NotTo (BeNil ())
774
862
775
- pointerBool := func (b bool ) * bool { return & b }
776
- pointerInt64 := func (i int64 ) * int64 { return & i }
777
863
defaultSecurityContext := v1.SecurityContext {
778
- RunAsNonRoot : pointerBool (true ),
779
- RunAsUser : pointerInt64 (10101 ),
780
- ReadOnlyRootFilesystem : pointerBool (true ),
781
- AllowPrivilegeEscalation : pointerBool (false ),
864
+ RunAsNonRoot : ptr . To (true ),
865
+ RunAsUser : ptr. To [ int64 ] (10101 ),
866
+ ReadOnlyRootFilesystem : ptr . To (true ),
867
+ AllowPrivilegeEscalation : ptr . To (false ),
782
868
Capabilities : & v1.Capabilities {
783
869
Drop : []v1.Capability {"ALL" },
784
870
},
@@ -798,8 +884,8 @@ var _ = Describe("Cmd", func() {
798
884
Entry ("renders empty map for container security context when set as false via helm cli" , nil , true ),
799
885
Entry ("overrides container security context with empty map" , & v1.SecurityContext {}, false ),
800
886
Entry ("overrides container security context" , & v1.SecurityContext {
801
- RunAsNonRoot : func ( b bool ) * bool { return & b } (true ),
802
- RunAsUser : func ( i int64 ) * int64 { return & i } (20202 ),
887
+ RunAsNonRoot : ptr . To (true ),
888
+ RunAsUser : ptr. To [ int64 ] (20202 ),
803
889
}, false ),
804
890
)
805
891
@@ -1928,7 +2014,7 @@ roleRef:
1928
2014
)
1929
2015
1930
2016
DescribeTable ("rendering conditional deployment strategy" ,
1931
- func (values map [string ]any , conditionalStrategy []model. ConditionalStrategy , expectedStrategy appsv1.DeploymentStrategy ) {
2017
+ func (values map [string ]any , conditionalStrategy []ConditionalStrategy , expectedStrategy appsv1.DeploymentStrategy ) {
1932
2018
cmd := & Command {
1933
2019
Chart : & Chart {
1934
2020
Operators : []Operator {
@@ -1999,7 +2085,7 @@ roleRef:
1999
2085
),
2000
2086
Entry ("when the condition is true" ,
2001
2087
map [string ]any {"enabled" : true , "condition" : true },
2002
- []model. ConditionalStrategy {
2088
+ []ConditionalStrategy {
2003
2089
{
2004
2090
Condition : "$.Values.painter.condition" ,
2005
2091
Strategy : appsv1.DeploymentStrategy {
@@ -2019,7 +2105,7 @@ roleRef:
2019
2105
),
2020
2106
Entry ("when the condition is false" ,
2021
2107
map [string ]any {"enabled" : true , "condition" : false },
2022
- []model. ConditionalStrategy {
2108
+ []ConditionalStrategy {
2023
2109
{
2024
2110
Condition : "$.Values.painter.condition" ,
2025
2111
Strategy : appsv1.DeploymentStrategy {
@@ -2114,23 +2200,23 @@ roleRef:
2114
2200
map [string ]interface {}{"fsGroup" : 1000 },
2115
2201
nil ,
2116
2202
& v1.PodSecurityContext {
2117
- FSGroup : pointer . Int64 (1000 ),
2203
+ FSGroup : ptr. To [ int64 ] (1000 ),
2118
2204
}),
2119
2205
Entry ("when PodSecurityContext is defined only in the operator" ,
2120
2206
nil ,
2121
2207
& v1.PodSecurityContext {
2122
- FSGroup : pointer . Int64 (1000 ),
2208
+ FSGroup : ptr. To [ int64 ] (1000 ),
2123
2209
},
2124
2210
& v1.PodSecurityContext {
2125
- FSGroup : pointer . Int64 (1000 ),
2211
+ FSGroup : ptr. To [ int64 ] (1000 ),
2126
2212
}),
2127
2213
Entry ("when PodSecurityContext is defined in both values and the operator" ,
2128
2214
map [string ]interface {}{"fsGroup" : 1024 },
2129
2215
& v1.PodSecurityContext {
2130
- FSGroup : pointer . Int64 (1000 ),
2216
+ FSGroup : ptr. To [ int64 ] (1000 ),
2131
2217
},
2132
2218
& v1.PodSecurityContext {
2133
- FSGroup : pointer . Int64 (1024 ), // should override the value defined in the operator
2219
+ FSGroup : ptr. To [ int64 ] (1024 ), // should override the value defined in the operator
2134
2220
}),
2135
2221
)
2136
2222
@@ -2229,7 +2315,9 @@ roleRef:
2229
2315
Value : "{{ $.Values.featureGates.Foo | quote }}" ,
2230
2316
},
2231
2317
},
2232
- nil ),
2318
+ []v1.EnvVar {
2319
+ {Name : "FEATURE_ENABLE_FOO" , Value : "true" },
2320
+ }),
2233
2321
Entry ("when Env and TemplateEnvVar are specified, true value" ,
2234
2322
map [string ]string {"Foo" : "true" },
2235
2323
[]v1.EnvVar {
@@ -2318,7 +2406,7 @@ roleRef:
2318
2406
})
2319
2407
2320
2408
DescribeTable ("validation" ,
2321
- func (values map [string ]any , defaultVolumes []v1.Volume , conditionalVolumes []model. ConditionalVolume , expected []v1.Volume ) {
2409
+ func (values map [string ]any , defaultVolumes []v1.Volume , conditionalVolumes []ConditionalVolume , expected []v1.Volume ) {
2322
2410
cmd := & Command {
2323
2411
Chart : & Chart {
2324
2412
Operators : []Operator {
@@ -2412,7 +2500,7 @@ roleRef:
2412
2500
"condition" : "true" ,
2413
2501
},
2414
2502
nil ,
2415
- []model. ConditionalVolume {
2503
+ []ConditionalVolume {
2416
2504
{
2417
2505
Condition : "$.Values.painter.condition" ,
2418
2506
Volume : v1.Volume {
@@ -2432,7 +2520,7 @@ roleRef:
2432
2520
"condition" : "true" ,
2433
2521
},
2434
2522
nil ,
2435
- []model. ConditionalVolume {
2523
+ []ConditionalVolume {
2436
2524
{
2437
2525
Condition : "$.Values.painter.invalidCondition" ,
2438
2526
Volume : v1.Volume {
@@ -2452,7 +2540,7 @@ roleRef:
2452
2540
Name : "vol-1" ,
2453
2541
},
2454
2542
},
2455
- []model. ConditionalVolume {
2543
+ []ConditionalVolume {
2456
2544
{
2457
2545
Condition : "$.Values.painter.condition" ,
2458
2546
Volume : v1.Volume {
@@ -2484,7 +2572,7 @@ roleRef:
2484
2572
})
2485
2573
2486
2574
DescribeTable ("validation" ,
2487
- func (values map [string ]any , defaultMounts []v1.VolumeMount , conditionalMounts []model. ConditionalVolumeMount , expected []v1.VolumeMount ) {
2575
+ func (values map [string ]any , defaultMounts []v1.VolumeMount , conditionalMounts []ConditionalVolumeMount , expected []v1.VolumeMount ) {
2488
2576
cmd := & Command {
2489
2577
Chart : & Chart {
2490
2578
Operators : []Operator {
@@ -2580,7 +2668,7 @@ roleRef:
2580
2668
"condition" : "true" ,
2581
2669
},
2582
2670
nil ,
2583
- []model. ConditionalVolumeMount {
2671
+ []ConditionalVolumeMount {
2584
2672
{
2585
2673
Condition : "$.Values.painter.condition" ,
2586
2674
VolumeMount : v1.VolumeMount {
@@ -2600,7 +2688,7 @@ roleRef:
2600
2688
"condition" : "true" ,
2601
2689
},
2602
2690
nil ,
2603
- []model. ConditionalVolumeMount {
2691
+ []ConditionalVolumeMount {
2604
2692
{
2605
2693
Condition : "$.Values.painter.invalidCondition" ,
2606
2694
VolumeMount : v1.VolumeMount {
@@ -2620,7 +2708,7 @@ roleRef:
2620
2708
Name : "vol-1" ,
2621
2709
},
2622
2710
},
2623
- []model. ConditionalVolumeMount {
2711
+ []ConditionalVolumeMount {
2624
2712
{
2625
2713
Condition : "$.Values.painter.condition" ,
2626
2714
VolumeMount : v1.VolumeMount {
0 commit comments