summaryrefslogtreecommitdiff
path: root/roles/kubernetes/kubeadm/control-plane/tasks/secondary.yml
blob: a2dbe081a3a5ac49f3724c1c65e2f5f3a7c3c7bc (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
---
- 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
    command: kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes -o name
    changed_when: False
    check_mode: no
    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
    when: "groups['_kubernetes_controlplane_nodes_'] | difference(kubernetes_current_nodes) | length > 0"
    command: kubeadm init phase upload-certs --upload-certs
    check_mode: no
    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: join kubernetes secondary control-plane node and store log
  block:
  - name: join kubernetes secondary control-plane node
    throttle: 1
    command: "kubeadm join 127.0.0.1:6443 --node-name {{ inventory_hostname }} --apiserver-bind-port 6442{% if kubernetes_overlay_node_ip is defined %} --apiserver-advertise-address {{ kubernetes_overlay_node_ip }}{% endif %} --cri-socket {{ kubernetes_cri_socket }} --token '{{ kube_bootstrap_token }}' --discovery-token-ca-cert-hash '{{ kube_bootstrap_ca_cert_hash }}' --control-plane --certificate-key {{ kubeadm_upload_certs_key }}"
    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

  # TODO: acutally check if node has registered
- name: give the new control-plane node(s) a moment to register
  when: kubeadm_join is changed
  pause: # noqa 503
    seconds: 5