---
#
# Upgrade kubernetes cluster
#
# https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade/
#
# Usage example:
#
# VERSION=1.28.8
# ansible-playbook playbooks/upgrade.yaml --extra-vars "version=$VERSION"
#
- name: Upgrade and hold kubeadm package
  hosts: master,ingress,nfs,worker,gpu
  become: true
  tasks:
    - name: Upgrade packages
      apt:
        name: kubeadm={{ version }}*
        state: present
        force: true
        update_cache: true
    - name: Hold packages
      dpkg_selections:
        name: "{{ item }}"
        selection: hold
      loop:
        - kubeadm

- name: Upgrade k8s master
  hosts: master
  become: true
  tasks:
    - name: Upgrade kubeadm
      command: |
        kubeadm upgrade apply --yes v{{ version }}
      when: true

- name: Upgrade k8s nodes
  hosts: ingress,nfs,worker,gpu
  become: true
  tasks:
    - name: Upgrade kubeadm
      command: |
        kubeadm upgrade node
      when: true

- name: Upgrade and hold packages
  hosts: master,ingress,nfs,worker,gpu
  become: true
  tasks:
    - name: Upgrade packages
      apt:
        name: kubectl={{ version }}*, kubelet={{ version }}*
        state: present
        force: true
        update_cache: true
    - name: Hold packages
      dpkg_selections:
        name: "{{ item }}"
        selection: hold
      loop:
        - kubectl
        - kubelet
    - name: Restart kubelet
      systemd:
        state: restarted
        name: kubelet

# pinned by grycap.kubernetes
# - name: Upgrade networking
#   hosts: master
#   become: true
#   tasks:
#     - name: Upgrade weave
#       shell: |
#         set -o pipefail
#         kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
#       environment:
#         KUBECONFIG: /etc/kubernetes/admin.conf
#       args:
#         executable: /bin/bash
#       when: true
