From 57dfa96bee3d6a07ddff51f46e8bb43595ca62bb Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 09:52:29 -0700 Subject: [PATCH 1/7] 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 + } + } + ] +} From f03018d230416f74a0017794f333859edf659bf7 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 09:53:10 -0700 Subject: [PATCH 2/7] example: added simple kubernetes example Signed-off-by: Sam Alba --- examples/kubernetes/main.cue | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/kubernetes/main.cue diff --git a/examples/kubernetes/main.cue b/examples/kubernetes/main.cue new file mode 100644 index 00000000..8f49f599 --- /dev/null +++ b/examples/kubernetes/main.cue @@ -0,0 +1,41 @@ +package main + +import ( + "encoding/yaml" + "dagger.io/aws" + "dagger.io/aws/eks" + "dagger.io/kubernetes" +) + +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 +} + +cluster: eks.#KubeConfig & { + config: awsConfig + clusterName: *"dagger-example-eks-cluster" | string +} + +apply: kubernetes.#Apply & { + source: yaml.Marshal(kubeSrc) + namespace: "test" + kubeconfig: cluster.kubeconfig +} From cbb4f325bffd401d07cc7cf64e0bd089658fd792 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 12:07:38 -0700 Subject: [PATCH 3/7] stdlib: implemented kubernetes/helm Signed-off-by: Sam Alba --- stdlib/kubernetes/helm/code.cue | 47 ++++++++++++++ stdlib/kubernetes/helm/helm.cue | 109 ++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 stdlib/kubernetes/helm/code.cue create mode 100644 stdlib/kubernetes/helm/helm.cue diff --git a/stdlib/kubernetes/helm/code.cue b/stdlib/kubernetes/helm/code.cue new file mode 100644 index 00000000..e22649ba --- /dev/null +++ b/stdlib/kubernetes/helm/code.cue @@ -0,0 +1,47 @@ +package helm + +#code: #""" + # Install Helm + 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 + + # 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..dc3b7bab --- /dev/null +++ b/stdlib/kubernetes/helm/helm.cue @@ -0,0 +1,109 @@ +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.#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_VERSION: version + 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/varlues.yaml": values + } + if (chart & dagger.#Artifact) != _|_ { + "/helm/chart": from: chart + } + } + }, + ] +} From 2daff0ee74ce6246f21fceabb9f845856c488c5a Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 12:08:42 -0700 Subject: [PATCH 4/7] example: added kubernetes example with Helm Signed-off-by: Sam Alba --- examples/kubernetes/main.cue | 52 ++++++++------ .../kubernetes/testdata/mychart/.helmignore | 23 +++++++ .../kubernetes/testdata/mychart/Chart.yaml | 21 ++++++ .../testdata/mychart/templates/NOTES.txt | 21 ++++++ .../testdata/mychart/templates/_helpers.tpl | 63 +++++++++++++++++ .../mychart/templates/deployment.yaml | 55 +++++++++++++++ .../testdata/mychart/templates/ingress.yaml | 41 +++++++++++ .../testdata/mychart/templates/service.yaml | 15 ++++ .../mychart/templates/serviceaccount.yaml | 12 ++++ .../templates/tests/test-connection.yaml | 15 ++++ .../kubernetes/testdata/mychart/values.yaml | 68 +++++++++++++++++++ stdlib/kubernetes/kubernetes.cue | 24 +++---- 12 files changed, 377 insertions(+), 33 deletions(-) create mode 100644 examples/kubernetes/testdata/mychart/.helmignore create mode 100644 examples/kubernetes/testdata/mychart/Chart.yaml create mode 100644 examples/kubernetes/testdata/mychart/templates/NOTES.txt create mode 100644 examples/kubernetes/testdata/mychart/templates/_helpers.tpl create mode 100644 examples/kubernetes/testdata/mychart/templates/deployment.yaml create mode 100644 examples/kubernetes/testdata/mychart/templates/ingress.yaml create mode 100644 examples/kubernetes/testdata/mychart/templates/service.yaml create mode 100644 examples/kubernetes/testdata/mychart/templates/serviceaccount.yaml create mode 100644 examples/kubernetes/testdata/mychart/templates/tests/test-connection.yaml create mode 100644 examples/kubernetes/testdata/mychart/values.yaml diff --git a/examples/kubernetes/main.cue b/examples/kubernetes/main.cue index 8f49f599..98cb8626 100644 --- a/examples/kubernetes/main.cue +++ b/examples/kubernetes/main.cue @@ -1,25 +1,25 @@ package main import ( - "encoding/yaml" - "dagger.io/aws" - "dagger.io/aws/eks" - "dagger.io/kubernetes" + "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" - }] - } + apiVersion: "v1" + kind: "Pod" + metadata: name: "kube-test" + spec: { + restartPolicy: "Never" + containers: [{ + name: "test" + image: "hello-world" + }] + } } // Fill using: @@ -29,13 +29,25 @@ 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 } -apply: kubernetes.#Apply & { - source: yaml.Marshal(kubeSrc) - namespace: "test" - kubeconfig: cluster.kubeconfig +// 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/kubernetes/kubernetes.cue b/stdlib/kubernetes/kubernetes.cue index 606e0e50..23e6959d 100644 --- a/stdlib/kubernetes/kubernetes.cue +++ b/stdlib/kubernetes/kubernetes.cue @@ -20,9 +20,9 @@ import ( #compute: [ llb.#Load & { from: alpine.#Image & { - package: bash: "=5.1.0-r0" - package: jq: "=1.6-r1" - package: curl: "=7.74.0-r1" + package: bash: "=5.1.0-r0" + package: jq: "=1.6-r1" + package: curl: "=7.74.0-r1" } }, llb.#WriteFile & { @@ -38,10 +38,8 @@ import ( "pipefail", "/entrypoint.sh", ] - env: { - KUBECTL_VERSION: version - } - } + env: KUBECTL_VERSION: version + }, ] } @@ -66,7 +64,7 @@ import ( #compute: [ llb.#Load & { - from: #Kubectl & { "version": version } + from: #Kubectl & {"version": version} }, llb.#WriteFile & { dest: "/entrypoint.sh" @@ -79,10 +77,10 @@ import ( }, if (source & string) != _|_ { llb.#WriteFile & { - dest: "/source" + dest: "/source" content: source } - } + }, llb.#Exec & { always: true args: [ @@ -94,12 +92,12 @@ import ( "/entrypoint.sh", ] env: { - KUBECONFIG: "/kubeconfig" + KUBECONFIG: "/kubeconfig" KUBE_NAMESPACE: namespace } if (source & dagger.#Artifact) != _|_ { - mount: "/source": source + mount: "/source": from: source } - } + }, ] } From 17ddf60bfcc1ee902e4f712aabc54d7fb6f1b476 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 22 Mar 2021 12:19:12 -0700 Subject: [PATCH 5/7] example: added README Signed-off-by: Sam Alba --- examples/README.md | 54 ++++++++++++++++++++++++++++++++++++++ examples/aws-eks/README.md | 12 --------- 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 examples/README.md delete mode 100644 examples/aws-eks/README.md 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 -``` From a3513ed479337a5cfde6c6f91eb112c1b675ac0b Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Wed, 24 Mar 2021 09:30:57 -0700 Subject: [PATCH 6/7] stdlib: helm: fixed typo + moved helm install to own exec to optimize caching Signed-off-by: Sam Alba --- stdlib/kubernetes/helm/code.cue | 6 ------ stdlib/kubernetes/helm/helm.cue | 21 +++++++++++++++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/stdlib/kubernetes/helm/code.cue b/stdlib/kubernetes/helm/code.cue index e22649ba..98da3765 100644 --- a/stdlib/kubernetes/helm/code.cue +++ b/stdlib/kubernetes/helm/code.cue @@ -1,12 +1,6 @@ package helm #code: #""" - # Install Helm - 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 - # Add the repository helm repo add repository "${HELM_REPO}" helm repo update diff --git a/stdlib/kubernetes/helm/helm.cue b/stdlib/kubernetes/helm/helm.cue index dc3b7bab..54e0805e 100644 --- a/stdlib/kubernetes/helm/helm.cue +++ b/stdlib/kubernetes/helm/helm.cue @@ -56,6 +56,24 @@ import ( 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" }, @@ -88,7 +106,6 @@ import ( KUBECONFIG: "/kubeconfig" KUBE_NAMESPACE: namespace - HELM_VERSION: version HELM_REPO: repository HELM_NAME: name HELM_ACTION: action @@ -98,7 +115,7 @@ import ( } mount: { if (values & string) != _|_ { - "/helm/varlues.yaml": values + "/helm/values.yaml": values } if (chart & dagger.#Artifact) != _|_ { "/helm/chart": from: chart From ca613ea50bcd1e74c4f7d6e88e4213b583b69026 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Wed, 24 Mar 2021 09:36:19 -0700 Subject: [PATCH 7/7] fix linting errors Signed-off-by: Sam Alba --- Makefile | 2 +- examples/kubernetes/main.cue | 6 +-- stdlib/kubernetes/helm/code.cue | 68 ++++++++++++++++----------------- 3 files changed, 38 insertions(+), 38 deletions(-) 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/kubernetes/main.cue b/examples/kubernetes/main.cue index 98cb8626..d35eb51d 100644 --- a/examples/kubernetes/main.cue +++ b/examples/kubernetes/main.cue @@ -46,8 +46,8 @@ kubeApply: kubernetes.#Apply & { // Fill using: // --input-dir helmChart.chart=./testdata/mychart helmChart: helm.#Chart & { - name: "test-helm" - namespace: "test" + name: "test-helm" + namespace: "test" kubeconfig: cluster.kubeconfig - chart: dagger.#Artifact + chart: dagger.#Artifact } diff --git a/stdlib/kubernetes/helm/code.cue b/stdlib/kubernetes/helm/code.cue index 98da3765..cb871e1f 100644 --- a/stdlib/kubernetes/helm/code.cue +++ b/stdlib/kubernetes/helm/code.cue @@ -1,41 +1,41 @@ package helm #code: #""" - # Add the repository - helm repo add repository "${HELM_REPO}" - helm repo update + # 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 + # 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" + 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 + # 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 - """# + 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 + """#