---

### tags:
###   namespace

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

- name: "Create docker registry secret for namespace <{{ k8s_namespace }}"
  become: yes
  kubernetes.core.k8s:
    state: present
    merge_type: merge
    definition:
      apiVersion: v1
      data:
        .dockerconfigjson: "{{ lookup('template', 'docker-secret.json.j2') | to_json | b64encode }}"
      kind: Secret
      metadata:
        name: "{{ k8s_docker_registry_key }}"
        namespace: "{{ k8s_namespace }}"
      type: kubernetes.io/dockerconfigjson
  when:
  - k8s_namespace is defined
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - namespace

- name: "Create secrets for <{{ k8s_namespace }}>"
  become: yes
  kubernetes.core.k8s:
    definition:
      api_version: v1
      kind: Secret
      metadata:
        namespace: "{{ k8s_namespace }}"
        name: "{{ item.name }}"
      type: Opaque
      data: "{{ item.data }}"
  loop: "{{ k8s_secrets }}"
  when: 
  - k8s_namespace is defined
  - inventory_hostname == groups['kube_control_plane'][0]
  tags:
    - namespace
