naviat
8/1/2018 - 4:40 PM

Update Kubernetes deployment image

Update Kubernetes deployment image

#!/bin/bash
#
# Update docker image tag in a kubernetes deployment

# Kubernetes cluster address
K8S_CLUSTER="change.me"
# Kubernetes cluster certificate path
K8S_CERTIFICATE="/change/me"
# Kubernetes cluster token
K8S_USER="CHANGEME"
# Kubernetes cluster Token
K8S_TOKEN="CHANGEME"

# Docker registry server and credentials
REGISTRY_SERVER="https://change.me:5000"
REGISTRY_USER="CHANGEME"
REGISTRY_PASSWORD="CHANGEME"

# Kubernetes deployment
APP=$1
# Docker image tag
VERSION=$2

echo "\n-- Check if image exists in docker registry"
curl --head --fail --silent --show-error --insecure --basic --user ${REGISTRY_USER}:${REGISTRY_PASSWORD} \
     https://${REGISTRY_SERVER}/v2/${APP}/manifests/${VERSION}

if [ ! -x ./kubectl ]; then
	echo "\n-- Download kubernetes client"
	wget --quiet --output-document=./kubectl https://storage.googleapis.com/kubernetes-release/release/v1.6.3/bin/linux/amd64/kubectl
	chmod +x ./kubectl
fi

echo "\n-- Check if kubernetes client is configured"
./kubectl config get-contexts ${K8S_USER}
if [ $? -ne 0 ]; then
	echo "\n-- Configure kubernetes client"
	./kubectl config set-cluster ${K8S_CLUSTER} \
  		--embed-certs=true \
  		--server=https://api.${K8S_CLUSTER} \
  		--certificate-authority=${K8S_CERTIFICATE}
  
	./kubectl config set-credentials ${K8S_USER} --token=${K8S_TOKEN}

	./kubectl config set-context ${K8S_USER} \
  		--cluster=${K8S_CLUSTER} \
  		--user=${K8S_USER}
  
	./kubectl config use-context ${K8S_USER}
fi

echo "\n-- Update deployment image"
./kubectl set image deployment/${APP}-deployment *=${REGISTRY_SERVER}/${APP}:${VERSION}