2021-06-15 10:46:56 +02:00
|
|
|
// Kubernetes client operations
|
2021-03-22 17:52:29 +01:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger/op"
|
|
|
|
"alpha.dagger.io/dagger"
|
|
|
|
"alpha.dagger.io/alpine"
|
2021-03-22 17:52:29 +01:00
|
|
|
)
|
|
|
|
|
2021-06-15 10:46:56 +02:00
|
|
|
// Kubectl client
|
2021-03-22 17:52:29 +01:00
|
|
|
#Kubectl: {
|
|
|
|
|
2021-06-15 10:46:56 +02:00
|
|
|
// Kubectl version
|
2021-09-06 15:17:38 +02:00
|
|
|
version: dagger.#Input & {*"v1.19.9" | string}
|
2021-03-22 17:52:29 +01:00
|
|
|
|
|
|
|
#code: #"""
|
|
|
|
[ -e /usr/local/bin/kubectl ] || {
|
|
|
|
curl -sfL https://dl.k8s.io/${KUBECTL_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
|
|
|
|
&& chmod +x /usr/local/bin/kubectl
|
|
|
|
}
|
|
|
|
"""#
|
|
|
|
|
2021-04-03 00:00:30 +02:00
|
|
|
#up: [
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Load & {
|
2021-03-22 17:52:29 +01:00
|
|
|
from: alpine.#Image & {
|
2021-04-14 23:06:51 +02:00
|
|
|
package: bash: "=~5.1"
|
|
|
|
package: jq: "=~1.6"
|
2021-05-27 14:20:15 +02:00
|
|
|
package: curl: true
|
2021-03-22 17:52:29 +01:00
|
|
|
}
|
|
|
|
},
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#WriteFile & {
|
2021-03-22 17:52:29 +01:00
|
|
|
dest: "/entrypoint.sh"
|
|
|
|
content: #code
|
|
|
|
},
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Exec & {
|
2021-03-22 17:52:29 +01:00
|
|
|
args: [
|
|
|
|
"/bin/bash",
|
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"/entrypoint.sh",
|
|
|
|
]
|
2021-03-22 20:08:42 +01:00
|
|
|
env: KUBECTL_VERSION: version
|
|
|
|
},
|
2021-03-22 17:52:29 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:03:35 +02:00
|
|
|
// Apply Kubernetes resources
|
|
|
|
#Resources: {
|
2021-04-07 05:57:45 +02:00
|
|
|
|
2021-03-22 17:52:29 +01:00
|
|
|
// Kubernetes config to deploy
|
2021-09-06 15:17:38 +02:00
|
|
|
source: dagger.#Input & {*null | dagger.#Artifact}
|
2021-04-07 05:57:45 +02:00
|
|
|
|
2021-06-02 03:58:18 +02:00
|
|
|
// Kubernetes manifest to deploy inlined in a string
|
2021-09-06 15:17:38 +02:00
|
|
|
manifest: dagger.#Input & {*null | string}
|
|
|
|
|
|
|
|
// Kubernetes manifest url to deploy remote configuration
|
|
|
|
url: dagger.#Input & {*null | string}
|
2021-03-22 17:52:29 +01:00
|
|
|
|
|
|
|
// Kubernetes Namespace to deploy to
|
2021-09-06 15:17:38 +02:00
|
|
|
namespace: dagger.#Input & {*"default" | string}
|
2021-03-22 17:52:29 +01:00
|
|
|
|
|
|
|
// Version of kubectl client
|
2021-09-06 15:17:38 +02:00
|
|
|
version: dagger.#Input & {*"v1.19.9" | string}
|
2021-03-22 17:52:29 +01:00
|
|
|
|
|
|
|
// Kube config file
|
2021-09-22 19:03:58 +02:00
|
|
|
kubeconfig: dagger.#Input & {string | dagger.#Secret}
|
2021-03-22 17:52:29 +01:00
|
|
|
|
|
|
|
#code: #"""
|
2021-06-02 03:58:18 +02:00
|
|
|
kubectl create namespace "$KUBE_NAMESPACE" > /dev/null 2>&1 || true
|
2021-09-06 15:17:38 +02:00
|
|
|
|
|
|
|
if [ -d /source ] || [ -f /source ]; then
|
|
|
|
kubectl --namespace "$KUBE_NAMESPACE" apply -R -f /source
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$DEPLOYMENT_URL" ]; then
|
|
|
|
kubectl --namespace "$KUBE_NAMESPACE" apply -R -f "$DEPLOYMENT_URL"
|
|
|
|
exit 0
|
|
|
|
fi
|
2021-03-22 17:52:29 +01:00
|
|
|
"""#
|
|
|
|
|
2021-04-03 00:00:30 +02:00
|
|
|
#up: [
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Load & {
|
2021-03-22 20:08:42 +01:00
|
|
|
from: #Kubectl & {"version": version}
|
2021-03-22 17:52:29 +01:00
|
|
|
},
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#WriteFile & {
|
2021-03-22 17:52:29 +01:00
|
|
|
dest: "/entrypoint.sh"
|
|
|
|
content: #code
|
|
|
|
},
|
2021-09-22 19:03:58 +02:00
|
|
|
|
|
|
|
if (kubeconfig & string) != _|_ {
|
|
|
|
op.#WriteFile & {
|
|
|
|
dest: "/kubeconfig"
|
|
|
|
content: kubeconfig
|
|
|
|
mode: 0o600
|
2021-09-22 19:11:35 +02:00
|
|
|
}
|
2021-06-17 03:06:39 +02:00
|
|
|
},
|
2021-09-22 19:03:58 +02:00
|
|
|
|
2021-09-06 15:17:38 +02:00
|
|
|
if manifest != null {
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#WriteFile & {
|
2021-03-22 20:08:42 +01:00
|
|
|
dest: "/source"
|
2021-06-02 03:58:18 +02:00
|
|
|
content: manifest
|
2021-03-22 17:52:29 +01:00
|
|
|
}
|
2021-03-22 20:08:42 +01:00
|
|
|
},
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Exec & {
|
2021-03-22 17:52:29 +01:00
|
|
|
always: true
|
|
|
|
args: [
|
|
|
|
"/bin/bash",
|
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"/entrypoint.sh",
|
|
|
|
]
|
|
|
|
env: {
|
2021-03-22 20:08:42 +01:00
|
|
|
KUBECONFIG: "/kubeconfig"
|
2021-03-22 17:52:29 +01:00
|
|
|
KUBE_NAMESPACE: namespace
|
2021-09-06 15:17:38 +02:00
|
|
|
if url != null {
|
|
|
|
DEPLOYMENT_URL: url
|
|
|
|
}
|
2021-03-22 17:52:29 +01:00
|
|
|
}
|
2021-09-06 15:17:38 +02:00
|
|
|
if manifest == null && source != null {
|
2021-03-22 20:08:42 +01:00
|
|
|
mount: "/source": from: source
|
2021-03-22 17:52:29 +01:00
|
|
|
}
|
2021-09-22 19:03:58 +02:00
|
|
|
if (kubeconfig & dagger.#Secret) != _|_ {
|
|
|
|
mount: "/kubeconfig": secret: kubeconfig
|
|
|
|
}
|
2021-03-22 20:08:42 +01:00
|
|
|
},
|
2021-03-22 17:52:29 +01:00
|
|
|
]
|
|
|
|
}
|