feat(kubernetes): add kustomize generation to kubernetes stdlib package

test(kubernetes): add test for kustomize

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-04-17 01:34:32 +02:00
parent 2804feb004
commit 253c1ce3f7
7 changed files with 243 additions and 0 deletions

View File

@ -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"
},
]
}

View File

@ -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

View File

@ -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
}
}

View File

@ -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

View File

@ -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

View File

@ -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
},
]
}

3
tom_test/tmp/main.cue Normal file
View File

@ -0,0 +1,3 @@
package tom
name: string