---
- block:

  # When openshift_hosted_manage_registry=true the openshift_hosted
  # role will create the appropriate route for the docker-registry.
  # When openshift_hosted_manage_registry=false then this code will
  # not be run.
  - name: fetch the docker-registry route
    oc_route:
      kubeconfig: "{{ openshift_master_config_dir }}/admin.kubeconfig"
      name: docker-registry
      namespace: default
      state: list
    register: docker_registry_route

  - name: Create passthrough route for registry-console
    oc_route:
      kubeconfig: "{{ openshift_master_config_dir }}/admin.kubeconfig"
      name: registry-console
      namespace: default
      service_name: registry-console
      state: present
      tls_termination: passthrough
    register: registry_console_cockpit_kube

  # XXX: Required for items still using command
  - name: Create temp directory for kubeconfig
    command: mktemp -d /tmp/openshift-ansible-XXXXXX
    register: mktemp
    changed_when: False

  - set_fact:
      openshift_hosted_kubeconfig: "{{ mktemp.stdout }}/admin.kubeconfig"

  - name: Copy the admin client config(s)
    command: >
      cp {{ openshift_master_config_dir }}/admin.kubeconfig {{ openshift_hosted_kubeconfig }}
    changed_when: False

  - name: Deploy registry-console
    command: >
      {{ openshift_client_binary }} new-app --template=registry-console
      {% if openshift_cockpit_deployer_prefix is defined  %}-p IMAGE_PREFIX="{{ openshift_cockpit_deployer_prefix }}"{% endif %}
      {% if openshift_cockpit_deployer_basename is defined  %}-p IMAGE_BASENAME="{{ openshift_cockpit_deployer_basename }}"{% endif %}
      {% if openshift_cockpit_deployer_version is defined  %}-p IMAGE_VERSION="{{ openshift_cockpit_deployer_version }}"{% endif %}
      -p OPENSHIFT_OAUTH_PROVIDER_URL="{{ openshift.master.public_api_url }}"
      -p REGISTRY_HOST="{{ docker_registry_route.results[0].spec.host }}"
      -p COCKPIT_KUBE_URL="https://{{ registry_console_cockpit_kube.results.results[0].spec.host }}"
      --config={{ openshift_hosted_kubeconfig }}
      -n default
    register: deploy_registry_console
    changed_when: "'already exists' not in deploy_registry_console.stderr"
    failed_when:
    - "'already exists' not in deploy_registry_console.stderr"
    - "deploy_registry_console.rc != 0"

  - name: Delete temp directory
    file:
      name: "{{ mktemp.stdout }}"
      state: absent
    changed_when: False
    # XXX: End required for items still using command
  run_once: true
