---
- name: Kubernetes bins | expecting all bins to be downloaded already
  set_fact:
    linux_kube_bins_missing: false
    windows_kube_bins_missing: false

- name: Kubernetes bins | checking if linux kubernetes bins have been downloaded
  action: stat path="{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/server/kubernetes/server/bin/{{item}}"
  register: linux_kube_stat_bins_exists
  with_items:
    - "{{kubernetes_binaries.linux_common}}"
    - "{{kubernetes_binaries.linux_master}}"
    - "{{kubernetes_binaries.linux_minion}}"

- name: Kubernetes bins | checking linux bins
  set_fact:
    linux_kube_bins_missing: true
  with_items:
    - "{{linux_kube_stat_bins_exists.results}}"
  loop_control:
    label: "{{item.item}}"
  when: not item.stat.exists

- name: Kubernetes bins | Download linux bins
  block:
    - name: Kubernetes bins | create temp directory
      file:
        path: "{{ kubernetes_binaries_info.tmp_download_path }}"
        state: directory
        mode: 0755

    - name: Kubernetes bins | get kubernetes.tar.gz archive
      get_url:
        url: "{{ kubernetes_binaries_info.kubernetes_targz_link }}"
        dest: "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes.tar.gz"
        force_basic_auth: yes
        timeout: 30
      retries: 3

    - name: Kubernetes bins | unarchive tar.gz
      unarchive:
        src: "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes.tar.gz"
        dest: "{{ kubernetes_binaries_info.tmp_download_path }}"
        remote_src: yes

    - name: Kubernetes bins | download kubernetes binaries
      action: shell yes | "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/cluster/get-kube-binaries.sh"

    - name: Kubernetes bins | unarchive binaries
      unarchive:
        src: "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/server/kubernetes-server-linux-amd64.tar.gz"
        dest: "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/server"
        remote_src: yes

    - name: Kubernetes bins | install Linux master binaries
      copy:
        src: "{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/server/kubernetes/server/bin/{{item}}"
        dest: "{{ kubernetes_binaries_info.install_path }}/{{item}}"
        owner: root
        group: root
        mode: 0755
        remote_src: yes
      with_items:
        - "{{kubernetes_binaries.linux_common}}"
        - "{{kubernetes_binaries.linux_master}}"
  when: linux_kube_bins_missing

- name: Kubernetes bins | checking if windows kubernetes bins have been downloaded
  action: stat path="{{ kubernetes_binaries_info.tmp_download_path }}/kubernetes/node/bin/{{item}}"
  register: windows_kube_stat_bins_exists
  with_items:
    - "{{kubernetes_binaries.windows}}"

- name: Kubernetes bins | checking windows bins
  set_fact:
    windows_kube_bins_missing: true
  with_items:
    - "{{windows_kube_stat_bins_exists.results}}"
  loop_control:
    label: "{{item.item}}"
  when: not item.stat.exists

- name: Kubernetes bins | Download Windows bins
  block:
    - name: Kubernetes bins | downloading Windows binaries
      get_url:
        url: "{{ kubernetes_binaries_info.windows_bins_link }}"
        dest: "{{ kubernetes_binaries_info.tmp_download_path }}/winbins.tar.gz"
        force_basic_auth: yes
        timeout: 30
      retries: 3

    - name: Kubernetes bins | unarchive Windows binaries
      unarchive:
        src: "{{ kubernetes_binaries_info.tmp_download_path }}/winbins.tar.gz"
        dest: "{{ kubernetes_binaries_info.tmp_download_path }}/"
        remote_src: yes
  when: windows_kube_bins_missing
