name: Lint and Test Charts

# code and jobs are mostly taken from https://jamiemagee.co.uk/blog/how-to-host-your-helm-chart-repository-on-github/
# and adapted for newer version of the test tools
#
# - ct - Helm project created Chart Testing, AKA ct, as a comprehensive linting tool for Helm charts
# - Helm-docs - not strictly a linting tool, but it makes sure that your documentation stays up-to-date with the current
#   state of your chart
# - Kubeval - It validates the output from Helm against schemas generated from the Kubernetes OpenAPI specification
#
# not used by now:
# - Kubernetes in Docker (KIND) - use Chart Testing again to install your Helm charts on a Kubernetes cluster running in
#   the GitHub Actions runner using Kubernetes in Docker (KIND)

on:
  pull_request:
    paths:
      - 'k8s/helm-chart/**'
  push:
    branches-ignore:
      - 'gh-pages'
    paths:
      - 'k8s/helm-chart/**'

env:
  HELM_VERSION: v3.8.1

jobs:
  lint-chart:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Helm
        uses: azure/setup-helm@v4
        with:
          version: ${{ env.HELM_VERSION }}
      - uses: actions/setup-python@v5
        with:
          python-version: "3.10"
      - name: Set up chart-testing
        uses: helm/chart-testing-action@v2.7.0
      - name: Run chart-testing (lint)
        run: "ct lint --config .github/ct.yml --chart-dirs k8/helm-chart"

  lint-docs:
    runs-on: ubuntu-latest
    needs: lint-chart
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Run helm-docs
        run: ".github/helm-docs.sh"

  kubeval-chart:
    runs-on: ubuntu-latest
    needs:
      - lint-chart
      - lint-docs
    strategy:
      matrix:
        k8s:
          - v1.11.10  # for openshift 3.11
          #- v1.16.15
          #- v1.17.14
          #- v1.18.20
          - v1.19.16
          #- v1.20.15
          - v1.21.14
          #- v1.22.15
          - v1.23.12
          #- v1.24.6
          - v1.25.10
          - v1.27.2
        include:
          - k8s: v1.11.10
            legacy_ingress: true
          - k8s: v1.19.16
            legacy_ingress: true
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Helm
        uses: azure/setup-helm@v4
        with:
          version: ${{ env.HELM_VERSION }}
      - name: Run kubeval with legacy ingress for older k8s
        if: ${{ matrix.legacy_ingress }}
        env:
          KUBERNETES_VERSION: ${{ matrix.k8s }}
          HELM_VALUES: "--set ingress.enabled=true --set ingress.legacy=true"
        run: ".github/kubeval.sh"
      - name: Run kubeval with new ingress
        if: ${{ ! matrix.legacy_ingress }}
        env:
          KUBERNETES_VERSION: ${{ matrix.k8s }}
          HELM_VALUES: "--set ingress.enabled=true"
        run: ".github/kubeval.sh"

#  install-chart:
#    name: install chart on KIND
#    runs-on: ubuntu-latest
#    needs:
#      - lint-chart
#      - lint-docs
#      - kubeval-chart
#    strategy:
#      matrix:
#        # not all k8s version are supported - check https://hub.docker.com/r/kindest/node/tags?page=1&ordering=last_updated
#        k8s:
#          - v1.11.10  # for openshift 3.11
#          #- v1.16.15
#          #- v1.17.17
#          - v1.18.20
#          - v1.19.16
#          - v1.20.15
#          - v1.21.14
#          - v1.22.15
#          - v1.23.12
#          - v1.24.6
#          - v1.25.2
#    steps:
#      - name: Checkout
#        uses: actions/checkout@v4
#      - name: Create kind ${{ matrix.k8s }} cluster
#        uses: helm/kind-action@master
#        with:
#          node_image: kindest/node:${{ matrix.k8s }}
#      - name: Run chart-testing (install)
#        uses: helm/chart-testing-action@master
#        with:
#          command: install
#          config: .github/ct.yaml
