name: k-bench
description: "Run K-Bench"

inputs:
  cloudProvider:
    description: "Which cloud provider to use."
    required: true
  kubeconfig:
    description: "The kubeconfig of the cluster to test."
    required: true

runs:
  using: "composite"

  steps:
    - name: Setup python
      uses: actions/setup-python@2c3dd9e7e29afd70cc0950079bde6c979d1f69f9 # v4.3.1
      with:
        python-version: "3.10"

    - name: Install evaluation dependencies
      shell: bash
      run: pip install -r .github/actions/e2e_kbench/evaluate/requirements.txt

    - name: Checkout patched K-Bench
      uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
      with:
        fetch-depth: 0
        repository: "edgelesssys/k-bench"
        ref: "feat/constellation"
        path: k-bench

    - name: Install patched K-Bench
      working-directory: "k-bench"
      run: ./install.sh
      shell: bash

    - name: Run K-Bench
      shell: bash
      working-directory: k-bench
      run: |
        mkdir -p ./out
        kubectl create namespace kbench-pod-namespace --dry-run=client -o yaml | kubectl apply -f -
        kubectl apply -f ../.github/actions/e2e_kbench/encrypted_storage.yml
        ./run.sh -r "kbench-constellation-${{ inputs.cloudProvider }}" -t "default" -o "./out/"
        kubectl delete namespace kbench-pod-namespace --wait=true || true
        kubectl create namespace kbench-pod-namespace --dry-run=client -o yaml |  kubectl apply -f -
        kubectl apply -f ../.github/actions/e2e_kbench/encrypted_storage.yml
        ./run.sh -r "kbench-constellation-${{ inputs.cloudProvider }}" -t "dp_fio" -o "./out/"
        kubectl delete namespace kbench-pod-namespace --wait=true || true
        kubectl create namespace kbench-pod-namespace --dry-run=client -o yaml |  kubectl apply -f -
        kubectl apply -f ../.github/actions/e2e_kbench/encrypted_storage.yml
        ./run.sh -r "kbench-constellation-${{ inputs.cloudProvider }}" -t "dp_network_internode" -o "./out/"
        kubectl delete namespace kbench-pod-namespace --wait=true || true
        kubectl create namespace kbench-pod-namespace --dry-run=client -o yaml |  kubectl apply -f -
        kubectl apply -f ../.github/actions/e2e_kbench/encrypted_storage.yml
        ./run.sh -r "kbench-constellation-${{ inputs.cloudProvider }}" -t "dp_network_intranode" -o "./out/"
      env:
        KUBECONFIG: ${{ inputs.kubeconfig }}

    - name: Merge K-Bench results
      working-directory: k-bench
      shell: bash
      run: |
        mkdir -p "./out/kbench-constellation-${{ inputs.cloudProvider }}"
        mv ./out/results_kbench-constellation-${{ inputs.cloudProvider }}_*m/* "./out/kbench-constellation-${{ inputs.cloudProvider }}/"
        ls -l "./out/kbench-constellation-${{ inputs.cloudProvider }}"
        cat ./out/kbench-constellation-${{ inputs.cloudProvider }}/*/kbench.log

    - name: Upload original benchmark results
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb # tag=v3.1.1
      if: ${{ !env.ACT }}
      with:
        path: "k-bench/out/kbench-constellation-${{ inputs.cloudProvider }}"
        name: "k-bench-constellation-${{ inputs.cloudProvider }}"

    - name: Assume AWS role to retrieve and update benchmarks in S3
      uses: aws-actions/configure-aws-credentials@67fbcbb121271f7775d2e7715933280b06314838 # tag=v1.7.0
      with:
        role-to-assume: arn:aws:iam::795746500882:role/GithubActionUpdateBenchmarks
        aws-region: us-east-2

    - name: Set S3 artifact store
      shell: bash
      run: echo S3_PATH=s3://${ARTIFACT_BUCKET_CONSTELLATION}/benchmarks >> $GITHUB_ENV
      env:
        ARTIFACT_BUCKET_CONSTELLATION: "edgeless-artifact-store/constellation"

    - name: Get previous benchmark records from S3
      shell: bash
      run: |
        mkdir -p benchmarks
        aws s3 cp --recursive ${S3_PATH} benchmarks --no-progress
        mv benchmarks/constellation-${CSP}.json benchmarks/constellation-${CSP}-previous.json
      env:
        KBENCH_RESULTS: ${{ github.workspace }}/k-bench/out/
        CSP: ${{ inputs.cloudProvider }}

    - name: Parse K-Bench results, create diagrams and post the progression summary
      shell: bash
      run: |
        python .github/actions/e2e_kbench/evaluate/parse.py
        python .github/actions/e2e_kbench/evaluate/graph.py
        python .github/actions/e2e_kbench/evaluate/compare.py >> $GITHUB_STEP_SUMMARY
      env:
        # Original K-Bench result directory
        KBENCH_RESULTS: k-bench/out/
        # Working directory containing the previous results as JSON and to contain the graphs
        BDIR: benchmarks
        # Paths to benchmark results as JSON of the previous run and the current run
        PREV_BENCH: benchmarks/constellation-${{ inputs.cloudProvider }}-previous.json
        CURR_BENCH: benchmarks/constellation-${{ inputs.cloudProvider }}.json
        CSP: ${{ inputs.cloudProvider }}

    - name: Upload benchmark results and graphs to action run
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb # tag=v3.1.1
      if: ${{ !env.ACT }}
      with:
        path: |
          benchmarks/*_perf.png
          benchmarks/constellation-${{ inputs.cloudProvider }}.json
        name: "benchmarks"

    - name: Update benchmark records in S3
      shell: bash
      run: |
        aws s3 cp benchmarks/constellation-${CSP}.json ${S3_PATH}/constellation-${CSP}.json
      env:
        CSP: ${{ inputs.cloudProvider }}
      if: github.ref == 'refs/heads/main'
