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.

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