Add test for kube-aws basic & deployment

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-08-16 16:34:07 +02:00 committed by Guillaume de Rouville
parent a91181bb5f
commit 5fcaaa6cfe
14 changed files with 376 additions and 108 deletions

View File

@ -223,25 +223,7 @@ The below `config.cue` defines:
- `eksConfig`, transform a `awsConfig` to a readable format for `kubernetes.#Resources.kubeconfig` - `eksConfig`, transform a `awsConfig` to a readable format for `kubernetes.#Resources.kubeconfig`
using `alpha.dagger.io/aws/eks` using `alpha.dagger.io/aws/eks`
```cue title="todoapp/kube/config.cue" ```cue file=tests/kube-aws/basic/config.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
}
``` ```
</TabItem> </TabItem>
@ -448,7 +430,7 @@ The two files have to be edited to do so.
`kube/config.cue` configuration has following change: `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" ```cue title="todoapp/kube/config.cue"
package main 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 - definition of a new `ecrCreds` value that contains ecr credentials for remote image push to ECR
```cue title="todoapp/kube/config.cue" ```cue file=tests/kube-aws/deployment/config.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
}
``` ```
`kube/todoapp.cue`, on the other hand, faces these changes: `kube/todoapp.cue`, on the other hand, faces these changes:
@ -598,67 +555,7 @@ ecrCreds: ecr.#Credentials & {
- `remoteImage`, push an image to the registry - `remoteImage`, push an image to the registry
- `kustomization`, apply kustomization to image - `kustomization`, apply kustomization to image
```cue title="todoapp/kube/todoapp.cue" ```cue file=tests/kube-aws/deployment/todoapp.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
}
}
``` ```
</TabItem> </TabItem>

View File

@ -0,0 +1,2 @@
# dagger state
state/**

View File

@ -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

View File

@ -0,0 +1,2 @@
# dagger state
state/**

View File

@ -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

View File

@ -80,7 +80,7 @@ setup() {
skip_unless_local_kube skip_unless_local_kube
#################### BASIC #################### #################### BASIC ####################
# Copy deployment to sendbox # Copy deployment to sandbox
copy_to_sandbox kube-kind-basic kube-kind copy_to_sandbox kube-kind-basic kube-kind
# Add kubeconfig # Add kubeconfig
@ -94,8 +94,10 @@ setup() {
# Clean # Clean
kubectl delete deployments --all kubectl delete deployments --all
kubectl delete services --all
#################### DEPLOYMENT #################### #################### DEPLOYMENT ####################
# Copy deployment to sandbox
copy_to_sandbox kube-kind-deployment kube-kind copy_to_sandbox kube-kind-deployment kube-kind
# Add kubeconfig # Add kubeconfig
@ -109,8 +111,10 @@ setup() {
# Clean # Clean
kubectl delete deployments --all kubectl delete deployments --all
kubectl delete services --all
#################### CUE MANIFEST #################### #################### CUE MANIFEST ####################
# Copy deployment to sandbox
copy_to_sandbox kube-kind-cue-manifest kube-kind copy_to_sandbox kube-kind-cue-manifest kube-kind
# Add kubeconfig # Add kubeconfig
@ -124,6 +128,24 @@ setup() {
# Clean # Clean
kubectl delete deployments --all 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" { @test "doc-1008-aws-cloudformation" {

View File

@ -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
}

View File

@ -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"
}

View File

@ -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"
},
]
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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"

View File

@ -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"
},
]
}

View File

@ -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
}
}