From 8480175eac3e7ad48dcfb7a2feb36f4ee5c0ea97 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sat, 16 May 2026 03:20:39 -0400 Subject: [PATCH] test: refactor cluster creation to a shell script Signed-off-by: Xe Iaso --- .tekton/anubis-test.yaml | 30 +++++++++++++++-------- test/k3k/create-cluster.sh | 49 ++++++++++++++++++++++++++++++++++++++ test/ssh-ci/Dockerfile | 2 +- 3 files changed, 70 insertions(+), 11 deletions(-) create mode 100755 test/k3k/create-cluster.sh diff --git a/.tekton/anubis-test.yaml b/.tekton/anubis-test.yaml index 66d20196..ca75e3aa 100644 --- a/.tekton/anubis-test.yaml +++ b/.tekton/anubis-test.yaml @@ -100,15 +100,21 @@ spec: - name: create-cluster image: $(tasks.docker-build-ci.results.IMAGE_URL)@$(tasks.docker-build-ci.results.IMAGE_DIGEST) workingDir: $(workspaces.repo.path)/repo + env: + - name: NAMESPACE + value: $(context.pipelineRun.namespace) + - name: PIPELINE_NAME + value: $(context.pipeline.name) + - name: PIPELINERUN_NAME + value: $(context.pipelineRun.name) + - name: PIPELINERUN_UID + value: $(context.pipelineRun.uid) + - name: KUBECONFIG_OUT + value: $(workspaces.repo.path)/kube/config script: | - CLUSTER_NAME=`kubectl create -f test/k3k/test-cluster.yaml -ojson | jq -r '.metadata.name'` - echo -n "${CLUSTER_NAME}" | tee "$(results.cluster-name.path)" - echo - kubectl label -n $(context.pipelineRun.namespace) clusters.k3k.io/"${CLUSTER_NAME}" tekton.dev/memberOf=tasks tekton.dev/pipeline="$(context.pipeline.name)" tekton.dev/pipelineRun=$(context.pipelineRun.name) tekton.dev/pipelineRunUID=$(context.pipelineRun.uid) - kubectl wait --for=condition=Ready clusters.k3k.io/"${CLUSTER_NAME}" -n $(context.pipelineRun.namespace) --timeout 5m - kubectl wait --for=create "secret/k3k-${CLUSTER_NAME}-kubeconfig" -n $(context.pipelineRun.namespace) --timeout 5m - mkdir -p $(workspaces.repo.path)/kube - kubectl get -ojson -n $(context.pipelineRun.namespace) "secret/k3k-${CLUSTER_NAME}-kubeconfig" | jq '.data["kubeconfig.yaml"]' -r | base64 -d > $(workspaces.repo.path)/kube/config + #!/usr/bin/env bash + set -euo pipefail + ./test/k3k/create-cluster.sh > "$(results.cluster-name.path)" - name: build-assets runAfter: ["docker-build-ci"] taskSpec: @@ -193,8 +199,12 @@ spec: env: - name: KUBECONFIG value: "$(workspaces.repo.path)/kube/config" + finally: - name: teardown-cluster - runAfter: ["provision-test-cluster", "go-test", "integration"] + when: + - input: "$(tasks.provision-test-cluster.status)" + operator: in + values: ["Succeeded"] taskSpec: workspaces: - name: repo @@ -204,4 +214,4 @@ spec: image: $(tasks.docker-build-ci.results.IMAGE_URL)@$(tasks.docker-build-ci.results.IMAGE_DIGEST) workingDir: $(workspaces.repo.path)/repo script: | - kubectl delete -n $(context.pipelineRun.namespace) clusters.k3k.io/"$(tasks.provision-test-cluster.results.cluster-name)" + kubectl delete --ignore-not-found -n $(context.pipelineRun.namespace) clusters.k3k.io/"$(tasks.provision-test-cluster.results.cluster-name)" diff --git a/test/k3k/create-cluster.sh b/test/k3k/create-cluster.sh new file mode 100755 index 00000000..e91ccc6e --- /dev/null +++ b/test/k3k/create-cluster.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# Create a k3k cluster, wait for it to be Ready, and write its kubeconfig. +# Prints the generated cluster name to stdout on success. +# +# Required env: +# NAMESPACE Kubernetes namespace to create the cluster in +# KUBECONFIG_OUT Path to write the resulting kubeconfig +# +# Optional env (set under Tekton to enable ownerReference-based GC + labels): +# PIPELINE_NAME Tekton Pipeline name +# PIPELINERUN_NAME Tekton PipelineRun name +# PIPELINERUN_UID Tekton PipelineRun UID + +set -euo pipefail + +: "${NAMESPACE:?NAMESPACE must be set}" +: "${KUBECONFIG_OUT:?KUBECONFIG_OUT must be set}" + +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) + +cluster_name=$(kubectl create -n "${NAMESPACE}" -f "${script_dir}/test-cluster.yaml" -ojson | jq -r '.metadata.name') + +if [[ -n "${PIPELINERUN_NAME:-}" && -n "${PIPELINERUN_UID:-}" ]]; then + owner_ref=$(jo \ + apiVersion=tekton.dev/v1 \ + kind=PipelineRun \ + name="${PIPELINERUN_NAME}" \ + uid="${PIPELINERUN_UID}" \ + blockOwnerDeletion=false) + patch=$(jo metadata=$(jo "ownerReferences[]=${owner_ref}")) + + kubectl patch -n "${NAMESPACE}" "clusters.k3k.io/${cluster_name}" --type=merge -p "${patch}" >&2 + + kubectl label -n "${NAMESPACE}" "clusters.k3k.io/${cluster_name}" \ + "tekton.dev/memberOf=tasks" \ + "tekton.dev/pipeline=${PIPELINE_NAME:-}" \ + "tekton.dev/pipelineRun=${PIPELINERUN_NAME}" \ + "tekton.dev/pipelineRunUID=${PIPELINERUN_UID}" >&2 +fi + +kubectl wait --for=condition=Ready "clusters.k3k.io/${cluster_name}" -n "${NAMESPACE}" --timeout 5m >&2 +kubectl wait --for=create "secret/k3k-${cluster_name}-kubeconfig" -n "${NAMESPACE}" --timeout 5m >&2 + +mkdir -p "$(dirname "${KUBECONFIG_OUT}")" +kubectl get -ojson -n "${NAMESPACE}" "secret/k3k-${cluster_name}-kubeconfig" \ + | jq -r '.data["kubeconfig.yaml"]' \ + | base64 -d > "${KUBECONFIG_OUT}" + +echo "${cluster_name}" diff --git a/test/ssh-ci/Dockerfile b/test/ssh-ci/Dockerfile index 000dc292..560603e1 100644 --- a/test/ssh-ci/Dockerfile +++ b/test/ssh-ci/Dockerfile @@ -11,5 +11,5 @@ RUN CGO_ENABLED=0 go install golang.org/dl/go1.23.6@latest \ FROM alpine:${ALPINE_VERSION} COPY --from=go /app/bin/go /usr/local/bin/go -RUN apk add -U nodejs git build-base git npm bash zstd brotli gzip jq kubectl python3 py3-pip py3-virtualenv \ +RUN apk add -U nodejs git build-base git npm bash zstd brotli gzip jq jo kubectl python3 py3-pip py3-virtualenv \ && go download \ No newline at end of file