Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render-helm-chart function changes #747

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ generators:
network: true
image: gcr.io/kpt-fn/render-helm-chart:unstable
helmCharts:
- 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: ["*"]
resources: ["*"]
- chartArgs:
name: ocp-pipeline
version: 0.1.16
repo: https://bcgov.github.io/helm-charts
templateOptions:
releaseName: moria
namespace: mynamespace
values:
valuesInline:
releaseNamespace: ""
rbac:
create: true
rules:
- apiGroups: [""]
verbs: ["*"]
resources: ["*"]
133 changes: 133 additions & 0 deletions examples/render-helm-chart-kustomize-values-files/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
diff --git a/resources.yaml b/resources.yaml
new file mode 100644
index 0000000..e2e869b
--- /dev/null
+++ b/resources.yaml
@@ -0,0 +1,127 @@
+apiVersion: rbac.authorization.k8s.io/v1
+kind: Role
+metadata:
+ name: moria-ocp-pipeline
+ namespace: mynamespace
+rules:
+- apiGroups:
+ - ""
+ resources:
+ - '*'
+ 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: 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: 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# kustomize 4.2.0 is preinstalled in github actions
kustomize build --enable-alpha-plugins --network > resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
81 changes: 81 additions & 0 deletions examples/render-helm-chart-kustomize-values-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# render-helm-chart: Kustomize Values Files

### Overview

This example demonstrates how to declaratively invoke the `render-helm-chart`
function with kustomize using multiple values files.

### Function invocation

To use the function with kustomize, you can specify the `functionConfig`
in your kustomization's `generators` field. This example specifies multiple remote
values files to use instead of the default values accompanying the chart:

kustomization.yaml:
```yaml
generators:
- |-
apiVersion: fn.kpt.dev/v1alpha1
kind: RenderHelmChart
metadata:
name: demo
annotations:
config.kubernetes.io/function: |
container:
network: true
image: gcr.io/kpt-fn/render-helm-chart:unstable
helmCharts:
- chartArgs:
name: ocp-pipeline
version: 0.1.16
repo: https://bcgov.github.io/helm-charts
templateOptions:
namespace: mynamespace
releaseName: moria
values:
valuesFiles:
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file1.yaml
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file2.yaml
```

Then, to build the kustomization with kustomize v4:

```shell
kustomize build --enable-alpha-plugins --network .
```

### Expected result

You should also be able to find the line `def releaseNamespace = ""` somewhere
in your output, as well as the following:

```yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: moria-ocp-pipeline
namespace: mynamespace
rules:
- apiGroups:
- ""
resources:
- '*'
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
```

which demonstrates that the correct values provided via `valuesFiles` were used.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
generators:
- |-
apiVersion: fn.kpt.dev/v1alpha1
kind: RenderHelmChart
metadata:
name: demo
annotations:
config.kubernetes.io/function: |
container:
network: true
image: gcr.io/kpt-fn/render-helm-chart:unstable
helmCharts:
- chartArgs:
name: ocp-pipeline
version: 0.1.16
repo: https://bcgov.github.io/helm-charts
templateOptions:
namespace: mynamespace
releaseName: moria
values:
valuesFiles:
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file1.yaml
- https://raw.githubusercontent.com/natasha41575/kpt-functions-catalog/a9c9cd765a05f7a7fb6923dbde4651b62c9c229c/examples/render-helm-chart-kustomize-values-files/file2.yaml
28 changes: 1 addition & 27 deletions examples/render-helm-chart-local/.expected/diff.patch
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,6 @@ index 0000000..8ba7f5c
+ path: /
+ port: http
+ resources: {}
diff --git a/pod_test-helloworld-chart-test-connection.yaml b/pod_test-helloworld-chart-test-connection.yaml
new file mode 100644
index 0000000..8793304
--- /dev/null
+++ b/pod_test-helloworld-chart-test-connection.yaml
@@ -0,0 +1,20 @@
+# Source: helloworld-chart/templates/tests/test-connection.yaml
+apiVersion: v1
+kind: Pod
+metadata:
+ name: "test-helloworld-chart-test-connection"
+ labels:
+ helm.sh/chart: helloworld-chart-0.1.0
+ app.kubernetes.io/name: helloworld-chart
+ app.kubernetes.io/instance: test
+ app.kubernetes.io/version: "1.16.0"
+ app.kubernetes.io/managed-by: Helm
+ annotations:
+ "helm.sh/hook": test-success
+spec:
+ containers:
+ - name: wget
+ image: busybox
+ command: ['wget']
+ args: ['test-helloworld-chart:80']
+ restartPolicy: Never
diff --git a/service_test-helloworld-chart.yaml b/service_test-helloworld-chart.yaml
new file mode 100644
index 0000000..7d734d3
Expand Down Expand Up @@ -116,4 +90,4 @@ index 0000000..5800f2a
+ app.kubernetes.io/name: helloworld-chart
+ app.kubernetes.io/instance: test
+ app.kubernetes.io/version: "1.16.0"
+ app.kubernetes.io/managed-by: Helm
+ app.kubernetes.io/managed-by: Helm
3 changes: 2 additions & 1 deletion examples/render-helm-chart-local/.expected/exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ kpt fn eval --image-pull-policy never --image gcr.io/kpt-fn/render-helm-chart:un
--mount type=bind,src="$(pwd)",dst=/tmp/charts -- \
name=helloworld-chart \
releaseName=test \
valuesFile=/tmp/charts/helloworld-values/values.yaml
valuesFile=/tmp/charts/helloworld-values/values.yaml \
skipTests=true
18 changes: 16 additions & 2 deletions examples/render-helm-chart-local/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@ $ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
releaseName=test
```

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

```shell
$ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
--mount type=bind,src=$(pwd),dst=/tmp/charts -- \
name=helloworld-chart \
releaseName=test \
valuesFile=tmp/charts/helloworld-values/values.yaml
valuesFile=/tmp/charts/helloworld-values/values.yaml
```

You can optionally skip tests in the templated output with `skipTests`.

```shell
$ kpt fn eval --image gcr.io/kpt-fn/render-helm-chart:unstable \
--mount type=bind,src=$(pwd),dst=/tmp/charts -- \
name=helloworld-chart \
releaseName=test \
valuesFile=/tmp/charts/helloworld-values/values.yaml \
skipTests=true
```

### Expected result
Expand All @@ -52,3 +63,6 @@ $ kpt pkg tree
You should be able to find `replicas: 5` in
file `deployment_test-helloworld-chart.yaml`, which demonstrates that
the correct values file provided by --valuesFile was used.

If you provided the `skipTests` option, `pod_test-helloworld-chart-test-connection.yaml`
will not appear in your files.
Loading