From 253c1ce3f7033b3a9f4554a49100ecee26995732 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Sat, 17 Apr 2021 01:34:32 +0200 Subject: [PATCH] feat(kubernetes): add kustomize generation to kubernetes stdlib package test(kubernetes): add test for kustomize Signed-off-by: Tom Chauveau --- stdlib/kubernetes/kustomize/kustomization.cue | 98 +++++++++++++++++++ tests/stdlib.bats | 4 + .../stdlib/kubernetes/kustomize/kustomize.cue | 32 ++++++ .../kustomize/testdata/deployment.yaml | 22 +++++ .../kubernetes/kustomize/testdata/pod.yaml | 12 +++ tests/stdlib/kubernetes/kustomize/verify.cue | 72 ++++++++++++++ tom_test/tmp/main.cue | 3 + 7 files changed, 243 insertions(+) create mode 100644 stdlib/kubernetes/kustomize/kustomization.cue create mode 100644 tests/stdlib/kubernetes/kustomize/kustomize.cue create mode 100644 tests/stdlib/kubernetes/kustomize/testdata/deployment.yaml create mode 100644 tests/stdlib/kubernetes/kustomize/testdata/pod.yaml create mode 100644 tests/stdlib/kubernetes/kustomize/verify.cue create mode 100644 tom_test/tmp/main.cue diff --git a/stdlib/kubernetes/kustomize/kustomization.cue b/stdlib/kubernetes/kustomize/kustomization.cue new file mode 100644 index 00000000..9aa5dec0 --- /dev/null +++ b/stdlib/kubernetes/kustomize/kustomization.cue @@ -0,0 +1,98 @@ +package kustomize + +import ( + "dagger.io/dagger/op" + "dagger.io/dagger" + "dagger.io/alpine" +) + +#Kustomization: { + // Kustomize binary version + version: *"v3.8.7" | string + + #code: #""" + [ -e /usr/local/bin/kubectl ] || { + curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash && mv kustomize /usr/local/bin + } + """# + + #up: [ + op.#Load & { + from: alpine.#Image & { + package: bash: "=~5.1" + package: jq: "=~1.6" + package: curl: "=~7.76" + } + }, + + op.#WriteFile & { + dest: "/entrypoint.sh" + content: #code + }, + + op.#Exec & { + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/entrypoint.sh", + ] + }, + ] +} + +// Apply a Kubernetes Kustomize folder +#Kustomize: { + // Kubernetes source + source: dagger.#Artifact + + // Optional Kustomization file + kustomization: string + + // Kustomize binary version + version: *"v3.8.7" | string + + #code: #""" + cp /kustomization.yaml /source | true + mkdir -p /output + kustomize build /source >> /output/result.yaml + """# + + #up: [ + op.#Load & { + from: #Kustomization & {"version": version} + }, + + op.#WriteFile & { + dest: "/entrypoint.sh" + content: #code + }, + + if kustomization != _|_ { + op.#WriteFile & { + dest: "/kustomization.yaml" + content: kustomization + mode: 0o600 + } + }, + + op.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/entrypoint.sh", + ] + mount: "/source": from: source + }, + + op.#Subdir & { + dir: "/output" + }, + ] +} diff --git a/tests/stdlib.bats b/tests/stdlib.bats index 0da60c0a..89884a2f 100644 --- a/tests/stdlib.bats +++ b/tests/stdlib.bats @@ -32,6 +32,10 @@ setup() { "$DAGGER" compute "$TESTDIR"/stdlib/kubernetes --input-dir kubeconfig=~/.kube } +@test "stdlib: kustomize" { + "$DAGGER" compute "$TESTDIR"/stdlib/kubernetes/kustomize --input-dir TestKustomize.kustom.source="$TESTDIR"/stdlib/kubernetes/kustomize/testdata +} + @test "stdlib: helm" { skip_unless_local_kube diff --git a/tests/stdlib/kubernetes/kustomize/kustomize.cue b/tests/stdlib/kubernetes/kustomize/kustomize.cue new file mode 100644 index 00000000..50fc9fb4 --- /dev/null +++ b/tests/stdlib/kubernetes/kustomize/kustomize.cue @@ -0,0 +1,32 @@ +package kustomize + +import ( + "encoding/yaml" + "dagger.io/dagger" + "dagger.io/kubernetes/kustomize" +) + +TestKustomize: { + testdata: dagger.#Artifact + + // Run Kustomize + kustom: kustomize.#Kustomize & { + source: testdata + kustomization: yaml.Marshal({ + resources: ["deployment.yaml", "pod.yaml"] + images: [{ + name: "nginx" + newTag: "v1" + }] + replicas: [{ + name: "nginx-deployment" + count: 2 + }] + }) + } + + // Verify kustomization generation + verify: #VerifyKustomize & { + source: kustom + } +} diff --git a/tests/stdlib/kubernetes/kustomize/testdata/deployment.yaml b/tests/stdlib/kubernetes/kustomize/testdata/deployment.yaml new file mode 100644 index 00000000..e47d13ff --- /dev/null +++ b/tests/stdlib/kubernetes/kustomize/testdata/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx-deployment +spec: + replicas: 1 + template: + metadata: + name: nginx-deployment + labels: + app: nginx-deployment + spec: + containers: + - name: nginx-deployment + image: nginx + imagePullPolicy: IfNotPresent + restartPolicy: Always + selector: + matchLabels: + app: nginx-deployment diff --git a/tests/stdlib/kubernetes/kustomize/testdata/pod.yaml b/tests/stdlib/kubernetes/kustomize/testdata/pod.yaml new file mode 100644 index 00000000..179e8199 --- /dev/null +++ b/tests/stdlib/kubernetes/kustomize/testdata/pod.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod + labels: + app: test-pod +spec: + containers: + - name: test-pod + image: nginx + imagePullPolicy: IfNotPresent + restartPolicy: Always diff --git a/tests/stdlib/kubernetes/kustomize/verify.cue b/tests/stdlib/kubernetes/kustomize/verify.cue new file mode 100644 index 00000000..18a923b5 --- /dev/null +++ b/tests/stdlib/kubernetes/kustomize/verify.cue @@ -0,0 +1,72 @@ +package kustomize + +import ( + "dagger.io/dagger/op" + "dagger.io/dagger" + "dagger.io/alpine" +) + +#VerifyKustomize: { + source: dagger.#Artifact + + #up: [ + op.#Load & { + from: alpine.#Image & { + package: bash: "=~5.1" + } + }, + + // Check files + op.#Exec & { + always: true + args: [ + "sh", "-c", "test $(ls /source | wc -l) = 1", + ] + mount: "/source": from: source + }, + + // Check image tag kustomization + op.#Exec & { + always: true + args: [ + "sh", "-c", #""" + grep -q "\- image: nginx:v1" /source/result.yaml + """#, + ] + mount: "/source": from: source + }, + + // Check replicas kustomization + op.#Exec & { + always: true + args: [ + "sh", "-c", #""" + grep -q "replicas: 2" /source/result.yaml + """#, + ] + mount: "/source": from: source + }, + + // Check pod merge by kustomization + op.#Exec & { + always: true + args: [ + "sh", "-c", #""" + grep -q "kind: Pod" /source/result.yaml + """#, + ] + mount: "/source": from: source + }, + + // Check pod name + op.#Exec & { + always: true + args: [ + "sh", "-c", #""" + grep -q "name: test-pod" /source/result.yaml + """#, + ] + mount: "/source": from: source + }, + ] +} diff --git a/tom_test/tmp/main.cue b/tom_test/tmp/main.cue new file mode 100644 index 00000000..7bc3f839 --- /dev/null +++ b/tom_test/tmp/main.cue @@ -0,0 +1,3 @@ +package tom + +name: string \ No newline at end of file