Add test for kube-gcp deployment step

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-08-17 00:04:54 +02:00 committed by Guillaume de Rouville
parent 2a15682386
commit 1830b34e13
7 changed files with 207 additions and 88 deletions

View File

@ -414,32 +414,7 @@ The two files have to be edited to do so.
- definition of a new `gcrCreds` value that contains ecr credentials for remote image push to GCR
```cue title="todoapp/kube/config.cue"
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
}
```cue file=tests/kube-gcp/deployment/config.cue title="todoapp/kube/config.cue"
```
`kube/todoapp.cue`, on the other hand, faces these changes:
@ -450,68 +425,7 @@ gcrCreds: gcr.#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
// GCR registry to push images to
registry: string & dagger.#Input
tag: "test-gcr"
// source of Kube config file.
// set with `dagger input dir manifest ./k8s -e kube`
manifest: dagger.#Artifact & dagger.#Input
// Declarative name
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: gcrCreds.username
secret: gcrCreds.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-gcp/deployment/todoapp.cue title="todoapp/kube/todoapp.cue"
```
</TabItem>

View File

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

View File

@ -0,0 +1,32 @@
plan:
package: ./kube-gcp/deployment/test
name: kube-gcp-deployment
inputs:
gcpConfig.project:
text: dagger-ci
gcpConfig.region:
text: us-west2-a
gcpConfig.serviceKey:
secret: ENC[AES256_GCM,data:Xbs0AHj7kFU6bfWGFuwQyCGLPhEsbBb42tc7f373Pof9sJ4J3TYnXu/PM2Ru+r/S6xA5jGhSebMAf8WPXuVqj35mOiGjeldRZY4Ilc9HdxD8BCaKvlgGXl2M+itRlANV2dGSQFPwFxp7X4Vz7h+kdAxnRwxa2vmKEQKbUKCXDph6Fc3VU1QhVlZzIFfR30rXHNhF53SoIlGiuD+O9GFYeI/dRJe+LrzXJcZmgo1On/qLGnMLrV9BmOuHsoZLyQHJaPyhLwSxmzrxQ3diwBv/89PGFiSSbq8hzpz8Ko7VQNb2KMb3Ng37q1As4J3R/uQd2EfytesLQ21bqHgXLlLNqyHF1FpWy+YXXuCiAwVR0vBZh9j0/6XL+HQiLx/6cSMTukoQ/EZiw5UTwPqm3B8LGdfzXbd50Ghxt5Il3ZTvR8zK+ZhXe/I5InIiv0ZQKNwnp5n06OpghZDEcBN52cqdj8Y/xaCFzy0rCuRDDn3/vVE8I+4gZncayePCMmhb9Pmod66BG6XHBqs+rRYqX/RmAQAmOzEhqK0+ZRLVtN0ui7HFjm91Pe2fTBrox/zcFSl61oSEd6PvY7L0MG6G7t+34YBXlqjzVYOOgvM1GXzUhl3RNLFWJtoDRk22my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EKEKVd4ajBHwVXhmERYbi0vt+dCkWkcorWtav7QJnsy9gizpNeJIgXCq1X1yPJwTgKuqVkxpRt5aHdeeVJueUBxaSc4SayYIzG/88js/9c+O9lx08zOOMTVDFPvpjFsmgsrXK7RpCXMbhLBBjeyYRM1s8pGdC8LiOPBWEEu1UQ7YjWdV5JFhkJrAV0h3JQS5vL/OB867EPJjANCjj6gmJfEF/wgOn7TZh4U1+jlHZYtZodsV77TKfN+dWk6JSfdQ+2yqrLhMdyRDocouJbh3QrkdQ9NWh+BE8xQzrAgPz+c0w,iv:btPOf+agGb0iUOXUHZVzbFGNMmzWd1Bk0PvM0EjBjXg=,tag:FYKEK3FyM46olIBW8wl9YA==,type:str]
gkeConfig.clusterName:
text: test-cluster
sops:
kms: []
gcp_kms: []
azure_kv: []
hc_vault: []
age:
- recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk
enc: |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSB6d2M5VGZ1Tjhhai90OVBE
K3B1b2ZETldIbjhnbXl2Q1FSU2ROYVUzY1FvCnVyYmtOOGZ0T3ZmTmJRcXZxYjho
SzZGWUlMdXZVVXg1STZJeStudjRQazAKLS0tIFdPeDc0SUU4bk5XdVJXcGRCb2c1
Ni8vY1dCV3UzVkcxWE1CdTVsclVnTVUK1fH9kbr2H75zTCjvTTYr8jN8bF0YrQ9f
ZiSDkc+kgj/WrwFyXX6nbF3eU5wM0EYsD2dVqrrSWKUKz4egrRpS6Q==
-----END AGE ENCRYPTED FILE-----
lastmodified: "2021-08-16T22:01:37Z"
mac: ENC[AES256_GCM,data:Izl+ILzP05dVZ5LcW1NcYyxl/jY+MWurMc277GSy/lrpBlKNJe5pSP0u1b0Ib1g4SX2p/gkXMtUp2gTCM/f6rgpN9prPLJMr/MREj/VVwPuDiLZzr5aPNYHJ19lDMkO5QvFXM6KDVixEs1HUHm/wSSIfO8h/Gk32S6Hp4mjq5+o=,iv:n6p4dXIE4GJ82ty0/XH11H+i44FA/DE1zRsw/fmUJFk=,tag:KWdZNRDh5kutW9tGoc5sJg==,type:str]
pgp: []
encrypted_suffix: secret
version: 3.7.1

View File

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

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: "gcr.io/dagger-ci/test"

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

View File

@ -0,0 +1,61 @@
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
// GCR registry to push images to
registry: string & dagger.#Input
tag: "test-gcr"
// source of Kube config file.
// set with `dagger input dir manifest ./k8s -e kube`
manifest: dagger.#Artifact & dagger.#Input
// Declarative name
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: gcrCreds.username
secret: gcrCreds.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
}
}