---
- name: adds the kubernetes repo and installs kubectl
  hosts: docker
  become: true
  tasks:
########## KUBECTL INSTALL ##########
  - name: update apt cache
    ansible.builtin.apt:
      update_cache: true

  - name: install core pre-reqs
    ansible.builtin.apt:
      pkg:
        - apt-transport-https
        - ca-certificates
        - curl
        - gnupg

  - name: check to see if gpg key exists
    ansible.builtin.stat:
      path: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    register: st

  - name: download the key and install it
    ansible.builtin.shell: |
      curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
      sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg # allow unprivileged APT programs to read this keyring
    when: st.stat.exists != true

  - name: add the kubernetes apt repository
    ansible.builtin.apt_repository:
      repo: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.32/deb/ /"
      state: present
      filename: kubernetes.list

  - name: update apt cache and install kubectl
    ansible.builtin.apt:
      name: kubectl
      update_cache: true

########## ARGOCD INSTALL ##########
  - name: does argocd exists
    ansible.builtin.stat:
      path: /usr/local/bin/argocd
    register: st
  - name: install argocd
    ansible.builtin.shell: |
      VERSION=$(curl -L -s https://raw.githubusercontent.com/argoproj/argo-cd/stable/VERSION)
      curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/download/v$VERSION/argocd-linux-amd64
      sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
      rm argocd-linux-amd64
    when: st.stat.exists != true

########## HELM INSTALL ##########
  - name: install repository and key (not idempotent)
    ansible.builtin.shell: |
      curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
      apt update

  - name: install helm kubernetes package manager
    ansible.builtin.apt:
      name: helm
