summaryrefslogtreecommitdiff
path: root/roles/kubernetes/decorations/tasks/labels.yml
blob: fdc5e5c18b28633ba599e5279e0b39aa5cf5ed26 (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
41
42
43
44
---
- name: get list of current labels
  delegate_to: "{{ groups['_kubernetes_primary_controlplane_node_'] | first }}"
  check_mode: no
  command: kubectl --kubeconfig /etc/kubernetes/admin.conf get node '{{ kubernetes_node_name }}' -o jsonpath='{.metadata.labels}'
  changed_when: false
  register: kubectl_list_labels

- set_fact:
    current_labels: "{{ kubectl_list_labels.stdout | ternary(kubectl_list_labels.stdout, '[]') | from_json }}"

- name: add labels to node
  delegate_to: "{{ groups['_kubernetes_primary_controlplane_node_'] | first }}"
  loop: "{{ kubernetes_node_labels | dict2items }}"
  loop_control:
    label: "{{ item.key }}={{ item.value }}"
  when: "item.key not in current_labels or current_labels[item.key] != item.value"
  command: kubectl --kubeconfig /etc/kubernetes/admin.conf label --overwrite node '{{ kubernetes_node_name }}' '{{ item.key }}={{ item.value }}'

- name: add stamp files for managed labels
  loop: "{{ kubernetes_node_labels | dict2items }}"
  loop_control:
    label: "{{ item.key }}={{ item.value }}"
  copy:
    dest: "/etc/kubernetes/decorations/labels/{{ item.key | replace('/', '%') }}"
    content: "{{ item.value }}"

- name: get list of managed labels
  find:
    path: /etc/kubernetes/decorations/labels
  register: managed_labels

- name: remove superflous labels from node
  delegate_to: "{{ groups['_kubernetes_primary_controlplane_node_'] | first }}"
  loop: "{{ managed_labels.files | map(attribute='path') | map('basename') | replace('%', '/') }}"
  when: "item not in kubernetes_node_labels"
  command: kubectl --kubeconfig /etc/kubernetes/admin.conf label --overwrite node '{{ kubernetes_node_name }}' '{{ item }}-'

- name: remove stamp files for superflous labels
  loop: "{{ managed_labels.files | map(attribute='path') | map('basename') | replace('%', '/') }}"
  when: "item not in kubernetes_node_labels"
  file:
    path: "/etc/kubernetes/decorations/labels/{{ item | replace('/', '%') }}"
    state: absent