Skip to content

Instantly share code, notes, and snippets.

@azr
Last active June 30, 2016 09:18
Show Gist options
  • Save azr/13e72daa9ec968f52d5d to your computer and use it in GitHub Desktop.
Save azr/13e72daa9ec968f52d5d to your computer and use it in GitHub Desktop.
Quickly replace a Google Cloud Kubernetes Cluster by a new one ( with new machine type, scopes and/or number of nodes ), keeping the credentials
#!/bin/bash
CLUSTER=""
PROJECT_CMD=""
ZONE_CMD=""
SCOPES=""
NUM_NODES="1"
MACHINE_TYPE=""
DISK_SIZE="100GB"
# Functions
function usage
{
echo "usage: replace-cluster [ [-h] | --cluster CLUSTER --project PROJECT --machine-type TYPE [--num-nodes X(DEFAULT:1)] [--disk-size 100GB] [--scope SCOPE]... ] "
}
#run !
while [ "$1" != "" ]; do
case $1 in
-c | --cluster ) shift
CLUSTER=$1
;;
-p | --project ) shift
PROJECT_CMD="--project=${1}"
;;
-z | --zone ) shift
ZONE_CMD="--zone=${1}"
;;
--num-nodes ) shift
NUM_NODES="${1}"
;;
--machine-type ) shift
MACHINE_TYPE="${1}"
;;
--disk-size ) shift
DISK_SIZE="${1}"
;;
--scope ) shift
SCOPES="${SCOPES} ${1}"
;;
* ) usage
exit
;;
esac
shift
done
if [ "${CLUSTER}" == "" ]; then
echo "Missing cluster name !"
usage
exit 1
fi
if [ "${MACHINE_TYPE}" == "" ]; then
echo "Missing machine type !"
usage
exit 1
fi
if gcloud compute machine-types list ${PROJECT_CMD} ${ZONE_CMD} | grep -w ${MACHINE_TYPE} > /dev/null ; then
echo "Using machine type ${MACHINE_TYPE}."
else
echo "Machine type ${MACHINE_TYPE} not found."
exit 1
fi
gcloud alpha container clusters get-credentials ${CLUSTER} ${PROJECT_CMD}
JSON_CLUSTER=$(gcloud alpha container clusters describe ${CLUSTER} ${PROJECT_CMD} --format=json)
SCOPES="$( (test -n "${SCOPES}" && echo ${SCOPES} | tr " " ","; echo ${JSON_CLUSTER} | jq -r ".nodeConfig.oauthScopes[] ") | paste -d ',' -s - )"
PASSWORD=$(echo ${JSON_CLUSTER} | jq -r .masterAuth.password)
USERNAME=$(echo ${JSON_CLUSTER} | jq -r .masterAuth.username)
echo "Scopes: " $SCOPES
echo "USERNAME: " $USERNAME
echo "PASSWORD: " $PASSWORD
TMPDIR=`mktemp -d /tmp/kubecfgs.XXXXXX` || exit 1
echo puting conf files in ${TMPDIR}
kubectl get rc --output=yaml > ${TMPDIR}/rc.yml
kubectl get services --output=yaml > ${TMPDIR}/services.yml
gcloud alpha container clusters delete ${PROJECT_CMD} ${ZONE_CMD} ${CLUSTER}
gcloud alpha container clusters create ${CLUSTER} ${PROJECT_CMD} --scopes ${SCOPES} --num-nodes ${NUM_NODES} --machine-type ${MACHINE_TYPE} --username=${USERNAME} --password=${PASSWORD}
gcloud alpha container clusters get-credentials ${CLUSTER} ${PROJECT_CMD}
kubectl create -f ${TMPDIR}/rc.yml
kubectl create -f ${TMPDIR}/services.yml
exit
@azr
Copy link
Author

azr commented Oct 8, 2015

With a bit of luck this keeps master ips.
With a huge bit of luck you can also keep a load balancer ingress ip.
( use at your own risk )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment