diff --git a/Makefile b/Makefile index c9ed0754..50814200 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ test: .PHONY: cuefmt cuefmt: @(cue fmt -s ./stdlib/...) - @(cue fmt -s ./examples/*) + @(cue fmt -s ./examples/*/) @(cue fmt -s ./tests/...) .PHONY: lint diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..3387f63b --- /dev/null +++ b/examples/README.md @@ -0,0 +1,54 @@ +# Dagger Examples + +## aws-eks: Kubernetes on AWS (EKS) + +This example provisions a Kubernetes (EKS) cluster on AWS using Cloudformation, +it also outputs the new generated kubeconfig for the `kubectl` client. + +How to run: + +```sh +dagger compute ./aws-eks \ + --input-string awsConfig.accessKey="MY_AWS_ACCESS_KEY" \ + --input-string awsConfig.secretKey="MY_AWS_SECRET_KEY" \ + | jq -j '.kubeconfig.kubeconfig' > kubeconfig +``` + +## aws-monitoring: HTTP Monitoring on AWS + +This example implements a full HTTP(s) Monitoring solution on AWS using +Cloudformation and Cloudwatch Synthetics. + +How to run: + +```sh +dagger compute ./aws-monitoring \ + --input-string awsConfig.accessKey="MY_AWS_ACCESS_KEY" \ + --input-string awsConfig.secretKey="MY_AWS_SECRET_KEY" \ +``` + +## kubernetes: Deploy to an existing Kubernetes cluster + +This example shows two different ways for deploying to an existing Kubernetes +(EKS) cluster: a simple deployment spec (written in Cue), and a local helm +chart. + +How to run: + +```sh +dagger compute ./kubernetes \ + --input-string awsConfig.accessKey="MY_AWS_ACCESS_KEY" \ + --input-string awsConfig.secretKey="MY_AWS_SECRET_KEY" \ + --input-dir helmChart.chart=./kubernetes/testdata/mychart +``` + +## react-netlify: Deploy a React Web app to Netlify + +This example shows how to deploy a simple React App to Netlify. + +How to run: + +```sh +dagger compute ./react-netlify \ + --input-string todoApp.account.token="MY_NETLIFY_API_TOKEN" +``` diff --git a/examples/aws-eks/README.md b/examples/aws-eks/README.md deleted file mode 100644 index dfc50a8b..00000000 --- a/examples/aws-eks/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Kubernetes on AWS (EKS) - -This example creates a new EKS cluster and outputs its corresponding kubeconfig - -## How to run - -```sh -dagger compute . \ - --input-string awsConfig.accessKey="MY_AWS_ACCESS_KEY" \ - --input-string awsConfig.secretKey="MY_AWS_SECRET_KEY" \ - | jq -j '.kubeconfig.kubeconfig' > kubeconfig -``` diff --git a/examples/kubernetes/main.cue b/examples/kubernetes/main.cue new file mode 100644 index 00000000..d35eb51d --- /dev/null +++ b/examples/kubernetes/main.cue @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/yaml" + "dagger.io/dagger" + "dagger.io/aws" + "dagger.io/aws/eks" + "dagger.io/kubernetes" + "dagger.io/kubernetes/helm" +) + +kubeSrc: { + apiVersion: "v1" + kind: "Pod" + metadata: name: "kube-test" + spec: { + restartPolicy: "Never" + containers: [{ + name: "test" + image: "hello-world" + }] + } +} + +// Fill using: +// --input-string awsConfig.accessKey=XXX +// --input-string awsConfig.secretKey=XXX +awsConfig: aws.#Config & { + region: *"us-east-2" | string +} + +// Take the kubeconfig from the EKS cluster +cluster: eks.#KubeConfig & { + config: awsConfig + clusterName: *"dagger-example-eks-cluster" | string +} + +// Example of a simple `kubectl apply` using a simple config +kubeApply: kubernetes.#Apply & { + source: yaml.Marshal(kubeSrc) + namespace: "test" + kubeconfig: cluster.kubeconfig +} + +// Example of a `helm install` using a local chart +// Fill using: +// --input-dir helmChart.chart=./testdata/mychart +helmChart: helm.#Chart & { + name: "test-helm" + namespace: "test" + kubeconfig: cluster.kubeconfig + chart: dagger.#Artifact +} diff --git a/examples/kubernetes/testdata/mychart/.helmignore b/examples/kubernetes/testdata/mychart/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/examples/kubernetes/testdata/mychart/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/examples/kubernetes/testdata/mychart/Chart.yaml b/examples/kubernetes/testdata/mychart/Chart.yaml new file mode 100644 index 00000000..1613bff5 --- /dev/null +++ b/examples/kubernetes/testdata/mychart/Chart.yaml @@ -0,0 +1,21 @@ +apiVersion: v2 +name: mychart +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. +appVersion: 1.16.0 diff --git a/examples/kubernetes/testdata/mychart/templates/NOTES.txt b/examples/kubernetes/testdata/mychart/templates/NOTES.txt new file mode 100644 index 00000000..8b795a8a --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/NOTES.txt @@ -0,0 +1,21 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "mychart.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "mychart.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "mychart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "mychart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80 +{{- end }} diff --git a/examples/kubernetes/testdata/mychart/templates/_helpers.tpl b/examples/kubernetes/testdata/mychart/templates/_helpers.tpl new file mode 100644 index 00000000..8479665d --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/_helpers.tpl @@ -0,0 +1,63 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "mychart.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "mychart.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "mychart.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Common labels +*/}} +{{- define "mychart.labels" -}} +helm.sh/chart: {{ include "mychart.chart" . }} +{{ include "mychart.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{/* +Selector labels +*/}} +{{- define "mychart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "mychart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define "mychart.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include "mychart.fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} diff --git a/examples/kubernetes/testdata/mychart/templates/deployment.yaml b/examples/kubernetes/testdata/mychart/templates/deployment.yaml new file mode 100644 index 00000000..38e698f8 --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/deployment.yaml @@ -0,0 +1,55 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "mychart.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/examples/kubernetes/testdata/mychart/templates/ingress.yaml b/examples/kubernetes/testdata/mychart/templates/ingress.yaml new file mode 100644 index 00000000..76d277c0 --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/ingress.yaml @@ -0,0 +1,41 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "mychart.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "mychart.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: +{{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} +{{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} +{{- end }} diff --git a/examples/kubernetes/testdata/mychart/templates/service.yaml b/examples/kubernetes/testdata/mychart/templates/service.yaml new file mode 100644 index 00000000..bdceb88f --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} diff --git a/examples/kubernetes/testdata/mychart/templates/serviceaccount.yaml b/examples/kubernetes/testdata/mychart/templates/serviceaccount.yaml new file mode 100644 index 00000000..145e891e --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "mychart.serviceAccountName" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end -}} diff --git a/examples/kubernetes/testdata/mychart/templates/tests/test-connection.yaml b/examples/kubernetes/testdata/mychart/templates/tests/test-connection.yaml new file mode 100644 index 00000000..0bd46ef1 --- /dev/null +++ b/examples/kubernetes/testdata/mychart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "mychart.fullname" . }}-test-connection" + labels: + {{- include "mychart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test-success +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "mychart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/examples/kubernetes/testdata/mychart/values.yaml b/examples/kubernetes/testdata/mychart/values.yaml new file mode 100644 index 00000000..dbf4257e --- /dev/null +++ b/examples/kubernetes/testdata/mychart/values.yaml @@ -0,0 +1,68 @@ +# Default values for mychart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} 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/helm/code.cue b/stdlib/kubernetes/helm/code.cue new file mode 100644 index 00000000..cb871e1f --- /dev/null +++ b/stdlib/kubernetes/helm/code.cue @@ -0,0 +1,41 @@ +package helm + +#code: #""" + # Add the repository + helm repo add repository "${HELM_REPO}" + helm repo update + + # If the chart is a file, then it's the chart name + # If it's a directly, then it's the contents of the cart + if [ -f "/helm/chart" ]; then + HELM_CHART="repository/$(cat /helm/chart)" + else + HELM_CHART="/helm/chart" + fi + + OPTS="" + OPTS="$OPTS --timeout "$HELM_TIMEOUT"" + OPTS="$OPTS --namespace "$KUBE_NAMESPACE"" + [ "$HELM_WAIT" = "true" ] && OPTS="$OPTS --wait" + [ "$HELM_ATOMIC" = "true" ] && OPTS="$OPTS --atomic" + [ -f "/helm/values.yaml" ] && OPTS="$OPTS -f /helm/values.yaml" + + # Select the namespace + kubectl create namespace "$KUBE_NAMESPACE" || true + + case "$HELM_ACTION" in + install) + helm install $OPTS "$HELM_NAME" "$HELM_CHART" + ;; + upgrade) + helm upgrade $OPTS "$HELM_NAME" "$HELM_CHART" + ;; + installOrUpgrade) + helm upgrade $OPTS --install "$HELM_NAME" "$HELM_CHART" + ;; + *) + echo unsupported helm action "$HELM_ACTION" + exit 1 + ;; + esac + """# diff --git a/stdlib/kubernetes/helm/helm.cue b/stdlib/kubernetes/helm/helm.cue new file mode 100644 index 00000000..54e0805e --- /dev/null +++ b/stdlib/kubernetes/helm/helm.cue @@ -0,0 +1,126 @@ +package helm + +import ( + "strconv" + + "dagger.io/dagger" + "dagger.io/llb" + "dagger.io/kubernetes" +) + +// Install a Helm chart +#Chart: { + // Helm deployment name + name: string + + // Helm chart to install + chart: string | dagger.#Artifact + + // Helm chart repository (defaults to stable) + repository: *"https://charts.helm.sh/stable" | string + + // Helm values (either a YAML string or a Cue structure) + values?: string + + // Kubernetes Namespace to deploy to + namespace: string + + // Helm action to apply + action: *"installOrUpgrade" | "install" | "upgrade" + + // time to wait for any individual Kubernetes operation (like Jobs for hooks) + timeout: string | *"5m" + + // if set, will wait until all Pods, PVCs, Services, and minimum number of + // Pods of a Deployment, StatefulSet, or ReplicaSet are in a ready state + // before marking the release as successful. + // It will wait for as long as timeout + wait: *true | bool + + // if set, installation process purges chart on fail. + // The wait option will be set automatically if atomic is used + atomic: *true | bool + + // Kube config file + kubeconfig: dagger.#Secret + + // Helm version + version: *"3.5.2" | string + + // Kubectl version + kubectlVersion: *"v1.19.9" | string + + #compute: [ + llb.#Load & { + from: kubernetes.#Kubectl & { + version: kubectlVersion + } + }, + llb.#Exec & { + env: HELM_VERSION: version + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "-c", + #""" + # Install Yarn + curl -sfL -S https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz | \ + tar -zx -C /tmp && \ + mv /tmp/linux-amd64/helm /usr/local/bin && \ + chmod +x /usr/local/bin/helm + """#, + ] + }, + llb.#Mkdir & { + path: "/helm" + }, + llb.#WriteFile & { + dest: "/entrypoint.sh" + content: #code + }, + llb.#WriteFile & { + dest: "/kubeconfig" + content: kubeconfig + mode: 0o600 + }, + if (chart & string) != _|_ { + llb.#WriteFile & { + dest: "/helm/chart" + content: chart + } + }, + llb.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/entrypoint.sh", + ] + env: { + KUBECONFIG: "/kubeconfig" + KUBE_NAMESPACE: namespace + + HELM_REPO: repository + HELM_NAME: name + HELM_ACTION: action + HELM_TIMEOUT: timeout + HELM_WAIT: strconv.FormatBool(wait) + HELM_ATOMIC: strconv.FormatBool(atomic) + } + mount: { + if (values & string) != _|_ { + "/helm/values.yaml": values + } + if (chart & dagger.#Artifact) != _|_ { + "/helm/chart": from: chart + } + } + }, + ] +} diff --git a/stdlib/kubernetes/kubernetes.cue b/stdlib/kubernetes/kubernetes.cue new file mode 100644 index 00000000..23e6959d --- /dev/null +++ b/stdlib/kubernetes/kubernetes.cue @@ -0,0 +1,103 @@ +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": from: source + } + }, + ] +}