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.

194 lines
6.5 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: "{{ etcd_access_addresses.split(',') | first }}/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. run_once: true
  84. - name: Calico | Check if calico network pool has already been configured
  85. command: |-
  86. curl \
  87. --cacert {{ etcd_cert_dir }}/ca.pem \
  88. --cert {{ etcd_cert_dir}}/node-{{ inventory_hostname }}.pem \
  89. --key {{ etcd_cert_dir }}/node-{{ inventory_hostname }}-key.pem \
  90. {{ etcd_access_addresses.split(',') | first }}/v2/keys/calico/v1/ipam/v4/pool
  91. register: calico_conf
  92. retries: 4
  93. delay: "{{ retry_stagger | random + 3 }}"
  94. run_once: true
  95. changed_when: false
  96. - name: Calico | Configure calico network pool
  97. shell: >
  98. echo '{
  99. "kind": "ipPool",
  100. "spec": {"disabled": false, "ipip": {"enabled": {{ ipip }}, "mode": "{{ ipip_mode }}"},
  101. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  102. "apiVersion": "v1",
  103. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  104. }'
  105. | {{ bin_dir }}/calicoctl create -f -
  106. environment:
  107. NO_DEFAULT_POOLS: true
  108. run_once: true
  109. when: ("Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  110. - name: Calico | Get calico configuration from etcd
  111. command: |-
  112. curl \
  113. --cacert {{ etcd_cert_dir }}/ca.pem \
  114. --cert {{ etcd_cert_dir}}/node-{{ inventory_hostname }}.pem \
  115. --key {{ etcd_cert_dir }}/node-{{ inventory_hostname }}-key.pem \
  116. {{ etcd_access_addresses.split(',') | first }}/v2/keys/calico/v1/ipam/v4/pool
  117. register: calico_pools_raw
  118. retries: 4
  119. delay: "{{ retry_stagger | random + 3 }}"
  120. run_once: true
  121. - set_fact:
  122. calico_pools: "{{ calico_pools_raw.stdout | from_json }}"
  123. run_once: true
  124. - name: Calico | Set global as_num
  125. command: "{{ bin_dir}}/calicoctl config set asNumber {{ global_as_num }}"
  126. run_once: true
  127. - name: Calico | Disable node mesh
  128. shell: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  129. retries: 4
  130. delay: "{{ retry_stagger | random + 3 }}"
  131. when: ((peer_with_router|default(false) or peer_with_calico_rr|default(false))
  132. and inventory_hostname in groups['k8s-cluster'])
  133. run_once: true
  134. - name: Calico | Configure peering with router(s)
  135. shell: >
  136. echo '{
  137. "kind": "bgpPeer",
  138. "spec": {"asNumber": "{{ item.as }}"},
  139. "apiVersion": "v1",
  140. "metadata": {"node": "{{ inventory_hostname }}", "scope": "node", "peerIP": "{{ item.router_id }}"}
  141. }'
  142. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  143. retries: 4
  144. delay: "{{ retry_stagger | random + 3 }}"
  145. with_items: "{{ peers|default([]) }}"
  146. when: peer_with_router|default(false) and inventory_hostname in groups['k8s-cluster']
  147. - name: Calico | Configure peering with route reflectors
  148. shell: >
  149. echo '{
  150. "kind": "bgpPeer",
  151. "spec": {"asNumber": "{{ local_as | default(global_as_num)}}"},
  152. "apiVersion": "v1",
  153. "metadata": {"node": "{{ inventory_hostname }}",
  154. "scope": "node",
  155. "peerIP": "{{ hostvars[item]["calico_rr_ip"]|default(hostvars[item]["ip"])|default(hostvars[item]["ansible_default_ipv4"]["address"]) }}"}
  156. }'
  157. | {{ bin_dir }}/calicoctl create --skip-exists -f -
  158. retries: 4
  159. delay: "{{ retry_stagger | random + 3 }}"
  160. with_items: "{{ groups['calico-rr'] | default([]) }}"
  161. when: (peer_with_calico_rr|default(false) and inventory_hostname in groups['k8s-cluster']
  162. and hostvars[item]['cluster_id'] == cluster_id)
  163. - name: Calico | Create calico manifests
  164. template:
  165. src: "{{item.file}}.j2"
  166. dest: "{{kube_config_dir}}/{{item.file}}"
  167. with_items:
  168. - {name: calico-config, file: calico-config.yml, type: cm}
  169. - {name: calico-node, file: calico-node.yml, type: ds}
  170. - {name: calico, file: calico-node-sa.yml, type: sa}
  171. - {name: calico, file: calico-cr.yml, type: clusterrole}
  172. - {name: calico, file: calico-crb.yml, type: clusterrolebinding}
  173. register: calico_node_manifests
  174. when:
  175. - inventory_hostname in groups['kube-master']
  176. - rbac_enabled or item.type not in rbac_resources