---

# Get the Openshift cluster's networking config object
- name: Check the IP version infrastructure provisioned
  k8s_info:
    api_version: "config.openshift.io/v1"
    kind: Network
    name: cluster
  register: cluster_network_config
  when: not meteringconfig_ocp_disabled

# Attempt to auto-detect whether there are any ipv4 addresses in the serviceNetwork array.
# If there are, treat the cluster as single-stack IPV4, else enable IPV6 networking.
#
# TODO: fix this logic when better support for Openshift dual-stack has been developed.
# Note: the ipv4 Jinja2 filter has a dependency on the "netaddr" python module.
- name: Detect if cluster is single-stack ipv4
  block:
  - set_fact:
      service_network_contains_ipv4_address: "{{ item | ipv4 }}"
    loop: "{{ serviceNetworkList.status.serviceNetwork }}"

  - set_fact:
      _ocp_enabled_use_ipv6_networking: true
    when: not service_network_contains_ipv4_address
  vars:
    serviceNetworkList: "{{ cluster_network_config.resources | first }}"
  when: cluster_network_config is defined and cluster_network_config.resources is defined and cluster_network_config.resources | length > 0

#
# Cluster Proxy Configuration
#
- name: Check if the cluster-wide Proxy configured
  k8s_info:
    api_version: "config.openshift.io/v1"
    kind: Proxy
    name: cluster
  register: cluster_proxy_config
  when: not meteringconfig_ocp_disabled

# In the case where a non-empty cluster-wide Proxy (.spec != nil) has
# been configured, start pulling out information we care about. The
# .status field should contain a more resolved copy of the httpProxy/httpsProxy/noProxy
# fields, and depending on whether the admin has specified any additional CA trustbundles
# for proxying HTTPS traffic, we would need to read that from the .spec object.
- name: Detect if a cluster-wide Proxy has been configured
  block:
  - set_fact:
      _ocp_enabled_global_proxy_enabled: true
      _ocp_enabled_global_proxy_no_proxy: "{{ _cluster_proxy_config.status.noProxy }}"

  - set_fact:
      _ocp_enabled_global_proxy_trusted_ca_bundle: "{{ _cluster_proxy_config.spec.trustedCA.name }}"
    when: _cluster_proxy_config.spec.trustedCA is defined

  - set_fact:
      _ocp_enabled_http_proxy_url: "{{ _ocp_enabled_global_proxy_http_proxy }}"
      _ocp_enabled_http_proxy_hostname: "{{ _ocp_enabled_global_proxy_http_proxy | urlsplit('hostname') }}"
      _ocp_enabled_http_proxy_port: "{{ _ocp_enabled_global_proxy_http_proxy | urlsplit('port') }}"
      _ocp_enabled_http_proxy_username: "{{ _ocp_enabled_global_proxy_http_proxy | urlsplit('username') }}"
      _ocp_enabled_http_proxy_password: "{{ _ocp_enabled_global_proxy_http_proxy | urlsplit('password') }}"

      _ocp_enabled_https_proxy_url: "{{ _ocp_enabled_global_proxy_https_proxy }}"
      _ocp_enabled_https_proxy_hostname: "{{ _ocp_enabled_global_proxy_https_proxy | urlsplit('hostname') }}"
      _ocp_enabled_https_proxy_port: "{{ _ocp_enabled_global_proxy_https_proxy | urlsplit('port') }}"
      _ocp_enabled_https_proxy_username: "{{ _ocp_enabled_global_proxy_https_proxy | urlsplit('username') }}"
      _ocp_enabled_https_proxy_password: "{{ _ocp_enabled_global_proxy_https_proxy | urlsplit('password') }}"
  vars:
    _cluster_proxy_config: "{{ cluster_proxy_config.resources | first }}"
    _ocp_enabled_global_proxy_http_proxy: "{{ _cluster_proxy_config.status.httpProxy }}"
    _ocp_enabled_global_proxy_https_proxy: "{{ _cluster_proxy_config.status.httpsProxy }}"
  no_log: true
  when:
  - cluster_proxy_config is defined
  - cluster_proxy_config.resources | length > 0
  - _cluster_proxy_config.status is defined
  - _cluster_proxy_config.status | length > 0
