---
- name: Join | Generate kubeadm join configuration file
  template:
    src: >-
      {{- if .kubernetes.kube_version | semverCompare ">=v1.24.0" -}}
      kubeadm/kubeadm-join.v1beta3
      {{- else -}}
      kubeadm/kubeadm-join.v1beta2
      {{- end -}}
    dest: /etc/kubernetes/kubeadm-config.yaml

- name: Join | Execute kubeadm join to add node to the Kubernetes cluster
  command: |
    /usr/local/bin/kubeadm join --config=/etc/kubernetes/kubeadm-config.yaml --ignore-preflight-errors=FileExisting-crictl,ImagePull

- name: Join | Synchronize kubeconfig to remote node
  copy:
    src: >-
      {{ .work_dir }}/kubekey/kubeconfig
    dest: /root/.kube/config

- name: Join | Configure node as worker
  when: .groups.kube_worker | default list | has .inventory_hostname
  block:
    - name: Join | Remove master and control-plane taints from node
      ignore_errors: true
      command: |
        /usr/local/bin/kubectl taint nodes {{ .hostname }} node-role.kubernetes.io/master=:NoSchedule-
        /usr/local/bin/kubectl taint nodes {{ .hostname }} node-role.kubernetes.io/control-plane=:NoSchedule-
    - name: Join | Add worker label to node
      command: |
        /usr/local/bin/kubectl label --overwrite node {{ .hostname }} node-role.kubernetes.io/worker=

- name: Join | Add custom annotations to node
  when: .annotations | empty | not
  command: |
    kubectl annotate {{ .hostname }} {{- range $k,$v := .annotations }}{{ printf "%s=%s" $k $v}} {{- end }}

# Reset local DNS for control_plane_endpoint to 127.0.0.1 and ::1.
# This ensures the control_plane_endpoint resolves locally before kube-vip is running,
# preventing failures for tasks that execute kubectl apply on the current node.
- name: Join | Reset local DNS for control_plane_endpoint
  block:
    - name: Join | Reset local DNS on control plane nodes
      when:
        - .groups.kube_control_plane | default list | has .inventory_hostname
      loop: "{{ .native.localDNS | toJson }}"
      command: |
        sed -i ':a;$!{N;ba};s@# kubekey kubernetes control_plane_endpoint BEGIN.*# kubekey kubernetes control_plane_endpoint END@@' {{ .item }}
        cat >> {{ .item }} <<EOF
        # kubekey kubernetes control_plane_endpoint BEGIN
        127.0.0.1 {{ .kubernetes.control_plane_endpoint.host }}
        ::1 {{ .kubernetes.control_plane_endpoint.host }}
        # kubekey kubernetes control_plane_endpoint END
        EOF
    - name: Join | Reset local DNS on worker nodes (for haproxy endpoint)
      when:
        - .groups.kube_worker | default list | has .inventory_hostname
        - .kubernetes.control_plane_endpoint.type | eq "haproxy"
      loop: "{{ .native.localDNS | toJson }}"
      command: |
        sed -i ':a;$!{N;ba};s@# kubekey kubernetes control_plane_endpoint BEGIN.*# kubekey kubernetes control_plane_endpoint END@@' {{ .item }}
        cat >> {{ .item }} <<EOF
        # kubekey kubernetes control_plane_endpoint BEGIN
        127.0.0.1 {{ .kubernetes.control_plane_endpoint.host }}
        ::1 {{ .kubernetes.control_plane_endpoint.host }}
        # kubekey kubernetes control_plane_endpoint END
        EOF