---
- name: Set os_family fact for other redhat-based operating systems
  set_fact:
    ansible_os_family: "RedHat"
    ansible_distribution_major_version: "8"
  when: ansible_distribution in redhat_os_family_extensions
  tags:
    - facts

- name: Check resolvconf
  command: which resolvconf
  register: resolvconf
  failed_when: false
  changed_when: false
  check_mode: false

- name: Check existence of /etc/resolvconf/resolv.conf.d
  stat:
    path: /etc/resolvconf/resolv.conf.d
    get_attributes: false
    get_checksum: false
    get_mime: false
  failed_when: false
  register: resolvconfd_path

- name: Check status of /etc/resolv.conf
  stat:
    path: /etc/resolv.conf
    follow: false
    get_attributes: false
    get_checksum: false
    get_mime: false
  failed_when: false
  register: resolvconf_stat

  # Used in vars/
- name: Fetch resolv.conf
  when: resolvconf_stat.stat.exists
  slurp:
    src: /etc/resolv.conf
  register: resolvconf_slurp

- name: NetworkManager | Check if host has NetworkManager
  # noqa command-instead-of-module - Should we use service_facts for this?
  command: systemctl is-active --quiet NetworkManager.service
  register: networkmanager_enabled
  failed_when: false
  changed_when: false
  check_mode: false

- name: Check systemd-resolved
  # noqa command-instead-of-module - Should we use service_facts for this?
  command: systemctl is-active systemd-resolved
  register: systemd_resolved_enabled
  failed_when: false
  changed_when: false
  check_mode: false

- name: Set default dns if remove_default_searchdomains is false
  set_fact:
    default_searchdomains: ["default.svc.{{ dns_domain }}", "svc.{{ dns_domain }}"]
  when: not remove_default_searchdomains | default() | bool or (remove_default_searchdomains | default() | bool and searchdomains | length == 0)

- name: Set dns facts
  set_fact:
    resolvconf: >-
      {%- if resolvconf.rc == 0 and resolvconfd_path.stat.isdir is defined and resolvconfd_path.stat.isdir -%}true{%- else -%}false{%- endif -%}

- name: Check if kubelet is configured
  stat:
    path: "{{ kube_config_dir }}/kubelet.env"
    get_attributes: false
    get_checksum: false
    get_mime: false
  register: kubelet_configured
  changed_when: false

- name: Check if early DNS configuration stage
  set_fact:
    dns_early: "{{ not kubelet_configured.stat.exists }}"

- name: Target resolv.conf files
  set_fact:
    resolvconffile: /etc/resolv.conf
    base: >-
      {%- if resolvconf | bool -%}/etc/resolvconf/resolv.conf.d/base{%- endif -%}
    head: >-
      {%- if resolvconf | bool -%}/etc/resolvconf/resolv.conf.d/head{%- endif -%}
  when: not ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"] and not is_fedora_coreos

- name: Target temporary resolvconf cloud init file (Flatcar Container Linux by Kinvolk / Fedora CoreOS)
  set_fact:
    resolvconffile: /tmp/resolveconf_cloud_init_conf
  when: ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"] or is_fedora_coreos

- name: Check if /etc/dhclient.conf exists
  stat:
    path: /etc/dhclient.conf
    get_attributes: false
    get_checksum: false
    get_mime: false
  register: dhclient_stat

- name: Target dhclient conf file for /etc/dhclient.conf
  set_fact:
    dhclientconffile: /etc/dhclient.conf
  when: dhclient_stat.stat.exists

- name: Check if /etc/dhcp/dhclient.conf exists
  stat:
    path: /etc/dhcp/dhclient.conf
    get_attributes: false
    get_checksum: false
    get_mime: false
  register: dhcp_dhclient_stat

- name: Target dhclient conf file for /etc/dhcp/dhclient.conf
  set_fact:
    dhclientconffile: /etc/dhcp/dhclient.conf
  when: dhcp_dhclient_stat.stat.exists

- name: Target dhclient hook file for Red Hat family
  set_fact:
    dhclienthookfile: /etc/dhcp/dhclient.d/zdnsupdate.sh
  when: ansible_os_family == "RedHat"

- name: Target dhclient hook file for Debian family
  set_fact:
    dhclienthookfile: /etc/dhcp/dhclient-exit-hooks.d/zdnsupdate
  when: ansible_os_family == "Debian"

- name: Set etcd vars if using kubeadm mode
  set_fact:
    etcd_cert_dir: "{{ kube_cert_dir }}"
    kube_etcd_cacert_file: "etcd/ca.crt"
    kube_etcd_cert_file: "apiserver-etcd-client.crt"
    kube_etcd_key_file: "apiserver-etcd-client.key"
  when:
    - etcd_deployment_type == "kubeadm"

- name: Check /usr readonly
  stat:
    path: "/usr"
    get_attributes: false
    get_checksum: false
    get_mime: false
  register: usr

- name: Set alternate flexvolume path
  set_fact:
    kubelet_flexvolumes_plugins_dir: /var/lib/kubelet/volumeplugins
  when: not usr.stat.writeable
