From 4788b7f8ffa1aa3ddb2d5b6a80bae5c26349c756 Mon Sep 17 00:00:00 2001 From: Alexander Chernov Date: Sat, 10 Dec 2022 11:35:53 +0000 Subject: [PATCH] feat: CICD refactoring --- .github/workflows/build.yml | 92 ----------------------------------- .github/workflows/tests.yaml | 59 ++++++++++++++++++++++ Makefile | 19 +------- README.md | 2 +- scripts/build.sh | 31 ------------ scripts/docker-compose.yaml | 28 ----------- scripts/get-version-matrix.sh | 9 ++++ scripts/registries.yaml | 4 -- scripts/start-k3s.sh | 61 ----------------------- scripts/stop-k3s.sh | 11 ----- 10 files changed, 70 insertions(+), 246 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/tests.yaml delete mode 100755 scripts/build.sh delete mode 100644 scripts/docker-compose.yaml create mode 100755 scripts/get-version-matrix.sh delete mode 100644 scripts/registries.yaml delete mode 100755 scripts/start-k3s.sh delete mode 100755 scripts/stop-k3s.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 7f5e2552..00000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,92 +0,0 @@ -name: "Build" - -on: - push: - branches: [master] - pull_request: - branches: [master] -jobs: - build: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v2 - - name: Set up GO 1.18.x - uses: actions/setup-go@v1 - with: - go-version: '1.18' - - run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - - name: Setup Environment - run: make ci-build-setup - - name: Build - run: make build - test: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v2 - - name: Set up GO 1.18.x - uses: actions/setup-go@v1 - with: - go-version: '1.18' - - run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - - name: Setup Environment - run: make ci-build-setup - - name: Run unit tests - run: make test - testacc: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v2 - - name: Set up GO 1.18.x - uses: actions/setup-go@v1 - with: - go-version: '1.18' - - run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH -# - name: Login to Docker Hub -# uses: docker/login-action@v1 -# with: -# username: ${{ secrets.DOCKERHUB_USERNAME }} -# password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Setup Environment - run: make ci-build-setup - - name: Start k3s - run: make k3s-start - - name: Run acceptance tests - run: make testacc - - name: Stop k3s - run: make k3s-stop - fmtcheck: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v2 - - name: Set up GO 1.18.x - uses: actions/setup-go@v1 - with: - go-version: '1.18' - - run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - - name: Setup Environment - run: make ci-build-setup - - name: Format check - run: make fmtcheck - vet: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v2 - - name: Set up GO 1.18.x - uses: actions/setup-go@v1 - with: - go-version: '1.18' - - run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - - name: Setup Environment - run: make ci-build-setup - - name: Run vet - run: make vet diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 00000000..e67ac2f2 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,59 @@ +name: "tests" +on: + push: + branches: + - master + pull_request: + branches: + - master + +env: + KUBECONFIG: ${{ github.workspace }}/.kube/config + +jobs: + get_version_matrix: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - id: get_version_matrix + shell: bash + run: scripts/get-version-matrix.sh + outputs: + version_matrix: ${{ steps.get_version_matrix.outputs.matrix }} + unit_test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: go.mod + - name: Unit tests + env: + GOPATH: /home/runner/go + run: | + make test + make vet + acc_test: + needs: get_version_matrix + runs-on: ubuntu-latest + strategy: + matrix: + terraform_version: ${{ fromJson(needs.get_version_matrix.outputs.version_matrix) }} + steps: + - uses: actions/checkout@master + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: go.mod + - uses: helm/kind-action@v1.4.0 + id: kind + with: + wait: 2m + - name: Acceptance Tests + env: + KUBE_CONFIG_PATH: ${{ env.KUBECONFIG }} + TF_ACC_TERRAFORM_VERSION: ${{ matrix.terraform_version }} + TESTARGS: "-parallel 1" + run: | + make testacc diff --git a/Makefile b/Makefile index 74ce2569..0b4b656b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ PKG_NAME=kubernetes export GO111MODULE=on export TESTARGS=-race -coverprofile=coverage.txt -covermode=atomic -export KUBECONFIG=$(CURRENT_DIR)/scripts/kubeconfig.yaml default: build @@ -23,12 +22,6 @@ test: testacc: TF_ACC=1 go test ./kubernetes -v $(TESTARGS) -timeout 120m -count=1 -k3s-start: - @bash scripts/start-k3s.sh - -k3s-stop: - @bash scripts/stop-k3s.sh - publish: goreleaser release --rm-dist @@ -50,14 +43,4 @@ fmtcheck: errcheck: @sh -c "'$(CURDIR)/scripts/errcheck.sh'" -ci-build-setup: - sudo rm /usr/local/bin/docker-compose - curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > docker-compose - chmod +x docker-compose - sudo mv docker-compose /usr/local/bin - curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.20.7/bin/linux/amd64/kubectl - chmod +x kubectl - sudo mv kubectl /usr/local/bin/ - bash scripts/gogetcookie.sh - -.PHONY: build dist test testacc k3s-start k3s-stop publish vet fmt fmtcheck errcheck ci-build-setup +.PHONY: build dist test testacc publish vet fmt fmtcheck errcheck diff --git a/README.md b/README.md index 9a2ebd91..756bbf8a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Kubernetes "kubectl" Provider -![Build Status](https://github.com/alekc/terraform-provider-kubectl/actions/workflows/build.yml/badge.svg) [![user guide](https://img.shields.io/badge/-user%20guide-blue)](https://registry.terraform.io/providers/alekc/kubectl) +![Build Status](https://github.com/alekc/terraform-provider-kubectl/actions/workflows/tests.yml/badge.svg) [![user guide](https://img.shields.io/badge/-user%20guide-blue)](https://registry.terraform.io/providers/alekc/kubectl) This provider is the best way of managing Kubernetes resources in Terraform, by allowing you to use the thing Kubernetes loves best - yaml! diff --git a/scripts/build.sh b/scripts/build.sh deleted file mode 100755 index 0e958795..00000000 --- a/scripts/build.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash -set -e -cd `dirname $0` -cd .. - -# This script builds executables for multiple platforms and architectures -# it is used by the CI system to output releases. When testing locally it shouldn't be required -# unless you wish to share a build with someone on a different platform -platforms=("linux/amd64" "windows/amd64" "windows/386" "darwin/amd64" "linux/386" "linux/arm") - -for platform in "${platforms[@]}" -do - platform_split=(${platform//\// }) - GOOS=${platform_split[0]} - GOARCH=${platform_split[1]} - CGO_ENABLED=0 - output_name='./bin/terraform-provider-kubectl-'$GOOS'-'$GOARCH - if [ $GOOS = "windows" ]; then - output_name+='.exe' - fi - echo "Building for $GOOS $GOARCH..." - - GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED go build -a -installsuffix cgo -o $output_name - - if [ $? -ne 0 ]; then - echo 'An error has occurred! Aborting the script execution...' - exit 1 - fi -done - -echo "Completed builds, for output see ./bin" diff --git a/scripts/docker-compose.yaml b/scripts/docker-compose.yaml deleted file mode 100644 index 2ea26db2..00000000 --- a/scripts/docker-compose.yaml +++ /dev/null @@ -1,28 +0,0 @@ -version: '3' -services: - server: - image: rancher/k3s:v1.20.7-k3s1 - command: server --disable-agent --tls-san 172.17.0.1 - environment: - - K3S_CLUSTER_SECRET=somethingtotallyrandom - - K3S_KUBECONFIG_OUTPUT=/output/kubeconfig.yaml - - K3S_KUBECONFIG_MODE=666 - volumes: - - ${TRAVIS_BUILD_DIR:-..}/scripts:/output - ports: - - 6443:6443 - - node: - image: rancher/k3s:v1.20.7-k3s1 - tmpfs: - - /run - - /var/run - privileged: true - environment: - - K3S_URL=https://server:6443 - - K3S_CLUSTER_SECRET=somethingtotallyrandom - - registry: - image: registry:2 - ports: - - 5000:5000 diff --git a/scripts/get-version-matrix.sh b/scripts/get-version-matrix.sh new file mode 100755 index 00000000..985f064a --- /dev/null +++ b/scripts/get-version-matrix.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +function get_latest_version() { + curl -s https://api.github.com/repos/hashicorp/terraform/git/refs/tags | \ + jq ".[] | .ref | split(\"/\") | .[2] | select(. | startswith(\"$1\"))" | \ + sort -V -r | head -1 +} + +echo "::set-output name=matrix::[$(get_latest_version v0.12), $(get_latest_version v0.13), $(get_latest_version v0.14), $(get_latest_version v0.15), $(get_latest_version v1.0), $(get_latest_version v1.1), $(get_latest_version v1.2), $(get_latest_version v1.3)]" diff --git a/scripts/registries.yaml b/scripts/registries.yaml deleted file mode 100644 index 21b79df0..00000000 --- a/scripts/registries.yaml +++ /dev/null @@ -1,4 +0,0 @@ -mirrors: - "registry:5000": - endpoint: - - http://registry:5000 diff --git a/scripts/start-k3s.sh b/scripts/start-k3s.sh deleted file mode 100755 index b47f61f8..00000000 --- a/scripts/start-k3s.sh +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -cd ${DIR} - -export KUBECONFIG="${DIR}/kubeconfig.yaml" -export COMPOSE_PROJECT_NAME=k3s - -echo "--> Tearing down k3s in docker-compose" -docker-compose down -v &>/dev/null || true -rm -rf ${KUBECONFIG} -sync; sync; - -echo "--> Starting k3s in docker-compose" -docker-compose up -d --build - -echo "--> Allow insecure access to registry" -docker exec k3s_node_1 /bin/sh -c 'mkdir -p /etc/rancher/k3s' -docker cp "${DIR}/registries.yaml" k3s_node_1:/etc/rancher/k3s/registries.yaml - -echo "--> Wait for k3s kubeconfig file to exist" -while [ ! -s "${KUBECONFIG}" ] || [ ! -f "${KUBECONFIG}" ]; do sleep 1; done -while ! grep "127.0.0.1" "${KUBECONFIG}" &>/dev/null; do sleep 1; done - -HOST_IP=127.0.0.1 -if [ -f /.dockerenv ]; then - HOST_IP="172.17.0.1" -fi - -echo "--> Update IP of server to match host ip ${HOST_IP}" -kubectl config set-cluster default --server=https://${HOST_IP}:6443 --kubeconfig ${KUBECONFIG} - -TIMEOUT=120 -INTERVAL=5 -echo "> Waiting for kubectl to make a successful connection to k3s (retrying every ${INTERVAL}s for ${TIMEOUT}s)" -TIMER_START=$SECONDS -COMMAND_RESULT="" -limit=$(( ${TIMEOUT} / ${INTERVAL} )) -count=0 -while : ; do - printf "." - - COMMAND_RESULT=$((kubectl get nodes --kubeconfig ${KUBECONFIG} -o json 2>/dev/null || true) | jq '.items | length') - [[ "${COMMAND_RESULT}" -gt 0 ]] && printf "\n" && break - - if [[ "${count}" -ge "${limit}" ]]; then - printf "\n[!] Timeout waiting for connection\n" >&2 - exit 1 - fi - - sleep ${INTERVAL} - count=$[$count+1] -done - -TIMER_DURATION=$(( SECONDS - TIMER_START )) - -# restart the node to make sure the registries configuration has been picked up -docker restart k3s_node_1 - -echo "> Connection established to k3s in ${TIMER_DURATION}s" diff --git a/scripts/stop-k3s.sh b/scripts/stop-k3s.sh deleted file mode 100755 index 7aaba4ad..00000000 --- a/scripts/stop-k3s.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -cd ${DIR} - -export COMPOSE_PROJECT_NAME=k3s - -echo "--> Stopping k3s in docker-compose" -docker-compose down -v -rm -rf kubeconfig.yaml