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.

221 lines
7.7 KiB

9 years ago
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.pem", d: "cert.crt"}
  28. - {s: "node-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: "/usr/bin/docker run --rm -v /opt/cni/bin:/cnibindir {{ hyperkube_image_repo }}:{{ hyperkube_image_tag }} /usr/bin/rsync -a /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: "/usr/bin/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 | wait for etcd
  56. uri: url=https://localhost:2379/health validate_certs=no
  57. register: result
  58. until: result.status == 200 or result.status == 401
  59. retries: 10
  60. delay: 5
  61. delegate_to: "{{groups['etcd'][0]}}"
  62. run_once: true
  63. - name: Calico | Check if calico network pool has already been configured
  64. command: |-
  65. curl \
  66. --cacert {{ etcd_cert_dir }}/ca.pem \
  67. --cert {{ etcd_cert_dir}}/admin.pem \
  68. --key {{ etcd_cert_dir }}/admin-key.pem \
  69. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  70. register: calico_conf
  71. delegate_to: "{{groups['etcd'][0]}}"
  72. run_once: true
  73. tags: facts
  74. - name: Calico | Configure calico network pool
  75. shell: >
  76. echo '{
  77. "kind": "ipPool",
  78. "spec": {"disabled": false, "ipip": {"enabled": {{ cloud_provider is defined or ipip }}},
  79. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  80. "apiVersion": "v1",
  81. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  82. }'
  83. | {{ bin_dir }}/calicoctl create -f -
  84. environment:
  85. NO_DEFAULT_POOLS: true
  86. run_once: true
  87. when: not legacy_calicoctl and
  88. ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  89. - name: Calico (old) | Define ipip pool argument
  90. run_once: true
  91. set_fact:
  92. ipip_arg: "--ipip"
  93. when: (legacy_calicoctl and
  94. cloud_provider is defined or ipip)
  95. tags: facts
  96. - name: Calico (old) | Define nat-outgoing pool argument
  97. run_once: true
  98. set_fact:
  99. nat_arg: "--nat-outgoing"
  100. when: (legacy_calicoctl and
  101. nat_outgoing|default(false) and not peer_with_router|default(false))
  102. tags: facts
  103. - name: Calico (old) | Define calico pool task name
  104. run_once: true
  105. set_fact:
  106. pool_task_name: "with options {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  107. when: (legacy_calicoctl and ipip_arg|default(false) or nat_arg|default(false))
  108. tags: facts
  109. - name: Calico (old) | Configure calico network pool {{ pool_task_name|default('') }}
  110. command: "{{ bin_dir}}/calicoctl pool add {{ kube_pods_subnet }} {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  111. environment:
  112. NO_DEFAULT_POOLS: true
  113. run_once: true
  114. when: legacy_calicoctl and
  115. ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  116. - name: Calico | Get calico configuration from etcd
  117. command: |-
  118. curl \
  119. --cacert {{ etcd_cert_dir }}/ca.pem \
  120. --cert {{ etcd_cert_dir}}/admin.pem \
  121. --key {{ etcd_cert_dir }}/admin-key.pem \
  122. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  123. register: calico_pools_raw
  124. delegate_to: "{{groups['etcd'][0]}}"
  125. run_once: true
  126. - set_fact:
  127. calico_pools: "{{ calico_pools_raw.stdout | from_json }}"
  128. run_once: true
  129. tags: facts
  130. - name: Calico | Check if calico pool is properly configured
  131. fail:
  132. msg: 'Only one network pool must be configured and it must be the subnet {{ kube_pods_subnet }}.
  133. Please erase calico configuration and run the playbook again ("etcdctl rm --recursive /calico/v1/ipam/v4/pool")'
  134. when: ( calico_pools['node']['nodes'] | length > 1 ) or
  135. ( not calico_pools['node']['nodes'][0]['key'] | search(".*{{ kube_pods_subnet | ipaddr('network') }}.*") )
  136. run_once: true
  137. tags: facts
  138. - name: Calico | Write /etc/network-environment
  139. template: src=network-environment.j2 dest=/etc/network-environment
  140. when: ansible_service_mgr in ["sysvinit","upstart"]
  141. - name: Calico (old) | Write calico-node systemd init file
  142. template: src=calico-node.service.legacy.j2 dest=/etc/systemd/system/calico-node.service
  143. when: ansible_service_mgr == "systemd" and legacy_calicoctl
  144. notify: restart calico-node
  145. - name: Calico | Write calico.env for systemd init file
  146. template: src=calico.env.j2 dest=/etc/calico/calico.env
  147. when: ansible_service_mgr == "systemd" and not legacy_calicoctl
  148. notify: restart calico-node
  149. - name: Calico | Write calico-node systemd init file
  150. template: src=calico-node.service.j2 dest=/etc/systemd/system/calico-node.service
  151. when: ansible_service_mgr == "systemd" and not legacy_calicoctl
  152. notify: restart calico-node
  153. - name: Calico | Write calico-node initd script
  154. template: src=deb-calico.initd.j2 dest=/etc/init.d/calico-node owner=root mode=0755
  155. when: ansible_service_mgr in ["sysvinit","upstart"] and ansible_os_family == "Debian"
  156. notify: restart calico-node
  157. - name: Calico | Write calico-node initd script
  158. template: src=rh-calico.initd.j2 dest=/etc/init.d/calico-node owner=root mode=0755
  159. when: ansible_service_mgr in ["sysvinit","upstart"] and ansible_os_family == "RedHat"
  160. notify: restart calico-node
  161. - meta: flush_handlers
  162. - name: Calico | Enable calico-node
  163. service:
  164. name: calico-node
  165. state: started
  166. enabled: yes
  167. - name: Calico | Disable node mesh
  168. shell: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  169. when: (not legacy_calicoctl and
  170. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  171. run_once: true
  172. - name: Calico | Configure peering with router(s)
  173. shell: >
  174. echo '{
  175. "kind": "bgpPeer",
  176. "spec": {"asNumber": "{{ item.as }}"},
  177. "apiVersion": "v1",
  178. "metadata": {"node": "{{ inventory_hostname }}", "scope": "node", "peerIP": "{{ item.router_id }}"}
  179. }'
  180. | {{ bin_dir }}/calicoctl create -f -
  181. with_items: "{{ peers|default([]) }}"
  182. when: (not legacy_calicoctl and
  183. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  184. - name: Calico (old) | Disable node mesh
  185. shell: "{{ bin_dir }}/calicoctl bgp node-mesh off"
  186. when: (legacy_calicoctl and
  187. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  188. run_once: true
  189. - name: Calico (old) | Configure peering with router(s)
  190. shell: "{{ bin_dir }}/calicoctl node bgp peer add {{ item.router_id }} as {{ item.as }}"
  191. with_items: "{{ peers|default([]) }}"
  192. when: (legacy_calicoctl and
  193. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])