---
# Simple bootstrap - uses Container Mom inventory structure with control_plane_nodes and worker_nodes lists
- name: Validate inventory structure
  fail:
    msg: "Inventory must have 'control_plane_nodes' and 'worker_nodes' variable lists"
  when: control_plane_nodes is not defined or worker_nodes is not defined

- name: Display cluster information
  debug:
    msg:
      - "Bootstrapping Kubernetes {{ kubernetes_version }}"
      - "Control plane nodes: {{ control_plane_nodes | length }}"
      - "Worker nodes: {{ worker_nodes | length }}"
      - "Network plugin: {{ network_plugin }}"
      - "Cluster: {{ cluster_name }}.{{ cluster_region }}.container.mom"

# Create dynamic inventory groups from node lists
- name: Add control plane nodes to inventory
  add_host:
    name: "{{ item.ip }}"
    groups:
      - control_plane
      - kubernetes
    ansible_host: "{{ item.ip }}"
    ansible_user: "{{ ansible_user }}"
    ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
    node_hostname: "{{ item.hostname }}"
    node_role: "{{ item.role }}"
    provider: "{{ item.provider | default('unknown') }}"
  loop: "{{ control_plane_nodes }}"

- name: Add worker nodes to inventory
  add_host:
    name: "{{ item.ip }}"
    groups:
      - worker_nodes
      - kubernetes
    ansible_host: "{{ item.ip }}"
    ansible_user: "{{ ansible_user }}"
    ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
    node_hostname: "{{ item.hostname }}"
    node_role: "{{ item.role }}"
    provider: "{{ item.provider | default('unknown') }}"
  loop: "{{ worker_nodes }}"

# Bootstrap cluster on the actual nodes
- name: Setup hosts for Kubernetes
  include_tasks: "{{ playbook_dir }}/tasks/setup_hosts.yml"
  vars:
    project_path: "prerelease:/main"
  delegate_to: "{{ item }}"
  loop: "{{ groups['kubernetes'] }}"

- name: Bootstrap Kubernetes cluster
  include_tasks: "{{ playbook_dir }}/tasks/cluster_install.yaml"
  vars:
    is_control_plane: "{{ 'true' if item in groups['control_plane'] else 'false' }}"
  delegate_to: "{{ item }}"
  loop: "{{ groups['kubernetes'] }}"

# Download kubeconfig for local access
- name: Download kubeconfig to localhost
  fetch:
    src: /etc/kubernetes/admin.conf
    dest: "{{ kubeconfig_localhost_path }}"
    flat: yes
    mode: '0600'
  delegate_to: "{{ groups['control_plane'][0] }}"
  when: download_kubeconfig | bool

- name: Update kubeconfig server endpoint for external access
  replace:
    path: "{{ kubeconfig_localhost_path }}"
    regexp: 'server: https://.*:6443'
    replace: 'server: https://{{ groups["control_plane"][0] }}:6443'
  delegate_to: localhost
  when: download_kubeconfig | bool

# Install network plugin
- name: Install network plugin
  include_tasks: "install_network.yml"
  delegate_to: "{{ groups['control_plane'][0] }}"
  when: network_plugin != "none"

# Label worker nodes
- name: Label worker nodes
  shell: |
    kubectl label nodes {{ hostvars[item]['node_hostname'] | regex_replace('\\..*$', '') }} node-role.kubernetes.io/worker=worker --kubeconfig=/etc/kubernetes/admin.conf --overwrite
  delegate_to: "{{ groups['control_plane'][0] }}"
  loop: "{{ groups['worker_nodes'] }}"
  when: groups['worker_nodes'] is defined

- name: Display cluster status
  shell: kubectl get nodes --kubeconfig=/etc/kubernetes/admin.conf
  register: cluster_status
  delegate_to: "{{ groups['control_plane'][0] }}"

- name: Show cluster nodes
  debug:
    msg: "{{ cluster_status.stdout_lines }}"
  when: cluster_status is defined