---
- include_tasks: kube.yaml

- include_tasks: etcd.yaml
  when:
    - .etcd.deployment_type | eq "external"
    - .groups.etcd | default list | empty | not

- name: Kubernetes | Restart Kubernetes control plane pods
  command: |
    {{- if .cri.container_manager | eq "docker" }}
    # Restarting Kubernetes control plane pods using Docker
    docker ps -af name=k8s_PODS_kube-apiserver* -q | xargs --no-run-if-empty docker rm -f
    docker ps -af name=k8s_PODS_kube-controller-manager* -q | xargs --no-run-if-empty docker rm -f
    docker ps -af name=k8s_PODS_kube-scheduler* -q | xargs --no-run-if-empty docker rm -f
      {{- if .etcd.deployment_type | eq "docker" }}
    # Restarting etcd pods managed by Docker
    docker ps -af name=k8s_PODS_etcd* -q | xargs --no-run-if-empty docker rm -f
      {{- end }}
    {{- else }}
    # Restarting Kubernetes control plane pods using crictl
    crictl pods --name kube-apiserver-* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
    crictl pods --name kube-controller-manager-* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
    crictl pods --name kube-scheduler-* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
      {{- if .etcd.deployment_type | eq "internal" }}
    # Restarting etcd pods managed by the container runtime
    crictl pods --name etcd-* -q | xargs -I% --no-run-if-empty bash -c 'crictl stopp % && crictl rmp %'
      {{- end }}
    {{- end }}
