---
- name: Check if Node Already Joined
  become_user: fdamstra
  shell: "kubectl get nodes | grep -c {{ inventory_hostname }} || true"
  register: joined
  changed_when: false # Do not mark as 'changed'

# joining takes awhile, so we're doing it asynchronously
- name: Join the cluster (non-control)
  shell: "kubeadm join {{ k8s_control }}:6443 --token {{ k8s_token }} --discovery-token-ca-cert-hash {{ k8s_discovery_token }}"
  async: 1200
  poll: 5
  when: inventory_hostname != "k8s1" and "k8scontrol" not in group_names

# From https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/
# sudo kubeadm join 192.168.0.200:6443 --token 9vr73a.a8uxyaju799qwdjv --discovery-token-ca-cert-hash sha256:7c2e69131a36ae2a042a339b33381c6d0d43887e2de83720eff5359e26aec866 --control-plane --certificate-key f8902e114ef118304e561c3ecd4d0b543adc226b7a07f675f56564185ffe0c07
- name: Join the cluster (control)
  # Open question: is this 'k8s_control' or 'k8s1.home.monkeybox.org'?
  # Open question: Where does the certificate key come from?
  shell: "kubeadm join 10.42.42.201:6443 --token {{ k8s_token }} --discovery-token-ca-cert-hash {{ k8s_discovery_token }} --control-plane --certificate-key {{ k8s_certificate_key }} --apiserver-advertise-address {{ ansible_default_ipv4.address }}"
  async: 1200
  poll: 5
  when: inventory_hostname != "k8s1" and "k8scontrol" in group_names
