summaryrefslogtreecommitdiff
path: root/common/kubernetes-cleanup.yml
blob: 83d6945c681132f468f7f72a9a63929c2504fb5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
---
- name: check for nodes to be removed
  hosts: _kubernetes_primary_master_
  tasks:
  - name: fetch list of current nodes
    command: kubectl get nodes -o name
    changed_when: False
    check_mode: no
    register: kubectl_node_list

  - name: generate list of nodes to be removed
    loop: "{{ kubectl_node_list.stdout_lines | map('replace', 'node/', '') | list | difference(groups['_kubernetes_nodes_']) }}"
    add_host:
      name: "{{ item }}"
      inventory_dir: "{{ hostvars[item].inventory_dir }}"
      group: _kubernetes_nodes_remove_
    changed_when: False

  - name: drain superflous nodes
    loop: "{{ groups['_kubernetes_nodes_remove_'] | default([]) }}"
    command: "kubectl drain {{ item }} --delete-local-data --force --ignore-daemonsets"


- name: remove nodes from api server
  hosts: _kubernetes_primary_master_
  tasks:
  - name: remove superflous nodes
    loop: "{{ groups['_kubernetes_nodes_remove_'] | default([]) }}"
    command: "kubectl delete node {{ item }}"

- name: try to clean superflous nodes
  hosts: _kubernetes_nodes_remove_
  roles:
  - role: kubernetes/kubeadm/reset
  - role: kubernetes/net/kubeguard/reset
    when: hostvars[groups['_kubernetes_primary_master_'][0]].kubernetes_network_plugin == 'kubeguard'