Skip to content

Commit 6695ae0

Browse files
Alex Couture-Beilalexcb
authored andcommitted
dockerfile: arg for controlling go build flags
This introduces new `BUILDFLAGS`, `VERIFYFLAGS`, and `CGO_ENABLED` build-args, which can be used to change how buildkitd is compiled. This, for example, can be used to enable the go data race detector during integration testing: CGO_ENABLED=1 BUILDFLAGS="-race" TESTFLAGS="-v -test.run=TestClientGatewayIntegration/TestClientGatewaySolve" TESTPKGS=./client ./hack/test integration This additionally introduces a new `make test-race` target, which simplifies how to run all tests with the race detector. Signed-off-by: Alex Couture-Beil <[email protected]>
1 parent 854fa75 commit 6695ae0

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ TESTPKGS=./client TESTFLAGS="--run //worker=containerd -v" ./hack/test integrati
158158
DOCKERFILE_RELEASES=labs TESTFLAGS="--run /TestRunGlobalNetwork/worker=oci$/ -v" ./hack/test dockerfile
159159

160160
# enabling go data race detector
161-
GO_RACE_ENABLED=1 ./hack/test integration
161+
CGO_ENABLED=1 BUILDFLAGS="-race" ./hack/test integration
162162
```
163163

164164
Set `TEST_KEEP_CACHE=1` for the test framework to keep external dependant images in a docker volume

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ ARG MINIO_VERSION=RELEASE.2022-05-03T20-36-08Z
1515
ARG MINIO_MC_VERSION=RELEASE.2022-05-04T06-07-55Z
1616
ARG AZURITE_VERSION=3.18.0
1717
ARG GOTESTSUM_VERSION=v1.9.0
18-
ARG GO_RACE_ENABLED=0
1918

2019
ARG GO_VERSION=1.20
2120
ARG ALPINE_VERSION=3.18
@@ -95,12 +94,14 @@ FROM buildkit-base AS buildkitd
9594
# BUILDKITD_TAGS defines additional Go build tags for compiling buildkitd
9695
ARG BUILDKITD_TAGS
9796
ARG TARGETPLATFORM
98-
ARG GO_RACE_ENABLED
97+
ARG BUILDFLAGS
98+
ARG VERIFYFLAGS="--static"
99+
ARG CGO_ENABLED=0
99100
RUN --mount=target=. --mount=target=/root/.cache,type=cache \
100101
--mount=target=/go/pkg/mod,type=cache \
101102
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version \
102-
CGO_ENABLED="$GO_RACE_ENABLED" xx-go build $(if [ "$GO_RACE_ENABLED" != "0" ]; then echo -race; fi) -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
103-
if [ "$GO_RACE_ENABLED" = "0" ]; then xx-verify --static /usr/bin/buildkitd; fi
103+
xx-go build ${BUILDFLAGS} -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
104+
xx-verify ${VERIFYFLAGS} /usr/bin/buildkitd
104105

105106
FROM scratch AS binaries-linux
106107
COPY --link --from=runc /usr/bin/runc /buildkit-runc

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test:
3636

3737
.PHONY: test-race
3838
test-race:
39-
GO_RACE_ENABLED=1 ./hack/test integration gateway dockerfile
39+
CGO_ENABLED=1 BUILDFLAGS="-race" ./hack/test integration gateway dockerfile
4040

4141
.PHONY: lint
4242
lint:

hack/test

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ set -eu -o pipefail
1313
: ${TEST_DOCKERD_BINARY=$(which dockerd)}
1414
: ${TEST_REPORT_SUFFIX=}
1515
: ${TEST_KEEP_CACHE=}
16-
: ${GO_RACE_ENABLED=}
16+
: ${BUILDFLAGS=}
17+
: ${VERIFYFLAGS=}
18+
: ${CGO_ENABLED=}
1719
: ${DOCKERFILE_RELEASES=}
1820
: ${BUILDKIT_WORKER_RANDOM=}
1921
: ${BUILDKITD_TAGS=}
@@ -61,12 +63,24 @@ testReportsVol="-v $testReportsDir:/testreports"
6163
gotestsumArgs="--format=standard-verbose --jsonfile=/testreports/go-test-report$TEST_REPORT_SUFFIX.json --junitfile=/testreports/junit-report$TEST_REPORT_SUFFIX.xml"
6264
gotestArgs="-mod=vendor -coverprofile=/testreports/coverage-report$TEST_REPORT_SUFFIX.txt -covermode=atomic"
6365

66+
if [[ "$BUILDFLAGS" == *"-race"* ]]; then
67+
if [ "$CGO_ENABLED" != "1" ]; then
68+
echo>&2 "go race detector requires CGO_ENABLED=1"
69+
exit 1
70+
fi
71+
# force buildkitd to halt on detected race conditions, which will cause the tests to fail
72+
export GORACE="halt_on_error=1"
73+
export VERIFYFLAGS="" # prevent -static verification
74+
fi
75+
6476
buildxCmd build $cacheFromFlags \
6577
--build-arg BUILDKITD_TAGS \
6678
--build-arg HTTP_PROXY \
6779
--build-arg HTTPS_PROXY \
6880
--build-arg NO_PROXY \
69-
--build-arg GO_RACE_ENABLED \
81+
--build-arg BUILDFLAGS \
82+
--build-arg VERIFYFLAGS \
83+
--build-arg CGO_ENABLED \
7084
--target "integration-tests" \
7185
--output "type=docker,name=$iid" \
7286
$currentcontext
@@ -76,11 +90,6 @@ if ! docker container inspect "$cacheVolume" >/dev/null 2>/dev/null; then
7690
docker create -v /root/.cache -v /root/.cache/registry -v /go/pkg/mod --name "$cacheVolume" alpine
7791
fi
7892

79-
if [ "$GO_RACE_ENABLED" = "1" ]; then
80-
# force buildkitd to half on detected race conditions, which will cause the tests to fail
81-
export GORACE="halt_on_error=1"
82-
fi
83-
8493
if [ "$TEST_INTEGRATION" == 1 ]; then
8594
cid=$(docker create --rm -v /tmp $testReportsVol --volumes-from=$cacheVolume -e GITHUB_REF -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e TEST_DOCKERD -e SKIP_INTEGRATION_TESTS -e BUILDKIT_TEST_ENABLE_FEATURES -e GOTESTSUM_FORMAT ${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry -e BUILDKIT_INTEGRATION_DOCKERD_FLAGS -e GORACE --privileged $iid gotestsum $gotestsumArgs --packages="${TESTPKGS:-./...}" -- $gotestArgs ${TESTFLAGS:--v})
8695
if [ "$TEST_DOCKERD" = "1" ]; then

0 commit comments

Comments
 (0)