---
# stash media manager

# helm should be run from local environment
- name: check we can access the cluster
  local_action:
    module: kubernetes.core.helm_info
#    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    context: "{{kube_context}}"
    name: test
    release_namespace: kube-system
#    release_state: all
  register: default_namespace

- name: create persistent volume resources
  kubernetes.core.k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    state: present
    definition: "{{ lookup('file', item) | from_yaml }}"
  loop:
    - stash/pv02.yaml
    - stash/pv-claim.yaml
#    - stash/pv-media.yaml
#    - stash/pv-media-claim.yaml
  become: true

- name: Add stash chart helm repo
  local_action:
    module: kubernetes.core.helm_repository
    name: k8s-at-home
    repo_url: https://k8s-at-home.com/charts/

- name: load variables files/stash/values.yaml
  ansible.builtin.include_vars:
    file: files/stash/values.yaml
    name: stash_values

- name: Install stash Chart
  local_action:
    module: kubernetes.core.helm
    release_state:  "{{ apps.stash.state }}"
    name: stash
    namespace: "{{ apps.stash.namespace }}"
    create_namespace: yes
    update_repo_cache: True
    chart_ref: k8s-at-home/stash
    values: "{{stash_values}}"
    wait: true

- name: Install oauth2-proxy in front of stash app
  block:
    - name: Add oauth2-proxy chart helm repo
      local_action:
        module: kubernetes.core.helm_repository
        name: oauth2-proxy
        repo_url: https://oauth2-proxy.github.io/manifests

    - name: load variables files/stash/proxy-values.yaml
      ansible.builtin.include_vars:
        file: files/stash/proxy-values.yaml
        name: proxy_values

    - name: Install oauth2-proxy Release
      local_action:
        module: kubernetes.core.helm
        release_state: "{{ apps.stash.state }}"
        name: oauth2-proxy
        namespace: "{{ apps.stash.namespace }}"
        create_namespace: no
        update_repo_cache: True
        chart_ref: oauth2-proxy/oauth2-proxy
        values: "{{proxy_values}}"
        wait: true
