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.

205 lines
7.0 KiB

9 years ago
9 years ago
  1. ---
  2. - name: Calico | Disable calico-node service if it exists
  3. service:
  4. name: calico-node
  5. state: stopped
  6. enabled: yes
  7. failed_when: false
  8. - name: Calico | Get kubelet hostname
  9. shell: >-
  10. {{ bin_dir }}/kubectl get node -o custom-columns='NAME:.metadata.name,INTERNAL-IP:.status.addresses[?(@.type=="InternalIP")].address'
  11. | egrep "{{ ansible_all_ipv4_addresses | join('$|') }}$" | cut -d" " -f1
  12. register: calico_kubelet_name
  13. delegate_to: "{{ groups['kube-master'][0] }}"
  14. when: cloud_provider is defined
  15. - name: Calico | Write Calico cni config
  16. template:
  17. src: "cni-calico.conflist.j2"
  18. dest: "/etc/cni/net.d/10-calico.conflist"
  19. owner: kube
  20. - name: Calico | Create calico certs directory
  21. file:
  22. dest: "{{ calico_cert_dir }}"
  23. state: directory
  24. mode: 0750
  25. owner: root
  26. group: root
  27. - name: Calico | Link etcd certificates for calico-node
  28. file:
  29. src: "{{ etcd_cert_dir }}/{{ item.s }}"
  30. dest: "{{ calico_cert_dir }}/{{ item.d }}"
  31. state: hard
  32. force: yes
  33. with_items:
  34. - {s: "ca.pem", d: "ca_cert.crt"}
  35. - {s: "node-{{ inventory_hostname }}.pem", d: "cert.crt"}
  36. - {s: "node-{{ inventory_hostname }}-key.pem", d: "key.pem"}
  37. - name: Calico | Install calicoctl container script
  38. template:
  39. src: calicoctl-container.j2
  40. dest: "{{ bin_dir }}/calicoctl"
  41. mode: 0755
  42. owner: root
  43. group: root
  44. changed_when: false
  45. - name: Calico | Copy cni plugins from hyperkube
  46. 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/"
  47. register: cni_task_result
  48. until: cni_task_result.rc == 0
  49. retries: 4
  50. delay: "{{ retry_stagger | random + 3 }}"
  51. changed_when: false
  52. tags:
  53. - hyperkube
  54. - upgrade
  55. - name: Calico | Copy cni plugins from calico/cni container
  56. 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/'"
  57. register: cni_task_result
  58. until: cni_task_result.rc == 0
  59. retries: 4
  60. delay: "{{ retry_stagger | random + 3 }}"
  61. changed_when: false
  62. when: overwrite_hyperkube_cni|bool
  63. tags:
  64. - hyperkube
  65. - upgrade
  66. - name: Calico | Set cni directory permissions
  67. file:
  68. path: /opt/cni/bin
  69. state: directory
  70. owner: kube
  71. recurse: true
  72. mode: 0755
  73. - name: Calico | wait for etcd
  74. uri:
  75. url: https://localhost:2379/health
  76. validate_certs: no
  77. client_cert: "{{ etcd_cert_dir }}/node-{{ inventory_hostname }}.pem"
  78. client_key: "{{ etcd_cert_dir }}/node-{{ inventory_hostname }}-key.pem"
  79. register: result
  80. until: result.status == 200 or result.status == 401
  81. retries: 10
  82. delay: 5
  83. delegate_to: "{{groups['etcd'][0]}}"
  84. run_once: true
  85. - name: Calico | Check if calico network pool has already been configured
  86. command: |-
  87. curl \
  88. --cacert {{ etcd_cert_dir }}/ca.pem \
  89. --cert {{ etcd_cert_dir}}/admin-{{ groups['etcd'][0] }}.pem \
  90. --key {{ etcd_cert_dir }}/admin-{{ groups['etcd'][0] }}-key.pem \
  91. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  92. register: calico_conf
  93. retries: 4
  94. delay: "{{ retry_stagger | random + 3 }}"
  95. delegate_to: "{{groups['etcd'][0]}}"
  96. run_once: true
  97. changed_when: false
  98. - name: Calico | Configure calico network pool
  99. shell: >
  100. echo '{
  101. "kind": "ipPool",
  102. "spec": {"disabled": false, "ipip": {"enabled": {{ ipip }}, "mode": "{{ ipip_mode }}"},
  103. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  104. "apiVersion": "v1",
  105. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  106. }'
  107. | {{ bin_dir }}/calicoctl create -f -
  108. environment:
  109. NO_DEFAULT_POOLS: true
  110. run_once: true
  111. when: ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  112. - name: Calico | Get calico configuration from etcd
  113. command: |-
  114. curl \
  115. --cacert {{ etcd_cert_dir }}/ca.pem \
  116. --cert {{ etcd_cert_dir}}/admin-{{ groups['etcd'][0] }}.pem \
  117. --key {{ etcd_cert_dir }}/admin-{{ groups['etcd'][0] }}-key.pem \
  118. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  119. register: calico_pools_raw
  120. retries: 4
  121. delay: "{{ retry_stagger | random + 3 }}"
  122. delegate_to: "{{groups['etcd'][0]}}"
  123. run_once: true
  124. - set_fact:
  125. calico_pools: "{{ calico_pools_raw.stdout | from_json }}"
  126. run_once: true
  127. - name: Calico | Check if calico pool is properly configured
  128. fail:
  129. msg: 'Only one network pool must be configured and it must be the subnet {{ kube_pods_subnet }}.
  130. Please erase calico configuration and run the playbook again ("etcdctl rm --recursive /calico/v1/ipam/v4/pool")'
  131. when: ( calico_pools['node']['nodes'] | length > 1 and not calico_ignore_extra_pools ) or
  132. ( not calico_pools['node']['nodes'][0]['key'] | search(".*{{ kube_pods_subnet | ipaddr('network') }}.*") )
  133. run_once: true
  134. - name: Calico | Set global as_num
  135. command: "{{ bin_dir}}/calicoctl config set asNumber {{ global_as_num }}"
  136. run_once: true
  137. - name: Calico | Disable node mesh
  138. shell: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  139. retries: 4
  140. delay: "{{ retry_stagger | random + 3 }}"
  141. when: ((peer_with_router|default(false) or peer_with_calico_rr|default(false))
  142. and inventory_hostname in groups['k8s-cluster'])
  143. run_once: true
  144. - name: Calico | Configure peering with router(s)
  145. shell: >
  146. echo '{
  147. "kind": "bgpPeer",
  148. "spec": {"asNumber": "{{ item.as }}"},
  149. "apiVersion": "v1",
  150. "metadata": {"node": "{{ inventory_hostname }}", "scope": "node", "peerIP": "{{ item.router_id }}"}
  151. }'
  152. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  153. retries: 4
  154. delay: "{{ retry_stagger | random + 3 }}"
  155. with_items: "{{ peers|default([]) }}"
  156. when: peer_with_router|default(false) and inventory_hostname in groups['k8s-cluster']
  157. - name: Calico | Configure peering with route reflectors
  158. shell: >
  159. echo '{
  160. "kind": "bgpPeer",
  161. "spec": {"asNumber": "{{ local_as | default(global_as_num)}}"},
  162. "apiVersion": "v1",
  163. "metadata": {"node": "{{ inventory_hostname }}",
  164. "scope": "node",
  165. "peerIP": "{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4"]["address"]) }}"}
  166. }'
  167. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  168. retries: 4
  169. delay: "{{ retry_stagger | random + 3 }}"
  170. with_items: "{{ groups['calico-rr'] | default([]) }}"
  171. when: (peer_with_calico_rr|default(false) and inventory_hostname in groups['k8s-cluster']
  172. and hostvars[item]['cluster_id'] == cluster_id)
  173. - name: Calico | Create calico manifests
  174. template:
  175. src: "{{item.file}}.j2"
  176. dest: "{{kube_config_dir}}/{{item.file}}"
  177. with_items:
  178. - {name: calico-config, file: calico-config.yml, type: cm}
  179. - {name: calico-node, file: calico-node.yml, type: ds}
  180. - {name: calico, file: calico-node-sa.yml, type: sa}
  181. - {name: calico, file: calico-cr.yml, type: clusterrole}
  182. - {name: calico, file: calico-crb.yml, type: clusterrolebinding}
  183. register: calico_node_manifests
  184. when:
  185. - inventory_hostname in groups['kube-master']
  186. - rbac_enabled or item.type not in rbac_resources