Last active
November 2, 2020 02:04
-
-
Save jijiechen/4376e37f177c830a32888aacb3d55dce to your computer and use it in GitHub Desktop.
Create a incremented version of Kubernetes deployment for a microservice by exporting the current version deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script depends on the "yq" utility: https://github.com/mikefarah/yq | |
SERVICE_NAME=$1 | |
NEW_IMAGE=$2 | |
LATEST_VERSION=$(kubectl get deployment --selector app=$SERVICE_NAME -o 'jsonpath={.items[*].metadata.labels.version}' | tr " " "\n" | sed 's/^v//' | sort -n -r | tr "\n" " " | cut -d ' ' -f 1) | |
NEXT_VERSION=$((LATEST_VERSION+1)) | |
kubectl get deployment/${SERVICE_NAME}-v${LATEST_VERSION} -o yaml \ | |
| yq delete - 'metadata.resourceVersion' \ | |
| yq delete - 'metadata.uid' \ | |
| yq delete - 'metadata.creationTimestamp' \ | |
| yq delete - 'metadata.selfLink' \ | |
| yq delete - 'metadata.generation' \ | |
| yq delete - 'metadata.managedFields' \ | |
| yq delete - 'metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"' \ | |
| yq delete - 'metadata.annotations."deployment.kubernetes.io/revision"' \ | |
| yq delete - 'status' \ | |
| yq write - 'metadata.name' "${SERVICE_NAME}-v${NEXT_VERSION}" \ | |
| yq write - 'metadata.labels.version' "v${NEXT_VERSION}" \ | |
| yq write - 'metadata.labels."app.kubernetes.io/version"' "v${NEXT_VERSION}" \ | |
| yq write - 'spec.selector.matchLabels.version' "v${NEXT_VERSION}" \ | |
| yq write - 'spec.template.metadata.labels.version' "v${NEXT_VERSION}" \ | |
| yq write - 'spec.template.metadata.labels."app.kubernetes.io/version"' "v${NEXT_VERSION}" \ | |
| yq write - 'spec.template.spec.containers[0].image' "${NEW_IMAGE}" \ | |
| kubectl apply -f - | |
# | yq write - "spec.template.spec.containers(name==${SERVICE_NAME}).image" "${NEW_IMAGE}" # | kubectl apply -f - | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment