---
- name: Ensure project exists
  oc_project:
    name: "{{ kuryr_namespace }}"
    state: present
    node_selector:
    - ""

- name: Perform OpenShift ServiceAccount config
  include_tasks: serviceaccount.yaml
  run_once: true

- name: Create kuryr manifests tempdir
  command: mktemp -d
  register: manifests_tmpdir

- name: Create OpenShift node's ImageStreamTag manifest
  become: yes
  template:
    src: node-images.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/node-images.yaml"

- name: Set certificate contents as fact
  set_fact:
    kuryr_ca_certificate: "{{ lookup('file', kuryr_openstack_ca | default(lookup('env', 'OS_CACERT'))) }}"
  ignore_errors: yes

- name: Create Kuryr certificates Secret manifest
  become: yes
  template:
    src: certificates-secret.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/certificates-secret.yaml"

- name: Create kuryr ConfigMap manifest
  become: yes
  template:
    src: configmap.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/configmap.yaml"

- name: Create kuryr-controller Deployment manifest
  become: yes
  template:
    src: controller-deployment.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/controller-deployment.yaml"

- name: Create kuryr-cni DaemonSet manifest
  become: yes
  template:
    src: cni-daemonset.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/cni-daemonset.yaml"

- name: Create kuryrnet CRD manifest
  become: yes
  template:
    src: kuryrnet.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/kuryrnet.yaml"
  when: openshift_kuryr_subnet_driver|default("default") == 'namespace'

- name: Create kuryrnetpolicy CRD manifest
  become: yes
  template:
    src: kuryrnetpolicy.yaml.j2
    dest: "{{ manifests_tmpdir.stdout }}/kuryrnetpolicy.yaml"
  when: openshift_kuryr_sg_driver|default("default") == 'policy'

- name: Apply OpenShift node's ImageStreamTag manifest
  oc_obj:
    state: present
    kind: ImageStreamTag
    name: "node:v3.11"
    namespace: "{{ kuryr_namespace }}"
    files:
    - "{{ manifests_tmpdir.stdout }}/node-images.yaml"
  run_once: true
  ignore_errors: yes

- name: Apply Kuryr certificates Secret
  oc_obj:
    state: present
    kind: Secret
    name: "kuryr-certificates"
    namespace: "{{ kuryr_namespace }}"
    files:
    - "{{ manifests_tmpdir.stdout }}/certificates-secret.yaml"
  run_once: true

- name: Apply ConfigMap manifest
  oc_obj:
    state: present
    kind: ConfigMap
    name: "kuryr-config"
    namespace: "{{ kuryr_namespace }}"
    files:
    - "{{ manifests_tmpdir.stdout }}/configmap.yaml"
  run_once: true

- name: Apply Controller Deployment manifest
  oc_obj:
    state: present
    kind: Deployment
    name: "kuryr-controller"
    namespace: "{{ kuryr_namespace }}"
    files:
    - "{{ manifests_tmpdir.stdout }}/controller-deployment.yaml"
  run_once: true

- name: Apply kuryr-cni DaemonSet manifest
  oc_obj:
    state: present
    kind: DaemonSet
    name: "kuryr-cni-ds"
    namespace: "{{ kuryr_namespace }}"
    files:
    - "{{ manifests_tmpdir.stdout }}/cni-daemonset.yaml"
  run_once: true

- name: Apply kuryrnet CRD manifest
  oc_obj:
    state: present
    kind: CustomResourceDefinition
    name: "kuryrnets.openstack.org"
    files:
    - "{{ manifests_tmpdir.stdout }}/kuryrnet.yaml"
  run_once: true
  when: openshift_kuryr_subnet_driver|default("default") == 'namespace'

- name: Apply kuryrnetpolicy CRD manifest
  oc_obj:
    state: present
    kind: CustomResourceDefinition
    name: "kuryrnetpolicies.openstack.org"
    files:
    - "{{ manifests_tmpdir.stdout }}/kuryrnetpolicy.yaml"
  run_once: true
  when: openshift_kuryr_sg_driver|default("default") == 'policy'
