---
# set_fact task copied from playbooks/common/openshift-master/config.yml
# so that openshift_master_default_subdomain has a default value of ""
# (emptry string). openshift_master_default_subdomain must have a default
# value for openshift_master_facts to set metrics_public_url.
# TODO: clean this up.
- set_fact:
    openshift_master_default_subdomain: "{{ lookup('oo_option', 'openshift_master_default_subdomain') | default(None, true) }}"
  when: openshift_master_default_subdomain is not defined

- fail:
    msg: openshift_master_default_subdomain must be set to deploy metrics
  when: openshift_hosted_metrics_deploy | default(false) | bool and openshift_master_default_subdomain | default("") == ""

# NOTE: These metrics variables are unfortunately needed by both the master and the metrics roles
# to properly configure the master-config.yaml file.
#
# NOTE: Today only changing the hostname for the metrics public URL is supported, the
# path must stay consistent. As such if openshift_hosted_metrics_public_url is set in
# inventory, we extract the hostname, and then reset openshift_hosted_metrics_public_url
# to the format that we know is valid. (This may change in future)
- set_fact:
    g_metrics_hostname: "{{ openshift_hosted_metrics_public_url
                        | default('hawkular-metrics.' ~ (openshift_master_default_subdomain))
                        | oo_hostname_from_url }}"

- set_fact:
    openshift_hosted_metrics_deploy_url: "https://{{ g_metrics_hostname }}/hawkular/metrics"
  when: (openshift_hosted_metrics_deploy | default(false) | bool) or (openshift_hosted_metrics_public_url is defined)

- name: Set master facts
  openshift_facts:
    role: master
    local_facts:
      cluster_method: "{{ openshift_master_cluster_method | default(None) }}"
      cluster_hostname: "{{ openshift_master_cluster_hostname | default(None) }}"
      cluster_public_hostname: "{{ openshift_master_cluster_public_hostname | default(None) }}"
      debug_level: "{{ openshift_master_debug_level | default(openshift.common.debug_level) }}"
      api_port: "{{ openshift_master_api_port | default(None) }}"
      api_url: "{{ openshift_master_api_url | default(None) }}"
      api_use_ssl: "{{ openshift_master_api_use_ssl | default(None) }}"
      public_api_url: "{{ openshift_master_public_api_url | default(None) }}"
      console_path: "{{ openshift_master_console_path | default(None) }}"
      console_port: "{{ openshift_master_console_port | default(None) }}"
      console_url: "{{ openshift_master_console_url | default(None) }}"
      console_use_ssl: "{{ openshift_master_console_use_ssl | default(None) }}"
      public_console_url: "{{ openshift_master_public_console_url | default(None) }}"
      logging_public_url: "{{ openshift_master_logging_public_url | default(None) }}"
      logout_url: "{{ openshift_master_logout_url | default(None) }}"
      extension_scripts: "{{ openshift_master_extension_scripts | default(None) }}"
      extension_stylesheets: "{{ openshift_master_extension_stylesheets | default(None) }}"
      extensions: "{{ openshift_master_extensions | default(None) }}"
      etcd_hosts: "{{ openshift_master_etcd_hosts | default(None) }}"
      etcd_port: "{{ openshift_master_etcd_port | default(None) }}"
      etcd_use_ssl: "{{ openshift_master_etcd_use_ssl | default(None) }}"
      etcd_urls: "{{ openshift_master_etcd_urls | default(None) }}"
      embedded_etcd: "{{ openshift_master_embedded_etcd | default(None) }}"
      embedded_kube: "{{ openshift_master_embedded_kube | default(None) }}"
      embedded_dns: "{{ openshift_master_embedded_dns | default(None) }}"
      # defaults to 8053 when using dnsmasq in 1.2/3.2
      dns_port: "{{ openshift_master_dns_port | default(None) }}"
      bind_addr: "{{ openshift_master_bind_addr | default(None) }}"
      pod_eviction_timeout: "{{ openshift_master_pod_eviction_timeout | default(None) }}"
      session_max_seconds: "{{ openshift_master_session_max_seconds | default(None) }}"
      session_name: "{{ openshift_master_session_name | default(None) }}"
      session_secrets_file: "{{ openshift_master_session_secrets_file | default(None) }}"
      session_auth_secrets: "{{ openshift_master_session_auth_secrets | default(None) }}"
      session_encryption_secrets: "{{ openshift_master_session_encryption_secrets | default(None) }}"
      access_token_max_seconds: "{{ openshift_master_access_token_max_seconds | default(None) }}"
      auth_token_max_seconds: "{{ openshift_master_auth_token_max_seconds | default(None) }}"
      identity_providers: "{{ openshift_master_identity_providers | default(None) }}"
      htpasswd_users: "{{ openshift_master_htpasswd_users | default(lookup('file', openshift_master_htpasswd_file) | oo_htpasswd_users_from_file if openshift_master_htpasswd_file is defined else None) }}"
      manage_htpasswd: "{{ openshift_master_manage_htpasswd | default(true) }}"
      ldap_ca: "{{ openshift_master_ldap_ca | default(lookup('file', openshift_master_ldap_ca_file) if openshift_master_ldap_ca_file is defined else None) }}"
      openid_ca: "{{ openshift_master_openid_ca | default(lookup('file', openshift_master_openid_ca_file) if openshift_master_openid_ca_file is defined else None) }}"
      request_header_ca: "{{ openshift_master_request_header_ca | default(lookup('file', openshift_master_request_header_ca_file) if openshift_master_request_header_ca_file is defined else None) }}"
      registry_url: "{{ oreg_url | default(None) }}"
      oauth_grant_method: "{{ openshift_master_oauth_grant_method | default(None) }}"
      sdn_cluster_network_cidr: "{{ osm_cluster_network_cidr | default(None) }}"
      sdn_host_subnet_length: "{{ osm_host_subnet_length | default(None) }}"
      default_subdomain: "{{ openshift_master_default_subdomain | default(osm_default_subdomain | default(None), true) }}"
      custom_cors_origins: "{{ osm_custom_cors_origins | default(None) }}"
      default_node_selector: "{{ osm_default_node_selector | default(None) }}"
      project_request_message: "{{ osm_project_request_message | default(None) }}"
      project_request_template: "{{ osm_project_request_template | default(None) }}"
      mcs_allocator_range: "{{ osm_mcs_allocator_range | default(None) }}"
      mcs_labels_per_project: "{{ osm_mcs_labels_per_project | default(None) }}"
      uid_allocator_range: "{{ osm_uid_allocator_range | default(None) }}"
      registry_selector: "{{ openshift_registry_selector | default(None) }}"
      api_server_args: "{{ osm_api_server_args | default(None) }}"
      controller_args: "{{ osm_controller_args | default(None) }}"
      disabled_features: "{{ osm_disabled_features | default(None) }}"
      master_count: "{{ openshift_master_count | default(None) }}"
      controller_lease_ttl: "{{ osm_controller_lease_ttl | default(None) }}"
      master_image: "{{ osm_image | default(None) }}"
      admission_plugin_order: "{{openshift_master_admission_plugin_order | default(None) }}"
      admission_plugin_config: "{{openshift_master_admission_plugin_config | default(None) }}"
      kube_admission_plugin_order: "{{openshift_master_kube_admission_plugin_order | default(None) }}"
      kube_admission_plugin_config: "{{openshift_master_kube_admission_plugin_config | default(None) }}"
      oauth_template: "{{ openshift_master_oauth_template | default(None) }}" # deprecated in origin 1.2 / OSE 3.2
      oauth_templates: "{{ openshift_master_oauth_templates | default(None) }}"
      oauth_always_show_provider_selection: "{{ openshift_master_oauth_always_show_provider_selection | default(None) }}"
      image_policy_config: "{{ openshift_master_image_policy_config | default(None) }}"
      dynamic_provisioning_enabled: "{{ openshift_master_dynamic_provisioning_enabled | default(None) }}"
      max_requests_inflight: "{{ openshift_master_max_requests_inflight | default(None) }}"
      api_env_vars: "{{ openshift_master_api_env_vars | default(None) }}"
      controllers_env_vars: "{{ openshift_master_controllers_env_vars | default(None) }}"
      audit_config: "{{ openshift_master_audit_config | default(None) }}"
      metrics_public_url: "{% if openshift_hosted_metrics_deploy | default(false) %}https://{{ metrics_hostname }}/hawkular/metrics{% endif %}"

- name: Determine if scheduler config present
  stat:
    path: "{{ openshift_master_scheduler_conf }}"
  register: scheduler_config_stat

- set_fact:
    openshift_master_scheduler_default_predicates: "{{ lookup('openshift_master_facts_default_predicates') }}"
    openshift_master_scheduler_default_priorities: "{{ lookup('openshift_master_facts_default_priorities') }}"

- block:
  - name: Retrieve current scheduler config
    slurp:
      src: "{{ openshift_master_scheduler_conf }}"
    register: current_scheduler_config

  - set_fact:
      openshift_master_scheduler_current_config: "{{ current_scheduler_config.content | b64decode | from_json }}"

  - fail:
      msg: "Unknown scheduler config apiVersion {{ openshift_master_scheduler_config.apiVersion }}"
    when: openshift_master_scheduler_current_config.apiVersion | default(None) != 'v1'

  - set_fact:
      openshift_master_scheduler_current_predicates: "{{ openshift_master_scheduler_current_config.predicates }}"
      openshift_master_scheduler_current_priorities: "{{ openshift_master_scheduler_current_config.priorities }}"
  when: scheduler_config_stat.stat.exists
