---
- name: Check if calicoctl is installed
  ignore_errors: true
  command: calicoctl version
  register: calicoctl_install_version
  register_type: yaml
- name: Install calicoctl
  when: .calicoctl_install_version.error | empty | not
  block:
    - name: Sync calicoctl to remote
      copy:
        src: >-
          {{ .binary_dir }}/cni/calico/{{ .calico_version }}/{{ .binary_type }}/calicoctl
        dest: /usr/local/bin/calicoctl
        mode: 0755

- name: Sync calico package to remote
  copy:
    src: >-
      {{ .binary_dir }}/cni/calico/tigera-operator-{{ .calico_version }}.tgz
    dest: >-
      /etc/kubernetes/cni/tigera-operator-{{ .calico_version }}.tgz

- name: Generate calico custom value file
  copy:
    content: |
      {{ .cni.calico.values }}
    dest: /etc/kubernetes/cni/calico-values.yaml

- name: Apply calico
  command: |
    helm upgrade --install --create-namespace --namespace tigera-operator calico /etc/kubernetes/cni/tigera-operator-{{ .calico_version }}.tgz -f /etc/kubernetes/cni/calico-values.yaml
