@@ -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 {
@@ -774,13 +862,11 @@ var _ = Describe("Cmd", func() {
774
862
}
775
863
Expect (renderedDeployment ).NotTo (BeNil ())
776
864
777
- pointerBool := func (b bool ) * bool { return & b }
778
- pointerInt64 := func (i int64 ) * int64 { return & i }
779
865
defaultSecurityContext := v1.SecurityContext {
780
- RunAsNonRoot : pointerBool (true ),
781
- RunAsUser : pointerInt64 (10101 ),
782
- ReadOnlyRootFilesystem : pointerBool (true ),
783
- AllowPrivilegeEscalation : pointerBool (false ),
866
+ RunAsNonRoot : ptr . To (true ),
867
+ RunAsUser : ptr. To [ int64 ] (10101 ),
868
+ ReadOnlyRootFilesystem : ptr . To (true ),
869
+ AllowPrivilegeEscalation : ptr . To (false ),
784
870
Capabilities : & v1.Capabilities {
785
871
Drop : []v1.Capability {"ALL" },
786
872
},
@@ -800,8 +886,8 @@ var _ = Describe("Cmd", func() {
800
886
Entry ("renders empty map for container security context when set as false via helm cli" , nil , true ),
801
887
Entry ("overrides container security context with empty map" , & v1.SecurityContext {}, false ),
802
888
Entry ("overrides container security context" , & v1.SecurityContext {
803
- RunAsNonRoot : func ( b bool ) * bool { return & b } (true ),
804
- RunAsUser : func ( i int64 ) * int64 { return & i } (20202 ),
889
+ RunAsNonRoot : ptr . To (true ),
890
+ RunAsUser : ptr. To [ int64 ] (20202 ),
805
891
}, false ),
806
892
)
807
893
@@ -1926,7 +2012,7 @@ roleRef:
1926
2012
)
1927
2013
1928
2014
DescribeTable ("rendering conditional deployment strategy" ,
1929
- func (values map [string ]any , conditionalStrategy []model. ConditionalStrategy , expectedStrategy appsv1.DeploymentStrategy ) {
2015
+ func (values map [string ]any , conditionalStrategy []ConditionalStrategy , expectedStrategy appsv1.DeploymentStrategy ) {
1930
2016
cmd := & Command {
1931
2017
Chart : & Chart {
1932
2018
Operators : []Operator {
@@ -1997,7 +2083,7 @@ roleRef:
1997
2083
),
1998
2084
Entry ("when the condition is true" ,
1999
2085
map [string ]any {"enabled" : true , "condition" : true },
2000
- []model. ConditionalStrategy {
2086
+ []ConditionalStrategy {
2001
2087
{
2002
2088
Condition : "$.Values.painter.condition" ,
2003
2089
Strategy : appsv1.DeploymentStrategy {
@@ -2017,7 +2103,7 @@ roleRef:
2017
2103
),
2018
2104
Entry ("when the condition is false" ,
2019
2105
map [string ]any {"enabled" : true , "condition" : false },
2020
- []model. ConditionalStrategy {
2106
+ []ConditionalStrategy {
2021
2107
{
2022
2108
Condition : "$.Values.painter.condition" ,
2023
2109
Strategy : appsv1.DeploymentStrategy {
@@ -2112,23 +2198,23 @@ roleRef:
2112
2198
map [string ]interface {}{"fsGroup" : 1000 },
2113
2199
nil ,
2114
2200
& v1.PodSecurityContext {
2115
- FSGroup : pointer . Int64 (1000 ),
2201
+ FSGroup : ptr. To [ int64 ] (1000 ),
2116
2202
}),
2117
2203
Entry ("when PodSecurityContext is defined only in the operator" ,
2118
2204
nil ,
2119
2205
& v1.PodSecurityContext {
2120
- FSGroup : pointer . Int64 (1000 ),
2206
+ FSGroup : ptr. To [ int64 ] (1000 ),
2121
2207
},
2122
2208
& v1.PodSecurityContext {
2123
- FSGroup : pointer . Int64 (1000 ),
2209
+ FSGroup : ptr. To [ int64 ] (1000 ),
2124
2210
}),
2125
2211
Entry ("when PodSecurityContext is defined in both values and the operator" ,
2126
2212
map [string ]interface {}{"fsGroup" : 1024 },
2127
2213
& v1.PodSecurityContext {
2128
- FSGroup : pointer . Int64 (1000 ),
2214
+ FSGroup : ptr. To [ int64 ] (1000 ),
2129
2215
},
2130
2216
& v1.PodSecurityContext {
2131
- FSGroup : pointer . Int64 (1024 ), // should override the value defined in the operator
2217
+ FSGroup : ptr. To [ int64 ] (1024 ), // should override the value defined in the operator
2132
2218
}),
2133
2219
)
2134
2220
@@ -2227,7 +2313,9 @@ roleRef:
2227
2313
Value : "{{ $.Values.featureGates.Foo | quote }}" ,
2228
2314
},
2229
2315
},
2230
- nil ),
2316
+ []v1.EnvVar {
2317
+ {Name : "FEATURE_ENABLE_FOO" , Value : "true" },
2318
+ }),
2231
2319
Entry ("when Env and TemplateEnvVar are specified, true value" ,
2232
2320
map [string ]string {"Foo" : "true" },
2233
2321
[]v1.EnvVar {
@@ -2316,7 +2404,7 @@ roleRef:
2316
2404
})
2317
2405
2318
2406
DescribeTable ("validation" ,
2319
- func (values map [string ]string , defaultVolumes []v1.Volume , conditionalVolumes []model. ConditionalVolume , expected []v1.Volume ) {
2407
+ func (values map [string ]string , defaultVolumes []v1.Volume , conditionalVolumes []ConditionalVolume , expected []v1.Volume ) {
2320
2408
cmd := & Command {
2321
2409
Chart : & Chart {
2322
2410
Operators : []Operator {
@@ -2409,7 +2497,7 @@ roleRef:
2409
2497
"condition" : "true" ,
2410
2498
},
2411
2499
nil ,
2412
- []model. ConditionalVolume {
2500
+ []ConditionalVolume {
2413
2501
{
2414
2502
Condition : "$.Values.painter.condition" ,
2415
2503
Volume : v1.Volume {
@@ -2428,7 +2516,7 @@ roleRef:
2428
2516
"condition" : "true" ,
2429
2517
},
2430
2518
nil ,
2431
- []model. ConditionalVolume {
2519
+ []ConditionalVolume {
2432
2520
{
2433
2521
Condition : "$.Values.painter.invalidCondition" ,
2434
2522
Volume : v1.Volume {
@@ -2447,7 +2535,7 @@ roleRef:
2447
2535
Name : "vol-1" ,
2448
2536
},
2449
2537
},
2450
- []model. ConditionalVolume {
2538
+ []ConditionalVolume {
2451
2539
{
2452
2540
Condition : "$.Values.painter.condition" ,
2453
2541
Volume : v1.Volume {
@@ -2479,7 +2567,7 @@ roleRef:
2479
2567
})
2480
2568
2481
2569
DescribeTable ("validation" ,
2482
- func (values map [string ]string , defaultMounts []v1.VolumeMount , conditionalMounts []model. ConditionalVolumeMount , expected []v1.VolumeMount ) {
2570
+ func (values map [string ]string , defaultMounts []v1.VolumeMount , conditionalMounts []ConditionalVolumeMount , expected []v1.VolumeMount ) {
2483
2571
cmd := & Command {
2484
2572
Chart : & Chart {
2485
2573
Operators : []Operator {
@@ -2574,7 +2662,7 @@ roleRef:
2574
2662
"condition" : "true" ,
2575
2663
},
2576
2664
nil ,
2577
- []model. ConditionalVolumeMount {
2665
+ []ConditionalVolumeMount {
2578
2666
{
2579
2667
Condition : "$.Values.painter.condition" ,
2580
2668
VolumeMount : v1.VolumeMount {
@@ -2593,7 +2681,7 @@ roleRef:
2593
2681
"condition" : "true" ,
2594
2682
},
2595
2683
nil ,
2596
- []model. ConditionalVolumeMount {
2684
+ []ConditionalVolumeMount {
2597
2685
{
2598
2686
Condition : "$.Values.painter.invalidCondition" ,
2599
2687
VolumeMount : v1.VolumeMount {
@@ -2612,7 +2700,7 @@ roleRef:
2612
2700
Name : "vol-1" ,
2613
2701
},
2614
2702
},
2615
- []model. ConditionalVolumeMount {
2703
+ []ConditionalVolumeMount {
2616
2704
{
2617
2705
Condition : "$.Values.painter.condition" ,
2618
2706
VolumeMount : v1.VolumeMount {
0 commit comments