From ad7d3b65573396c8f9b5ca04447398fe3c89e3e6 Mon Sep 17 00:00:00 2001 From: Michael Kebe <michael.kebe@gmail.com> Date: Sat, 1 Feb 2025 03:39:36 +0000 Subject: [PATCH] feat(kustomize): add TBC envsubst support for Kustomize based deployment --- README.md | 9 +++------ templates/gitlab-ci-k8s.yml | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8348072..4994d3c 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ The Kubernetes template supports three techniques to deploy your code: 1. script-based deployment, 2. template-based deployment using raw Kubernetes manifests (with [variables substitution](#variables-substitution-mechanism)), -3. template-based deployment using [Kustomization files](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/). +3. template-based deployment using [Kustomization files](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/) (with [variables substitution](#variables-substitution-mechanism)). #### 1: script-based deployment @@ -182,15 +182,12 @@ in your project structure, and let the template [`kubectl apply`](https://kubern The template processes the following steps: 1. _optionally_ executes the `k8s-pre-apply.sh` script in your project to perform specific environment pre-initialization (for e.g. create required services), -2. looks for your Kustomization file, performs variables substitution and [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it, +2. looks for your Kustomization file, performs [variables substitution](#variables-substitution-mechanism), generates the manifests with [`kubectl kustomize`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#kustomize) and [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it, 1. looks for an environment-specific [overlay](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay) file `./$environment_type/kustomization.yaml` (e.g. `./staging/kustomization.yaml ` for staging environment), 2. fallbacks to default `kustomization.yaml`. 3. _optionally_ executes the `k8s-post-apply.sh` script in your project to perform specific environment post-initialization stuff, :warning: `k8s-pre-apply.sh` or `k8s-post-apply.sh` needs to be executable, you can add flag execution with: `git update-index --chmod=+x k8s-pre-apply.sh` - -Variables substitution is performed by the deprecated feature from Kustomize based on `configMapGenerator`, using a non-valuated variable from a config map. - #### Readiness script After deployment (either script-based or template-based), the GitLab CI template _optionally_ executes the `k8s-readiness-check.sh` hook script to wait & check for the application to be ready (if not found, the template assumes the application was successfully started). @@ -228,7 +225,7 @@ In this mode, you mainly let Kubernetes delete all objects from your Kubernetes The template processes the following steps: 1. _optionally_ executes the `k8s-pre-cleanup.sh` script in your project to perform specific environment pre-cleanup stuff, -2. looks for your Kubernetes deployment file, performs [variables substitution](#using-variables) and [`kubectl delete`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it, +2. looks for your Kubernetes deployment file, performs [variables substitution](#variables-substitution-mechanism) and [`kubectl delete`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it, 1. look for a specific `deployment-$environment_type.yml` in your project (e.g. `deployment-staging.yml` for staging environment), 2. fallbacks to default `deployment.yml`. 3. _optionally_ executes the `k8s-post-cleanup.sh` script in your project to perform specific environment post-cleanup (for e.g. delete bound services). diff --git a/templates/gitlab-ci-k8s.yml b/templates/gitlab-ci-k8s.yml index dd47592..24924ec 100644 --- a/templates/gitlab-ci-k8s.yml +++ b/templates/gitlab-ci-k8s.yml @@ -593,10 +593,16 @@ stages: fi deploymentdir=$(dirname "$kustofile") + # variables substitution + tbc_envsubst "$kustofile" > generated-kustomization.yml + # overwrite kustomization file with substitued variables + mv generated-kustomization.yml "$kustofile" + # apply/delete deployment descriptor log_info "--- \\e[32mkustomize\\e[0m" # shellcheck disable=SC2086 - kubectl kustomize "$deploymentdir" ${K8S_KUSTOMIZE_ARGS} > ./generated-deployment.yml + kubectl kustomize "$deploymentdir" ${K8S_KUSTOMIZE_ARGS} > generated-deployment.yml + log_info "--- \\e[32mkubectl $action\\e[0m" kubectl ${TRACE+-v=5} "$action" -f ./generated-deployment.yml else @@ -785,7 +791,7 @@ stages: log_info "--- \\e[32mpre-cleanup hook\\e[0m (\\e[33;1m${prescript}\\e[0m) not found: skip" fi - # has to be valuated for envsubst + # has to be valuated for tbc_envsubst export hostname=hostname do_kubectl delete @@ -813,8 +819,18 @@ stages: fi deploymentdir=$(dirname "$kustofile") + # variables substitution + tbc_envsubst "$kustofile" > generated-kustomization.yml + # overwrite kustomization file with substitued variables + mv generated-kustomization.yml "$kustofile" + + # apply/delete deployment descriptor + log_info "--- \\e[32mkustomize\\e[0m" # shellcheck disable=SC2086 - kustomize build ${K8S_KUSTOMIZE_ARGS} "${deploymentdir}" | /usr/bin/kube-score score $K8S_SCORE_EXTRA_OPTS - + kustomize build ${K8S_KUSTOMIZE_ARGS} "$deploymentdir" > generated-deployment.yml + + # shellcheck disable=SC2086 + /usr/bin/kube-score score $K8S_SCORE_EXTRA_OPTS generated-deployment.yml else # find deployment file deploymentfile=$(ls -1 "$K8S_SCRIPTS_DIR/deployment-${environment_type}.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/deployment.yml" 2>/dev/null || echo "") -- GitLab