Skip to content

Instantly share code, notes, and snippets.

@colinjfw
Last active December 23, 2021 18:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save colinjfw/58ec321708b761b79c0fd9c33eec8716 to your computer and use it in GitHub Desktop.
Save colinjfw/58ec321708b761b79c0fd9c33eec8716 to your computer and use it in GitHub Desktop.
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": "pr123.example.com"}'
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:
deliverybot.io/release: $release
commonAnnotations:
deliverybot.io/target: $target
deliverybot.io/revision: $revision
images: $images
resources:
- ../overlays/$target
EOF
cat > namespace.yaml <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: $namespace
EOF
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 deliverybot.io/release=$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 deliverybot.io/release=$release
remove_namespace=$(echo $delete | jq 'contains(["namespace"])')
if [ "$remove_namespace" = "true" ]; then
kubectl delete ns/$namespace || exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment