---
#https://github.com/twuni/docker-registry.helm



#- name: Create a namespace for docker registry
#  k8s:
#    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
#    name: "{{apps.dkregistry.namespace}}"
#    api_version: v1
#    kind: Namespace
#    state: "{{apps.dkregistry.state}}"
#  become: true

- name: create persistent volume resources
  kubernetes.core.k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    state: "{{apps.dkregistry.state}}"
    definition: "{{ lookup('template', item) | from_yaml }}"
  loop:
    - dkregistry/pv.yaml
    - dkregistry/pv-claim.yaml
    - dkregistry/pv-auth.yaml
    - dkregistry/pv-auth-claim.yaml
  become: true

- name: create secret for dkregistry
  kubernetes.core.k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    state: "{{apps.dkregistry.state}}"
    definition:
      apiVersion: v1
      kind: Secret
      type: Opaque
      metadata:
        name: auth-secret
        namespace: "{{apps.dkregistry.namespace}}"
      stringData:
        htpassword: "richard:$2y$05$Zp.GEiUbsGYYVOYWE71truuERCAE.D5wwGzU3Xi3wIVAWjH60t/U."
  become: true

- name: create docker-registry resources
  kubernetes.core.k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    state: "{{apps.dkregistry.state}}"
    definition: "{{ lookup('template', item) | from_yaml }}"
  loop:
#    - dkregistry/configmap.yaml
    - dkregistry/ingress.yaml
    - dkregistry/service.yaml
    - dkregistry/deployment.yaml
  become: true


#- name: Install dkregistry globally available
#  block:
#    - name: Add dkregistry chart helm repo
#      local_action:
#        module: kubernetes.core.helm_repository
#        name: twuni
#        repo_url: https://helm.twun.io
#
#    - name: load variables files/dkregistry/values.yaml
#      ansible.builtin.include_vars:
#        file: files/dkregistry/values.yaml
#        name: stash_values
#
#    - name: Install dkregistry Release
#      local_action:
#        module: kubernetes.core.helm
#        release_state: "{{apps.dkregistry.state}}"
#        name: dkregistry
#        namespace: "{{apps.dkregistry.namespace}}"
#        create_namespace: yes
#        update_repo_cache: True
#        chart_ref: twuni/docker-registry
#        values: "{{stash_values}}"
#        wait: true
