From 5fcaaa6cfe4b3eaee6479643baee6b2eeab43a8e Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Mon, 16 Aug 2021 16:34:07 +0200 Subject: [PATCH] Add test for kube-aws basic & deployment Signed-off-by: Tom Chauveau --- docs/learn/1007-kubernetes.md | 111 +----------------- .../.dagger/env/kube-aws-basic/.gitignore | 2 + .../.dagger/env/kube-aws-basic/values.yaml | 32 +++++ .../env/kube-aws-deployment/.gitignore | 2 + .../env/kube-aws-deployment/values.yaml | 32 +++++ docs/learn/tests/doc.bats | 24 +++- docs/learn/tests/kube-aws/basic/config.cue | 18 +++ docs/learn/tests/kube-aws/basic/input.cue | 11 ++ docs/learn/tests/kube-aws/basic/test/test.cue | 66 +++++++++++ docs/learn/tests/kube-aws/basic/todoapp.cue | 16 +++ .../tests/kube-aws/deployment/config.cue | 25 ++++ .../learn/tests/kube-aws/deployment/input.cue | 19 +++ .../tests/kube-aws/deployment/test/test.cue | 66 +++++++++++ .../tests/kube-aws/deployment/todoapp.cue | 60 ++++++++++ 14 files changed, 376 insertions(+), 108 deletions(-) create mode 100644 docs/learn/tests/.dagger/env/kube-aws-basic/.gitignore create mode 100644 docs/learn/tests/.dagger/env/kube-aws-basic/values.yaml create mode 100644 docs/learn/tests/.dagger/env/kube-aws-deployment/.gitignore create mode 100644 docs/learn/tests/.dagger/env/kube-aws-deployment/values.yaml create mode 100644 docs/learn/tests/kube-aws/basic/config.cue create mode 100644 docs/learn/tests/kube-aws/basic/input.cue create mode 100644 docs/learn/tests/kube-aws/basic/test/test.cue create mode 100644 docs/learn/tests/kube-aws/basic/todoapp.cue create mode 100644 docs/learn/tests/kube-aws/deployment/config.cue create mode 100644 docs/learn/tests/kube-aws/deployment/input.cue create mode 100644 docs/learn/tests/kube-aws/deployment/test/test.cue create mode 100644 docs/learn/tests/kube-aws/deployment/todoapp.cue diff --git a/docs/learn/1007-kubernetes.md b/docs/learn/1007-kubernetes.md index 46edd96c..9343c123 100644 --- a/docs/learn/1007-kubernetes.md +++ b/docs/learn/1007-kubernetes.md @@ -223,25 +223,7 @@ The below `config.cue` defines: - `eksConfig`, transform a `awsConfig` to a readable format for `kubernetes.#Resources.kubeconfig` using `alpha.dagger.io/aws/eks` -```cue title="todoapp/kube/config.cue" -package main - -import ( - "alpha.dagger.io/aws" - "alpha.dagger.io/aws/eks" -) - -// Value created for generic reference of `kubeconfig` in `todoapp.cue` -kubeconfig: eksConfig.kubeconfig - -// awsConfig for Amazon connection -awsConfig: aws.#Config - -// eksConfig used for deployment -eksConfig: eks.#KubeConfig & { - // config field references `gkeConfig` value to set in once - config: awsConfig -} +```cue file=tests/kube-aws/basic/config.cue title="todoapp/kube/config.cue" ``` @@ -448,7 +430,7 @@ The two files have to be edited to do so. `kube/config.cue` configuration has following change: -- definition of a new `ecrCreds` value that contains ecr credentials for remote image push to GCR +- definition of a new `gcrCreds` value that contains ecr credentials for remote image push to GCR ```cue title="todoapp/kube/config.cue" package main @@ -562,32 +544,7 @@ The two files have to be edited to do so. - definition of a new `ecrCreds` value that contains ecr credentials for remote image push to ECR -```cue title="todoapp/kube/config.cue" -package main - -import ( - "alpha.dagger.io/aws" - "alpha.dagger.io/aws/eks" - "alpha.dagger.io/aws/ecr" -) - -// Value created for generic reference of `kubeconfig` in `todoapp.cue` -kubeconfig: eksConfig.kubeconfig - -// awsConfig for Amazon connection -awsConfig: aws.#Config - -// eksConfig used for deployment -eksConfig: eks.#KubeConfig & { - // config field references `awsConfig` value to set in once - config: awsConfig -} - -// ecrCreds used for remote image push -ecrCreds: ecr.#Credentials & { - // config field references `awsConfig` value to set in once - config: awsConfig -} +```cue file=tests/kube-aws/deployment/config.cue title="todoapp/kube/config.cue" ``` `kube/todoapp.cue`, on the other hand, faces these changes: @@ -598,67 +555,7 @@ ecrCreds: ecr.#Credentials & { - `remoteImage`, push an image to the registry - `kustomization`, apply kustomization to image -```cue title="todoapp/kube/todoapp.cue" -package main - -import ( - "encoding/yaml" - - "alpha.dagger.io/dagger" - "alpha.dagger.io/docker" - "alpha.dagger.io/kubernetes" - "alpha.dagger.io/kubernetes/kustomize" -) - -// input: source code repository, must contain a Dockerfile -// set with `dagger input dir repository . -e kube` -repository: dagger.#Artifact & dagger.#Input - -// ECR registry to push images to -registry: string & dagger.#Input -tag: "test-ecr" - -// source of Kube config file. -// set with `dagger input dir manifest ./k8s -e kube` -manifest: dagger.#Artifact & dagger.#Input - -todoApp: { - // Build an image from the project repository - image: docker.#Build & { - source: repository - } - - // Push the image to a remote registry - remoteImage: docker.#Push & { - target: "\(registry):\(tag)" - source: image - auth: { - username: ecrCreds.username - secret: ecrCreds.secret - } - } - - // Update the image of the deployment to the deployed image - kustomization: kustomize.#Kustomize & { - source: manifest - - // Convert CUE to YAML. - kustomization: yaml.Marshal({ - resources: ["deployment.yaml", "service.yaml"] - - images: [{ - name: "public.ecr.aws/j7f8d3t2/todoapp" - newName: remoteImage.ref - }] - }) - } - - // Value created for generic reference of `kubeconfig` in `todoapp.cue` - kubeSrc: kubernetes.#Resources & { - "kubeconfig": kubeconfig - source: kustomization - } -} +```cue file=tests/kube-aws/deployment/todoapp.cue title="todoapp/kube/todoapp.cue" ``` diff --git a/docs/learn/tests/.dagger/env/kube-aws-basic/.gitignore b/docs/learn/tests/.dagger/env/kube-aws-basic/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-aws-basic/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/docs/learn/tests/.dagger/env/kube-aws-basic/values.yaml b/docs/learn/tests/.dagger/env/kube-aws-basic/values.yaml new file mode 100644 index 00000000..ba31f0f1 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-aws-basic/values.yaml @@ -0,0 +1,32 @@ +plan: + package: ./kube-aws/basic/test +name: kube-aws-basic +inputs: + awsConfig.accessKey: + secret: ENC[AES256_GCM,data:aUxhaAdN44fGz2kKHIl3mdx1vIk=,iv:jgTqZiAwxAtzSjD+IOgjm2rxz43dqtRTcIEuKL3jUDo=,tag:SNU6auwJ/WcXsCc/mrc8sg==,type:str] + awsConfig.region: + text: us-east-2 + awsConfig.secretKey: + secret: ENC[AES256_GCM,data:MAZ/xqZc6kuFO/7EA+degCQ7W7VaJXqseq1+etNSvVNVa+oiPNSnaw==,iv:yGyCKY8S6rwk2EQ7ba3BqirnNyfCtKX+z8EBfLx0WLY=,tag:pOqco/DzRa+lY4LNYmuwYw==,type:str] + eksConfig.clusterName: + text: dagger-example-eks-cluster +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBJZmFKRXhBZmQveFcvcEVH + UmhRS2g0WEdFNFFkZXlLaTZiUHlXSy9IcXdrCmNsTG1pTFVrSDh2NmlpeU1Xa0ZL + aWFMdU1HNytDWUlMK05RMlpIdmtOTDAKLS0tIGpYbUZ6UnBGYzR2SmlOZnE0cWtF + M2loUUEvOGJaRmltTTFmdWduaFMvTkUKtBy9ogOnVqqnoV8A4EBU6j2XPljp9jEz + 25UHK3ecw2SZvKG1u0UcVL0E2KAwUTPzoPJrzRaRKXkSPI8ZXn+OGg== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-08-16T13:31:31Z" + mac: ENC[AES256_GCM,data:9G4o5dpnI0FVvNPz4MbyeNMmop3Umuf5xDrm/JDqfvaFRS2DNVABkczu53y7oPcnlwVWzxdF0Jrul7AizWNCKmw8/NAsPaK9w/6Rff8HnZ2NjoM9rAux8pB5n1GLBjXCMsKcmh1LVmUjzh6WvQSBiCSzzKusdy1UxziP8/ntQJA=,iv:auWG3QOv/ZHMhqAcrdSlZCPmxB7CgrY5IGZFIATDje8=,tag:AL2nHsEVZO/5gO7oKPHpRg==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/docs/learn/tests/.dagger/env/kube-aws-deployment/.gitignore b/docs/learn/tests/.dagger/env/kube-aws-deployment/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-aws-deployment/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/docs/learn/tests/.dagger/env/kube-aws-deployment/values.yaml b/docs/learn/tests/.dagger/env/kube-aws-deployment/values.yaml new file mode 100644 index 00000000..d0770d33 --- /dev/null +++ b/docs/learn/tests/.dagger/env/kube-aws-deployment/values.yaml @@ -0,0 +1,32 @@ +plan: + package: ./kube-aws/deployment/test +name: kube-aws-deployment +inputs: + awsConfig.accessKey: + secret: ENC[AES256_GCM,data:I+FBavqrb2l1jNTLQ9P8x/UnYds=,iv:P5YfvxSbJ1wjPHqZDKOwf2R6AWw1E08C8jLPgjyxvR4=,tag:E6zC01+AkJS+AC1GFTdJGg==,type:str] + awsConfig.region: + text: us-east-2 + awsConfig.secretKey: + secret: ENC[AES256_GCM,data:O8g4xk+xum+gw0mN7H0PYCpmX7FmdgslB8dVY4RWRvUGtHAp5AT/Jw==,iv:LmdJhlL6t7nihr7xDRRPLkd+nWMT9DbNs9pFF1+KazM=,tag:SWlmHyg4/Czn9ygUjv01yg==,type:str] + eksConfig.clusterName: + text: dagger-example-eks-cluster +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBsU0N2bmpTZjh4TjBwRDNP + OG5oM29zZmlhdkV0cWhhbVJZaEdod2hPZTJBCnhoYW9wTnI5ajZSaVcxdVNPSGNT + Q2VvWXZKU0xoZmk4QXJVL011K2t0dU0KLS0tIDVSV001dnhEYkZPSXl5ZFFCb3dC + a2VtRXR0NVZaTWRQSUVWTFdEWUdtazgKhbfhOHL1tdpMiUNgZYUuXo2A8ly54WuE + FJAIoRTFV6Ls5rlW2oI9Ocw+8MdUr/Lbzqtm9ihPEXveOHYE/oZUDA== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-08-16T13:55:15Z" + mac: ENC[AES256_GCM,data:Jlpb72VuYUKvFuCKJRjuNMVxVNCA81EosFpJhheluYcbrO022MMq3rbGdaMRim96N8i0Ux1gMVM/bHumRvJ963DF0i2PGurfaV0QnJiEnu9hBYmNmeL0MV3Qx+ds0+RuoM6gyMgLU4rU/hUZBfVIJqdsvZ5KAUkyQnzgyeQ9ZII=,iv:Jq/ksjU0RKeZnoJruaBkQCR+L1JDRuCTCxf8sNAKVnQ=,tag:RxSXxeCOMIYjdiQXVgaG0g==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/docs/learn/tests/doc.bats b/docs/learn/tests/doc.bats index fbebeb08..6ec13d77 100644 --- a/docs/learn/tests/doc.bats +++ b/docs/learn/tests/doc.bats @@ -80,7 +80,7 @@ setup() { skip_unless_local_kube #################### BASIC #################### - # Copy deployment to sendbox + # Copy deployment to sandbox copy_to_sandbox kube-kind-basic kube-kind # Add kubeconfig @@ -94,8 +94,10 @@ setup() { # Clean kubectl delete deployments --all + kubectl delete services --all #################### DEPLOYMENT #################### + # Copy deployment to sandbox copy_to_sandbox kube-kind-deployment kube-kind # Add kubeconfig @@ -109,8 +111,10 @@ setup() { # Clean kubectl delete deployments --all + kubectl delete services --all #################### CUE MANIFEST #################### + # Copy deployment to sandbox copy_to_sandbox kube-kind-cue-manifest kube-kind # Add kubeconfig @@ -124,6 +128,24 @@ setup() { # Clean kubectl delete deployments --all + kubectl delete services --all +} + +@test "doc-1007-kube-aws" { + #################### BASIC #################### + # Copy deployment to sandbox + copy_to_sandbox kube-aws-basic kube-aws + + # Up deployment + dagger -w "$DAGGER_SANDBOX" -e kube-aws-basic up + + #################### DEPLOYMENT #################### + # Copy deployment to sandbox + copy_to_sandbox kube-aws-deployment kube-aws + + # Up deployment + dagger -w "$DAGGER_SANDBOX" -e kube-aws-deployment up + #################### CUE MANIFEST #################### } @test "doc-1008-aws-cloudformation" { diff --git a/docs/learn/tests/kube-aws/basic/config.cue b/docs/learn/tests/kube-aws/basic/config.cue new file mode 100644 index 00000000..e927e6fe --- /dev/null +++ b/docs/learn/tests/kube-aws/basic/config.cue @@ -0,0 +1,18 @@ +package main + +import ( + "alpha.dagger.io/aws" + "alpha.dagger.io/aws/eks" +) + +// Value created for generic reference of `kubeconfig` in `todoapp.cue` +kubeconfig: eksConfig.kubeconfig + +// awsConfig for Amazon connection +awsConfig: aws.#Config + +// eksConfig used for deployment +eksConfig: eks.#KubeConfig & { + // config field references `gkeConfig` value to set in once + config: awsConfig +} diff --git a/docs/learn/tests/kube-aws/basic/input.cue b/docs/learn/tests/kube-aws/basic/input.cue new file mode 100644 index 00000000..95c4430a --- /dev/null +++ b/docs/learn/tests/kube-aws/basic/input.cue @@ -0,0 +1,11 @@ +package main + +import ( + "alpha.dagger.io/git" +) + +manifest: git.#Repository & { + remote: "https://github.com/dagger/examples.git" + ref: "main" + subdir: "todoapp/k8s" +} diff --git a/docs/learn/tests/kube-aws/basic/test/test.cue b/docs/learn/tests/kube-aws/basic/test/test.cue new file mode 100644 index 00000000..7e5fcecb --- /dev/null +++ b/docs/learn/tests/kube-aws/basic/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.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-aws/basic/todoapp.cue b/docs/learn/tests/kube-aws/basic/todoapp.cue new file mode 100644 index 00000000..878f3f85 --- /dev/null +++ b/docs/learn/tests/kube-aws/basic/todoapp.cue @@ -0,0 +1,16 @@ +package main + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/kubernetes" +) + +// input: kubernetes objects directory to deploy to +// set with `dagger input dir manifest ./k8s -e kube` +manifest: dagger.#Artifact & dagger.#Input + +// Deploy the manifest to a kubernetes cluster +todoApp: kubernetes.#Resources & { + "kubeconfig": kubeconfig + source: manifest +} diff --git a/docs/learn/tests/kube-aws/deployment/config.cue b/docs/learn/tests/kube-aws/deployment/config.cue new file mode 100644 index 00000000..568ad9eb --- /dev/null +++ b/docs/learn/tests/kube-aws/deployment/config.cue @@ -0,0 +1,25 @@ +package main + +import ( + "alpha.dagger.io/aws" + "alpha.dagger.io/aws/eks" + "alpha.dagger.io/aws/ecr" +) + +// Value created for generic reference of `kubeconfig` in `todoapp.cue` +kubeconfig: eksConfig.kubeconfig + +// awsConfig for Amazon connection +awsConfig: aws.#Config + +// eksConfig used for deployment +eksConfig: eks.#KubeConfig & { + // config field references `awsConfig` value to set in once + config: awsConfig +} + +// ecrCreds used for remote image push +ecrCreds: ecr.#Credentials & { + // config field references `awsConfig` value to set in once + config: awsConfig +} diff --git a/docs/learn/tests/kube-aws/deployment/input.cue b/docs/learn/tests/kube-aws/deployment/input.cue new file mode 100644 index 00000000..2fb57750 --- /dev/null +++ b/docs/learn/tests/kube-aws/deployment/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: "125635003186.dkr.ecr.\(awsConfig.region).amazonaws.com/dagger-ci" diff --git a/docs/learn/tests/kube-aws/deployment/test/test.cue b/docs/learn/tests/kube-aws/deployment/test/test.cue new file mode 100644 index 00000000..7e5fcecb --- /dev/null +++ b/docs/learn/tests/kube-aws/deployment/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.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-aws/deployment/todoapp.cue b/docs/learn/tests/kube-aws/deployment/todoapp.cue new file mode 100644 index 00000000..52e4770c --- /dev/null +++ b/docs/learn/tests/kube-aws/deployment/todoapp.cue @@ -0,0 +1,60 @@ +package main + +import ( + "encoding/yaml" + + "alpha.dagger.io/dagger" + "alpha.dagger.io/docker" + "alpha.dagger.io/kubernetes" + "alpha.dagger.io/kubernetes/kustomize" +) + +// input: source code repository, must contain a Dockerfile +// set with `dagger input dir repository . -e kube` +repository: dagger.#Artifact & dagger.#Input + +// ECR registry to push images to +registry: string & dagger.#Input +tag: "test-ecr" + +// source of Kube config file. +// set with `dagger input dir manifest ./k8s -e kube` +manifest: dagger.#Artifact & dagger.#Input + +todoApp: { + // Build an image from the project repository + image: docker.#Build & { + source: repository + } + + // Push the image to a remote registry + remoteImage: docker.#Push & { + target: "\(registry):\(tag)" + source: image + auth: { + username: ecrCreds.username + secret: ecrCreds.secret + } + } + + // Update the image of the deployment to the deployed image + kustomization: kustomize.#Kustomize & { + source: manifest + + // Convert CUE to YAML. + kustomization: yaml.Marshal({ + resources: ["deployment.yaml", "service.yaml"] + + images: [{ + name: "public.ecr.aws/j7f8d3t2/todoapp" + newName: remoteImage.ref + }] + }) + } + + // Value created for generic reference of `kubeconfig` in `todoapp.cue` + kubeSrc: kubernetes.#Resources & { + "kubeconfig": kubeconfig + source: kustomization + } +}