---
- name: Preinstall | propagate resolvconf to k8s components
  command: /bin/true
  notify:
    - Preinstall | reload kubelet
    - Preinstall | kube-controller configured
    - Preinstall | kube-apiserver configured
    - Preinstall | restart kube-controller-manager docker
    - Preinstall | restart kube-controller-manager crio/containerd
    - Preinstall | restart kube-apiserver docker
    - Preinstall | restart kube-apiserver crio/containerd
  when: not ansible_os_family in ["CoreOS", "Coreos", "Container Linux by CoreOS"]

- name: Preinstall | update resolvconf for Container Linux by CoreOS
  command: /bin/true
  notify:
    - Preinstall | apply resolvconf cloud-init
    - Preinstall | reload kubelet
  when: ansible_os_family in ["CoreOS", "Coreos", "Container Linux by CoreOS"]

- name: Preinstall | apply resolvconf cloud-init
  command: /usr/bin/coreos-cloudinit --from-file {{ resolveconf_cloud_init_conf }}
  when: ansible_os_family in ["CoreOS", "Coreos", "Container Linux by CoreOS"]

- name: Preinstall | reload kubelet
  service:
    name: kubelet
    state: restarted
  notify:
    - Preinstall | kube-controller configured
    - Preinstall | kube-apiserver configured
    - Preinstall | restart kube-controller-manager docker
    - Preinstall | restart kube-controller-manager crio/containerd
    - Preinstall | restart kube-apiserver docker
    - Preinstall | restart kube-apiserver crio/containerd
  when: not dns_early|bool

# FIXME(mattymo): Also restart for kubeadm mode
- name: Preinstall | kube-apiserver configured
  stat: path="{{ kube_manifest_dir }}/kube-apiserver.manifest"
  register: kube_apiserver_set
  when: inventory_hostname in groups['kube-master'] and dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'

# FIXME(mattymo): Also restart for kubeadm mode
- name: Preinstall | kube-controller configured
  stat: path="{{ kube_manifest_dir }}/kube-controller-manager.manifest"
  register: kube_controller_set
  when: inventory_hostname in groups['kube-master'] and dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'

- name: Preinstall | restart kube-controller-manager docker
  shell: "docker ps -f name=k8s_POD_kube-controller-manager* -q | xargs --no-run-if-empty docker rm -f"
  when:
    - container_manager == "docker"
    - inventory_hostname in groups['kube-master']
    - dns_mode != 'none'
    - resolvconf_mode == 'host_resolvconf'
    - kube_controller_set.stat.exists

- name: Preinstall | restart kube-controller-manager crio/containerd
  shell: crictl pods --name kube-controller-manager* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
  when:
    - container_manager in ['crio', 'containerd']
    - inventory_hostname in groups['kube-master']
    - dns_mode != 'none'
    - resolvconf_mode == 'host_resolvconf'
    - kube_controller_set.stat.exists

- name: Preinstall | restart kube-apiserver docker
  shell: "docker ps -f name=k8s_POD_kube-apiserver* -q | xargs --no-run-if-empty docker rm -f"
  when:
    - container_manager == "docker"
    - inventory_hostname in groups['kube-master']
    - dns_mode != 'none'
    - resolvconf_mode == 'host_resolvconf'

- name: Preinstall | restart kube-apiserver crio/containerd
  shell: crictl pods --name kube-apiserver* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
  when:
    - container_manager in ['crio', 'containerd']
    - inventory_hostname in groups['kube-master']
    - dns_mode != 'none'
    - resolvconf_mode == 'host_resolvconf'

- name: Preinstall | Restart systemd-resolved
  service:
    name: systemd-resolved
    state: restarted
