# vim: set ft=ansible
---
# Implement your Workload deployment tasks here

# TODO: change to variable-based versions
- name: set relevant facts for re-use
  set_fact:
    elastic_version: "elasticsearch-operator.4.2.4-201911050122"
    jaeger_version: "jaeger-operator.v1.13.1"
    kiali_version: "kiali-operator.v1.0.7"
    servicemesh_version: "servicemeshoperator.v1.0.2"

- name: create a namespace for the service mesh operators
  k8s:
    state: present
    definition:
      apiVersion: project.openshift.io/v1
      kind: Project
      metadata:
        name: service-mesh-operators

- name: create an OperatorGroup so that the CSVs will get installed
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1
      kind: OperatorGroup
      metadata:
        name: service-mesh-operators
        namespace: service-mesh-operators

- name: create the CatalogSource for the snapshot
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: CatalogSource
      metadata:
        name: redhat-operators-snapshot
        namespace: service-mesh-operators
      spec:
        sourceType: grpc
        image: quay.io/openshiftroadshow/operator-snapshot@sha256:e4c2f914d49d47f283e4a683eb75e17853aaf26eedf1ce3b04e97da6ea81ea5c
        displayName: Red Hat Operators Snapshot

- name: create elastic subscription
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: Subscription
      metadata:
        name: service-mesh-elastic
        namespace: service-mesh-operators
      spec:
        channel: "4.2"
        source: redhat-operators-snapshot
        sourceNamespace: service-mesh-operators
        name: elasticsearch-operator
        startingCSV: "{{ elastic_version }}"
        installPlanApproval: Manual

- name: wait for the status of the elastic subscription to not be empty
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: Subscription
    name: service-mesh-elastic
    namespace: service-mesh-operators
  register: operator_subscription_out
  until: 
    - operator_subscription_out is defined
    - operator_subscription_out.resources is defined
    - operator_subscription_out.resources[0] is defined
    - operator_subscription_out.resources[0].status is defined
    - operator_subscription_out.resources[0].status.installplan is defined
  retries: 30
  delay: 20

- name: patch the installplan to approve it
  k8s:
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: InstallPlan
      metadata:
        namespace: service-mesh-operators
        name: "{{ operator_subscription_out.resources[0].status.installplan.name }}"
      spec:
        approved: true

- name: create Jaeger subscription
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: Subscription
      metadata:
        name: service-mesh-jaeger
        namespace: service-mesh-operators
      spec:
        channel: stable
        source: redhat-operators-snapshot
        sourceNamespace: service-mesh-operators
        name: jaeger-product
        startingCSV: "{{ jaeger_version }}"
        installPlanApproval: Manual

- name: wait for the status of the jaeger subscription to not be empty
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: Subscription
    name: service-mesh-jaeger
    namespace: service-mesh-operators
  register: operator_subscription_out
  until: 
    - operator_subscription_out is defined
    - operator_subscription_out.resources is defined
    - operator_subscription_out.resources[0] is defined
    - operator_subscription_out.resources[0].status is defined
    - operator_subscription_out.resources[0].status.installplan is defined
  retries: 30
  delay: 20

- name: patch the installplan to approve it
  k8s:
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: InstallPlan
      metadata:
        namespace: service-mesh-operators
        name: "{{ operator_subscription_out.resources[0].status.installplan.name }}"
      spec:
        approved: true

- name: create Kiali subscription
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: Subscription
      metadata:
        name: service-mesh-kiali
        namespace: service-mesh-operators
      spec:
        channel: stable
        source: redhat-operators-snapshot
        sourceNamespace: service-mesh-operators
        name: kiali-ossm
        startingCSV: "{{ kiali_version }}"
        installPlanApproval: Manual

- name: wait for the status of the kiali subscription to not be empty
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: Subscription
    name: service-mesh-kiali
    namespace: service-mesh-operators
  register: operator_subscription_out
  until: 
    - operator_subscription_out is defined
    - operator_subscription_out.resources is defined
    - operator_subscription_out.resources[0] is defined
    - operator_subscription_out.resources[0].status is defined
    - operator_subscription_out.resources[0].status.installplan is defined
  retries: 30
  delay: 20

- name: patch the installplan to approve it
  k8s:
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: InstallPlan
      metadata:
        namespace: service-mesh-operators
        name: "{{ operator_subscription_out.resources[0].status.installplan.name }}"
      spec:
        approved: true

- name: create Service Mesh Operator subscription
  k8s:
    state: present
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: Subscription
      metadata:
        name: service-mesh-operator
        namespace: service-mesh-operators
      spec:
        channel: "1.0"
        source: redhat-operators-snapshot
        sourceNamespace: service-mesh-operators
        name: servicemeshoperator
        startingCSV: "{{ servicemesh_version }}"
        installPlanApproval: Manual

- name: wait for the status of the servicemesh subscription to not be empty
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: Subscription
    name: service-mesh-operator
    namespace: service-mesh-operators
  register: operator_subscription_out
  until: 
    - operator_subscription_out is defined
    - operator_subscription_out.resources is defined
    - operator_subscription_out.resources[0] is defined
    - operator_subscription_out.resources[0].status is defined
    - operator_subscription_out.resources[0].status.installplan is defined
  retries: 30
  delay: 20

- name: patch the installplan to approve it
  k8s:
    definition:
      apiVersion: operators.coreos.com/v1alpha1
      kind: InstallPlan
      metadata:
        namespace: service-mesh-operators
        name: "{{ operator_subscription_out.resources[0].status.installplan.name }}"
      spec:
        approved: true

- name: wait for the CSVs to exist
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: ClusterServiceVersion
    name: "{{ item }}"
    namespace: service-mesh-operators
  register: csv_exists_out
  retries: 5
  delay: 70
  until: csv_exists_out.resources | length > 0
  with_items:
    - "{{ elastic_version }}"
    - "{{ jaeger_version }}"
    - "{{ kiali_version }}"
    - "{{ servicemesh_version }}"

- name: wait for the CSVs to be Succeeded
  k8s_facts:
    api_version: operators.coreos.com/v1alpha1
    kind: ClusterServiceVersion
    name: "{{ item }}"
    namespace: service-mesh-operators
  register: csv_exists_out
  retries: 5
  delay: 70
  until: csv_exists_out.resources[0].status.phase == "Succeeded"
  with_items:
    - "{{ elastic_version }}"
    - "{{ jaeger_version }}"
    - "{{ kiali_version }}"
    - "{{ servicemesh_version }}"

# Leave this as the last task in the playbook. 
- name: workload tasks complete
  debug:
    msg: "Workload Tasks completed successfully." 
  when: not silent|bool

