From 4d036389110ce284561243e0ff758117f38b3359 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Tue, 17 Aug 2021 00:14:41 +0200 Subject: [PATCH] Add test for kube-gcp cue-manifest step Signed-off-by: Tom Chauveau --- docs/learn/1007-kubernetes.md | 47 +------------ .../env/kube-gcp-cue-manifest/.gitignore | 2 + .../env/kube-gcp-cue-manifest/values.yaml | 32 +++++++++ .../tests/kube-gcp/cue-manifest/config.cue | 25 +++++++ .../kube-gcp/cue-manifest/deployment.cue | 43 ++++++++++++ .../tests/kube-gcp/cue-manifest/input.cue | 19 ++++++ .../tests/kube-gcp/cue-manifest/manifest.cue | 29 ++++++++ .../tests/kube-gcp/cue-manifest/service.cue | 36 ++++++++++ .../tests/kube-gcp/cue-manifest/test/test.cue | 66 +++++++++++++++++++ .../tests/kube-gcp/cue-manifest/todoapp.cue | 45 +++++++++++++ 10 files changed, 298 insertions(+), 46 deletions(-) create mode 100644 docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/.gitignore create mode 100644 docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/values.yaml create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/config.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/deployment.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/input.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/manifest.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/service.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/test/test.cue create mode 100644 docs/learn/tests/kube-gcp/cue-manifest/todoapp.cue diff --git a/docs/learn/1007-kubernetes.md b/docs/learn/1007-kubernetes.md index 341b02c0..77460b9c 100644 --- a/docs/learn/1007-kubernetes.md +++ b/docs/learn/1007-kubernetes.md @@ -594,52 +594,7 @@ values={[ -```cue title="todoapp/kube/todoapp.cue" -package main - -import ( - "alpha.dagger.io/dagger" - "alpha.dagger.io/docker" - "alpha.dagger.io/kubernetes" -) - -// input: source code repository, must contain a Dockerfile -// set with `dagger input dir repository . -e kube` -repository: dagger.#Artifact & dagger.#Input - -// GCR registry to push images to -registry: string & dagger.#Input -tag: "test-gcr" - -// Todoapp deployment pipeline -todoApp: { - // Build the image from repositoru artifact - image: docker.#Build & { - source: repository - } - - // Push image to registry - remoteImage: docker.#Push & { - target: "\(registry):\(tag)" - source: image - auth: { - username: gcrCreds.username - secret: gcrCreds.secret - } - } - - // Generate deployment manifest - deployment: #AppManifest & { - name: "todoapp" - image: remoteImage.ref - } - - // Deploy the customized manifest to a kubernetes cluster - kubeSrc: kubernetes.#Resources & { - "kubeconfig": kubeconfig - manifest: deployment.manifest - } -} +```cue file=tests/kube-gcp/cue-manifest/todoapp.cue title="todoapp/kube/todoapp.cue" ``` diff --git a/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/.gitignore b/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/values.yaml b/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/values.yaml new file mode 100644 index 00000000..de7748c9 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-gcp-cue-manifest/values.yaml @@ -0,0 +1,32 @@ +plan: + package: ./kube-gcp/cue-manifest/test +name: kube-gcp-cue-manifest +inputs: + gcpConfig.project: + text: dagger-ci + gcpConfig.region: + text: us-west2-a + gcpConfig.serviceKey: + secret: ENC[AES256_GCM,data:WlC5J7rNdHlI6hlskgCzuApBU0fvHmibFh9xJ604KbC2b3Mf/Daf2PtxvfEbdfoJXWJZBb7xYgH7LJOEfhmrkKyK5Qp9xr6I05AuO0H76pLnYTLcWru9AoM9ekYm6TW7r94OONueyOrEumZr59qdaQSr3uMx2etZ2JAVpwYBMyUg3gFVkAjSC8DAPtocA0QWZQuRjvhWWGAG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TJiXGttgDqOCOpGqM/CtGQgofaYKJPrjhpxGeAFbV/+HkFpEY4UwJrHSKu9Hrv6flK36Wzi1n3xnXwQzsvbez1A2VRjIAkd3xwmo+pmBBiAnxiaq1P7WLEmyvPR5UwMW2uzQ8aw0v7OTmcY0AbZZOTiMY0MRh+LCiMJvUsOv3FKfgvdxCaGFpApkQAHqHbMVZa6X8mk8MTAUGMMJ+/Ir8uW3EBtDjqU2RzH7w6D6SjnMOZJ1nqpGDG3YWU/wtPQqG0/TiIGuwNDkKfTD6wZrgDjb+bGvQWi9H6maiaUg9H8O/qwhQqsiFRIgSnOX1nm/p2/wj16raH7NjQIG4UHfox96fNBzzp6xGyyJXfgymIOWge/a3VhfXB5/kb/iFTcw8uTaHu+rmAelSUUgimQ86Ybdfzs7lsPAIQgGJOQJ1KgyQ8wxF8QP8cYqu10EiTGi/JGx7z8WeLavx542X4xVezPenqVOqQlcpF6M8LR2R2p8zptjX1qsftjPNZyRx9YvI2MsGVW9ifB1hs4bs56dTKNYX3itgVM9T/q9B5pbNY3eMf2KpYT8AtfkJgtJNttNNUkLUjcoTxnU0d2u+1e0I7r12aRFcIKHvGDMt5dDyN8b2/8rhg42i/A/WaXGNIecljNBsVK/wS0Hv56roCyiTBg4ZoCzTA1sCPLR9V9T2AId8QZv2yMPlKVzSH7rrEMGoSc+BWuec98B1QrIBdC6ICnoin,iv:uGLdGFprxBx2JX4GXdJlwiikOxarEX3zzJnuiF3jtV8=,tag:aZIAWTfjgDBCAKDDGXVHZw==,type:str] + gkeConfig.clusterName: + text: test-cluster +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA2TWhqdVQ4Si9CQU5mRmQy + SEpCM0l6UGsydkM1SlFIREZmSTh5YmtuT1cwClN5eUJlNGkyS29jYmlEMWhYTDZK + NzdLeitIL0Erdm1IVHAxSitQWWRDOG8KLS0tIGNFa1JEclJNWEQ2SHVlbW5lY3kw + enc0b29RU2xKMWFoWFVqcnJ4S3NOKzQK6coqwcUNfO/D3OFcEnVHqjZTUFv9zJPr + OT4sQCfYA4tJWL6Xm8aDiWKr91GqURo4aTpZnwzlWIrIrTTs25IQig== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-08-16T22:09:04Z" + mac: ENC[AES256_GCM,data:kXzvMaEEy9vJneQU9FlRGBzwP73aJhhyIu/axCu36ee84pYuHYEXkJB6Irh/1kYyDDwj+m3k3ySszlkQfa5+Lhy7U+sKb7KFJKmy2rYJekBIjTnYyPNeM1M0mG52pKwbUTElZ9Wo3z4vDkSrjfYxsgYM2cSvEKgoiC4M5VhzXjM=,iv:WNar5O7/+Vwh0+S8tcTKNbloUUpB+79QJEyrVJVeGsc=,tag:iAwTMUidHmTv+TNJxt0BBg==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/docs/learn/tests/kube-gcp/cue-manifest/config.cue b/docs/learn/tests/kube-gcp/cue-manifest/config.cue new file mode 100644 index 00000000..844b77f3 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/config.cue @@ -0,0 +1,25 @@ +package main + +import ( + "alpha.dagger.io/gcp" + "alpha.dagger.io/gcp/gcr" + "alpha.dagger.io/gcp/gke" +) + +// Value created for generic reference of `kubeconfig` in `todoapp.cue` +kubeconfig: gkeConfig.kubeconfig + +// gcpConfig used for Google connection +gcpConfig: gcp.#Config + +// gkeConfig used for deployment +gkeConfig: gke.#KubeConfig & { + // config field references `gkeConfig` value to set in once + config: gcpConfig +} + +// gcrCreds used for remote image push +gcrCreds: gcr.#Credentials & { + // config field references `gcpConfig` value to set in once + config: gcpConfig +} diff --git a/docs/learn/tests/kube-gcp/cue-manifest/deployment.cue b/docs/learn/tests/kube-gcp/cue-manifest/deployment.cue new file mode 100644 index 00000000..88d7b800 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/deployment.cue @@ -0,0 +1,43 @@ +package main + +// Deployment template containing all the common boilerplate shared by +// deployments of this application. +#Deployment: { + // Name of the deployment. This will be used to label resources automatically + // and generate selectors. + name: string + + // Container image. + image: string + + // 80 is the default port. + port: *80 | int + + // 1 is the default, but we allow any number. + replicas: *1 | int + + // Deployment manifest. Uses the name, image, port and replicas above to + // generate the resource manifest. + manifest: { + apiVersion: "apps/v1" + kind: "Deployment" + metadata: { + "name": name + labels: app: name + } + spec: { + "replicas": replicas + selector: matchLabels: app: name + template: { + metadata: labels: app: name + spec: containers: [{ + "name": name + "image": image + ports: [{ + containerPort: port + }] + }] + } + } + } +} diff --git a/docs/learn/tests/kube-gcp/cue-manifest/input.cue b/docs/learn/tests/kube-gcp/cue-manifest/input.cue new file mode 100644 index 00000000..fa993210 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/input.cue @@ -0,0 +1,19 @@ +package main + +import ( + "alpha.dagger.io/git" +) + +manifest: git.#Repository & { + remote: "https://github.com/dagger/examples.git" + ref: "main" + subdir: "todoapp/k8s" +} + +repository: git.#Repository & { + remote: "https://github.com/dagger/examples.git" + ref: "main" + subdir: "todoapp" +} + +registry: "gcr.io/dagger-ci/test" diff --git a/docs/learn/tests/kube-gcp/cue-manifest/manifest.cue b/docs/learn/tests/kube-gcp/cue-manifest/manifest.cue new file mode 100644 index 00000000..df4ff0d9 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/manifest.cue @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/yaml" +) + +// Define and generate kubernetes deployment to deploy to kubernetes cluster +#AppManifest: { + // Name of the application + name: string + + // Image to deploy to + image: string + + // Define a kubernetes deployment object + deployment: #Deployment & { + "name": name + "image": image + } + + // Define a kubernetes service object + service: #Service & { + "name": name + ports: http: deployment.port + } + + // Merge definitions and convert them back from CUE to YAML + manifest: yaml.MarshalStream([deployment.manifest, service.manifest]) +} diff --git a/docs/learn/tests/kube-gcp/cue-manifest/service.cue b/docs/learn/tests/kube-gcp/cue-manifest/service.cue new file mode 100644 index 00000000..8c5dc957 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/service.cue @@ -0,0 +1,36 @@ +package main + +// Service template containing all the common boilerplate shared by +// services of this application. +#Service: { + // Name of the service. This will be used to label resources automatically + // and generate selector. + name: string + + // NodePort is the default service type. + type: *"NodePort" | "LoadBalancer" | "ClusterIP" | "ExternalName" + + // Ports where the service should listen + ports: [string]: number + + // Service manifest. Uses the name, type and ports above to + // generate the resource manifest. + manifest: { + apiVersion: "v1" + kind: "Service" + metadata: { + "name": "\(name)-service" + labels: app: name + } + spec: { + "type": type + "ports": [ + for k, v in ports { + name: k + port: v + }, + ] + selector: app: name + } + } +} diff --git a/docs/learn/tests/kube-gcp/cue-manifest/test/test.cue b/docs/learn/tests/kube-gcp/cue-manifest/test/test.cue new file mode 100644 index 00000000..82f2fb40 --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/test/test.cue @@ -0,0 +1,66 @@ +package main + +import ( + "alpha.dagger.io/dagger/op" + "alpha.dagger.io/kubernetes" +) + +TestEks: { + #_GetDeployment: """ + kubectl describe deployment todoapp | grep 'True' + """ + + #_DeleteDeployment: """ + kubectl delete deployment todoapp + kubectl delete service todoapp-service + """ + + #up: [ + op.#Load & { + from: kubernetes.#Kubectl + }, + + op.#WriteFile & { + dest: "/kubeconfig" + content: todoApp.kubeSrc.kubeconfig + }, + + op.#WriteFile & { + dest: "/getPods.sh" + content: #_GetDeployment + }, + + op.#WriteFile & { + dest: "/deletePods.sh" + content: #_DeleteDeployment + }, + + // Get pods + op.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/getPods.sh", + ] + env: KUBECONFIG: "/kubeconfig" + }, + + // Delete pods + op.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "/deletePods.sh", + ] + env: KUBECONFIG: "/kubeconfig" + }, + ] +} diff --git a/docs/learn/tests/kube-gcp/cue-manifest/todoapp.cue b/docs/learn/tests/kube-gcp/cue-manifest/todoapp.cue new file mode 100644 index 00000000..42135f3f --- /dev/null +++ b/docs/learn/tests/kube-gcp/cue-manifest/todoapp.cue @@ -0,0 +1,45 @@ +package main + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/docker" + "alpha.dagger.io/kubernetes" +) + +// input: source code repository, must contain a Dockerfile +// set with `dagger input dir repository . -e kube` +repository: dagger.#Artifact & dagger.#Input + +// GCR registry to push images to +registry: string & dagger.#Input +tag: "test-gcr" + +// Todoapp deployment pipeline +todoApp: { + // Build the image from repositoru artifact + image: docker.#Build & { + source: repository + } + + // Push image to registry + remoteImage: docker.#Push & { + target: "\(registry):\(tag)" + source: image + auth: { + username: gcrCreds.username + secret: gcrCreds.secret + } + } + + // Generate deployment manifest + deployment: #AppManifest & { + name: "todoapp" + image: remoteImage.ref + } + + // Deploy the customized manifest to a kubernetes cluster + kubeSrc: kubernetes.#Resources & { + "kubeconfig": kubeconfig + manifest: deployment.manifest + } +}