Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Kubernetes deployment strategy using Kustomize and a few basic tools. Replaces helm charts with simple tooling solutions.
# Kustomize based apply workflow. Requires jq, yq, kubectl, kustomize, mustache.
# Expected variables
# - namespace Namespace for all resources.
# - release A unique name to give to this collection of manifests.
# - revision Release revision.
# - images Image replacements.
# - variables Variable replacements.
# Example inputs:
# namespace=pr123
# release=app-pr123
# target=review
# revision=ab331a
# images='[{ "name": "nginx", "newTag": "latest" }]'
# variables='{"host": ""}'
set -e
echo "deploying $release into namespace $namespace using overlay overlays/$target"
mkdir -p build
cd build
cat > kustomization.yaml <<EOF
namespace: $namespace
namePrefix: $release-
commonLabels: $release
commonAnnotations: $target $revision
images: $images
- ../overlays/$target
cat > namespace.yaml <<EOF
apiVersion: v1
kind: Namespace
name: $namespace
echo $variables > variables.yaml
yq --yaml-output -s add ../overlays/$target/variables.yaml variables.yaml > merged.yaml
kustomize build | mustache merged.yaml - > build.yaml
kubectl apply -f namespace.yaml
kubectl apply -f build.yaml --prune --selector$release --prune-whitelist=core/v1/Namespace
# Kustomize based deletion workflow. Requires jq, kubectl.
# Expected variables:
# - namespace Namespace for the release.
# -
# - release A unique name to give to this collection of manifests.
# - delete Resource delete whitelist.
# Example inputs:
# namespace=pr123
# release=app-pr123
# delete='["deployment", "service", "ingress", "namespace"]'
set -e
echo "removing $release with resources $delete"
resources=$(echo $delete | jq -r 'join(",")')
kubectl --namespace $namespace delete $resources --selector$release
remove_namespace=$(echo $delete | jq 'contains(["namespace"])')
if [ "$remove_namespace" = "true" ]; then
kubectl delete ns/$namespace || exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment