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.

261 lines
9.0 KiB

9 years ago
9 years ago
9 years ago
  1. ---
  2. - name: Calico | Check calicoctl version
  3. run_once: true
  4. set_fact:
  5. legacy_calicoctl: "{{ calicoctl_image_tag | version_compare('v1.0.0', '<') }}"
  6. tags: facts
  7. - name: Calico | Write Calico cni config
  8. template:
  9. src: "cni-calico.conf.j2"
  10. dest: "/etc/cni/net.d/10-calico.conf"
  11. owner: kube
  12. - name: Calico | Create calico certs directory
  13. file:
  14. dest: "{{ calico_cert_dir }}"
  15. state: directory
  16. mode: 0750
  17. owner: root
  18. group: root
  19. - name: Calico | Link etcd certificates for calico-node
  20. file:
  21. src: "{{ etcd_cert_dir }}/{{ item.s }}"
  22. dest: "{{ calico_cert_dir }}/{{ item.d }}"
  23. state: hard
  24. force: yes
  25. with_items:
  26. - {s: "ca.pem", d: "ca_cert.crt"}
  27. - {s: "node-{{ inventory_hostname }}.pem", d: "cert.crt"}
  28. - {s: "node-{{ inventory_hostname }}-key.pem", d: "key.pem"}
  29. - name: Calico | Install calicoctl container script
  30. template:
  31. src: calicoctl-container.j2
  32. dest: "{{ bin_dir }}/calicoctl"
  33. mode: 0755
  34. owner: root
  35. group: root
  36. changed_when: false
  37. notify: restart calico-node
  38. - name: Calico | Copy cni plugins from hyperkube
  39. command: "{{ docker_bin_dir }}/docker run --rm -v /opt/cni/bin:/cnibindir {{ hyperkube_image_repo }}:{{ hyperkube_image_tag }} /usr/bin/rsync -ac /opt/cni/bin/ /cnibindir/"
  40. register: cni_task_result
  41. until: cni_task_result.rc == 0
  42. retries: 4
  43. delay: "{{ retry_stagger | random + 3 }}"
  44. changed_when: false
  45. tags: [hyperkube, upgrade]
  46. - name: Calico | Copy cni plugins from calico/cni container
  47. command: "{{ docker_bin_dir }}/docker run --rm -v /opt/cni/bin:/cnibindir {{ calico_cni_image_repo }}:{{ calico_cni_image_tag }} sh -c 'cp -a /opt/cni/bin/* /cnibindir/'"
  48. register: cni_task_result
  49. until: cni_task_result.rc == 0
  50. retries: 4
  51. delay: "{{ retry_stagger | random + 3 }}"
  52. changed_when: false
  53. when: overwrite_hyperkube_cni|bool
  54. tags: [hyperkube, upgrade]
  55. - name: Calico | Set cni directory permissions
  56. file:
  57. path: /opt/cni/bin
  58. state: directory
  59. owner: kube
  60. recurse: true
  61. mode: 0755
  62. - name: Calico | wait for etcd
  63. uri:
  64. url: https://localhost:2379/health
  65. validate_certs: no
  66. register: result
  67. until: result.status == 200 or result.status == 401
  68. retries: 10
  69. delay: 5
  70. delegate_to: "{{groups['etcd'][0]}}"
  71. run_once: true
  72. - name: Calico | Check if calico network pool has already been configured
  73. command: |-
  74. curl \
  75. --cacert {{ etcd_cert_dir }}/ca.pem \
  76. --cert {{ etcd_cert_dir}}/admin-{{ groups['etcd'][0] }}.pem \
  77. --key {{ etcd_cert_dir }}/admin-{{ groups['etcd'][0] }}-key.pem \
  78. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  79. register: calico_conf
  80. delegate_to: "{{groups['etcd'][0]}}"
  81. run_once: true
  82. changed_when: false
  83. - name: Calico | Configure calico network pool
  84. shell: >
  85. echo '{
  86. "kind": "ipPool",
  87. "spec": {"disabled": false, "ipip": {"enabled": {{ cloud_provider is defined or ipip }}},
  88. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  89. "apiVersion": "v1",
  90. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  91. }'
  92. | {{ bin_dir }}/calicoctl create -f -
  93. environment:
  94. NO_DEFAULT_POOLS: true
  95. run_once: true
  96. when: not legacy_calicoctl and
  97. ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  98. - name: Calico (old) | Define ipip pool argument
  99. run_once: true
  100. set_fact:
  101. ipip_arg: "--ipip"
  102. when: (legacy_calicoctl and
  103. cloud_provider is defined or ipip)
  104. tags: facts
  105. - name: Calico (old) | Define nat-outgoing pool argument
  106. run_once: true
  107. set_fact:
  108. nat_arg: "--nat-outgoing"
  109. when: (legacy_calicoctl and
  110. nat_outgoing|default(false) and not peer_with_router|default(false))
  111. tags: facts
  112. - name: Calico (old) | Define calico pool task name
  113. run_once: true
  114. set_fact:
  115. pool_task_name: "with options {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  116. when: (legacy_calicoctl and ipip_arg|default(false) or nat_arg|default(false))
  117. tags: facts
  118. - name: Calico (old) | Configure calico network pool {{ pool_task_name|default('') }}
  119. command: "{{ bin_dir}}/calicoctl pool add {{ kube_pods_subnet }} {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  120. environment:
  121. NO_DEFAULT_POOLS: true
  122. run_once: true
  123. when: legacy_calicoctl and
  124. ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  125. - name: Calico | Get calico configuration from etcd
  126. command: |-
  127. curl \
  128. --cacert {{ etcd_cert_dir }}/ca.pem \
  129. --cert {{ etcd_cert_dir}}/admin-{{ groups['etcd'][0] }}.pem \
  130. --key {{ etcd_cert_dir }}/admin-{{ groups['etcd'][0] }}-key.pem \
  131. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  132. register: calico_pools_raw
  133. delegate_to: "{{groups['etcd'][0]}}"
  134. run_once: true
  135. - set_fact:
  136. calico_pools: "{{ calico_pools_raw.stdout | from_json }}"
  137. run_once: true
  138. - name: Calico | Check if calico pool is properly configured
  139. fail:
  140. msg: 'Only one network pool must be configured and it must be the subnet {{ kube_pods_subnet }}.
  141. Please erase calico configuration and run the playbook again ("etcdctl rm --recursive /calico/v1/ipam/v4/pool")'
  142. when: ( calico_pools['node']['nodes'] | length > 1 ) or
  143. ( not calico_pools['node']['nodes'][0]['key'] | search(".*{{ kube_pods_subnet | ipaddr('network') }}.*") )
  144. run_once: true
  145. - name: Calico | Set global as_num
  146. command: "{{ bin_dir}}/calicoctl config set asNumber {{ global_as_num }}"
  147. run_once: true
  148. when: not legacy_calicoctl
  149. - name: Calico (old) | Set global as_num
  150. command: "{{ bin_dir}}/calicoctl bgp default-node-as {{ global_as_num }}"
  151. run_once: true
  152. when: legacy_calicoctl
  153. - name: Calico (old) | Write calico-node systemd init file
  154. template:
  155. src: calico-node.service.legacy.j2
  156. dest: /etc/systemd/system/calico-node.service
  157. when: legacy_calicoctl
  158. notify: restart calico-node
  159. - name: Calico | Write calico.env for systemd init file
  160. template:
  161. src: calico.env.j2
  162. dest: /etc/calico/calico.env
  163. when: not legacy_calicoctl
  164. notify: restart calico-node
  165. - name: Calico | Write calico-node systemd init file
  166. template:
  167. src: calico-node.service.j2
  168. dest: /etc/systemd/system/calico-node.service
  169. when: not legacy_calicoctl
  170. notify: restart calico-node
  171. - name: Calico | Restart calico-node if secrets changed
  172. command: /bin/true
  173. when: secret_changed|default(false) or etcd_secret_changed|default(false)
  174. notify: restart calico-node
  175. - meta: flush_handlers
  176. - name: Calico | Enable calico-node
  177. service:
  178. name: calico-node
  179. state: started
  180. enabled: yes
  181. - name: Calico | Disable node mesh
  182. shell: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  183. when: ((peer_with_router|default(false) or peer_with_calico_rr|default(false))
  184. and inventory_hostname in groups['k8s-cluster']
  185. and not legacy_calicoctl)
  186. run_once: true
  187. - name: Calico | Configure peering with router(s)
  188. shell: >
  189. echo '{
  190. "kind": "bgpPeer",
  191. "spec": {"asNumber": "{{ item.as }}"},
  192. "apiVersion": "v1",
  193. "metadata": {"node": "{{ inventory_hostname }}", "scope": "node", "peerIP": "{{ item.router_id }}"}
  194. }'
  195. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  196. with_items: "{{ peers|default([]) }}"
  197. when: (not legacy_calicoctl and
  198. peer_with_router|default(false) and inventory_hostname in groups['k8s-cluster'])
  199. - name: Calico | Configure peering with route reflectors
  200. shell: >
  201. echo '{
  202. "kind": "bgpPeer",
  203. "spec": {"asNumber": "{{ local_as | default(global_as_num)}}"},
  204. "apiVersion": "v1",
  205. "metadata": {"node": "{{ inventory_hostname }}",
  206. "scope": "node",
  207. "peerIP": "{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4.address"]) }}"}
  208. }'
  209. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  210. with_items: "{{ groups['calico-rr'] | default([]) }}"
  211. when: (not legacy_calicoctl and
  212. peer_with_calico_rr|default(false) and inventory_hostname in groups['k8s-cluster']
  213. and hostvars[item]['cluster_id'] == cluster_id)
  214. - name: Calico (old) | Disable node mesh
  215. shell: "{{ bin_dir }}/calicoctl bgp node-mesh off"
  216. when: ((peer_with_router|default(false) or peer_with_calico_rr|default(false))
  217. and inventory_hostname in groups['k8s-cluster']
  218. and legacy_calicoctl)
  219. run_once: true
  220. - name: Calico (old) | Configure peering with router(s)
  221. shell: "{{ bin_dir }}/calicoctl node bgp peer add {{ item.router_id }} as {{ item.as }}"
  222. with_items: "{{ peers|default([]) }}"
  223. when: (legacy_calicoctl and
  224. peer_with_router|default(false) and inventory_hostname in groups['k8s-cluster'])
  225. - name: Calico (old) | Configure peering with route reflectors
  226. shell: "{{ bin_dir }}/calicoctl node bgp peer add {{ hostvars[item]['calico_rr_ip']|default(hostvars[item]['ip'])|default(hostvars[item]['ansible_default_ipv4.address']) }} as {{ local_as | default(global_as_num) }}"
  227. with_items: "{{ groups['calico-rr'] | default([]) }}"
  228. when: (legacy_calicoctl and
  229. peer_with_calico_rr|default(false) and inventory_hostname in groups['k8s-cluster']
  230. and hostvars[item]['cluster_id'] == cluster_id)