version: 2
jobs:
  validate:
    resource_class: xlarge
    docker:
      - image: circleci/golang:1.13
        environment:
          GO111MODULE: "on"
          GOPROXY: https://proxy.golang.org
    working_directory: /go/src/github.com/virtual-kubelet/virtual-kubelet
    steps:
      - checkout
      - restore_cache:
          keys:
            - validate-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}
      - run:
          name: go vet
          command: V=1 CI=1 make vet
      - run:
          name: Install linters
          command: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.17.1 && mv ./bin/* /go/bin/
      - run:
          name: Lint
          command: golangci-lint run ./...
      - run:
          name: Dependencies
          command: scripts/validate/gomod.sh
      - save_cache:
          key: validate-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

  test:
    resource_class: xlarge
    docker:
      - image: circleci/golang:1.13
        environment:
          GO111MODULE: "on"
    working_directory: /go/src/github.com/virtual-kubelet/virtual-kubelet
    steps:
      - checkout
      - restore_cache:
          keys:
            - test-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}
      - run:
          name: Build
          command: V=1 make build
      - run:
          name: Tests
          command: V=1 CI=1 make test
      - save_cache:
          key: test-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

  e2e:
    machine:
      image: circleci/classic:201808-01
    working_directory: /home/circleci/go/src/github.com/virtual-kubelet/virtual-kubelet
    environment:
      CHANGE_MINIKUBE_NONE_USER: true
      GOPATH: /home/circleci/go
      KUBECONFIG: /home/circleci/.kube/config
      KUBERNETES_VERSION: v1.17.6
      MINIKUBE_HOME: /home/circleci
      MINIKUBE_VERSION: v1.2.0
      MINIKUBE_WANTUPDATENOTIFICATION: false
      MINIKUBE_WANTREPORTERRORPROMPT: false
      SKAFFOLD_VERSION: v0.33.0
      GO111MODULE: "on"
    steps:
      - checkout
      - run:
          name: Install kubectl
          command: |
            curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/${KUBERNETES_VERSION}/bin/linux/amd64/kubectl
            chmod +x kubectl
            sudo mv kubectl /usr/local/bin/
            mkdir -p ${HOME}/.kube
            touch ${HOME}/.kube/config
      - run:
          name: Install Skaffold
          command: |
            curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/${SKAFFOLD_VERSION}/skaffold-linux-amd64
            chmod +x skaffold
            sudo mv skaffold /usr/local/bin/
      - run:
          name: Install Minikube
          command: |
            curl -Lo minikube https://storage.googleapis.com/minikube/releases/${MINIKUBE_VERSION}/minikube-linux-amd64
            chmod +x minikube
            sudo mv minikube /usr/local/bin/
      - run:
          name: Start Minikube
          command: |
            sudo -E minikube start --vm-driver=none --cpus 2 --memory 2048 --kubernetes-version=${KUBERNETES_VERSION}
      - run:
          name: Wait for Minikube
          command: |
            JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}';
            until kubectl get nodes -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do
              sleep 1;
            done
      - run:
          name: Watch pods
          command: kubectl get pods -o json --watch
          background: true
      - run:
          name: Watch nodes
          command: kubectl get nodes -o json --watch
          background: true
      - restore_cache:
          keys:
            - e2e-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}-2
      - run:
          name: Run the end-to-end test suite
          command: |
            mkdir $HOME/.go
            export PATH=$HOME/.go/bin:${PATH}
            curl -fsSL -o "/tmp/go.tar.gz" "https://dl.google.com/go/go1.13.12.linux-amd64.tar.gz"
            tar -C $HOME/.go --strip-components=1 -xzf "/tmp/go.tar.gz"
            go version
            make e2e
      - save_cache:
          key: e2e-{{ checksum "go.mod" }}-{{ checksum "go.sum" }}-2
          paths:
            - "/home/circleci/go/pkg/mod"
      - run:
          name: Collect logs on failure from vkubelet-mock-0
          command: |
            kubectl logs vkubelet-mock-0
          when: on_fail

workflows:
  version: 2
  validate_and_test:
    jobs:
      - validate
      - test
      - e2e:
          requires:
            - validate
            - test
