---
- name: "Create namespace <{{ k8s_argocd_helm__release_namespace }}>"
  become: yes
  kubernetes.core.k8s:
    name: "{{ k8s_argocd_helm__release_namespace }}"
    api_version: v1
    kind: Namespace
    state: present
  when:
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - argo-cd

- name: "Create a k8s Secret containing GPG key"
  become: yes
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: v1
      data:
       gpg_key_smardigo_automation__private: '{{ gpg_key_smardigo_automation__private | string | b64encode }}'
      kind: Secret
      metadata:
        name: sops-gpg
        namespace: '{{ k8s_argocd_helm__release_namespace }}'
      type: Opaque
  when:
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - argo-cd

# - name: "Create VAR to overwrite specific helm value - prepare combining dicts"
#   set_fact:
#     additional_helm_values:
#       configs:
#         secret:
#           extra:
#             oidc.keycloak.clientSecret: '{{ client_secret.json.value }}'
#   when:
#   - inventory_hostname == groups['kube_control_plane'][0]
#   tags:
#     - argo-cd

- name: "Combining helm release values"
  set_fact:
    combined_helm__release_values: '{{ k8s_argocd_helm__release_values | combine(additional_helm_values| default({}), recursive=True) }}'
  when:
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - argo-cd

- name: "DEBUG"
  debug:
    msg: "DEBUGGING: {{ combined_helm__release_values }}"
  when:
  - debug
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - argo-cd

- name: Deploy argo-cd inside argo-cd namespace
  become: yes
  kubernetes.core.helm:
    create_namespace: yes
    name: "{{ k8s_argocd_helm__name }}"
    chart_ref: "{{ k8s_argocd_helm__chart_ref | default('argo-cd') }}"
    chart_repo_url: "{{ k8s_argocd_helm__chart_repo_url | default('https://argoproj.github.io/argo-helm') }}"
    chart_version: "{{ k8s_argocd_helm__chart_version }}"
    release_namespace: "{{ k8s_argocd_helm__release_namespace }}"
    release_values: "{{ combined_helm__release_values }}"
  when:
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - argo-cd
