version: 2.1

orbs:
  kurtosis-docs-checker: kurtosis-tech/docs-checker@0.2.3

executors:
  ubuntu_vm:
    machine:
      image: ubuntu-2204:current

parameters:
  should-enable-check-latest-version-workflow:
    type: boolean
    default: false
  # To enable/disabled the check_code workflow execution which will be triggered by the PR's checkers
  should-enable-build-workflow:
    type: boolean
    default: true
  kurtosis-cluster-setting-abs-filepath:
    type: string
    default: "/home/circleci/.local/share/kurtosis/cluster-setting"


# Install go version 1.19 to run our integration tests on the minimal version we support
setup_kurtosis: &setup_kurtosis
  - run: |
      echo "deb [trusted=yes] https://apt.fury.io/kurtosis-tech/ /" | sudo tee /etc/apt/sources.list.d/kurtosis.list
      sudo apt update
      sudo apt install kurtosis-cli
      kurtosis analytics disable
      kurtosis engine restart

# Steps to prepare a job for Kubernetes testing (with K3S K8S distribution)
setup_kurtosis_k3s: &setup_kurtosis_k3s
  pre-steps:
    - run:
        name: Install Kurtosis
        command: |
          echo "deb [trusted=yes] https://apt.fury.io/kurtosis-tech/ /" | sudo tee /etc/apt/sources.list.d/kurtosis.list
          sudo apt update
          sudo apt install kurtosis-cli
          kurtosis analytics disable
    - run:
        name: Install K3D and create the K3D/K3S cluster on Docker
        command: |
          curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
          k3d cluster create --servers 1 --no-lb --wait --verbose
    # Edit the Kurtosis config file in order to have K3S cluster configuration
    - run:
        name: Add K3S cluster config in Kurtosis config file
        command: |
          KURTOSIS_CONFIG_FILEPATH=$(kurtosis config path)
          cat \<< EOF > "$KURTOSIS_CONFIG_FILEPATH"
          config-version: 2
          should-send-metrics: true
          kurtosis-clusters:
            docker:
              type: docker
            k3d-k3s-default:
              type: kubernetes
              config:
                kubernetes-cluster-name: k3d-k3s-default
                storage-class: local-path
                enclave-size-in-megabytes: 2048
          EOF
    # Set the K3S cluster with some previous steps to force this cluster type in the cluster-setting file. This save us to start the engine with the cluster set command
    # because we are going to start it on the next step
    - run:
        name: Set K3S Kurtosis cluster
        command: |
          KURTOSIS_CLUSTER_SETTING_FILEPATH="<< pipeline.parameters.kurtosis-cluster-setting-abs-filepath >>"
          echo -n "k3d-k3s-default" > "$KURTOSIS_CLUSTER_SETTING_FILEPATH"
          echo "Kurtosis cluster-setting file content: $(cat $KURTOSIS_CLUSTER_SETTING_FILEPATH)"
          kurtosis cluster set k3d-k3s-default --cli-log-level trace
    - run:
        name: Start Kurtosis engine in K8s backend
        command: |
          kurtosis engine start --enclave-pool-size 2
    - run:
        name: Run Kurtosis gateway
        command: "kurtosis gateway"
        background: true


# NOTE: Because CircleCI jobs run on separate machines from each other, we duplicate steps (like checkout) between jobs. This is because doing the "correct" DRY
#  refactoring of, "one job for checkout, one job for build Docker image, etc." would require a) persisting files between jobs and b) persisting Docker images between
#  jobs. Both are annoying (saving/loading workspaces require re-downloading the workspace over the network, and there doesn't seem to be a good way to do Docker
#  images), so we run everything inside a single job.
# See also: https://discuss.circleci.com/t/can-docker-images-be-preserved-between-jobs-in-a-workflow-without-a-manual-load-save/23388/12
jobs:
  run_starlark_arm64:
    executor: ubuntu_vm
    resource_class: arm.medium
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD}

  mev_k8s:
    executor: ubuntu_vm
    steps:
      - checkout
      - run: kurtosis run ${PWD} --verbosity detailed --args-file=./.github/tests/mev.yaml

  mix_with_tools_k8s:
    resource_class: xlarge
    executor: ubuntu_vm
    steps:
      - checkout
      - run: kurtosis run ${PWD} --verbosity detailed --args-file=./.github/tests/mev.yaml

workflows:
  nightly_tests:
    when: << pipeline.parameters.should-enable-check-latest-version-workflow >>
    jobs:
      - mev_k8s:
          <<: *setup_kurtosis_k3s
      - mix_with_tools_k8s:
          <<: *setup_kurtosis_k3s
      - run_starlark_arm64:
          filters:
            branches:
              ignore:
                - main