Skip to content

Commit b766c8d

Browse files
authored
render-helm-chart function changes (#747)
* render-helm-chart function changes * test for skip-tests and suggested changes * code review
1 parent 19ddf41 commit b766c8d

File tree

15 files changed

+510
-245
lines changed

15 files changed

+510
-245
lines changed

examples/render-helm-chart-kustomize-inline-values/kustomization.yaml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ generators:
1010
network: true
1111
image: gcr.io/kpt-fn/render-helm-chart:unstable
1212
helmCharts:
13-
- name: ocp-pipeline
14-
namespace: mynamespace
15-
version: 0.1.16
16-
repo: https://bcgov.github.io/helm-charts
17-
releaseName: moria
18-
valuesInline:
19-
releaseNamespace: ""
20-
rbac:
21-
create: true
22-
rules:
23-
- apiGroups: [""]
24-
verbs: ["*"]
25-
resources: ["*"]
13+
- chartArgs:
14+
name: ocp-pipeline
15+
version: 0.1.16
16+
repo: https://bcgov.github.io/helm-charts
17+
templateOptions:
18+
releaseName: moria
19+
namespace: mynamespace
20+
values:
21+
valuesInline:
22+
releaseNamespace: ""
23+
rbac:
24+
create: true
25+
rules:
26+
- apiGroups: [""]
27+
verbs: ["*"]
28+
resources: ["*"]
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
diff --git a/resources.yaml b/resources.yaml
2+
new file mode 100644
3+
index 0000000..e2e869b
4+
--- /dev/null
5+
+++ b/resources.yaml
6+
@@ -0,0 +1,127 @@
7+
+apiVersion: rbac.authorization.k8s.io/v1
8+
+kind: Role
9+
+metadata:
10+
+ name: moria-ocp-pipeline
11+
+ namespace: mynamespace
12+
+rules:
13+
+- apiGroups:
14+
+ - ""
15+
+ resources:
16+
+ - '*'
17+
+ verbs:
18+
+ - '*'
19+
+---
20+
+apiVersion: rbac.authorization.k8s.io/v1
21+
+kind: RoleBinding
22+
+metadata:
23+
+ name: moria-ocp-pipeline
24+
+ namespace: mynamespace
25+
+roleRef:
26+
+ apiGroup: rbac.authorization.k8s.io
27+
+ kind: Role
28+
+ name: moria-ocp-pipeline
29+
+subjects:
30+
+- kind: ServiceAccount
31+
+ name: jenkins
32+
+ namespace: mynamespace
33+
+---
34+
+apiVersion: v1
35+
+data:
36+
+ config: eyJleGFtcGxlIjoidmFsdWUifQ==
37+
+kind: Secret
38+
+metadata:
39+
+ labels:
40+
+ chart: ocp-pipeline-0.1.16
41+
+ heritage: Helm
42+
+ release: moria
43+
+ name: moria-config
44+
+type: Opaque
45+
+---
46+
+apiVersion: v1
47+
+data:
48+
+ WebHookSecretKey: MTIzNDU2Nzg=
49+
+kind: Secret
50+
+metadata:
51+
+ labels:
52+
+ chart: ocp-pipeline-0.1.16
53+
+ heritage: Helm
54+
+ release: moria
55+
+ name: moria-git-webhook-secret
56+
+type: Opaque
57+
+---
58+
+apiVersion: build.openshift.io/v1
59+
+kind: BuildConfig
60+
+metadata:
61+
+ labels:
62+
+ app: ocp-pipeline
63+
+ chart: ocp-pipeline-0.1.16
64+
+ heritage: Helm
65+
+ release: moria
66+
+ name: moria-ocp-pipeline-deploy
67+
+ namespace: null
68+
+spec:
69+
+ nodeSelector: {}
70+
+ resources:
71+
+ limits:
72+
+ cpu: 4000m
73+
+ memory: 8G
74+
+ requests:
75+
+ cpu: 2000m
76+
+ memory: 4G
77+
+ strategy:
78+
+ jenkinsPipelineStrategy:
79+
+ jenkinsfile: |-
80+
+ def helmName = "helm-v3.1.0-linux-amd64.tar.gz"
81+
+ def chartName = "metadata-curator"
82+
+ def chartRepo = "http://bcgov.github.io/helm-charts"
83+
+ def releaseName = "mc"
84+
+ def releaseNamespace = ""
85+
+ def forceRecreate = "false"
86+
+ def callAnotherPipe = "false"
87+
+ def useEnv = "false"
88+
+ def fromEnv = "commit"
89+
+ def setFlag = "image.tag"
90+
+
91+
+ node("nodejs") {
92+
+ stage("deploy (it's already built)") {
93+
+ sh """
94+
+ curl -L -O https://get.helm.sh/${helmName}
95+
+ tar -zxvf ${helmName}
96+
+ cd linux-amd64
97+
+
98+
+ curl -L -O https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux32
99+
+ chmod ugo+x ./jq-linux32
100+
+ npm install -g json2yaml
101+
+
102+
+ export CONF1=`oc get secret moria-config -o json | ./jq-linux32 .data.config`
103+
+ export CONF2=`sed -e 's/^"//' -e 's/"\$//' <<<"\$CONF1"`
104+
+ export CONF3=`echo \$CONF2 | base64 -d -`
105+
+ export CONF=`echo \$CONF3 | json2yaml`
106+
+
107+
+ echo "\$CONF" > ./config.yaml
108+
+ oc project ${releaseNamespace}
109+
+ ./helm repo add chart ${chartRepo}
110+
+ ./helm repo update
111+
+ if [ "${forceRecreate}" = "true" ]; then
112+
+ ./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)"
113+
+ elif [ "${useEnv}" = "true" ]; then
114+
+ ./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install --set ${setFlag}=${env[fromEnv]}
115+
+ else
116+
+ ./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install
117+
+ fi
118+
+
119+
+ if [ "${callAnotherPipe}" = "true" ]; then
120+
+ curl -d '' http://otherwebhookUrl
121+
+ fi
122+
+ """
123+
+ }
124+
+ }
125+
+ type: JenkinsPipeline
126+
+ triggers:
127+
+ - generic:
128+
+ allowEnv: true
129+
+ secretReference:
130+
+ name: moria-git-webhook-secret
131+
+ type: generic
132+
+status:
133+
+ lastVersion: 0
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
# kustomize 4.2.0 is preinstalled in github actions
4+
kustomize build --enable-alpha-plugins --network > resources.yaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.expected
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# render-helm-chart: Kustomize Values Files
2+
3+
### Overview
4+
5+
This example demonstrates how to declaratively invoke the `render-helm-chart`
6+
function with kustomize using multiple values files.
7+
8+
### Function invocation
9+
10+
To use the function with kustomize, you can specify the `functionConfig`
11+
in your kustomization's `generators` field. This example specifies multiple remote
12+
values files to use instead of the default values accompanying the chart:
13+
14+
kustomization.yaml:
15+
```yaml
16+
generators:
17+
- |-
18+
apiVersion: fn.kpt.dev/v1alpha1
19+
kind: RenderHelmChart
20+
metadata:
21+
name: demo
22+
annotations:
23+
config.kubernetes.io/function: |
24+
container:
25+
network: true
26+
image: gcr.io/kpt-fn/render-helm-chart:unstable
27+
helmCharts:
28+
- chartArgs:
29+
name: ocp-pipeline
30+
version: 0.1.16
31+
repo: https://bcgov.github.io/helm-charts
32+
templateOptions:
33+
namespace: mynamespace
34+
releaseName: moria
35+
values:
36+
valuesFiles:
37+
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file1.yaml
38+
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file2.yaml
39+
```
40+
41+
Then, to build the kustomization with kustomize v4:
42+
43+
```shell
44+
kustomize build --enable-alpha-plugins --network .
45+
```
46+
47+
### Expected result
48+
49+
You should also be able to find the line `def releaseNamespace = ""` somewhere
50+
in your output, as well as the following:
51+
52+
```yaml
53+
apiVersion: rbac.authorization.k8s.io/v1
54+
kind: Role
55+
metadata:
56+
name: moria-ocp-pipeline
57+
namespace: mynamespace
58+
rules:
59+
- apiGroups:
60+
- ""
61+
resources:
62+
- '*'
63+
verbs:
64+
- '*'
65+
---
66+
apiVersion: rbac.authorization.k8s.io/v1
67+
kind: RoleBinding
68+
metadata:
69+
name: moria-ocp-pipeline
70+
namespace: mynamespace
71+
roleRef:
72+
apiGroup: rbac.authorization.k8s.io
73+
kind: Role
74+
name: moria-ocp-pipeline
75+
subjects:
76+
- kind: ServiceAccount
77+
name: jenkins
78+
namespace: mynamespace
79+
```
80+
81+
which demonstrates that the correct values provided via `valuesFiles` were used.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
generators:
2+
- |-
3+
apiVersion: fn.kpt.dev/v1alpha1
4+
kind: RenderHelmChart
5+
metadata:
6+
name: demo
7+
annotations:
8+
config.kubernetes.io/function: |
9+
container:
10+
network: true
11+
image: gcr.io/kpt-fn/render-helm-chart:unstable
12+
helmCharts:
13+
- chartArgs:
14+
name: ocp-pipeline
15+
version: 0.1.16
16+
repo: https://bcgov.github.io/helm-charts
17+
templateOptions:
18+
namespace: mynamespace
19+
releaseName: moria
20+
values:
21+
valuesFiles:
22+
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file1.yaml
23+
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file2.yaml

examples/render-helm-chart-local/.expected/diff.patch

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,6 @@ index 0000000..8ba7f5c
4747
+ path: /
4848
+ port: http
4949
+ resources: {}
50-
diff --git a/pod_test-helloworld-chart-test-connection.yaml b/pod_test-helloworld-chart-test-connection.yaml
51-
new file mode 100644
52-
index 0000000..8793304
53-
--- /dev/null
54-
+++ b/pod_test-helloworld-chart-test-connection.yaml
55-
@@ -0,0 +1,20 @@
56-
+# Source: helloworld-chart/templates/tests/test-connection.yaml
57-
+apiVersion: v1
58-
+kind: Pod
59-
+metadata:
60-
+ name: "test-helloworld-chart-test-connection"
61-
+ labels:
62-
+ helm.sh/chart: helloworld-chart-0.1.0
63-
+ app.kubernetes.io/name: helloworld-chart
64-
+ app.kubernetes.io/instance: test
65-
+ app.kubernetes.io/version: "1.16.0"
66-
+ app.kubernetes.io/managed-by: Helm
67-
+ annotations:
68-
+ "helm.sh/hook": test-success
69-
+spec:
70-
+ containers:
71-
+ - name: wget
72-
+ image: busybox
73-
+ command: ['wget']
74-
+ args: ['test-helloworld-chart:80']
75-
+ restartPolicy: Never
7650
diff --git a/service_test-helloworld-chart.yaml b/service_test-helloworld-chart.yaml
7751
new file mode 100644
7852
index 0000000..7d734d3
@@ -116,4 +90,4 @@ index 0000000..5800f2a
11690
+ app.kubernetes.io/name: helloworld-chart
11791
+ app.kubernetes.io/instance: test
11892
+ app.kubernetes.io/version: "1.16.0"
119-
+ app.kubernetes.io/managed-by: Helm
93+
+ app.kubernetes.io/managed-by: Helm

examples/render-helm-chart-local/.expected/exec.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ kpt fn eval --image-pull-policy never --image gcr.io/kpt-fn/render-helm-chart:un
44
--mount type=bind,src="$(pwd)",dst=/tmp/charts -- \
55
name=helloworld-chart \
66
releaseName=test \
7-
valuesFile=/tmp/charts/helloworld-values/values.yaml
7+
valuesFile=/tmp/charts/helloworld-values/values.yaml \
8+
skipTests=true

examples/render-helm-chart-local/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ $ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
2727
releaseName=test
2828
```
2929

30-
You can optionally provide your own values files using `--valuesFile`.
30+
You can optionally provide your own values files using `valuesFile`.
3131

3232
```shell
3333
$ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
3434
--mount type=bind,src=$(pwd),dst=/tmp/charts -- \
3535
name=helloworld-chart \
3636
releaseName=test \
37-
valuesFile=tmp/charts/helloworld-values/values.yaml
37+
valuesFile=/tmp/charts/helloworld-values/values.yaml
38+
```
39+
40+
You can optionally skip tests in the templated output with `skipTests`.
41+
42+
```shell
43+
$ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
44+
--mount type=bind,src=$(pwd),dst=/tmp/charts -- \
45+
name=helloworld-chart \
46+
releaseName=test \
47+
valuesFile=/tmp/charts/helloworld-values/values.yaml \
48+
skipTests=true
3849
```
3950

4051
### Expected result
@@ -52,3 +63,6 @@ $ kpt pkg tree
5263
You should be able to find `replicas: 5` in
5364
file `deployment_test-helloworld-chart.yaml`, which demonstrates that
5465
the correct values file provided by --valuesFile was used.
66+
67+
If you provided the `skipTests` option, `pod_test-helloworld-chart-test-connection.yaml`
68+
will not appear in your files.

0 commit comments

Comments
 (0)