From 97cd00332ab2d8196c546bc758b7f3bb40a2b274 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Mon, 6 Sep 2021 15:17:38 +0200 Subject: [PATCH 1/2] Improve kubernetes package to handle deployment from url When I was working on ArgoCD test integration, I found that to expose ArgoCD server, we need to add `ingress` to expose an endpoint. To do it with Kind, it's necessary to apply a custom yaml available on Github. It simply then need `kubectl apply -f ` Signed-off-by: Tom Chauveau --- stdlib/kubernetes/kubernetes.cue | 33 +++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/stdlib/kubernetes/kubernetes.cue b/stdlib/kubernetes/kubernetes.cue index f98838ab..3c1699d2 100644 --- a/stdlib/kubernetes/kubernetes.cue +++ b/stdlib/kubernetes/kubernetes.cue @@ -11,7 +11,7 @@ import ( #Kubectl: { // Kubectl version - version: *"v1.19.9" | string + version: dagger.#Input & {*"v1.19.9" | string} #code: #""" [ -e /usr/local/bin/kubectl ] || { @@ -50,23 +50,35 @@ import ( #Resources: { // Kubernetes config to deploy - source?: dagger.#Artifact @dagger(input) + source: dagger.#Input & {*null | dagger.#Artifact} // Kubernetes manifest to deploy inlined in a string - manifest?: string @dagger(input) + manifest: dagger.#Input & {*null | string} + + // Kubernetes manifest url to deploy remote configuration + url: dagger.#Input & {*null | string} // Kubernetes Namespace to deploy to - namespace: *"default" | string @dagger(input) + namespace: dagger.#Input & {*"default" | string} // Version of kubectl client - version: *"v1.19.9" | string @dagger(input) + version: dagger.#Input & {*"v1.19.9" | string} // Kube config file - kubeconfig: string @dagger(input) + kubeconfig: dagger.#Input & {string} #code: #""" kubectl create namespace "$KUBE_NAMESPACE" > /dev/null 2>&1 || true - kubectl --namespace "$KUBE_NAMESPACE" apply -R -f /source + + if [ -d /source ] || [ -f /source ]; then + kubectl --namespace "$KUBE_NAMESPACE" apply -R -f /source + exit 0 + fi + + if [ -n "$DEPLOYMENT_URL" ]; then + kubectl --namespace "$KUBE_NAMESPACE" apply -R -f "$DEPLOYMENT_URL" + exit 0 + fi """# #up: [ @@ -82,7 +94,7 @@ import ( content: kubeconfig mode: 0o600 }, - if manifest != _|_ { + if manifest != null { op.#WriteFile & { dest: "/source" content: manifest @@ -101,8 +113,11 @@ import ( env: { KUBECONFIG: "/kubeconfig" KUBE_NAMESPACE: namespace + if url != null { + DEPLOYMENT_URL: url + } } - if manifest == _|_ { + if manifest == null && source != null { mount: "/source": from: source } }, From c05f7d50d485dfb9bf4f68d8c3cddd8dc923c217 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Mon, 6 Sep 2021 15:20:19 +0200 Subject: [PATCH 2/2] Update kubernetes testing policy to test deployment from url Signed-off-by: Tom Chauveau --- docs/reference/universe/kubernetes/README.md | 17 +++++++++------ stdlib/kubernetes/tests/kubernetes.cue | 22 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/docs/reference/universe/kubernetes/README.md b/docs/reference/universe/kubernetes/README.md index 9b4f102c..d2f71aa4 100644 --- a/docs/reference/universe/kubernetes/README.md +++ b/docs/reference/universe/kubernetes/README.md @@ -16,7 +16,9 @@ Kubectl client ### kubernetes.#Kubectl Inputs -_No input._ +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*version* | `*"v1.19.9" \| string` |Kubectl version | ### kubernetes.#Kubectl Outputs @@ -28,11 +30,14 @@ Apply Kubernetes resources ### kubernetes.#Resources Inputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*namespace* | `*"default" \| string` |Kubernetes Namespace to deploy to | -|*version* | `*"v1.19.9" \| string` |Version of kubectl client | -|*kubeconfig* | `string` |Kube config file | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*source* | `dagger.#Artifact` |Kubernetes config to deploy | +|*manifest* | `*null \| string` |Kubernetes manifest to deploy inlined in a string | +|*url* | `*null \| string` |Kubernetes manifest url to deploy remote configuration | +|*namespace* | `*"default" \| string` |Kubernetes Namespace to deploy to | +|*version* | `*"v1.19.9" \| string` |Version of kubectl client | +|*kubeconfig* | `string` |Kube config file | ### kubernetes.#Resources Outputs diff --git a/stdlib/kubernetes/tests/kubernetes.cue b/stdlib/kubernetes/tests/kubernetes.cue index ac6bf8ff..73b736f4 100644 --- a/stdlib/kubernetes/tests/kubernetes.cue +++ b/stdlib/kubernetes/tests/kubernetes.cue @@ -29,7 +29,7 @@ TestKubeApply: { } // Apply deployment - apply: #Resources & { + resources: #Resources & { kubeconfig: TestKubeconfig namespace: "dagger-test" manifest: yaml.Marshal(kubeSrc) @@ -38,6 +38,24 @@ TestKubeApply: { // Verify deployment verify: #VerifyApply & { podname: kubeSrc.metadata.name - namespace: apply.namespace + namespace: resources.namespace + } +} + +TestLinkApply: { + // Podname from hello-world-pod + _podname: "hello-world" + + // Apply deployment + resources: #Resources & { + kubeconfig: TestKubeconfig + namespace: "dagger-test" + url: "https://raw.githubusercontent.com/mstrzele/intro-to-k8s/master/hello-world-pod.yaml" + } + + // Verify deployment + verify: #VerifyApply & { + podname: _podname + namespace: resources.namespace } }