name: 'install-cilium-cli'
description: 'Install Cilium CLI'
inputs:
  repository:
    description: 'Repository from which the release is downloaded'
    default: 'cilium/cilium-cli'
  release-version:
    description: 'Cilium CLI release version'
  ci-version:
    description: 'Cilium CLI CI build version'
  local-path:
    description: 'Path to the local copy of the Cilium CLI repository'
    default: '*/cilium-cli'
  go-mod-directory:
    description: >
      Specify the directory that contains go.mod when building the Cilium CLI
      from the source. By default, This action assumes that go.mod is in the
      top-level directory.
    required: true
    default: '.'
  binary-dir:
    description: 'Directory to store Cilium CLI executable'
    required: true
    default: '/usr/local/bin'
  binary-name:
    description: 'Cilium CLI executable name'
    required: true
    default: 'cilium'
  skip-build:
    description: 'Skip building CLI from source'
    default: 'false'
  image-repo:
    description: 'Container image repo to download cilium-cli image from'
    default: 'quay.io/cilium/cilium-cli-ci'
  image-tag:
    description: >
      Container image tag to use. If this input parameter is specified, this
      action downloads the container image and sets up Cilium CLI to be executed
      inside a container.
  kubeconfig:
    description: >
      Kubeconfig to be used by cilium-cli in docker
    default: '~/.kube/config'
runs:
  using: "composite"
  steps:
    - name: Check if we should build the Cilium CLI from source
      if: ${{ inputs.skip-build != 'true' }}
      id: build-cli
      shell: bash
      run: |
        CLI_PATH=$(find . -iwholename '${{ inputs.local-path }}' -type d -not -path './.git/*' -not -path './vendor/*' | sort | tail -n 1)
        echo path="${CLI_PATH}" >> $GITHUB_OUTPUT
        echo go-mod-path="${{ inputs.go-mod-directory }}/go.mod" >> $GITHUB_OUTPUT

    - name: Pick a version to install
      id: target
      shell: bash
      run: |
        if [[ -n "${{ inputs.release-version }}" ]]; then
          echo release="true" >> $GITHUB_OUTPUT
        elif [[ -n "${{ steps.build-cli.outputs.path }}" ]]; then
          echo build="true" >> $GITHUB_OUTPUT
        elif [[ -n "${{ inputs.image-tag }}" ]]; then
          echo image="true" >> $GITHUB_OUTPUT
        elif [[ -n "${{ inputs.ci-version }}" ]]; then
          echo ci_image="true" >> $GITHUB_OUTPUT
        else
          echo "One of 'release-version', 'ci-version', or 'image-tag' has to be specified!"
          exit 42
        fi

    - name: Setup Go
      uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
      if: ${{ steps.target.outputs.build != '' }}
      with:
        go-version-file: '${{ steps.build-cli.outputs.go-mod-path }}'
        cache: true
        cache-dependency-path: '**/go.sum'

    - name: Build Cilium CLI from source
      if: ${{ steps.target.outputs.build != '' }}
      shell: bash
      run: |
        TARGET=/tmp/cilium
        make -C ${{ steps.build-cli.outputs.path }} TARGET=${TARGET}
        # Install the binary in a separate step (rather than executing make install)
        # to avoid building the binary as root, which would cause issues with caching.
        sudo mv ${TARGET} ${{ inputs.binary-dir }}/${{ inputs.binary-name }}

    - name: Install Released Cilium CLI
      if: ${{ steps.target.outputs.release != '' }}
      shell: bash
      run: |
        curl -sSL --remote-name-all https://github.com/${{ inputs.repository }}/releases/download/${{ inputs.release-version }}/cilium-linux-amd64.tar.gz{,.sha256sum}
        sha256sum --check cilium-linux-amd64.tar.gz.sha256sum
        tar xzvfC cilium-linux-amd64.tar.gz /tmp
        sudo mv /tmp/cilium ${{ inputs.binary-dir }}/${{ inputs.binary-name }}
        rm cilium-linux-amd64.tar.gz{,.sha256sum}

    - name: Install Cilium CLI from CI
      if: ${{ steps.target.outputs.ci_image != '' }}
      shell: bash
      run: |
        cid=$(docker create ${{ inputs.image-repo }}:${{ inputs.ci-version }} ls)
        docker cp $cid:/usr/local/bin/cilium ${{ inputs.binary-dir }}/${{ inputs.binary-name }}
        docker rm $cid

    - name: Set up Cilium CLI to be executed inside a container
      if: ${{ steps.target.outputs.image != '' && inputs.image-tag != '' }}
      shell: bash
      run: |
        until docker pull ${{ inputs.image-repo }}:${{ inputs.image-tag }} &> /dev/null
        do
          echo "Waiting for ${{ inputs.image-repo }}:${{ inputs.image-tag }} image to become available..."
          sleep 10
        done
        export CILIUM_CLI_IMAGE_REPO=${{ inputs.image-repo }}
        export CILIUM_CLI_IMAGE_TAG=${{ inputs.image-tag }}
        export KUBECONFIG=${{ inputs.kubeconfig }}
        cat ${{ github.action_path }}/.github/tools/cilium.sh | envsubst > /tmp/cilium
        sudo install /tmp/cilium ${{ inputs.binary-dir }}/${{ inputs.binary-name }}

    - name: Run Cilium CLI Version
      shell: bash
      run: |
        ${{ inputs.binary-dir }}/${{ inputs.binary-name }} version --client
