summaryrefslogtreecommitdiff
path: root/common/kubernetes-cluster-cleanup.yml
blob: 5c2fac45a5155c1b1918199d9b3021f1e9f18476 (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
37
38
39
40
---
- name: check for nodes to be removed
  hosts: _kubernetes_primary_controlplane_node_
  tasks:
  - name: fetch list of current nodes
    command: kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes -o jsonpath='{range .items[*]}{.metadata.name}{":"}{.metadata.labels.ansible\.spreadsapce\.org/inventory_hostname}{"\n"}{end}'
    changed_when: False
    check_mode: no
    register: kubectl_node_list_yaml

  - name: generate group of nodes to be removed
    loop: "{{ kubectl_node_list_yaml.stdout_lines | map('split', ':', 1) | rejectattr('1', 'in', groups['_kubernetes_nodes_']) }}"
    loop_control:
      label: "{{ item[1] }} ({{ item[0] }})"
    add_host:
      name: "{{ item[1] }}"
      inventory_dir: "{{ hostvars[item[1]].inventory_dir }}"
      group: _kubernetes_nodes_prune_
      kubernetes_node_name: "{{ item[0] }}"
    changed_when: False

  - name: get list of bootstrap-tokens
    command: "kubeadm token list --show-managed-fields  -o  jsonpath='{.token};'"
    changed_when: False
    check_mode: no
    register: kubeadm_token_list_json

  - name: delete all bootstrap tokens
    loop: "{{ kubeadm_token_list_json.stdout | split(';') | reject('==', '') }}"
    command: "kubeadm token delete {{ item }}"

- name: prune superflous nodes from cluster
  hosts: _kubernetes_nodes_
  roles:
  - role: kubernetes/kubeadm/prune

- name: wipe superflous nodes
  hosts: _kubernetes_nodes_prune_
  roles:
  - role: kubernetes/kubeadm/reset