Skip to content

Commit

Permalink
feat: add basic helm workshop
Browse files Browse the repository at this point in the history
initial version
  • Loading branch information
karlderkaefer authored Oct 9, 2022
1 parent 7165484 commit 3a04f7e
Show file tree
Hide file tree
Showing 34 changed files with 444 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
assets/**/charts
Chart.lock
25 changes: 0 additions & 25 deletions helm-template-advanced/index.json

This file was deleted.

1 change: 0 additions & 1 deletion helm-template-advanced/intro.md

This file was deleted.

3 changes: 0 additions & 3 deletions helm-template-advanced/step1/verify.sh

This file was deleted.

4 changes: 4 additions & 0 deletions helm-template-basic/assets/step2/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v2
name: step2
type: application
version: 0.1.0
13 changes: 13 additions & 0 deletions helm-template-basic/assets/step2/policy/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

name = input.metadata.name

is_deployment {
input.kind = "Deployment"
}

deny[msg] {
is_deployment
not name == "fancy"
msg = sprintf("your deployment should have name %s and not %s ", ["fancy", name])
}
8 changes: 8 additions & 0 deletions helm-template-basic/assets/step2/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ required "need to set some name for deployment" .Values.name }}
annotations:
chart: {{ default .Chart.Name .Values.nameOverride }}
labels:
app: nginx
4 changes: 4 additions & 0 deletions helm-template-basic/assets/step3/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v2
name: step3
type: application
version: 0.1.0
32 changes: 32 additions & 0 deletions helm-template-basic/assets/step3/policy/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

name = input.metadata.name

is_deployment {
input.kind = "Deployment"
}

required_labels {
input.metadata.labels["env"]
input.metadata.labels["team"]
}

deny[msg] {
is_deployment
not required_labels
msg = sprintf("deployment %s must have labels env and team", [name])
}

deny[msg] {
is_deployment
label := input.metadata.labels["env"]
not label == "prod"
msg = sprintf("label %s must have value %s but found %s", ["env", "prod", label])
}

deny[msg] {
is_deployment
label := input.metadata.labels["team"]
not label == "dev"
msg = sprintf("label %s must have value %s but has value %s", ["team", "dev", label])
}
7 changes: 7 additions & 0 deletions helm-template-basic/assets/step3/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
annotations:
chart: {{ default .Chart.Name .Values.nameOverride }}
labels:
5 changes: 5 additions & 0 deletions helm-template-basic/assets/step3/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
customLabels:
- name: env
value: "prod"
- name: team
value: "dev"
4 changes: 4 additions & 0 deletions helm-template-basic/assets/step4/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v2
name: step4
type: application
version: 0.1.0
39 changes: 39 additions & 0 deletions helm-template-basic/assets/step4/policy/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

name = input.metadata.name

required_deployment_labels {
input.metadata.labels["app"]
input.metadata.labels["addon"]
input.metadata.labels["triple"]
}

is_deployment {
input.kind = "Deployment"
}

deny[msg] {
is_deployment
not required_deployment_labels
msg = sprintf("deployment %s must have labels app, addon and triple", [name])
}

deny[msg] {
is_deployment
label := input.metadata.labels["app"]
not label = "nginx-trace-addon"
msg = sprintf("label %s must have value %s but have %s", ["app", "nginx-trace-addon", label])
}

deny[msg] {
is_deployment
label := input.metadata.labels["addon"]
not label = "TRACE-ADDON"
msg = sprintf("label %s must have value %s but have %s", ["addon", "TRACE-ADDON", label])
}

deny[msg] {
is_deployment
not input.metadata.labels["triple"] = "nginx-trace-addonnginx-trace-addonnginx-trace-addon"
msg = sprintf("label %s must have value %s", ["triple", "nginx-trace-addonnginx-trace-addonnginx-trace-addon"])
}
10 changes: 10 additions & 0 deletions helm-template-basic/assets/step4/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
annotations:
chart: {{ default .Chart.Name .Values.nameOverride }}
labels:
app: {{ .Values.app.name }}
addon: {{ .Values.app.name }}
triple: {{ .Values.app.name }}
2 changes: 2 additions & 0 deletions helm-template-basic/assets/step4/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app:
name: "nginx-trace-addon"
9 changes: 9 additions & 0 deletions helm-template-basic/assets/step5/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v2
name: step5
type: application
version: 0.1.0
dependencies:
- name: step2
alias: steps2
version: 0.1.0
repository: file://../step2
31 changes: 31 additions & 0 deletions helm-template-basic/assets/step5/policy/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

name = input.metadata.name
chart = input.metadata.annotations.chart
manifest = input[_]

is_deployment {
input.kind = "Deployment"
}

deny[msg] {
is_deployment
name = "nginx"
chart != "frontend"
msg = sprintf("Expected alias frontend for step 3 chart but found %s", [chart])
}

deny[msg] {
is_deployment
name = "nginx2"
chart != "backend"
msg = sprintf("Expected alias backend for step 4 chart but found %s", [chart])
}

deny[msg] {
is_deployment
name = "nginx2"
label := input.metadata.labels["app"]
label != "umbrella"
msg = sprintf("Expected value app.name of step4 chart to be equal to umbrella but found %s", [label])
}
6 changes: 6 additions & 0 deletions helm-template-basic/assets/step5/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx3
annotations:
chart: {{ default .Chart.Name .Values.nameOverride }}
Empty file.
9 changes: 9 additions & 0 deletions helm-template-basic/background.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -x # to test stderr output in /var/log/killercoda

echo starting... # to test stdout output in /var/log/killercoda

sleep 3 # add some time to wait for IDE

touch /tmp/finished
3 changes: 3 additions & 0 deletions helm-template-basic/foreground.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "waiting for IDE..."
while [ ! -f /tmp/finished ]; do sleep 1; done
echo DONE
35 changes: 35 additions & 0 deletions helm-template-basic/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"title": "Basic Helm Templating",
"description": "Train to work with helm templates",
"details": {
"intro": {
"text": "intro.md",
"foreground": "foreground.sh",
"background": "background.sh"
},
"steps": [
{
"title": "Install Helm",
"foreground": "step1/foreground.sh",
"text": "step1/instruction.md",
"verify": "step1/verify.sh"
},
{
"title": "Umbrella Chart",
"text": "step5/instruction.md",
"verify": "step5/verify.sh"
}
],
"assets": {
"host01": [
{"file": "**", "target": "/app", "chmod": "+w"}
]
}
},
"backend": {
"imageid": "ubuntu"
},
"interface": {
"layout": "ide"
}
}
3 changes: 3 additions & 0 deletions helm-template-basic/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Helm template demo

Please wait for IDE to finish loading before starting.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Check [Helm documentation](https://helm.sh/docs/intro/install/) to see how to install helm.
In this scenario it's already done for you.

Install Helm plugin which will be used to verify scenarios.
```
helm plugin install https:/instrumenta/helm-conftest
```{{exec}}
### Verify installation
check helm version `helm version`{{exec}}
4 changes: 4 additions & 0 deletions helm-template-basic/step1/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

command -v helm
helm plugin ls | grep "conftest"
23 changes: 23 additions & 0 deletions helm-template-basic/step2/instruction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Create Values Files

First create a file values.yaml in chart directory.
`touch /app/step2/values.yaml`{{exec}}

Add the key `name` with the value `fancy` to created file.

You can read more in [official helm documentation](https://helm.sh/docs/chart_template_guide/values_files/)

### Verify

Move to helm chart dir.
`cd /app/step2`{{exec}}

Check if you can render helm template correctly.
`helm template .`{{exec}}

Check if you have passed the task.
`helm conftest .`{{exec}}

See error and result log in
`cat /tmp/results.log`{{exec}}

5 changes: 5 additions & 0 deletions helm-template-basic/step2/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

rm -rf /tmp/results.log
cd /app/step2
helm conftest . > /tmp/results.log 2>&1
51 changes: 51 additions & 0 deletions helm-template-basic/step3/instruction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### Loops

Fix the chart of step 3 `cd /app/step3`{{exec}}

The goal is to add all labels defined in `customLabels` in `values.yaml` by using a loop.

The result helm template should look like this:
```yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
env: prod
team: dev
```
You can read more about control structures in [helm docu](https://helm.sh/docs/chart_template_guide/control_structures/#looping-with-the-range-action)
You can loop over a list of values
```yaml
toppings: |-
{{- range .Values.pizzaToppings }}
- {{ . | title | quote }}
{{- end }}
```
You can loop over a key-value map by
```yaml
toppings: |-
{{- range .Values.pizzaToppings }}
{{ .name }}: {{ .value }}
{{- end }}
```
If you are inside the loop you are in the scope of the object. You have access to attributes with `.`{{}}.
But in order to reference other values, you need to prepend `$`{{}} e.g. `$.Values.otherValue`{{}}

### Verify

Move to helm chart dir.
`cd /app/step3`{{exec}}

Check if you can render helm template correctly.
`helm template .`{{exec}}

Check if you have passed the task.
`helm conftest .`{{exec}}

See error and result log in
`cat /tmp/results.log`{{exec}}

5 changes: 5 additions & 0 deletions helm-template-basic/step3/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

rm -rf /tmp/results.log
cd /app/step3
helm conftest . > /tmp/results.log 2>&1
Loading

0 comments on commit 3a04f7e

Please sign in to comment.