Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile: arg for controlling go build flags #3994

Merged
merged 4 commits into from
Jul 7, 2023

Conversation

alexcb
Copy link
Collaborator

@alexcb alexcb commented Jul 5, 2023

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 alex@earthly.dev

This introduces a new `make test-race` target, which will compile
buildkitd using the `go build -race` flag, which is useful for detecting
data races.

Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
@alexcb
Copy link
Collaborator Author

alexcb commented Jul 6, 2023

here's a sample run against a specific test:

time TESTFLAGS="-v -test.run=TestClientGatewayIntegration/TestClientGatewaySolve" TESTPKGS=./client 

which replicates a data race:

...

response.header.etag="\"sha256:2461e8255644c236fb21f20d482f3ca7017d69b52f3177737502fbcfbdc82f55\"" response.status="200 OK" size=2588339 spanID=31dcd2311686db6b traceID=4d0a98fc696ae9a1952c3dd78f36c13e url="http://localhost:40005/v2/library/busybox/blobs/sha256:2461e8255644c236fb21f20d482f3ca7017d69b52f3177737502fbcfbdc82f55?ns=docker.io"
    sandbox.go:288: ==================
    sandbox.go:288: WARNING: DATA RACE
    sandbox.go:288: Write at 0x00c0003f5430 by goroutine 235:
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.trackProgress()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:113 +0x544
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.(*ProviderWithProgress).ReaderAt.func1()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:36 +0x12b
    sandbox.go:288: 
    sandbox.go:288: Previous read at 0x00c0003f5430 by goroutine 236:
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.trackProgress.func1()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:97 +0x39
    sandbox.go:288: 
    sandbox.go:288: Goroutine 235 (running) created at:
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.(*ProviderWithProgress).ReaderAt()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:36 +0x388
    sandbox.go:288:   github.com/moby/buildkit/util/contentutil.(*localFetcher).Fetch()
    sandbox.go:288:       /src/util/contentutil/copy.go:30 +0x104
    sandbox.go:288:   github.com/moby/buildkit/util/resolver/limited.(*fetcher).Fetch()
    sandbox.go:288:       /src/util/resolver/limited/group.go:113 +0x184
    sandbox.go:288:   github.com/containerd/containerd/remotes.Fetch()
    sandbox.go:288:       /src/vendor/github.com/containerd/containerd/remotes/handlers.go:147 +0x617
    sandbox.go:288:   github.com/containerd/containerd/remotes.FetchHandler.func1()
    sandbox.go:288:       /src/vendor/github.com/containerd/containerd/remotes/handlers.go:104 +0x377
    sandbox.go:288:   github.com/moby/buildkit/util/resolver/retryhandler.New.func1()
    sandbox.go:288:       /src/util/resolver/retryhandler/retry.go:25 +0xc4
    sandbox.go:288:   github.com/moby/buildkit/util/contentutil.Copy()
    sandbox.go:288:       /src/util/contentutil/copy.go:19 +0x1a8
    sandbox.go:288:   github.com/moby/buildkit/cache.lazyRefProvider.Unlazy.func1()
    sandbox.go:288:       /src/cache/remote.go:335 +0x64e
    sandbox.go:288:   github.com/moby/buildkit/util/flightcontrol.(*call[...]).run()
    sandbox.go:288:       /src/util/flightcontrol/flightcontrol.go:121 +0x133
    sandbox.go:288:   github.com/moby/buildkit/util/flightcontrol.(*call[...]).wait.func1()
    sandbox.go:288:       /src/util/flightcontrol/flightcontrol.go:165 +0x47
    sandbox.go:288:   sync.(*Once).doSlow()
    sandbox.go:288:       /usr/local/go/src/sync/once.go:74 +0x101
    sandbox.go:288:   sync.(*Once).Do()
    sandbox.go:288:       /usr/local/go/src/sync/once.go:65 +0x46
    sandbox.go:288:   github.com/moby/buildkit/util/flightcontrol.(*call[...]).wait.func2()
    sandbox.go:288:       /src/util/flightcontrol/flightcontrol.go:165 +0x47
    sandbox.go:288: 
    sandbox.go:288: Goroutine 236 (finished) created at:
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.trackProgress()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:96 +0x231
    sandbox.go:288:   github.com/moby/buildkit/util/pull/pullprogress.(*ProviderWithProgress).ReaderAt.func1()
    sandbox.go:288:       /src/util/pull/pullprogress/progress.go:36 +0x12b
    sandbox.go:288: ==================
    sandbox.go:288: > stopped 2023-07-06 22:03:39.582449134 +0000 UTC m=+3.961754838 exit status 66 66
    sandbox.go:285: stdout: /usr/bin/containerd-stargz-grpc --log-level debug --address /tmp/bktest_containerd_stargz_grpc1107475506/containerd-stargz-grpc.sock --root /tmp/bktest_containerd_stargz_grpc1107475506/root
    sandbox.go:285: stderr: /usr/bin/containerd-stargz-grpc --log-level debug --address /tmp/bktest_containerd_stargz_grpc1107475506/containerd-stargz-grpc.sock --root /tmp/bktest_containerd_stargz_grpc1107475506/root
    sandbox.go:288: > startCmd 2023-07-06 22:03:37.063325469 +0000 UTC m=+1.442631175 /usr/bin/containerd-stargz-grpc --log-level debug --address /tmp/bktest_containerd_stargz_grpc1107475506/containerd-stargz-grpc.sock --root /tmp/bktest_containerd_stargz_grpc1107475506/root

...

=== FAIL: client TestClientGatewayIntegration (1.44s)

DONE 8 tests, 6 failures in 4.665s

real	0m13.816s
user	0m0.308s
sys	0m0.126s

@@ -34,6 +34,10 @@ clean:
test:
./hack/test integration gateway dockerfile

.PHONY: test-race
test-race:
Copy link
Collaborator Author

@alexcb alexcb Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this new target is overkill, and it's sufficient to just indicate the new CGO_ENABLED=1 BUILDFLAGS="-race" envs in the CONTRIBUTING.md?

@alexcb
Copy link
Collaborator Author

alexcb commented Jul 6, 2023

Adding this to all PRs might be overkill, but perhaps there's a limited number of integration tests that could be called via github actions (once the existing data races are fixed, see #3862 for instance)?

Dockerfile Outdated
Comment on lines 98 to 103
ARG GO_RACE_ENABLED
RUN --mount=target=. --mount=target=/root/.cache,type=cache \
--mount=target=/go/pkg/mod,type=cache \
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version \
CGO_ENABLED=0 xx-go build -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
xx-verify --static /usr/bin/buildkitd
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 && \
if [ "$GO_RACE_ENABLED" = "0" ]; then xx-verify --static /usr/bin/buildkitd; fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a generic name for this, maybe BUILDFLAGS empty by default? Might be useful in the future. Same for CGO_ENABLED defaulting to 0 or we could detect if -race is being passed and set the right value?

ARG BUILDFLAGS
ARG CGO_ENABLED=0
RUN --mount=target=. --mount=target=/root/.cache,type=cache \
  --mount=target=/go/pkg/mod,type=cache \
  --mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version \
  xx-go build ${BUILDFLAGS} -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
  xx-verify --static /usr/bin/buildkitd

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; I also introduced a VERIFYFLAGS which is used to prevent the xx-verify --static ... call, by explicitly doing export VERIFYFLAGS="" (since --race requires cgo which is not static).

If there's a use-case for allowing users to change the VERIFYFLAGS, we could make the bash code allow users to set it (and maybe error out if it contains -static).

@alexcb alexcb changed the title dockerfile: arg to enable go race detection dockerfile: arg for controlling go build flags Jul 6, 2023
@alexcb alexcb marked this pull request as ready for review July 6, 2023 23:31
@alexcb alexcb force-pushed the acb/race branch 2 times, most recently from c63fb17 to a92c55f Compare July 6, 2023 23:34
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 <alex@earthly.dev>
Dockerfile Outdated Show resolved Hide resolved
Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants