You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.3 KiB

  1. ---
  2. - name: Set kubeadm_discovery_address
  3. set_fact:
  4. kubeadm_discovery_address: >-
  5. {%- if "127.0.0.1" in kube_apiserver_endpoint or "localhost" in kube_apiserver_endpoint -%}
  6. {{ first_kube_master }}:{{ kube_apiserver_port }}
  7. {%- else -%}
  8. {{ kube_apiserver_endpoint | replace("https://", "") }}
  9. {%- endif %}
  10. tags:
  11. - facts
  12. - name: Check if kubelet.conf exists
  13. stat:
  14. path: "{{ kube_config_dir }}/kubelet.conf"
  15. register: kubelet_conf
  16. - name: Check if kubeadm CA cert is accessible
  17. stat:
  18. path: "{{ kube_cert_dir }}/ca.crt"
  19. register: kubeadm_ca_stat
  20. delegate_to: "{{ groups['kube-master'][0] }}"
  21. run_once: true
  22. - name: Calculate kubeadm CA cert hash
  23. shell: set -o pipefail && openssl x509 -pubkey -in {{ kube_cert_dir }}/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  24. args:
  25. executable: /bin/bash
  26. register: kubeadm_ca_hash
  27. when:
  28. - kubeadm_ca_stat.stat is defined
  29. - kubeadm_ca_stat.stat.exists
  30. delegate_to: "{{ groups['kube-master'][0] }}"
  31. run_once: true
  32. changed_when: false
  33. - name: Create kubeadm token for joining nodes with 24h expiration (default)
  34. command: "{{ bin_dir }}/kubeadm token create"
  35. register: temp_token
  36. delegate_to: "{{ groups['kube-master'][0] }}"
  37. when: kubeadm_token is not defined
  38. changed_when: false
  39. - name: Set kubeadm_token to generated token
  40. set_fact:
  41. kubeadm_token: "{{ temp_token.stdout }}"
  42. when: kubeadm_token is not defined
  43. - name: Get the kubeadm version
  44. command: "{{ bin_dir }}/kubeadm version -o short"
  45. register: kubeadm_output
  46. changed_when: false
  47. - name: Set kubeadm api version to v1beta2
  48. set_fact:
  49. kubeadmConfig_api_version: v1beta2
  50. - name: Create kubeadm client config
  51. template:
  52. src: "kubeadm-client.conf.{{ kubeadmConfig_api_version }}.j2"
  53. dest: "{{ kube_config_dir }}/kubeadm-client.conf"
  54. backup: yes
  55. when: not is_kube_master
  56. - name: Join to cluster if needed
  57. environment:
  58. PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}:/sbin" # Make sure we can workaround RH / CentOS conservative path management
  59. when: not is_kube_master and (not kubelet_conf.stat.exists)
  60. block:
  61. - name: Join to cluster
  62. command: >-
  63. timeout -k {{ kubeadm_join_timeout }} {{ kubeadm_join_timeout }}
  64. {{ bin_dir }}/kubeadm join
  65. --config {{ kube_config_dir }}/kubeadm-client.conf
  66. --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests
  67. register: kubeadm_join
  68. rescue:
  69. - name: Join to cluster with ignores
  70. command: >-
  71. timeout -k {{ kubeadm_join_timeout }} {{ kubeadm_join_timeout }}
  72. {{ bin_dir }}/kubeadm join
  73. --config {{ kube_config_dir }}/kubeadm-client.conf
  74. --ignore-preflight-errors=all
  75. register: kubeadm_join
  76. always:
  77. - name: Display kubeadm join stderr if any
  78. when: kubeadm_join is failed
  79. debug:
  80. msg: |
  81. Joined with warnings
  82. {{ kubeadm_join.stderr_lines }}
  83. - name: Update server field in kubelet kubeconfig
  84. lineinfile:
  85. dest: "{{ kube_config_dir }}/kubelet.conf"
  86. regexp: 'server:'
  87. line: ' server: {{ kube_apiserver_endpoint }}'
  88. backup: yes
  89. when:
  90. - kubeadm_config_api_fqdn is not defined
  91. - not is_kube_master
  92. - kubeadm_discovery_address != kube_apiserver_endpoint | replace("https://", "")
  93. notify: Kubeadm | restart kubelet
  94. # FIXME(mattymo): Need to point to localhost, otherwise masters will all point
  95. # incorrectly to first master, creating SPoF.
  96. - name: Update server field in kube-proxy kubeconfig
  97. shell: >-
  98. set -o pipefail && {{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf get configmap kube-proxy -n kube-system -o yaml
  99. | sed 's#server:.*#server: https://127.0.0.1:{{ kube_apiserver_port }}#g'
  100. | {{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf replace -f -
  101. args:
  102. executable: /bin/bash
  103. run_once: true
  104. delegate_to: "{{ groups['kube-master']|first }}"
  105. delegate_facts: false
  106. when:
  107. - inventory_hostname in groups['kube-master']
  108. - kubeadm_config_api_fqdn is not defined
  109. - kubeadm_discovery_address != kube_apiserver_endpoint | replace("https://", "")
  110. - kube_proxy_deployed
  111. - loadbalancer_apiserver_localhost
  112. tags:
  113. - kube-proxy
  114. - name: Set ca.crt file permission
  115. file:
  116. path: "{{ kube_cert_dir }}/ca.crt"
  117. owner: root
  118. group: root
  119. mode: "0644"
  120. - name: Restart all kube-proxy pods to ensure that they load the new configmap
  121. command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf delete pod -n kube-system -l k8s-app=kube-proxy --force --grace-period=0"
  122. run_once: true
  123. delegate_to: "{{ groups['kube-master']|first }}"
  124. delegate_facts: false
  125. when:
  126. - inventory_hostname in groups['kube-master']
  127. - kubeadm_config_api_fqdn is not defined
  128. - kubeadm_discovery_address != kube_apiserver_endpoint | replace("https://", "")
  129. - kube_proxy_deployed
  130. tags:
  131. - kube-proxy
  132. - name: Extract etcd certs from control plane if using etcd kubeadm mode
  133. include_tasks: kubeadm_etcd_node.yml
  134. when:
  135. - etcd_kubeadm_enabled
  136. - inventory_hostname not in groups['kube-master']
  137. - kube_network_plugin in ["calico", "flannel", "canal", "cilium"] or cilium_deploy_additionally | default(false) | bool
  138. - kube_network_plugin != "calico" or calico_datastore == "etcd"