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.

344 lines
11 KiB

  1. ---
  2. - name: Calico | Write Calico cni config
  3. template:
  4. src: "cni-calico.conflist.j2"
  5. dest: "/etc/cni/net.d/10-calico.conflist"
  6. owner: kube
  7. - name: Calico | Create calico certs directory
  8. file:
  9. dest: "{{ calico_cert_dir }}"
  10. state: directory
  11. mode: 0750
  12. owner: root
  13. group: root
  14. - name: Calico | Link etcd certificates for calico-node
  15. file:
  16. src: "{{ etcd_cert_dir }}/{{ item.s }}"
  17. dest: "{{ calico_cert_dir }}/{{ item.d }}"
  18. state: hard
  19. force: yes
  20. with_items:
  21. - {s: "ca.pem", d: "ca_cert.crt"}
  22. - {s: "node-{{ inventory_hostname }}.pem", d: "cert.crt"}
  23. - {s: "node-{{ inventory_hostname }}-key.pem", d: "key.pem"}
  24. - name: Calico | Install calicoctl container script
  25. template:
  26. src: calicoctl-container.j2
  27. dest: "{{ bin_dir }}/calicoctl"
  28. mode: 0755
  29. owner: root
  30. group: root
  31. changed_when: false
  32. - name: Calico | Copy cni plugins from hyperkube
  33. command: "{{ docker_bin_dir }}/docker run --rm -v /opt/cni/bin:/cnibindir {{ hyperkube_image_repo }}:{{ hyperkube_image_tag }} /bin/cp -r /opt/cni/bin/. /cnibindir/"
  34. register: cni_task_result
  35. until: cni_task_result.rc == 0
  36. retries: 4
  37. delay: "{{ retry_stagger | random + 3 }}"
  38. changed_when: false
  39. tags:
  40. - hyperkube
  41. - upgrade
  42. - name: Calico | Copy cni plugins from calico/cni container
  43. command: "{{ docker_bin_dir }}/docker run --rm -v /opt/cni/bin:/cnibindir {{ calico_cni_image_repo }}:{{ calico_cni_image_tag }} sh -c 'cp /opt/cni/bin/* /cnibindir/'"
  44. register: cni_task_result
  45. until: cni_task_result.rc == 0
  46. retries: 4
  47. delay: "{{ retry_stagger | random + 3 }}"
  48. changed_when: false
  49. when:
  50. - "overwrite_hyperkube_cni|bool"
  51. tags:
  52. - hyperkube
  53. - upgrade
  54. - name: Calico | Set cni directory permissions
  55. file:
  56. path: /opt/cni/bin
  57. state: directory
  58. owner: kube
  59. recurse: true
  60. mode: 0755
  61. - name: Calico | wait for etcd
  62. uri:
  63. url: "{{ etcd_access_addresses.split(',') | first }}/health"
  64. validate_certs: no
  65. client_cert: "{{ etcd_cert_dir }}/node-{{ inventory_hostname }}.pem"
  66. client_key: "{{ etcd_cert_dir }}/node-{{ inventory_hostname }}-key.pem"
  67. register: result
  68. until: result.status == 200 or result.status == 401
  69. retries: 10
  70. delay: 5
  71. run_once: true
  72. - name: Calico | Check if calico network pool has already been configured
  73. shell: >
  74. {{ bin_dir }}/calicoctl get ippool | grep -w "{{ kube_pods_subnet }}" | wc -l
  75. register: calico_conf
  76. retries: 4
  77. delay: "{{ retry_stagger | random + 3 }}"
  78. delegate_to: "{{ groups['kube-master'][0] }}"
  79. run_once: true
  80. - name: Calico | Configure calico network pool
  81. shell: >
  82. echo "
  83. { "kind": "IPPool",
  84. "apiVersion": "projectcalico.org/v3",
  85. "metadata": {
  86. "name": "{{ calico_pool_name }}",
  87. },
  88. "spec": {
  89. "cidr": "{{ kube_pods_subnet }}",
  90. "ipipMode": "{{ ipip_mode }}",
  91. "natOutgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }} }} " | {{ bin_dir }}/calicoctl create -f -
  92. run_once: true
  93. delegate_to: "{{ groups['kube-master'][0] }}"
  94. when:
  95. - 'calico_conf.stdout == "0"'
  96. - calico_version is version("v3.0.0", ">=")
  97. - name: Calico | Configure calico network pool (legacy)
  98. shell: >
  99. echo '
  100. { "kind": "ipPool",
  101. "spec": {"disabled": false, "ipip": {"enabled": {{ ipip }}, "mode": "{{ ipip_mode|lower }}"},
  102. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  103. "apiVersion": "v1",
  104. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  105. }' | {{ bin_dir }}/calicoctl apply -f -
  106. environment:
  107. NO_DEFAULT_POOLS: true
  108. run_once: true
  109. delegate_to: "{{ groups['kube-master'][0] }}"
  110. when:
  111. - 'calico_conf.stdout == "0"'
  112. - calico_version is version("v3.0.0", "<")
  113. - name: "Determine nodeToNodeMesh needed state"
  114. set_fact:
  115. nodeToNodeMeshEnabled: "false"
  116. when:
  117. - peer_with_router|default(false) or peer_with_calico_rr|default(false)
  118. - inventory_hostname in groups['k8s-cluster']
  119. run_once: yes
  120. - name: Calico | Set global as_num
  121. shell: >
  122. echo '
  123. { "kind": "BGPConfiguration",
  124. "apiVersion": "projectcalico.org/v3",
  125. "metadata": {
  126. "name": "default",
  127. },
  128. "spec": {
  129. "logSeverityScreen": "Info",
  130. "nodeToNodeMeshEnabled": {{ nodeToNodeMeshEnabled|default('true') }} ,
  131. "asNumber": {{ global_as_num }} }} ' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  132. run_once: true
  133. delegate_to: "{{ groups['kube-master'][0] }}"
  134. when:
  135. - calico_version is version('v3.0.0', '>=')
  136. - name: Calico | Set global as_num (legacy)
  137. command: "{{ bin_dir}}/calicoctl config set asNumber {{ global_as_num }}"
  138. run_once: true
  139. when:
  140. - calico_version is version('v3.0.0', '<')
  141. - name: Calico | Disable node mesh (legacy)
  142. command: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  143. run_once: yes
  144. when:
  145. - calico_version is version('v3.0.0', '<')
  146. - nodeToMeshEnabled|default(True)
  147. - name: Calico | Configure node asNumber for per node peering
  148. shell: >
  149. echo '{
  150. "apiVersion": "projectcalico.org/v3",
  151. "kind": "Node",
  152. "metadata": {
  153. "name": "{{ inventory_hostname }}"
  154. },
  155. "spec": {
  156. "bgp": {
  157. "asNumber": "{{ local_as }}"
  158. },
  159. "orchRefs":[{"nodeName":"{{ inventory_hostname }}","orchestrator":"k8s"}]
  160. }}' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  161. retries: 4
  162. delay: "{{ retry_stagger | random + 3 }}"
  163. when:
  164. - calico_version is version('v3.0.0', '>=')
  165. - peer_with_router|default(false)
  166. - inventory_hostname in groups['k8s-cluster']
  167. - local_as is defined
  168. - groups['calico-rr'] | default([]) | length == 0
  169. - name: Calico | Configure node asNumber for per node peering (legacy)
  170. shell: >
  171. echo '{
  172. "apiVersion": "v1",
  173. "kind": "node",
  174. "metadata": {
  175. "name": "{{ inventory_hostname }}"
  176. },
  177. "spec": {
  178. "bgp": {
  179. "asNumber": "{{ local_as }}"
  180. },
  181. "orchRefs":[{"nodeName":"{{ inventory_hostname }}","orchestrator":"k8s"}]
  182. }}' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  183. retries: 4
  184. delay: "{{ retry_stagger | random + 3 }}"
  185. when:
  186. - calico_version is version('v3.0.0', '<')
  187. - peer_with_router|default(false)
  188. - inventory_hostname in groups['k8s-cluster']
  189. - local_as is defined
  190. - groups['calico-rr'] | default([]) | length == 0
  191. - name: Calico | Configure peering with router(s) at node scope
  192. shell: >
  193. echo '{
  194. "apiVersion": "projectcalico.org/v3",
  195. "kind": "BGPPeer",
  196. "metadata": {
  197. "name": "{{ inventory_hostname }}-{{ item.router_id }}"
  198. },
  199. "spec": {
  200. "asNumber": "{{ item.as }}",
  201. "node": "{{ inventory_hostname }}",
  202. "peerIP": "{{ item.router_id }}"
  203. }}' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  204. retries: 4
  205. delay: "{{ retry_stagger | random + 3 }}"
  206. with_items:
  207. - "{{ peers|rejectattr('scope','equalto', 'global')|default([]) }}"
  208. when:
  209. - calico_version is version('v3.0.0', '>=')
  210. - peer_with_router|default(false)
  211. - inventory_hostname in groups['k8s-cluster']
  212. - name: Calico | Configure peering with router(s) at node scope (legacy)
  213. shell: >
  214. echo '{
  215. "kind": "bgpPeer",
  216. "spec": {"asNumber": "{{ item.as }}"},
  217. "apiVersion": "v1",
  218. "metadata": {"node": "{{ inventory_hostname }}", "scope": "node", "peerIP": "{{ item.router_id }}"}
  219. }'
  220. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  221. retries: 4
  222. delay: "{{ retry_stagger | random + 3 }}"
  223. with_items: "{{ peers|rejectattr('scope','equalto', 'global')|default([]) }}"
  224. when:
  225. - calico_version | version_compare('v3.0.0', '<')
  226. - peer_with_router|default(false)
  227. - inventory_hostname in groups['k8s-cluster']
  228. - name: Calico | Configure peering with router(s) at global scope
  229. shell: >
  230. echo '{
  231. "apiVersion": "projectcalico.org/v3",
  232. "kind": "BGPPeer",
  233. "metadata": {
  234. "name": "global-{{ item.router_id }}"
  235. },
  236. "spec": {
  237. "asNumber": "{{ item.as }}",
  238. "peerIP": "{{ item.router_id }}"
  239. }}' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  240. retries: 4
  241. delay: "{{ retry_stagger | random + 3 }}"
  242. with_items:
  243. - "{{ peers|selectattr('scope','equalto', 'global')|default([]) }}"
  244. run_once: true
  245. when:
  246. - calico_version | version_compare('v3.0.0', '>=')
  247. - peer_with_router|default(false)
  248. - inventory_hostname in groups['k8s-cluster']
  249. - name: Calico | Configure peering with router(s) at global scope (legacy)
  250. shell: >
  251. echo '{
  252. "kind": "bgpPeer",
  253. "spec": {"asNumber": "{{ item.as }}"},
  254. "apiVersion": "v1",
  255. "metadata": {"scope": "global", "peerIP": "{{ item.router_id }}"}
  256. }'
  257. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  258. retries: 4
  259. delay: "{{ retry_stagger | random + 3 }}"
  260. with_items: "{{ peers|selectattr('scope','equalto', 'global')|default([]) }}"
  261. run_once: true
  262. when:
  263. - calico_version is version('v3.0.0', '<')
  264. - peer_with_router|default(false)
  265. - inventory_hostname in groups['k8s-cluster']
  266. - name: Calico | Configure peering with route reflectors
  267. shell: >
  268. echo '{
  269. "apiVersion": "projectcalico.org/v3",
  270. "kind": "BGPPeer",
  271. "metadata": {
  272. "name": "{{ inventory_hostname }}-{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4"]["address"]) }}"
  273. },
  274. "spec": {
  275. "asNumber": "{{ local_as | default(global_as_num)}}",
  276. "node": "{{ inventory_hostname }}",
  277. "peerIP": "{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4"]["address"]) }}"
  278. }}' | {{ bin_dir }}/calicoctl create --skip-exists -f -
  279. retries: 4
  280. delay: "{{ retry_stagger | random + 3 }}"
  281. with_items:
  282. - "{{ groups['calico-rr'] | default([]) }}"
  283. when:
  284. - calico_version is version('v3.0.0', '>=')
  285. - peer_with_calico_rr|default(false)
  286. - inventory_hostname in groups['k8s-cluster']
  287. - hostvars[item]['cluster_id'] == cluster_id
  288. - name: Calico | Configure peering with route reflectors (legacy)
  289. shell: >
  290. echo '{
  291. "kind": "bgpPeer",
  292. "spec": {"asNumber": "{{ local_as | default(global_as_num)}}"},
  293. "apiVersion": "v1",
  294. "metadata": {"node": "{{ inventory_hostname }}",
  295. "scope": "node",
  296. "peerIP": "{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4"]["address"]) }}"}
  297. }'
  298. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  299. retries: 4
  300. delay: "{{ retry_stagger | random + 3 }}"
  301. with_items: "{{ groups['calico-rr'] | default([]) }}"
  302. when:
  303. - calico_version is version('v3.0.0', '<')
  304. - not calico_upgrade_enabled
  305. - peer_with_calico_rr|default(false)
  306. - hostvars[item]['cluster_id'] == cluster_id
  307. - name: Calico | Create calico manifests
  308. template:
  309. src: "{{item.file}}.j2"
  310. dest: "{{kube_config_dir}}/{{item.file}}"
  311. with_items:
  312. - {name: calico-config, file: calico-config.yml, type: cm}
  313. - {name: calico-node, file: calico-node.yml, type: ds}
  314. - {name: calico, file: calico-node-sa.yml, type: sa}
  315. - {name: calico, file: calico-cr.yml, type: clusterrole}
  316. - {name: calico, file: calico-crb.yml, type: clusterrolebinding}
  317. register: calico_node_manifests
  318. when:
  319. - inventory_hostname in groups['kube-master']
  320. - rbac_enabled or item.type not in rbac_resources