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.

503 lines
17 KiB

  1. ---
  2. - name: Calico | Install Wireguard packages
  3. package:
  4. name: "{{ item }}"
  5. state: present
  6. with_items: "{{ calico_wireguard_packages }}"
  7. register: calico_package_install
  8. until: calico_package_install is succeeded
  9. retries: 4
  10. when: calico_wireguard_enabled
  11. - name: Calico | Copy calicoctl binary from download dir
  12. copy:
  13. src: "{{ local_release_dir }}/calicoctl"
  14. dest: "{{ bin_dir }}/calicoctl"
  15. mode: 0755
  16. remote_src: yes
  17. - name: Calico | Write Calico cni config
  18. template:
  19. src: "cni-calico.conflist.j2"
  20. dest: "/etc/cni/net.d/calico.conflist.template"
  21. mode: 0644
  22. owner: root
  23. register: calico_conflist
  24. notify: reset_calico_cni
  25. - name: Calico | Create calico certs directory
  26. file:
  27. dest: "{{ calico_cert_dir }}"
  28. state: directory
  29. mode: 0750
  30. owner: root
  31. group: root
  32. when: calico_datastore == "etcd"
  33. - name: Calico | Link etcd certificates for calico-node
  34. file:
  35. src: "{{ etcd_cert_dir }}/{{ item.s }}"
  36. dest: "{{ calico_cert_dir }}/{{ item.d }}"
  37. state: hard
  38. mode: 0640
  39. force: yes
  40. with_items:
  41. - {s: "{{ kube_etcd_cacert_file }}", d: "ca_cert.crt"}
  42. - {s: "{{ kube_etcd_cert_file }}", d: "cert.crt"}
  43. - {s: "{{ kube_etcd_key_file }}", d: "key.pem"}
  44. when: calico_datastore == "etcd"
  45. - name: Calico | Generate typha certs
  46. include_tasks: typha_certs.yml
  47. when:
  48. - typha_secure
  49. - inventory_hostname == groups['kube_control_plane'][0]
  50. - name: Calico | Generate apiserver certs
  51. include_tasks: calico_apiserver_certs.yml
  52. when:
  53. - calico_apiserver_enabled
  54. - inventory_hostname == groups['kube_control_plane'][0]
  55. - name: Calico | Install calicoctl wrapper script
  56. template:
  57. src: "calicoctl.{{ calico_datastore }}.sh.j2"
  58. dest: "{{ bin_dir }}/calicoctl.sh"
  59. mode: 0755
  60. owner: root
  61. group: root
  62. - name: Calico | wait for etcd
  63. uri:
  64. url: "{{ etcd_access_addresses.split(',') | first }}/health"
  65. validate_certs: no
  66. client_cert: "{{ calico_cert_dir }}/cert.crt"
  67. client_key: "{{ calico_cert_dir }}/key.pem"
  68. register: result
  69. until: result.status == 200 or result.status == 401
  70. retries: 10
  71. delay: 5
  72. run_once: true
  73. when: calico_datastore == "etcd"
  74. - name: Calico | Check if calico network pool has already been configured
  75. # noqa 306 - grep will exit 1 if no match found
  76. shell: >
  77. {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr | default(kube_pods_subnet) }}" | wc -l
  78. args:
  79. executable: /bin/bash
  80. register: calico_conf
  81. retries: 4
  82. until: calico_conf.rc == 0
  83. delay: "{{ retry_stagger | random + 3 }}"
  84. changed_when: false
  85. when:
  86. - inventory_hostname == groups['kube_control_plane'][0]
  87. - name: Calico | Ensure that calico_pool_cidr is within kube_pods_subnet when defined
  88. assert:
  89. that: "[calico_pool_cidr] | ipaddr(kube_pods_subnet) | length == 1"
  90. msg: "{{ calico_pool_cidr }} is not within or equal to {{ kube_pods_subnet }}"
  91. when:
  92. - inventory_hostname == groups['kube_control_plane'][0]
  93. - 'calico_conf.stdout == "0"'
  94. - calico_pool_cidr is defined
  95. - name: Calico | Check if calico IPv6 network pool has already been configured
  96. # noqa 306 - grep will exit 1 if no match found
  97. shell: >
  98. {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}" | wc -l
  99. args:
  100. executable: /bin/bash
  101. register: calico_conf_ipv6
  102. retries: 4
  103. until: calico_conf_ipv6.rc == 0
  104. delay: "{{ retry_stagger | random + 3 }}"
  105. changed_when: false
  106. when:
  107. - inventory_hostname == groups['kube_control_plane'][0]
  108. - enable_dual_stack_networks
  109. - name: Calico | Ensure that calico_pool_cidr_ipv6 is within kube_pods_subnet_ipv6 when defined
  110. assert:
  111. that: "[calico_pool_cidr_ipv6] | ipaddr(kube_pods_subnet_ipv6) | length == 1"
  112. msg: "{{ calico_pool_cidr_ipv6 }} is not within or equal to {{ kube_pods_subnet_ipv6 }}"
  113. when:
  114. - inventory_hostname == groups['kube_control_plane'][0]
  115. - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
  116. - calico_pool_cidr_ipv6 is defined
  117. - enable_dual_stack_networks
  118. - block:
  119. - name: Calico | Check if extra directory is needed
  120. stat:
  121. path: "{{ local_release_dir }}/calico-{{ calico_version }}-kdd-crds/kdd"
  122. register: kdd_path
  123. - name: Calico | Set kdd path
  124. set_fact:
  125. calico_kdd_path: "{{ local_release_dir }}/calico-{{ calico_version }}-kdd-crds{{ '/kdd' if kdd_path.stat.exists is defined and kdd_path.stat.exists }}"
  126. - name: Calico | Create calico manifests for kdd
  127. assemble:
  128. src: "{{ calico_kdd_path }}"
  129. dest: "{{ kube_config_dir }}/kdd-crds.yml"
  130. mode: 0644
  131. delimiter: "---\n"
  132. regexp: ".*\\.yaml"
  133. remote_src: true
  134. - name: Calico | Create Calico Kubernetes datastore resources
  135. kube:
  136. kubectl: "{{ bin_dir }}/kubectl"
  137. filename: "{{ kube_config_dir }}/kdd-crds.yml"
  138. state: "latest"
  139. when:
  140. - inventory_hostname == groups['kube_control_plane'][0]
  141. when:
  142. - inventory_hostname in groups['kube_control_plane']
  143. - calico_datastore == "kdd"
  144. - name: Calico | Configure calico FelixConfiguration
  145. command:
  146. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  147. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  148. vars:
  149. stdin: >
  150. { "kind": "FelixConfiguration",
  151. "apiVersion": "projectcalico.org/v3",
  152. "metadata": {
  153. "name": "default",
  154. },
  155. "spec": {
  156. "ipipEnabled": {{ calico_ipip_mode != 'Never' }},
  157. "reportingInterval": "{{ calico_felix_reporting_interval }}",
  158. "bpfLogLevel": "{{ calico_bpf_log_level }}",
  159. "bpfEnabled": {{ calico_bpf_enabled | bool }},
  160. "bpfExternalServiceMode": "{{ calico_bpf_service_mode }}",
  161. "wireguardEnabled": {{ calico_wireguard_enabled | bool }},
  162. "logSeverityScreen": "{{ calico_felix_log_severity_screen }}",
  163. "vxlanEnabled": {{ calico_vxlan_mode != 'Never' }} }}
  164. when:
  165. - inventory_hostname == groups['kube_control_plane'][0]
  166. - name: Calico | Configure calico network pool
  167. command:
  168. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  169. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  170. vars:
  171. stdin: >
  172. { "kind": "IPPool",
  173. "apiVersion": "projectcalico.org/v3",
  174. "metadata": {
  175. "name": "{{ calico_pool_name }}",
  176. },
  177. "spec": {
  178. "blockSize": {{ calico_pool_blocksize | default(kube_network_node_prefix) }},
  179. "cidr": "{{ calico_pool_cidr | default(kube_pods_subnet) }}",
  180. "ipipMode": "{{ calico_ipip_mode }}",
  181. "vxlanMode": "{{ calico_vxlan_mode }}",
  182. "natOutgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }} }}
  183. when:
  184. - inventory_hostname == groups['kube_control_plane'][0]
  185. - 'calico_conf.stdout == "0"'
  186. - name: Calico | Configure calico ipv6 network pool
  187. command:
  188. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  189. stdin: >
  190. { "kind": "IPPool",
  191. "apiVersion": "projectcalico.org/v3",
  192. "metadata": {
  193. "name": "{{ calico_pool_name }}-ipv6",
  194. },
  195. "spec": {
  196. "blockSize": {{ calico_pool_blocksize_ipv6 | default(kube_network_node_prefix_ipv6) }},
  197. "cidr": "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}",
  198. "ipipMode": "{{ calico_ipip_mode_ipv6 }}",
  199. "vxlanMode": "{{ calico_vxlan_mode_ipv6 }}",
  200. "natOutgoing": {{ nat_outgoing_ipv6|default(false) and not peer_with_router_ipv6|default(false) }} }}
  201. when:
  202. - inventory_hostname == groups['kube_control_plane'][0]
  203. - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
  204. - enable_dual_stack_networks | bool
  205. - name: Populate Service External IPs
  206. set_fact:
  207. _service_external_ips: "{{ _service_external_ips|default([]) + [ {'cidr': item} ] }}"
  208. with_items: "{{ calico_advertise_service_external_ips }}"
  209. run_once: yes
  210. - name: Populate Service LoadBalancer IPs
  211. set_fact:
  212. _service_loadbalancer_ips: "{{ _service_loadbalancer_ips|default([]) + [ {'cidr': item} ] }}"
  213. with_items: "{{ calico_advertise_service_loadbalancer_ips }}"
  214. run_once: yes
  215. - name: "Determine nodeToNodeMesh needed state"
  216. set_fact:
  217. nodeToNodeMeshEnabled: "false"
  218. when:
  219. - peer_with_router|default(false) or peer_with_calico_rr|default(false)
  220. - inventory_hostname in groups['k8s_cluster']
  221. run_once: yes
  222. - name: Calico | Set up BGP Configuration
  223. command:
  224. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  225. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  226. vars:
  227. stdin: >
  228. { "kind": "BGPConfiguration",
  229. "apiVersion": "projectcalico.org/v3",
  230. "metadata": {
  231. "name": "default",
  232. },
  233. "spec": {
  234. "listenPort": {{ calico_bgp_listen_port }},
  235. "logSeverityScreen": "Info",
  236. {% if not calico_no_global_as_num|default(false) %}"asNumber": {{ global_as_num }},{% endif %}
  237. "nodeToNodeMeshEnabled": {{ nodeToNodeMeshEnabled|default('true') }} ,
  238. {% if calico_advertise_cluster_ips|default(false) %}
  239. "serviceClusterIPs": [{"cidr": "{{ kube_service_addresses }}" } {{ ',{"cidr":"' + kube_service_addresses_ipv6 + '"}' if enable_dual_stack_networks else '' }}],{% endif %}
  240. {% if calico_advertise_service_loadbalancer_ips|length > 0 %}"serviceLoadBalancerIPs": {{ _service_loadbalancer_ips }},{% endif %}
  241. "serviceExternalIPs": {{ _service_external_ips|default([]) }} }}
  242. changed_when: false
  243. when:
  244. - inventory_hostname == groups['kube_control_plane'][0]
  245. - name: Calico | Configure peering with router(s) at global scope
  246. command:
  247. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  248. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  249. vars:
  250. stdin: >
  251. {"apiVersion": "projectcalico.org/v3",
  252. "kind": "BGPPeer",
  253. "metadata": {
  254. "name": "global-{{ item.name | default(item.router_id|replace(':','-')) }}"
  255. },
  256. "spec": {
  257. "asNumber": "{{ item.as }}",
  258. "peerIP": "{{ item.router_id }}"
  259. }}
  260. register: output
  261. retries: 4
  262. until: output.rc == 0
  263. delay: "{{ retry_stagger | random + 3 }}"
  264. with_items:
  265. - "{{ peers|selectattr('scope','defined')|selectattr('scope','equalto', 'global')|list|default([]) }}"
  266. when:
  267. - inventory_hostname == groups['kube_control_plane'][0]
  268. - peer_with_router|default(false)
  269. - name: Calico | Configure peering with route reflectors at global scope
  270. command:
  271. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  272. # revert when it's already a string
  273. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  274. vars:
  275. stdin: >
  276. {"apiVersion": "projectcalico.org/v3",
  277. "kind": "BGPPeer",
  278. "metadata": {
  279. "name": "peer-to-rrs"
  280. },
  281. "spec": {
  282. "nodeSelector": "!has(i-am-a-route-reflector)",
  283. "peerSelector": "has(i-am-a-route-reflector)"
  284. }}
  285. register: output
  286. retries: 4
  287. until: output.rc == 0
  288. delay: "{{ retry_stagger | random + 3 }}"
  289. with_items:
  290. - "{{ groups['calico_rr'] | default([]) }}"
  291. when:
  292. - inventory_hostname == groups['kube_control_plane'][0]
  293. - peer_with_calico_rr|default(false)
  294. - name: Calico | Configure route reflectors to peer with each other
  295. command:
  296. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  297. # revert when it's already a string
  298. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  299. vars:
  300. stdin: >
  301. {"apiVersion": "projectcalico.org/v3",
  302. "kind": "BGPPeer",
  303. "metadata": {
  304. "name": "rr-mesh"
  305. },
  306. "spec": {
  307. "nodeSelector": "has(i-am-a-route-reflector)",
  308. "peerSelector": "has(i-am-a-route-reflector)"
  309. }}
  310. register: output
  311. retries: 4
  312. until: output.rc == 0
  313. delay: "{{ retry_stagger | random + 3 }}"
  314. with_items:
  315. - "{{ groups['calico_rr'] | default([]) }}"
  316. when:
  317. - inventory_hostname == groups['kube_control_plane'][0]
  318. - peer_with_calico_rr|default(false)
  319. - name: Calico | Create calico manifests
  320. template:
  321. src: "{{ item.file }}.j2"
  322. dest: "{{ kube_config_dir }}/{{ item.file }}"
  323. mode: 0644
  324. with_items:
  325. - {name: calico-config, file: calico-config.yml, type: cm}
  326. - {name: calico-node, file: calico-node.yml, type: ds}
  327. - {name: calico, file: calico-node-sa.yml, type: sa}
  328. - {name: calico, file: calico-cr.yml, type: clusterrole}
  329. - {name: calico, file: calico-crb.yml, type: clusterrolebinding}
  330. - {name: kubernetes-services-endpoint, file: kubernetes-services-endpoint.yml, type: cm }
  331. register: calico_node_manifests
  332. when:
  333. - inventory_hostname in groups['kube_control_plane']
  334. - rbac_enabled or item.type not in rbac_resources
  335. - name: Calico | Create calico manifests for typha
  336. template:
  337. src: "{{ item.file }}.j2"
  338. dest: "{{ kube_config_dir }}/{{ item.file }}"
  339. mode: 0644
  340. with_items:
  341. - {name: calico, file: calico-typha.yml, type: typha}
  342. register: calico_node_typha_manifest
  343. when:
  344. - inventory_hostname in groups['kube_control_plane']
  345. - typha_enabled
  346. - name: Calico | get calico apiserver caBundle
  347. command: "{{ bin_dir }}/kubectl get secret -n calico-apiserver calico-apiserver-certs -o jsonpath='{.data.apiserver\\.crt}'"
  348. changed_when: false
  349. register: calico_apiserver_cabundle
  350. when:
  351. - inventory_hostname == groups['kube_control_plane'][0]
  352. - calico_apiserver_enabled
  353. - name: Calico | set calico apiserver caBundle fact
  354. set_fact:
  355. calico_apiserver_cabundle: "{{ calico_apiserver_cabundle.stdout }}"
  356. when:
  357. - inventory_hostname == groups['kube_control_plane'][0]
  358. - calico_apiserver_enabled
  359. - name: Calico | Create calico manifests for apiserver
  360. template:
  361. src: "{{ item.file }}.j2"
  362. dest: "{{ kube_config_dir }}/{{ item.file }}"
  363. mode: 0644
  364. with_items:
  365. - {name: calico, file: calico-apiserver.yml, type: calico-apiserver}
  366. register: calico_apiserver_manifest
  367. when:
  368. - inventory_hostname in groups['kube_control_plane']
  369. - calico_apiserver_enabled
  370. - name: Start Calico resources
  371. kube:
  372. name: "{{ item.item.name }}"
  373. namespace: "kube-system"
  374. kubectl: "{{ bin_dir }}/kubectl"
  375. resource: "{{ item.item.type }}"
  376. filename: "{{ kube_config_dir }}/{{ item.item.file }}"
  377. state: "latest"
  378. with_items:
  379. - "{{ calico_node_manifests.results }}"
  380. - "{{ calico_node_typha_manifest.results }}"
  381. when:
  382. - inventory_hostname == groups['kube_control_plane'][0]
  383. - not item is skipped
  384. loop_control:
  385. label: "{{ item.item.file }}"
  386. - name: Start Calico apiserver resources
  387. kube:
  388. name: "{{ item.item.name }}"
  389. namespace: "calico-apiserver"
  390. kubectl: "{{ bin_dir }}/kubectl"
  391. resource: "{{ item.item.type }}"
  392. filename: "{{ kube_config_dir }}/{{ item.item.file }}"
  393. state: "latest"
  394. with_items:
  395. - "{{ calico_apiserver_manifest.results }}"
  396. when:
  397. - inventory_hostname == groups['kube_control_plane'][0]
  398. - not item is skipped
  399. loop_control:
  400. label: "{{ item.item.file }}"
  401. - name: Wait for calico kubeconfig to be created
  402. wait_for:
  403. path: /etc/cni/net.d/calico-kubeconfig
  404. when:
  405. - inventory_hostname not in groups['kube_control_plane']
  406. - calico_datastore == "kdd"
  407. - name: Calico | Configure node asNumber for per node peering
  408. command:
  409. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  410. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  411. vars:
  412. stdin: >
  413. {"apiVersion": "projectcalico.org/v3",
  414. "kind": "Node",
  415. "metadata": {
  416. "name": "{{ inventory_hostname }}"
  417. },
  418. "spec": {
  419. "bgp": {
  420. "asNumber": "{{ local_as }}"
  421. },
  422. "orchRefs":[{"nodeName":"{{ inventory_hostname }}","orchestrator":"k8s"}]
  423. }}
  424. register: output
  425. retries: 4
  426. until: output.rc == 0
  427. delay: "{{ retry_stagger | random + 3 }}"
  428. when:
  429. - peer_with_router|default(false)
  430. - inventory_hostname in groups['k8s_cluster']
  431. - local_as is defined
  432. - groups['calico_rr'] | default([]) | length == 0
  433. - name: Calico | Configure peering with router(s) at node scope
  434. command:
  435. cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
  436. stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  437. vars:
  438. stdin: >
  439. {"apiVersion": "projectcalico.org/v3",
  440. "kind": "BGPPeer",
  441. "metadata": {
  442. "name": "{{ inventory_hostname }}-{{ item.name | default(item.router_id|replace(':','-')) }}"
  443. },
  444. "spec": {
  445. "asNumber": "{{ item.as }}",
  446. "node": "{{ inventory_hostname }}",
  447. "peerIP": "{{ item.router_id }}",
  448. "sourceAddress": "{{ item.sourceaddress|default('UseNodeIP') }}"
  449. }}
  450. register: output
  451. retries: 4
  452. until: output.rc == 0
  453. delay: "{{ retry_stagger | random + 3 }}"
  454. with_items:
  455. - "{{ peers|selectattr('scope','undefined')|list|default([]) | union(peers|selectattr('scope','defined')|selectattr('scope','equalto', 'node')|list|default([])) }}"
  456. when:
  457. - peer_with_router|default(false)
  458. - inventory_hostname == groups['kube_control_plane'][0]
  459. - name: Calico | Configure ipam strictaffinity
  460. command:
  461. cmd: "{{ bin_dir }}/calicoctl.sh ipam configure --strictaffinity={{ calico_ipam_strictaffinity }}"
  462. register: output
  463. retries: 4
  464. until: output.rc == 0
  465. delay: "{{ retry_stagger | random + 3 }}"
  466. when:
  467. - calico_ipam_strictaffinity is defined
  468. - inventory_hostname == groups['kube_control_plane'][0]