---
- name: Calico | Check if calicoctl is installed
  ignore_errors: true
  command: calicoctl version
  register: calicoctl_install_version
  register_type: yaml

- name: Calico | Install calicoctl if it is not present
  when: .calicoctl_install_version.error | empty | not
  block:
    - name: Calico | Copy calicoctl binary to remote node
      copy:
        src: >-
          {{ .binary_dir }}/cni/calico/{{ .cni.calico_version }}/{{ .binary_type }}/calicoctl
        dest: /usr/local/bin/calicoctl
        mode: 0755

- name: Calico | Copy Calico Helm package to remote node
  copy:
    src: >-
      {{ .binary_dir }}/cni/calico/tigera-operator-{{ .cni.calico_version }}.tgz
    dest: >-
      /etc/kubernetes/cni/tigera-operator-{{ .cni.calico_version }}.tgz

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

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