From 5c8f2364c45fb037b7b21cf17f93e21a74651d0c Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Wed, 5 Feb 2025 00:27:26 +0000 Subject: [PATCH] Move Windows compilation test to GH Actions and enable integration/coverage Signed-off-by: Brad Davidson --- .drone.yml | 8 ---- .github/workflows/build-k3s.yaml | 26 ++++--------- .github/workflows/unitcoverage.yaml | 27 +++++++++++++- Dockerfile.dapper | 2 +- scripts/build | 53 ++++++++++++++++---------- scripts/package | 1 + scripts/package-airgap | 4 ++ scripts/package-image | 4 ++ scripts/validate-cross-compilation | 58 ----------------------------- 9 files changed, 76 insertions(+), 107 deletions(-) delete mode 100755 scripts/validate-cross-compilation diff --git a/.drone.yml b/.drone.yml index 54482bcaaf..66a602db9a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -47,14 +47,6 @@ steps: - name: docker path: /var/run/docker.sock -- name: validate-cross-compilation - image: rancher/dapper:v0.6.0 - commands: - - dapper validate-cross-compilation - volumes: - - name: docker - path: /var/run/docker.sock - - name: fossa image: rancher/drone-fossa:latest failure: ignore diff --git a/.github/workflows/build-k3s.yaml b/.github/workflows/build-k3s.yaml index fde93e00d1..5335c64747 100644 --- a/.github/workflows/build-k3s.yaml +++ b/.github/workflows/build-k3s.yaml @@ -7,10 +7,6 @@ on: type: string description: 'Architecture to build' default: 'ubuntu-latest' - upload-repo: - type: boolean - required: false - default: false upload-image: type: boolean required: false @@ -23,7 +19,7 @@ jobs: build: name: Build runs-on: ${{ inputs.arch }} # defaults to ubuntu-latest, for arm64 use ubuntu-24.04-arm - timeout-minutes: 20 + timeout-minutes: 30 steps: - name: Checkout K3s uses: actions/checkout@v4 @@ -31,25 +27,19 @@ jobs: run: | DOCKER_BUILDKIT=1 SKIP_IMAGE=1 SKIP_AIRGAP=1 SKIP_VALIDATE=1 GOCOVER=1 make sha256sum dist/artifacts/k3s | sed 's|dist/artifacts/||' > dist/artifacts/k3s.sha256sum + - name: Build K3s binary (windows) + if: inputs.arch == 'ubuntu-latest' + run: | + DOCKER_BUILDKIT=1 SKIP_IMAGE=1 SKIP_AIRGAP=1 SKIP_VALIDATE=1 GOCOVER=1 GOOS=windows make + sha256sum dist/artifacts/k3s.exe | sed 's|dist/artifacts/||' > dist/artifacts/k3s.exe.sha256sum - name: Build K3s image if: inputs.upload-image == true run: make package-image - - name: bundle repo - if: inputs.upload-repo == true - run: | - tar -czvf ../k3s-repo.tar.gz . - mv ../k3s-repo.tar.gz . - - name: "Upload K3s directory" - if: inputs.upload-repo == true - uses: actions/upload-artifact@v4 - with: - name: k3s-repo.tar.gz - path: k3s-repo.tar.gz - name: "Save K3s image" if: inputs.upload-image == true run: docker image save rancher/k3s -o ./dist/artifacts/k3s-image.tar - name: "Upload K3s Artifacts" - if: inputs.upload-repo == false && inputs.arch == 'ubuntu-latest' + if: inputs.arch == 'ubuntu-latest' uses: actions/upload-artifact@v4 with: name: k3s @@ -59,4 +49,4 @@ jobs: uses: actions/upload-artifact@v4 with: name: k3s-arm64 - path: dist/artifacts/k3s* \ No newline at end of file + path: dist/artifacts/k3s* diff --git a/.github/workflows/unitcoverage.yaml b/.github/workflows/unitcoverage.yaml index bad46ec249..fdb54dd1b4 100644 --- a/.github/workflows/unitcoverage.yaml +++ b/.github/workflows/unitcoverage.yaml @@ -38,8 +38,8 @@ jobs: - name: Install Go uses: ./.github/actions/setup-go - name: Run Unit Tests - run: | - go test -coverpkg=./... -coverprofile=coverage.out ./pkg/... -run Unit + run: | + go test -coverpkg ./pkg/... -coverprofile coverage.out ./pkg/... -run Unit go tool cover -func coverage.out - name: On Failure, Launch Debug Session if: ${{ failure() }} @@ -53,3 +53,26 @@ jobs: files: ./coverage.out flags: unittests # optional verbose: true # optional (default = false) + wtest: + name: Unit Tests (Windows 2022) + runs-on: windows-2022 + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: Install Go + uses: actions/setup-go@v5 + - name: Run Unit Tests + run: | + go test -coverpkg ./pkg/... -coverprofile coverage.out ./pkg/... -run Unit + go tool cover -func coverage.out + - name: Upload Results To Codecov + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./coverage.out + flags: unittests # optional + verbose: true # optional (default = false) + diff --git a/Dockerfile.dapper b/Dockerfile.dapper index 5d5cb875f0..89b31edded 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -52,7 +52,7 @@ ENV SELINUX=${SELINUX} # Set Dapper configuration variables ENV DAPPER_RUN_ARGS="--privileged -v k3s-cache:/go/src/github.com/k3s-io/k3s/.cache -v trivy-cache:/root/.cache/trivy" \ - DAPPER_ENV="REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_IMAGE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG" \ + DAPPER_ENV="REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_IMAGE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER GOOS DEBUG" \ DAPPER_SOURCE="/go/src/github.com/k3s-io/k3s/" \ DAPPER_OUTPUT="./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy" \ DAPPER_DOCKER_SOCKET=true \ diff --git a/scripts/build b/scripts/build index 257b20ec27..6aef3029f5 100755 --- a/scripts/build +++ b/scripts/build @@ -60,29 +60,42 @@ else LDFLAGS="-w -s" fi -STATIC=" - -extldflags '-static -lm -ldl -lz -lpthread' -" -TAGS="ctrd apparmor seccomp netcgo osusergo providerless urfave_cli_no_docs" -RUNC_TAGS="apparmor seccomp" -RUNC_STATIC="static" +STATIC="" +TAGS="ctrd netcgo osusergo providerless urfave_cli_no_docs sqlite_omit_load_extension" +RUNC_STATIC="" +RUNC_TAGS="" -if [ ${OS} = windows ]; then - TAGS="ctrd netcgo osusergo providerless" -fi +case ${OS} in + linux) + TAGS="$TAGS apparmor seccomp" + RUNC_TAGS="$RUNC_TAGS apparmor seccomp" -if [ "$SELINUX" = "true" ]; then - TAGS="$TAGS selinux" - RUNC_TAGS="$RUNC_TAGS selinux" -fi + if [ "$STATIC_BUILD" == "true" ]; then + STATIC=$'\n-extldflags \'-static -lm -ldl -lz -lpthread\'\n' + TAGS="static_build libsqlite3 $TAGS" + RUNC_STATIC="static" + fi + if [ "$SELINUX" = "true" ]; then + TAGS="$TAGS selinux" + RUNC_TAGS="$RUNC_TAGS selinux" + fi + ;; + windows) + TAGS="$TAGS no_cri_dockerd" -if [ "$STATIC_BUILD" != "true" ]; then - STATIC=" -" - RUNC_STATIC="" -else - TAGS="static_build libsqlite3 $TAGS" -fi + if [ "$STATIC_BUILD" == "true" ]; then + STATIC=$'\n-extldflags \'-static -lpthread\'\n' + TAGS="static_build $TAGS" + fi + + export CXX="x86_64-w64-mingw32-g++" + export CC="x86_64-w64-mingw32-gcc" + ;; + *) + echo "[ERROR] unrecognized opertaing system: ${OS}" + exit 1 + ;; +esac if [ -n "${GOCOVER}" ]; then BLDFLAGS="-cover" diff --git a/scripts/package b/scripts/package index ca2123657e..68d464ed16 100755 --- a/scripts/package +++ b/scripts/package @@ -8,6 +8,7 @@ if [ ! -e ../bin/containerd ]; then fi ./package-cli + if [ -z "$SKIP_IMAGE" ]; then ./package-image fi diff --git a/scripts/package-airgap b/scripts/package-airgap index 911357181a..ce27ae680e 100755 --- a/scripts/package-airgap +++ b/scripts/package-airgap @@ -5,6 +5,10 @@ cd $(dirname $0)/.. . ./scripts/version.sh +if [ "${OS}" != "linux" ]; then + exit +fi + airgap_image_file='scripts/airgap/image-list.txt' images=$(cat "${airgap_image_file}") xargs -n1 docker pull <<< "${images}" diff --git a/scripts/package-image b/scripts/package-image index 823511d4d4..9ae43e58b7 100755 --- a/scripts/package-image +++ b/scripts/package-image @@ -5,6 +5,10 @@ cd $(dirname $0)/.. . ./scripts/version.sh +if [ "${OS}" != "linux" ]; then + exit +fi + TAG=${TAG:-${VERSION_TAG}${SUFFIX}} REPO=${REPO:-rancher} IMAGE_NAME=${IMAGE_NAME:-k3s} diff --git a/scripts/validate-cross-compilation b/scripts/validate-cross-compilation deleted file mode 100755 index a3cabae08d..0000000000 --- a/scripts/validate-cross-compilation +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -set -e -x - -cd $(dirname $0)/.. - -. ./scripts/version.sh - -GO=${GO-go} - -PKG="github.com/k3s-io/k3s" -PKG_CONTAINERD="github.com/containerd/containerd" -PKG_K3S_CONTAINERD="github.com/k3s-io/containerd" -PKG_CRICTL="sigs.k8s.io/cri-tools/pkg" -PKG_K8S_BASE="k8s.io/component-base" -PKG_K8S_CLIENT="k8s.io/client-go/pkg" - -buildDate=$(date -u '+%Y-%m-%dT%H:%M:%SZ') - -VERSIONFLAGS=" - -X ${PKG}/pkg/version.Version=${VERSION} - -X ${PKG}/pkg/version.GitCommit=${COMMIT:0:8} - - -X ${PKG_K8S_CLIENT}/version.gitVersion=${VERSION} - -X ${PKG_K8S_CLIENT}/version.gitCommit=${COMMIT} - -X ${PKG_K8S_CLIENT}/version.gitTreeState=${TREE_STATE} - -X ${PKG_K8S_CLIENT}/version.buildDate=${buildDate} - - -X ${PKG_K8S_BASE}/version.gitVersion=${VERSION} - -X ${PKG_K8S_BASE}/version.gitCommit=${COMMIT} - -X ${PKG_K8S_BASE}/version.gitTreeState=${TREE_STATE} - -X ${PKG_K8S_BASE}/version.buildDate=${buildDate} - - -X ${PKG_CRICTL}/version.Version=${VERSION_CRICTL} - - -X ${PKG_CONTAINERD}/version.Version=${VERSION_CONTAINERD} - -X ${PKG_CONTAINERD}/version.Package=${PKG_K3S_CONTAINERD} - - -X ${PKG_CONTAINERD}/version.Version=${VERSION_CONTAINERD} - -X ${PKG_CONTAINERD}/version.Package=${PKG_K3S_CONTAINERD} -" - -LDFLAGS=" - -w -s" - -STATIC="" - -TAGS="netcgo osusergo providerless" - -mkdir -p bin - -# Sanity check for downstream dependencies -echo 'Validate K3s cross-compilation on Windows x86_64' -GOOS=windows CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc \ - "${GO}" build -tags "${TAGS}" -ldflags "${VERSIONFLAGS} ${LDFLAGS} ${STATIC}" -o bin/k3s.exe ./cmd/server/main.go - -if [ "${KEEP_WINDOWS_BIN}" != 'true' ]; then - rm -rf bin/k3s.exe -fi