From 57dfa96bee3d6a07ddff51f46e8bb43595ca62bb Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 09:52:29 -0700 Subject: [PATCH] stdlib: implemented generic kubectl apply + fixes in eks Signed-off-by: Sam Alba --- stdlib/aws/eks/code.cue | 3 +- stdlib/aws/eks/eks.cue | 5 ++ stdlib/kubernetes/kubernetes.cue | 105 +++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 stdlib/kubernetes/kubernetes.cue diff --git a/stdlib/aws/eks/code.cue b/stdlib/aws/eks/code.cue index 9cb1a630..6c84a6d0 100644 --- a/stdlib/aws/eks/code.cue +++ b/stdlib/aws/eks/code.cue @@ -2,7 +2,8 @@ package eks #Code: #""" [ -e /cache/bin/kubectl ] || { - curl -sfL https://dl.k8s.io/v1.19.9/bin/linux/amd64/kubectl -o /cache/bin/kubectl && chmod +x /cache/bin/kubectl + curl -sfL https://dl.k8s.io/${KUBECTL_VERSION}/bin/linux/amd64/kubectl -o /cache/bin/kubectl \ + && chmod +x /cache/bin/kubectl } export KUBECONFIG=/kubeconfig diff --git a/stdlib/aws/eks/eks.cue b/stdlib/aws/eks/eks.cue index 58eae8c5..7325cb7f 100644 --- a/stdlib/aws/eks/eks.cue +++ b/stdlib/aws/eks/eks.cue @@ -1,6 +1,7 @@ package eks import ( + "dagger.io/dagger" "dagger.io/llb" "dagger.io/aws" ) @@ -13,6 +14,9 @@ import ( // EKS cluster name clusterName: string + // Kubectl version + version: *"v1.19.9" | string + // kubeconfig is the generated kube configuration file kubeconfig: { dagger.#Secret @@ -44,6 +48,7 @@ import ( AWS_DEFAULT_OUTPUT: "json" AWS_PAGER: "" EKS_CLUSTER: clusterName + KUBECTL_VERSION: version } mount: { "/cache/aws": "cache" diff --git a/stdlib/kubernetes/kubernetes.cue b/stdlib/kubernetes/kubernetes.cue new file mode 100644 index 00000000..606e0e50 --- /dev/null +++ b/stdlib/kubernetes/kubernetes.cue @@ -0,0 +1,105 @@ +package kubernetes + +import ( + "dagger.io/llb" + "dagger.io/dagger" + "dagger.io/alpine" +) + +#Kubectl: { + + version: *"v1.19.9" | string + + #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 + } + """# + + #compute: [ + llb.#Load & { + from: alpine.#Image & { + package: bash: "=5.1.0-r0" + package: jq: "=1.6-r1" + package: curl: "=7.74.0-r1" + } + }, + llb.#WriteFile & { + dest: "/entrypoint.sh" + content: #code + }, + llb.#Exec & { + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/entrypoint.sh", + ] + env: { + KUBECTL_VERSION: version + } + } + ] +} + +// Apply a Kubernetes configuration +#Apply: { + // Kubernetes config to deploy + source: string | dagger.#Artifact + + // Kubernetes Namespace to deploy to + namespace: string + + // Version of kubectl client + version: *"v1.19.9" | string + + // Kube config file + kubeconfig: dagger.#Secret + + #code: #""" + kubectl create namespace "$KUBE_NAMESPACE" || true + kubectl --namespace "$KUBE_NAMESPACE" apply -R -f /source + """# + + #compute: [ + llb.#Load & { + from: #Kubectl & { "version": version } + }, + llb.#WriteFile & { + dest: "/entrypoint.sh" + content: #code + }, + llb.#WriteFile & { + dest: "/kubeconfig" + content: kubeconfig + mode: 0o600 + }, + if (source & string) != _|_ { + llb.#WriteFile & { + dest: "/source" + content: source + } + } + llb.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/entrypoint.sh", + ] + env: { + KUBECONFIG: "/kubeconfig" + KUBE_NAMESPACE: namespace + } + if (source & dagger.#Artifact) != _|_ { + mount: "/source": source + } + } + ] +}