summaryrefslogtreecommitdiff
path: root/roles/kubernetes/kubeadm/control-plane/tasks/secondary.yml
blob: 0c7a285fdefa06f51b3ff939aa4e4bdaeb01da0d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
- name: fetch secrets needed for secondary control-plane node
  run_once: true
  delegate_to: "{{ groups['_kubernetes_primary_controlplane_node_'] | first }}"
  block:

  - name: fetch list of current nodes
    check_mode: no
    command: kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes -o name
    changed_when: False
    register: kubectl_node_list

  - name: save list of current nodes
    set_fact:
      kubernetes_current_nodes: "{{ kubectl_node_list.stdout_lines | map('replace', 'node/', '') | list }}"

  - name: upload certs
    check_mode: no
    when: "groups['_kubernetes_controlplane_nodes_'] | map('extract', hostvars, 'kubernetes_node_name') | difference(kubernetes_current_nodes) | length > 0"
    command: kubeadm init phase upload-certs --upload-certs
    register: kubeadm_upload_certs

- name: extracting encryption key for certs
  set_fact:
    kubeadm_upload_certs_key: "{% if kubeadm_upload_certs.stdout is defined %}{{ kubeadm_upload_certs.stdout_lines | last }}{% endif %}"


- name: generate kubeadm.config
  template:
    src: kubeadm-join.config.j2
    dest: /etc/kubernetes/kubeadm.config
    mode: 0600
  changed_when: False

- name: join kubernetes secondary control-plane node and store log
  block:
  - name: join kubernetes secondary control-plane node
    throttle: 1
    command: "kubeadm join --config /etc/kubernetes/kubeadm.config"
    args:
      creates: /etc/kubernetes/kubelet.conf
    register: kubeadm_join

  always:
  - name: dump output of kubeadm join to log file
    when: kubeadm_join is changed
    # This is not a handler by design to make sure this action runs at this point of the play.
    copy: # noqa 503
      content: "{{ kubeadm_join.stdout }}\n"
      dest: /etc/kubernetes/kubeadm-join.log

  - name: dump error output of kubeadm join to log file
    when: kubeadm_join.changed and kubeadm_join.stderr
    copy:
      content: "{{ kubeadm_join.stderr }}\n"
      dest: /etc/kubernetes/kubeadm-join.errors

- name: wait for new control-plane node to register
  delegate_to: "{{ groups['_kubernetes_primary_controlplane_node_'] | first }}"
  check_mode: no
  command: "kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes -o name {{ kubernetes_node_name }}"
  changed_when: False
  register: kubectl_node_get
  until: kubectl_node_get.rc == 0
  retries: 30
  dely: 2