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.

211 lines
7.5 KiB

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