Skip to content

Commit

Permalink
add support for upgrading edge cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrr committed Jan 29, 2024
1 parent 00f25a6 commit 78fce47
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/docker_build_upgrade_k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and Push Docker Upgrade-Edge image

on:
push:
branches:
- main
paths:
- 'upgrade_edge/Dockerfile'
pull_request:
branches:
- main
paths:
- 'upgrade_edge/Dockerfile'
workflow_dispatch:

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:

- name: Checkout
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push management image
uses: docker/build-push-action@v5
with:
context: .
file: ./upgrade_edge/Dockerfile
push: true
tags: ghcr.io/${{ github.repository_owner }}/edgek8s-upgrade:latest
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Steps to perform:

2. Run the ansible-playbook in the docker container

Build the Dockerfile provided locally, this docker image is to be used as a provision image, the dot at the end is important

```docker build -t eficode-academy/edgek8s-provision:latest .```

Note: If talosctl apply-config fails with the error that the defined install_disk doesn't exist, run ```talosctl -n <controlplane_ip> disks --insecure``` to see what disks are available, see docs https://www.talos.dev/v1.6/introduction/getting-started/#modifying-the-machine-configs standard predefined in the playbook is /dev/sda but can differ.

Note: Your host & the host you're deploying talos on needs to be able to reach eachother.
Expand Down
7 changes: 7 additions & 0 deletions upgrade_edge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ghcr.io/eficode-academy/edgek8s-management

COPY upgrade_edge.sh /opt/upgrade_edge.sh

RUN chmod +x /opt/upgrade_edge.sh

CMD ["/opt/upgrade_edge.sh"]
86 changes: 86 additions & 0 deletions upgrade_edge/upgrade_edge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash

KUBECONFIG="/opt/kubeconfig/kubeconfig"
TALOSCONFIG="/opt/talosconfig/talosconfig"
SERVER_IP="192.168.1.109"
#UPGRADE_TALOS="true"
#TALOS_VERSION="v1.5.5"
#UPGRADE_KUBERENTES="true"

if [[ "$UPGRADE_KUBERNETES" == "true" ]] || [[ "$UPGRADE_TALOS" == "true" ]]; then
echo "None of the variables UPGRADE_KUBERNETES or UPGRADE_TALOS has been set to true (true as a string, not a boolean), exiting without any upgrading"
fi

check_talos_version_existence() {
local version_url="https://github.com/siderolabs/talos/releases/tag/${TALOS_VERSION}"
if ! curl --output /dev/null --silent --head --fail "$version_url"; then
echo "Invalid Talos version: ${TALOS_VERSION}."
exit 1
fi
}

# Function to check talosctl version consistency
check_talosctl_version() {


# Get client and server versions
client_version=$(talosctl version -n $SERVER_IP -e $SERVER_IP | grep 'Client:' -A 1 | grep 'Tag:' | awk '{print $2}')
server_version=$(talosctl -e $SERVER_IP -n $SERVER_IP version | grep 'Server:' -A 2 | grep 'Tag:' | awk '{print $2}')


if [ "$client_version" != "$server_version" ]; then
echo "Version mismatch. Downloading server version of talosctl..."

# Formulate download URL
download_url="https://github.com/siderolabs/talos/releases/download/${server_version}/talosctl-linux-amd64"

# Download new version
#wget $download_url -O "talosctl-$server_version"
curl -Lo "talosctl-$server_version" "$download_url"

# Make it executable
chmod +x "talosctl-$server_version"
fi

}

# Upgrade Talos
upgrade_talos() {
echo "Upgrading Talos..."

./talosctl-"${server_version}" upgrade --nodes $SERVER_IP -e $SERVER_IP \
--image ghcr.io/siderolabs/installer:${TALOS_VERSION} --preserve=true --stage
}

get_current_k8s_version() {
local version_string=$(kubectl get nodes -o jsonpath="{.items[0].status.nodeInfo.kubeletVersion}")
echo $version_string

#local current_version=$($version_string)
local major=$(echo $version_string | cut -d. -f1)
local minor=$(echo $version_string | cut -d. -f2)
local patch=$(echo $version_string | cut -d. -f3)
local next_minor=$((minor + 1))
echo "${major}.${next_minor}.${patch}"
NEXT_VERSION="${major}.${next_minor}.${patch}"
}

# Upgrade Kubernetes
upgrade_kubernetes() {

echo "Upgrading Kubernetes to version $NEXT_VERSION..."
./talosctl-"${server_version}" -n $SERVER_IP -e $SERVER_IP upgrade-k8s --to $NEXT_VERSION
}

if [ "$UPGRADE_TALOS" = "true" ] && [ "$UPGRADE_KUBERENTES" = "true" ]; then
echo "Cannot upgrade both Talos and Kubernetes at the same time."
exit 1
elif [ "$UPGRADE_TALOS" = "true" ]; then
check_talos_version_existence
check_talosctl_version
upgrade_talos
elif [ "$UPGRADE_KUBERENTES" = "true" ]; then
check_talosctl_version
get_current_k8s_version
upgrade_kubernetes
fi

0 comments on commit 78fce47

Please sign in to comment.