---
- name: Delete Node
  command: |
    if kubectl get node {{ .hostname }} > /dev/null 2>&1; then
      kubectl cordon {{ .hostname }}
      if [ $(kubectl get nodes --no-headers | wc -l) -gt 1 ]; then
        kubectl drain {{ .hostname }} --ignore-daemonsets --delete-emptydir-data --force --disable-eviction
      else
        kubectl drain {{ .hostname }} --ignore-daemonsets --delete-emptydir-data --force
      fi
    {{- if .cni.type | eq "calico" }}
      calicoctl delete node {{ .hostname }}
    {{- end }}
      kubectl delete node {{ .hostname }}
      kubeadm reset -f
    fi

- name: Stop kubelet service
  ignore_errors: true
  command: |
    systemctl stop kubelet.service
    systemctl disable kubelet.service
    rm -rf /etc/systemd/system/kubelet.service*
    systemctl daemon-reload
    systemctl reset-failed kubelet.service

- name: Delete residue files
  command: |
    rm -rf /usr/local/bin/kubeadm && rm -rf /usr/local/bin/kubelet && rm -rf /usr/local/bin/kubectl
    rm -rf /var/lib/kubelet/
    rm -rf /etc/kubernetes/
    rm -rf .kube/config
    rm -rf /var/lib/etcd