---
#############################################################
#   Create default load balancer for kubernetes
#############################################################

- name: "Create default load balancer <{{ stage_kube_load_balancer }}>"
  hosts: "{{ host | default('kube_control_plane') }}"
  gather_facts: false
  connection: local

  pre_tasks:
    - name: "Check if ansible version is at least {{ ansible_minimal_version }}"
      assert:
        that:
          - ansible_version.string is version(ansible_minimal_version, ">=")
        msg: "The ansible version has to be at least {{ ansible_minimal_version }}"
      tags:
        - always

  tasks:
    - name: "Create default load balancer <{{ stage_kube_load_balancer }}>"
      include_role:
        name: hetzner-ansible-hcloud
        tasks_from: configure_load_balancer
      vars: 
        - hetzner_load_balancer_name: "{{ stage_kube_load_balancer }}"
        - hetzner_load_balancer_labels: "stage={{ stage }}"

#############################################################
#   Bootstrapping kubernetes cluster
#############################################################

- name: "apply kubernetes setup to {{ host | default('kube_control_plane') }}"
  hosts: "{{ host | default('kube_control_plane') }}"
  serial: "{{ serial_number | default(10) }}"
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"

  pre_tasks:
    - name: "Import autodiscover pre-tasks"
      import_tasks: tasks/autodiscover_pre_tasks.yml
      tags:
        - always

  roles:
    - role: hetzner-ansible-dns
      vars:
        record_data: "{{ shared_service_kube_loadbalancer_public_ip }}"
        record_name: "{{ stage_kube_load_balancer }}"

    - { role: kubernetes/base }

    - role: kubernetes/cloud_controller_manager
      when: kubernetes_with_ccm | default(true)
      tags:
        - ccm

    - role: kubernetes/container_storage_interface
      when: kubernetes_with_csi | default(true)
      tags:
        - csi

    - role: kubernetes/external_dns
      when: kubernetes_with_externaldns | default(false)
      tags:
        - external-dns

    - role: kubernetes/cert_manager
      when: kubernetes_with_certmanager | default(false)
      tags:
        - cert-manager

    - role: kubernetes/ingress_controller
      when: kubernetes_with_ingress | default(false)
      tags:
        - ingress

    - role: kubernetes/gitea
      when: kubernetes_with_gitea | default(false)
      tags:
        - gitea

    - role: kubernetes/argocd
      when: kubernetes_with_argocd | default(true)
      tags:
        - argocd

    - role: kubernetes/bootstrap
      when: kubernetes_with_bootstrap | default(true)
      tags:
        - bootstrap
