-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathHelmChartInflationGenerator_test.go
553 lines (531 loc) · 12.9 KB
/
HelmChartInflationGenerator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main_test
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)
func TestHelmChartInflationGenerator(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: myPipeline
name: ocp-pipeline
namespace: mynamespace
version: 0.1.16
repo: https://bcgov.github.io/helm-charts
releaseName: moria
valuesInline:
releaseNamespace: ""
rbac:
create: true
rules:
- apiGroups: [""]
verbs: ["*"]
resouces: ["*"]
`)
th.AssertActualEqualsExpected(rm, `
apiVersion: v1
data:
config: eyJleGFtcGxlIjoidmFsdWUifQ==
kind: Secret
metadata:
labels:
chart: ocp-pipeline-0.1.16
heritage: Helm
release: moria
name: moria-config
type: Opaque
---
apiVersion: v1
data:
WebHookSecretKey: MTIzNDU2Nzg=
kind: Secret
metadata:
labels:
chart: ocp-pipeline-0.1.16
heritage: Helm
release: moria
name: moria-git-webhook-secret
type: Opaque
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: moria-ocp-pipeline
namespace: mynamespace
rules:
- apiGroups:
- ""
resouces:
- '*'
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: moria-ocp-pipeline
namespace: mynamespace
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: moria-ocp-pipeline
subjects:
- kind: ServiceAccount
name: jenkins
namespace: mynamespace
---
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
labels:
app: ocp-pipeline
chart: ocp-pipeline-0.1.16
heritage: Helm
release: moria
name: moria-ocp-pipeline-deploy
namespace: null
spec:
nodeSelector: {}
resources:
limits:
cpu: 4000m
memory: 8G
requests:
cpu: 2000m
memory: 4G
strategy:
jenkinsPipelineStrategy:
jenkinsfile: |-
def helmName = "helm-v3.1.0-linux-amd64.tar.gz"
def chartName = "metadata-curator"
def chartRepo = "http://bcgov.github.io/helm-charts"
def releaseName = "mc"
def releaseNamespace = ""
def forceRecreate = "false"
def callAnotherPipe = "false"
def useEnv = "false"
def fromEnv = "commit"
def setFlag = "image.tag"
node("nodejs") {
stage("deploy (it's already built)") {
sh """
curl -L -O https://get.helm.sh/${helmName}
tar -zxvf ${helmName}
cd linux-amd64
curl -L -O https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux32
chmod ugo+x ./jq-linux32
npm install -g json2yaml
export CONF1=`+"`"+`oc get secret moria-config -o json | ./jq-linux32 .data.config`+"`"+`
export CONF2=`+"`"+`sed -e 's/^"//' -e 's/"\$//' <<<"\$CONF1"`+"`"+`
export CONF3=`+"`"+`echo \$CONF2 | base64 -d -`+"`"+`
export CONF=`+"`"+`echo \$CONF3 | json2yaml`+"`"+`
echo "\$CONF" > ./config.yaml
oc project ${releaseNamespace}
./helm repo add chart ${chartRepo}
./helm repo update
if [ "${forceRecreate}" = "true" ]; then
./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install --set hashLabel="${releaseName}\$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)"
elif [ "${useEnv}" = "true" ]; then
./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install --set ${setFlag}=${env[fromEnv]}
else
./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install
fi
if [ "${callAnotherPipe}" = "true" ]; then
curl -d '' http://otherwebhookUrl
fi
"""
}
}
type: JenkinsPipeline
triggers:
- generic:
allowEnv: true
secretReference:
name: moria-git-webhook-secret
type: generic
status:
lastVersion: 0
`)
}
const expectedInflationFmt = `
apiVersion: v1
data:
rcon-password: Q0hBTkdFTUUh
kind: Secret
metadata:
labels:
app: moria-minecraft
chart: minecraft-3.1.3
heritage: Helm
release: moria
name: moria-minecraft
type: Opaque
---
apiVersion: v1
kind: Service
metadata:
annotations: {}
labels:
app: moria-minecraft
chart: minecraft-3.1.3
heritage: Helm
release: moria
name: moria-minecraft
spec:
ports:
- name: minecraft
port: 25565
protocol: TCP
targetPort: minecraft
selector:
app: moria-minecraft
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
annotations: {}
labels:
app: moria-minecraft
chart: minecraft-3.1.3
heritage: Helm
release: moria
name: moria-minecraft-rcon
spec:
ports:
- name: rcon
port: 25575
protocol: TCP
targetPort: rcon
selector:
app: moria-minecraft
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: moria-minecraft
chart: minecraft-3.1.3
heritage: Helm
release: moria
name: moria-minecraft
spec:
selector:
matchLabels:
app: moria-minecraft
strategy:
type: Recreate
template:
metadata:
labels:
app: moria-minecraft
spec:
containers:
- env:
- name: EULA
value: "true"
- name: TYPE
value: VANILLA
- name: VERSION
value: LATEST
- name: DIFFICULTY
value: %s
- name: WHITELIST
value: ""
- name: OPS
value: ""
- name: ICON
value: ""
- name: MAX_PLAYERS
value: "20"
- name: MAX_WORLD_SIZE
value: "10000"
- name: ALLOW_NETHER
value: "true"
- name: ANNOUNCE_PLAYER_ACHIEVEMENTS
value: "true"
- name: ENABLE_COMMAND_BLOCK
value: "true"
- name: FORCE_GAMEMODE
value: "false"
- name: GENERATE_STRUCTURES
value: "true"
- name: HARDCORE
value: "false"
- name: MAX_BUILD_HEIGHT
value: "256"
- name: MAX_TICK_TIME
value: "60000"
- name: SPAWN_ANIMALS
value: "true"
- name: SPAWN_MONSTERS
value: "true"
- name: SPAWN_NPCS
value: "true"
- name: VIEW_DISTANCE
value: "10"
- name: SEED
value: ""
- name: MODE
value: survival
- name: MOTD
value: Welcome to Minecraft on Kubernetes!
- name: PVP
value: "false"
- name: LEVEL_TYPE
value: DEFAULT
- name: GENERATOR_SETTINGS
value: ""
- name: LEVEL
value: world
- name: ONLINE_MODE
value: "true"
- name: MEMORY
value: 1024M
- name: JVM_OPTS
value: ""
- name: JVM_XX_OPTS
value: ""
- name: ENABLE_RCON
value: "true"
- name: RCON_PASSWORD
valueFrom:
secretKeyRef:
key: rcon-password
name: moria-minecraft
image: itzg/minecraft-server:latest
imagePullPolicy: Always
livenessProbe:
failureThreshold: 10
initialDelaySeconds: 30
periodSeconds: 5
successThreshold: 1
tcpSocket:
port: 25565
timeoutSeconds: 1
name: moria-minecraft
ports:
- containerPort: 25565
name: minecraft
protocol: TCP
- containerPort: 25575
name: rcon
protocol: TCP
readinessProbe:
failureThreshold: 10
initialDelaySeconds: 30
periodSeconds: 5
successThreshold: 1
tcpSocket:
port: 25565
timeoutSeconds: 1
resources:
requests:
cpu: %dm
memory: %dMi
volumeMounts:
- mountPath: /data
name: datadir
securityContext:
fsGroup: 2000
runAsUser: 1000
volumes:
- emptyDir: {}
name: datadir
`
func TestHelmChartInflationGeneratorWithValuesInlineOverride(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: myMc
name: minecraft
version: 3.1.3
repo: https://itzg.github.io/minecraft-server-charts
releaseName: moria
valuesInline:
minecraftServer:
eula: true
difficulty: hard
rcon:
enabled: true
`)
th.AssertActualEqualsExpected(
rm, fmt.Sprintf(expectedInflationFmt,
"hard", // difficulty
500, // cpu
512, // memory
))
}
func TestHelmChartInflationGeneratorWithLocalValuesFile(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
th.WriteF(filepath.Join(th.GetRoot(), "myValues.yaml"), `
minecraftServer:
eula: true
difficulty: peaceful
rcon:
enabled: true
resources:
requests:
cpu: 888m
memory: 666Mi
`)
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: myMc
name: minecraft
version: 3.1.3
repo: https://itzg.github.io/minecraft-server-charts
releaseName: moria
valuesFile: myValues.yaml
`)
th.AssertActualEqualsExpected(
rm, fmt.Sprintf(expectedInflationFmt,
"peaceful", // difficulty
888, // cpu
666, // memory
))
}
func TestHelmChartInflationGeneratorWithInLineReplace(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: myMc
name: minecraft
version: 3.1.3
repo: https://itzg.github.io/minecraft-server-charts
releaseName: moria
valuesInline:
minecraftServer:
eula: true
difficulty: OMG_PLEASE_NO
rcon:
enabled: true
resources:
requests:
cpu: 555m
memory: 111Mi
valuesMerge: replace
`)
th.AssertActualEqualsExpected(
rm, fmt.Sprintf(expectedInflationFmt,
"OMG_PLEASE_NO", // difficulty
555, // cpu
111, // memory
))
}
func TestHelmChartInflationGeneratorWithIncludeCRDs(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
// we store this data outside of the _test.go file as its sort of huge
// and has backticks, which makes string literals wonky
testData, err := ioutil.ReadFile("include_crds_testdata.txt")
if err != nil {
t.Error(fmt.Errorf("unable to read test data for includeCRDs: %w", err))
}
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: terraform
name: terraform
version: 1.0.0
repo: https://helm.releases.hashicorp.com
releaseName: terraforming-mars
includeCRDs: true
valuesInline:
global:
enabled: false
tests:
enabled: false
`)
th.AssertActualEqualsExpected(rm, string(testData))
}
func TestHelmChartInflationGeneratorWithExcludeCRDs(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
// we choose this helm chart as it has the ability to turn
// everything off, except CRDs.
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: terraform
name: terraform
version: 1.0.0
repo: https://helm.releases.hashicorp.com
releaseName: terraforming-mars
includeCRDs: false
valuesInline:
global:
enabled: false
tests:
enabled: false
`)
th.AssertActualEqualsExpected(rm, "")
}
func TestHelmChartInflationGeneratorWithIncludeCRDsNotSpecified(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}
rm := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: terraform
name: terraform
version: 1.0.0
repo: https://helm.releases.hashicorp.com
releaseName: terraforming-mars
valuesInline:
global:
enabled: false
tests:
enabled: false
`)
th.AssertActualEqualsExpected(rm, "")
}