---
- name: ovn-kubernetes | check if ovn-kubernetes is cloned
  stat:
    path: "{{ ovn_kubernetes_info.build_path }}/ovn-kubernetes-checkout"
  register: ovnkubernetes_repo

- name: ovn-kubernetes | git clone
  git:
    repo: "{{ ovn_kubernetes_info.git_url }}"
    dest: "{{ ovn_kubernetes_info.build_path }}/ovn-kubernetes-checkout"
    version: "{{ ovn_kubernetes_info.branch }}"
  when: not ovnkubernetes_repo.stat.exists

- name: ovn-kubernetes | expecting all bins to be built already
  set_fact:
    linux_bins_missing: false
    windows_bins_missing: false

- name: ovn-kubernetes | checking if linux bins have been built
  action: stat path="{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller/_output/go/bin/{{item}}"
  register: linux_stat_bins_exists
  with_items:
    - "{{ovn_kubernetes_binaries.linux}}"

- name: ovn-kubernetes | checking linux bins
  set_fact:
    linux_bins_missing: true
  with_items:
    - "{{linux_stat_bins_exists.results}}"
  loop_control:
    label: "{{item.item}}"
  when: not item.stat.exists

- name: ovn-kubernetes | checking if windows bins have been built
  action: stat path="{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller/_output/go/windows/{{item}}"
  register: windows_stat_bins_exists
  with_items:
    - "{{ovn_kubernetes_binaries.windows}}"

- name: ovn-kubernetes | checking windows bins
  set_fact:
    windows_bins_missing: true
  with_items:
    - "{{windows_stat_bins_exists.results}}"
  loop_control:
    label: "{{item.item}}"
  when: not item.stat.exists

- name: ovn-kubernetes | build binaries
  block:
  - name: ovn-kubernetes | Install golang dependency
    import_role:
      name: linux/common
      tasks_from: golang

  - name: ovn-kubernetes | compile sources - make clean
    make:
      chdir: "{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller"
      target: clean

  - name: ovn-kubernetes | compile sources - make linux
    make:
      chdir: "{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller"

  - name: ovn-kubernetes | compile sources - make windows
    make:
      chdir: "{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller"
      target: windows

  - name: ovn-kubernetes | compile sources - make install
    make:
      chdir: "{{ovn_kubernetes_info.build_path}}/ovn-kubernetes-checkout/go-controller"
      target: install
  when: windows_bins_missing or linux_bins_missing
