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.

193 lines
7.4 KiB

6 years ago
6 years ago
6 years ago
  1. ---
  2. - name: Install OIDC certificate
  3. copy:
  4. content: "{{ kube_oidc_ca_cert | b64decode }}"
  5. dest: "{{ kube_oidc_ca_file }}"
  6. owner: root
  7. group: root
  8. mode: "0644"
  9. when:
  10. - kube_oidc_auth
  11. - kube_oidc_ca_cert is defined
  12. - name: kubeadm | Check if kubeadm has already run
  13. stat:
  14. path: "/var/lib/kubelet/config.yaml"
  15. get_attributes: no
  16. get_checksum: no
  17. get_mime: no
  18. register: kubeadm_already_run
  19. - name: kubeadm | Backup kubeadm certs / kubeconfig
  20. import_tasks: kubeadm-backup.yml
  21. when:
  22. - kubeadm_already_run.stat.exists
  23. - name: kubeadm | aggregate all SANs
  24. set_fact:
  25. apiserver_sans: "{{ (sans_base + groups['kube-master'] + sans_lb + sans_lb_ip + sans_supp + sans_access_ip + sans_ip + sans_address + sans_override + sans_hostname + sans_fqdn) | unique }}"
  26. vars:
  27. sans_base:
  28. - "kubernetes"
  29. - "kubernetes.default"
  30. - "kubernetes.default.svc"
  31. - "kubernetes.default.svc.{{ dns_domain }}"
  32. - "{{ kube_apiserver_ip }}"
  33. - "localhost"
  34. - "127.0.0.1"
  35. sans_lb: "{{ [apiserver_loadbalancer_domain_name] if apiserver_loadbalancer_domain_name is defined else [] }}"
  36. sans_lb_ip: "{{ [loadbalancer_apiserver.address] if loadbalancer_apiserver is defined and loadbalancer_apiserver.address is defined else [] }}"
  37. sans_supp: "{{ supplementary_addresses_in_ssl_keys if supplementary_addresses_in_ssl_keys is defined else [] }}"
  38. sans_access_ip: "{{ groups['kube-master'] | map('extract', hostvars, 'access_ip') | list | select('defined') | list }}"
  39. sans_ip: "{{ groups['kube-master'] | map('extract', hostvars, 'ip') | list | select('defined') | list }}"
  40. sans_address: "{{ groups['kube-master'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list | select('defined') | list }}"
  41. sans_override: "{{ [kube_override_hostname] if kube_override_hostname else [] }}"
  42. sans_hostname: "{{ groups['kube-master'] | map('extract', hostvars, ['ansible_hostname']) | list | select('defined') | list }}"
  43. sans_fqdn: "{{ groups['kube-master'] | map('extract', hostvars, ['ansible_fqdn']) | list | select('defined') | list }}"
  44. tags: facts
  45. - name: Create audit-policy directory
  46. file:
  47. path: "{{ audit_policy_file | dirname }}"
  48. state: directory
  49. when: kubernetes_audit|default(false) or kubernetes_audit_webhook|default(false)
  50. - name: Write api audit policy yaml
  51. template:
  52. src: apiserver-audit-policy.yaml.j2
  53. dest: "{{ audit_policy_file }}"
  54. when: kubernetes_audit|default(false) or kubernetes_audit_webhook|default(false)
  55. - name: Write api audit webhook config yaml
  56. template:
  57. src: apiserver-audit-webhook-config.yaml.j2
  58. dest: "{{ audit_webhook_config_file }}"
  59. when: kubernetes_audit_webhook|default(false)
  60. # Nginx LB(default), If kubeadm_config_api_fqdn is defined, use other LB by kubeadm controlPlaneEndpoint.
  61. - name: set kubeadm_config_api_fqdn define
  62. set_fact:
  63. kubeadm_config_api_fqdn: "{{ apiserver_loadbalancer_domain_name|default('lb-apiserver.kubernetes.local') }}"
  64. when: loadbalancer_apiserver is defined
  65. - name: Set kubeadm api version to v1beta2
  66. set_fact:
  67. kubeadmConfig_api_version: v1beta2
  68. - name: kubeadm | Create kubeadm config
  69. template:
  70. src: "kubeadm-config.{{ kubeadmConfig_api_version }}.yaml.j2"
  71. dest: "{{ kube_config_dir }}/kubeadm-config.yaml"
  72. mode: 0640
  73. - name: kubeadm | Check if apiserver.crt contains all needed SANs
  74. shell: |
  75. set -o pipefail
  76. for IP in {{ apiserver_ips | join(' ') }}; do
  77. openssl x509 -noout -in "{{ kube_cert_dir }}/apiserver.crt" -checkip $IP | grep -q 'does match certificate' || echo 'NEED-RENEW'
  78. done
  79. for HOST in {{ apiserver_hosts | join(' ') }}; do
  80. openssl x509 -noout -in "{{ kube_cert_dir }}/apiserver.crt" -checkhost $HOST | grep -q 'does match certificate' || echo 'NEED-RENEW'
  81. done
  82. vars:
  83. apiserver_ips: "{{ apiserver_sans|map('ipaddr')|reject('equalto', False)|list }}"
  84. apiserver_hosts: "{{ apiserver_sans|difference(apiserver_ips) }}"
  85. args:
  86. executable: /bin/bash
  87. register: apiserver_sans_check
  88. changed_when: "'NEED-RENEW' in apiserver_sans_check.stdout"
  89. when:
  90. - kubeadm_already_run.stat.exists
  91. - name: kubeadm | regenerate apiserver cert 1/2
  92. file:
  93. state: absent
  94. path: "{{ kube_cert_dir }}/{{ item }}"
  95. with_items:
  96. - apiserver.crt
  97. - apiserver.key
  98. when:
  99. - kubeadm_already_run.stat.exists
  100. - apiserver_sans_check.changed
  101. - name: kubeadm | regenerate apiserver cert 2/2
  102. command: >-
  103. {{ bin_dir }}/kubeadm
  104. init phase certs apiserver
  105. --config={{ kube_config_dir }}/kubeadm-config.yaml
  106. when:
  107. - kubeadm_already_run.stat.exists
  108. - apiserver_sans_check.changed
  109. - name: kubeadm | Initialize first master
  110. command: >-
  111. timeout -k 300s 300s
  112. {{ bin_dir }}/kubeadm init
  113. --config={{ kube_config_dir }}/kubeadm-config.yaml
  114. --ignore-preflight-errors=all
  115. --skip-phases={{ kubeadm_init_phases_skip | join(',') }}
  116. --upload-certs
  117. register: kubeadm_init
  118. # Retry is because upload config sometimes fails
  119. retries: 3
  120. until: kubeadm_init is succeeded or "field is immutable" in kubeadm_init.stderr
  121. when: inventory_hostname == groups['kube-master']|first and not kubeadm_already_run.stat.exists
  122. failed_when: kubeadm_init.rc != 0 and "field is immutable" not in kubeadm_init.stderr
  123. environment:
  124. PATH: "{{ bin_dir }}:{{ ansible_env.PATH }}"
  125. notify: Master | restart kubelet
  126. - name: set kubeadm certificate key
  127. set_fact:
  128. kubeadm_certificate_key: "{{ item | regex_search('--certificate-key ([^ ]+)','\\1') | first }}"
  129. with_items: "{{ hostvars[groups['kube-master'][0]]['kubeadm_init'].stdout_lines | default([]) }}"
  130. when:
  131. - kubeadm_certificate_key is not defined
  132. - (item | trim) is match('.*--certificate-key.*')
  133. - name: Create hardcoded kubeadm token for joining nodes with 24h expiration (if defined)
  134. shell: >-
  135. {{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token delete {{ kubeadm_token }} || :;
  136. {{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token create {{ kubeadm_token }}
  137. changed_when: false
  138. when:
  139. - inventory_hostname == groups['kube-master']|first
  140. - kubeadm_token is defined
  141. - kubeadm_refresh_token
  142. tags:
  143. - kubeadm_token
  144. - name: Create kubeadm token for joining nodes with 24h expiration (default)
  145. command: "{{ bin_dir }}/kubeadm --kubeconfig /etc/kubernetes/admin.conf token create"
  146. changed_when: false
  147. register: temp_token
  148. retries: 5
  149. delay: 5
  150. until: temp_token is succeeded
  151. delegate_to: "{{ groups['kube-master'] | first }}"
  152. when: kubeadm_token is not defined
  153. tags:
  154. - kubeadm_token
  155. - name: Set kubeadm_token
  156. set_fact:
  157. kubeadm_token: "{{ temp_token.stdout }}"
  158. when: temp_token.stdout is defined
  159. tags:
  160. - kubeadm_token
  161. - name: kubeadm | Join other masters
  162. include_tasks: kubeadm-secondary.yml
  163. - name: kubeadm | upgrade kubernetes cluster
  164. include_tasks: kubeadm-upgrade.yml
  165. when:
  166. - upgrade_cluster_setup
  167. - kubeadm_already_run.stat.exists
  168. # FIXME(mattymo): from docs: If you don't want to taint your control-plane node, set this field to an empty slice, i.e. `taints: {}` in the YAML file.
  169. - name: kubeadm | Remove taint for master with node role
  170. command: "{{ bin_dir }}/kubectl --kubeconfig {{ kube_config_dir }}/admin.conf taint node {{ inventory_hostname }} node-role.kubernetes.io/master:NoSchedule-"
  171. delegate_to: "{{ groups['kube-master'] | first }}"
  172. when: inventory_hostname in groups['kube-node']
  173. failed_when: false