|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +function gitRepoVersion() { |
| 6 | + # Returns the current git branch name, tag, or commit hash |
| 7 | + echo "$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match 2>/dev/null || git rev-parse HEAD)" |
| 8 | +} |
| 9 | + |
| 10 | +echo "This script will help you upgrade your Kubernetes cluster managed by Kubespray." |
| 11 | + |
| 12 | +# Ask the user for the target Kubernetes version |
| 13 | +read -p "Enter the target Kubernetes version number (e.g., 1.34.0): " VERSION_NUMBER |
| 14 | + |
| 15 | +# Confirm the version number with the user |
| 16 | +echo "You have entered Kubernetes version number: ${VERSION_NUMBER}" |
| 17 | + |
| 18 | +echo "Your current setup is as follows:" |
| 19 | +# Show checkout version of Genestack and the branch being used |
| 20 | +pushd /opt/genestack &>/dev/null |
| 21 | + echo "[+] Current Genestack version: $(gitRepoVersion) (SHA:$(git rev-parse HEAD))" |
| 22 | +popd &>/dev/null |
| 23 | + |
| 24 | +pushd /opt/genestack/submodules/kubespray &>/dev/null |
| 25 | + echo "[+] Current Kubespray version: $(gitRepoVersion) (SHA:$(git rev-parse HEAD))" |
| 26 | +popd &>/dev/null |
| 27 | + |
| 28 | +read -p "Is all of this correct? If yes type \`DOTHETHINGNOW\`: " CONFIRMATION |
| 29 | + |
| 30 | +if [[ "$CONFIRMATION" != "DOTHETHINGNOW" ]]; then |
| 31 | + echo "Aborting. Please run the script again and enter the correct version number and confirmation." |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +set -v |
| 36 | + |
| 37 | +# Load Genestack environment variables |
| 38 | +. /opt/genestack/scripts/genestack.rc |
| 39 | + |
| 40 | +# Navigate to the Kubespray directory and perform the upgrade |
| 41 | +pushd /opt/genestack/submodules/kubespray &>/dev/null |
| 42 | + echo "Gathering cluster facts" |
| 43 | + ansible-playbook playbooks/facts.yml --become |
| 44 | + |
| 45 | + echo "Upgrading cluster to Kubernetes version ${VERSION_NUMBER}" |
| 46 | + ansible-playbook upgrade-cluster.yml --become -e kube_version${VERSION_NUMBER} --limit "kube_control_plane:etcd" |
| 47 | + |
| 48 | + echo "Upgrading worker nodes to Kubernetes version ${VERSION_NUMBER}" |
| 49 | + ansible-playbook upgrade-cluster.yml --become -e kube_version${VERSION_NUMBER} --limit "!kube_control_plane:!etcd" |
| 50 | +popd &>/dev/null |
| 51 | + |
| 52 | +echo "Kubernetes cluster upgrade to version ${VERSION_NUMBER} completed successfully." |
| 53 | + |
| 54 | +if command -v yq &>/dev/null; then |
| 55 | + echo "Updating Kubernetes version in inventory files" |
| 56 | + yq -i ".kube_version = \"${VERSION_NUMBER}\"" /etc/genestack/inventory/group_vars/k8s_cluster/k8s-cluster.yml |
| 57 | +else |
| 58 | + echo "yq command not found. Please install yq to update inventory files." |
| 59 | + echo "update the Kubernetes version in /etc/genestack/inventory/group_vars/k8s_cluster/k8s-cluster.yml manually to complete the upgrade." |
| 60 | +fi |
0 commit comments