Skip to content

chore(k0s-node-upgrade): updated upgrade task #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions ansible/upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
gather_facts: yes

tasks:
- name: Get list of k0s workers hosts
- name: get hosts
local_action:
module: shell
_raw_params: "kubectl --kubeconfig $KUBECONFIG get nodes"
become: false
register: k0s_workers

- name: Descriptions
- name: verbose
ansible.builtin.debug:
msg:
- "The following playbook allows you to upgrade the k0s version on nodes safely and one at a time"
- "Make sure you first update the k0s_version variable value in your inventory."
- "If you are upgrading a k0s controller node check inventory for the desired hostname."
- "The k0s worker nodes in your current cluster are:"

- name: List nodes
- name: nodes
ansible.builtin.debug:
msg:
- "{{ item }}"
with_items: "{{ k0s_workers.stdout_lines }}"

- name: Prompt for target_host
- name: target_host
pause:
prompt: "Specify which node to upgrade:"
echo: yes
Expand All @@ -41,21 +41,32 @@
target_host: "{{ target_host.user_input }}"

# End of inventory on the fly block
- ansible.builtin.debug:
msg:
- "{{ target_host }}"

- name: Drain/cordon node
- ansible.builtin.debug:
msg: "{{ groups }}"

- ansible.builtin.fail:
msg: "Failing play as specified host does not exist in inventory.
Check that you are specifying a valid node and rerun"
when: "'{{ target_host }}' not in groups.k0s"

- name: drain node
local_action:
module: shell
_raw_params: "kubectl --kubeconfig $KUBECONFIG drain {{ target_host }} --force --ignore-daemonsets --delete-emptydir-data"
_raw_params: "kubectl --kubeconfig $KUBECONFIG drain {{ target_host }} --disable-eviction=true --force --ignore-daemonsets --delete-emptydir-data"
become: false
register: node_drain_out

- name: Set k8s_version_upgrade_eviction_seconds fact
- name: set k8s_version_upgrade_eviction_seconds fact
set_fact:
k8s_version_upgrade_eviction_seconds: 45
run_once: true
when: k8s_version_upgrade_eviction_seconds is undefined

- name: Allow some time for the evictions
- name: allow some time for the evictions
pause:
seconds: "{{ k8s_version_upgrade_eviction_seconds }}"

Expand All @@ -73,15 +84,30 @@
tags: download

- hosts: dynamically_created_hosts
name: Start the k0s service
name: start the k0s service
become: yes
tasks:
- shell: k0s start

- name: "Wait for k8s apiserver"
ansible.builtin.wait_for:
host: localhost
port: 6443
timeout: 100
when: inventory_hostname not in groups.worker

- name: Get k0s status on newly upgraded node
command: k0s status
register: k0s_status

- ansible.builtin.debug:
msg: "k0s status: {{ k0s_status.stdout_lines }}"
failed_when: k0s_status.rc != 0

- hosts: localhost
gather_facts: yes
tasks:
- name: Uncordon node
- name: uncordon node
local_action:
module: shell
_raw_params: "kubectl --kubeconfig $KUBECONFIG uncordon {{ target_host }}"
Expand Down
5 changes: 3 additions & 2 deletions taskfiles/hosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ tasks:
cmds:
- ansible-playbook -vv launchpad-core/ansible/install_k0s_cluster.yaml --diff -i {{.INVENTORY_PATH}} {{.CLI_ARGS}}

upgrade-k0s-node:
k0s-node-upgrade:
interactive: true # for interactive scripts
desc: Playbook to upgrade version of k0s on one cluster node at a time
desc: Safely upgrade the version of a k0s node in a cluster
cmds:
- ansible-playbook -vv launchpad-core/ansible/upgrade.yaml --diff -i {{.INVENTORY_PATH}} {{.CLI_ARGS}}

# k0s-sysinfo: # TODO add task for running k0s sysinfo on a host/hosts