---
- name: InitKubernetes | Configure control_plane_endpoint in local DNS files
  when: 
    - or (.kubernetes.control_plane_endpoint.type | eq "local") (.kubernetes.control_plane_endpoint.type | eq "haproxy")
    - .inventory_hostname | eq .init_kubernetes_node | not
  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
    {{- if and (index .hostvars .init_kubernetes_node "internal_ipv4") (ne (index .hostvars .init_kubernetes_node "internal_ipv4") "") }}
    {{ index .hostvars .init_kubernetes_node "internal_ipv4" }} {{ .kubernetes.control_plane_endpoint.host }}
    {{- end }}
    {{- if and (index .hostvars .init_kubernetes_node "internal_ipv6") (ne (index .hostvars .init_kubernetes_node "internal_ipv6") "") }}
    {{ index .hostvars .init_kubernetes_node "internal_ipv6" }} {{ .kubernetes.control_plane_endpoint.host }}
    {{- end }}
    # kubekey kubernetes control_plane_endpoint END
    EOF

- name: InitKubernetes | Initialize the Kubernetes cluster and distribute credentials
  when: .init_kubernetes_node | eq .inventory_hostname
  block:
    - name: InitKubernetes | Fresh Kubernetes Installation
      when: .kubernetes_install_LoadState.stdout | eq "not-found"
      block:
        - include_tasks: init_kubernetes.yaml
          when: .kubernetes_install_LoadState.stdout | eq "not-found"
        - include_tasks: deploy_cluster_dns.yaml
    - name: InitKubernetes | Kubernetes Already Installed
      when: .kubernetes_install_LoadState.stdout | eq "load"
      command: |
        if [ ! -f /etc/kubernetes/kubeadm-config.yaml ]; then
          kubectl get cm kubeadm-config -n kube-system -o=jsonpath='{.data.ClusterConfiguration}' > /etc/kubernetes/kubeadm-config.yaml
        fi
    - name: InitKubernetes | Fetch kubeconfig to local workspace
      fetch:
        src: /etc/kubernetes/admin.conf
        dest: >-
          {{ .work_dir }}/kubekey/kubeconfig
    - name: InitKubernetes | Generate certificate key using kubeadm
      command: |
        /usr/local/bin/kubeadm init phase upload-certs --upload-certs --config=/etc/kubernetes/kubeadm-config.yaml 2>&1 \
          | awk '/Using certificate key:/{getline; print}'
      register: kubeadm_cert_result
    - name: InitKubernetes | Distribute certificate key to all cluster hosts
      add_hostvars:
        hosts: k8s_cluster
        vars:
          kubeadm_cert: >-
            {{ .kubeadm_cert_result.stdout }}
    - name: InitKubernetes | Generate and distribute kubeadm token
      block:
        - name: InitKubernetes | Generate kubeadm join token
          command: /usr/local/bin/kubeadm token create
          register: kubeadm_token_result
        - name: InitKubernetes | Share kubeadm token with all cluster hosts
          add_hostvars:
            hosts: k8s_cluster
            vars:
              kubeadm_token: >-
                {{ .kubeadm_token_result.stdout }}
