Skip to content

Commit

Permalink
build: opt to set progress warnings in response
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Jun 24, 2024
1 parent d155747 commit eca95f6
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ jobs:
endpoint: tcp://localhost:1234
- driver: docker-container
metadata-provenance: max
- driver: docker-container
metadata-warnings: true
exclude:
- driver: docker
multi-node: mnode-true
Expand Down Expand Up @@ -134,6 +136,9 @@ jobs:
if [ -n "${{ matrix.metadata-provenance }}" ]; then
echo "BUILDX_METADATA_PROVENANCE=${{ matrix.metadata-provenance }}" >> $GITHUB_ENV
fi
if [ -n "${{ matrix.metadata-warnings }}" ]; then
echo "BUILDX_METADATA_WARNINGS=${{ matrix.metadata-warnings }}" >> $GITHUB_ENV
fi
-
name: Install k3s
if: matrix.driver == 'kubernetes'
Expand Down
2 changes: 1 addition & 1 deletion commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
if len(in.metadataFile) > 0 {
dt := make(map[string]interface{})
for t, r := range resp {
dt[t] = decodeExporterResponse(r.ExporterResponse)
dt[t] = decodeExporterResponse(r.ExporterResponse, printer.Warnings())
}
if err := writeMetadataFile(in.metadataFile, dt); err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
return err
}
} else if options.metadataFile != "" {
if err := writeMetadataFile(options.metadataFile, decodeExporterResponse(resp.ExporterResponse)); err != nil {
if err := writeMetadataFile(options.metadataFile, decodeExporterResponse(resp.ExporterResponse, printer.Warnings())); err != nil {
return err
}
}
Expand Down Expand Up @@ -728,7 +728,7 @@ func writeMetadataFile(filename string, dt interface{}) error {
return ioutils.AtomicWriteFile(filename, b, 0644)
}

func decodeExporterResponse(exporterResponse map[string]string) map[string]interface{} {
func decodeExporterResponse(exporterResponse map[string]string, warnings []client.VertexWarning) map[string]interface{} {
out := make(map[string]interface{})
for k, v := range exporterResponse {
dt, err := base64.StdEncoding.DecodeString(v)
Expand All @@ -743,6 +743,9 @@ func decodeExporterResponse(exporterResponse map[string]string) map[string]inter
}
out[k] = json.RawMessage(dt)
}
if len(warnings) > 0 && confutil.MetadataWarningsEnabled() {
out["buildx.build.warnings"] = warnings
}
return out
}

Expand Down
7 changes: 7 additions & 0 deletions docs/reference/buildx_bake.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ $ cat metadata.json

```json
{
"buildx.build.warnings": {},
"db": {
"buildx.build.provenance": {},
"buildx.build.ref": "mybuilder/mybuilder0/0fjb6ubs52xx3vygf6fgdl611",
Expand Down Expand Up @@ -161,6 +162,12 @@ $ cat metadata.json
> * `max` sets full provenance.
> * `disabled`, `false` or `0` does not set any provenance.
> **Note**
>
> Build warnings (`buildx.build.warnings`) are not included by default. Set the
> `BUILDX_METADATA_WARNING` environment variable to `1` or `true` to
> include build record warnings.
### <a name="no-cache"></a> Don't use cache when building the image (--no-cache)

Same as `build --no-cache`. Don't use cache when building the image.
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/buildx_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ $ cat metadata.json
{
"buildx.build.provenance": {},
"buildx.build.ref": "mybuilder/mybuilder0/0fjb6ubs52xx3vygf6fgdl611",
"buildx.build.warnings": {},
"containerimage.config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
"containerimage.descriptor": {
"annotations": {
Expand All @@ -353,6 +354,12 @@ $ cat metadata.json
> * `max` sets full provenance.
> * `disabled`, `false` or `0` does not set any provenance.
> **Note**
>
> Build warnings (`buildx.build.warnings`) are not included by default. Set the
> `BUILDX_METADATA_WARNING` environment variable to `1` or `true` to
> include build record warnings.
### <a name="no-cache-filter"></a> Ignore build cache for specific stages (--no-cache-filter)

The `--no-cache-filter` lets you specify one or more stages of a multi-stage
Expand Down
8 changes: 4 additions & 4 deletions hack/test-driver
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ buildxCmd inspect --bootstrap --builder="${builderName}"

# create dockerfile
cat > "${dockerfile}" <<EOL
FROM busybox as build
fRom busybox as build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on \$BUILDPLATFORM, building for \$TARGETPLATFORM" > /log
FROM busybox AS log
FROM busybox As log
COPY --from=build /log /log
RUN cat /log
RUN uname -a
FROM busybox AS hello
FROm busybox AS hello
RUN echo hello > /hello
FROM scratch
COPY --from=log /log /log
CoPY --from=log /log /log
COPY --from=hello /hello /hello
EOL

Expand Down
74 changes: 68 additions & 6 deletions tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/containerd/continuity/fs/fstest"
"github.com/creack/pty"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/frontend/subrequests/lint"
"github.com/moby/buildkit/frontend/subrequests/outline"
"github.com/moby/buildkit/frontend/subrequests/targets"
Expand Down Expand Up @@ -59,7 +60,8 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
testBuildNetworkModeBridge,
testBuildShmSize,
testBuildUlimit,
testBuildMetadata,
testBuildMetadataProvenance,
testBuildMetadataWarnings,
testBuildMultiExporters,
testBuildLoadPush,
testBuildSecret,
Expand Down Expand Up @@ -560,19 +562,22 @@ COPY --from=build /ulimit /
require.Contains(t, string(dt), `1024`)
}

func testBuildMetadata(t *testing.T, sb integration.Sandbox) {
func testBuildMetadataProvenance(t *testing.T, sb integration.Sandbox) {
t.Run("default", func(t *testing.T) {
buildMetadataProvenance(t, sb, "")
})
t.Run("max", func(t *testing.T) {
buildMetadata(t, sb, "max")
buildMetadataProvenance(t, sb, "max")
})
t.Run("min", func(t *testing.T) {
buildMetadata(t, sb, "min")
buildMetadataProvenance(t, sb, "min")
})
t.Run("disabled", func(t *testing.T) {
buildMetadata(t, sb, "disabled")
buildMetadataProvenance(t, sb, "disabled")
})
}

func buildMetadata(t *testing.T, sb integration.Sandbox, metadataMode string) {
func buildMetadataProvenance(t *testing.T, sb integration.Sandbox, metadataMode string) {
dir := createTestProject(t)
dirDest := t.TempDir()

Expand Down Expand Up @@ -616,6 +621,63 @@ func buildMetadata(t *testing.T, sb integration.Sandbox, metadataMode string) {
require.Equal(t, provenancetypes.BuildKitBuildType, prv.BuildType)
}

func testBuildMetadataWarnings(t *testing.T, sb integration.Sandbox) {
t.Run("default", func(t *testing.T) {
buildMetadataStatus(t, sb, "")
})
t.Run("true", func(t *testing.T) {
buildMetadataStatus(t, sb, "true")
})
t.Run("false", func(t *testing.T) {
buildMetadataStatus(t, sb, "false")
})
}

func buildMetadataStatus(t *testing.T, sb integration.Sandbox, mode string) {
dockerfile := []byte(`
frOM busybox as base
cOpy Dockerfile .
from scratch
COPy --from=base \
/Dockerfile \
/
`)
dir := tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)

cmd := buildxCmd(
sb,
withArgs("build", "--metadata-file", filepath.Join(dir, "md.json"), dir),
withEnv("BUILDX_METADATA_WARNINGS="+mode),
)
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))

dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
require.NoError(t, err)

type mdT struct {
BuildRef string `json:"buildx.build.ref"`
BuildWarnings []client.VertexWarning `json:"buildx.build.warnings"`
}
var md mdT
err = json.Unmarshal(dt, &md)
require.NoError(t, err, string(dt))

require.NotEmpty(t, md.BuildRef)
if mode == "" || mode == "false" {
require.Empty(t, md.BuildWarnings)
return
} else {
require.NotEmpty(t, md.BuildWarnings)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.13.1, docker-container, ./tests)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true (0.26s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.13.1, docker-container, ./tests, experimental)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true (0.39s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.13.1, remote, ./tests)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=remote/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=remote/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=remote/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=remote/true (0.25s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.13.1, remote, ./tests, experimental)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=remote/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=remote/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=remote/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=remote/true (0.27s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.12.5, docker-container, ./tests)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true (0.29s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.12.5, docker-container, ./tests, experimental)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=docker-container/true (0.36s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.12.5, remote, ./tests)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=remote/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=remote/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=remote/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=remote/true (0.21s)

Check failure on line 674 in tests/build.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.12.5, remote, ./tests, experimental)

Failed: tests/TestIntegration/TestBuildMetadataWarnings/worker=remote/true

=== RUN TestIntegration/TestBuildMetadataWarnings/worker=remote/true build.go:674: Error Trace: /src/tests/build.go:674 /src/tests/build.go:629 Error: Should NOT be empty, but was [] Test: TestIntegration/TestBuildMetadataWarnings/worker=remote/true --- FAIL: TestIntegration/TestBuildMetadataWarnings/worker=remote/true (0.27s)
}

skipNoCompatBuildKit(t, sb, ">= 0.14.0-0", "lint")
require.Len(t, md.BuildWarnings, 3)
}

func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker")
Expand Down
9 changes: 9 additions & 0 deletions util/confutil/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ func ParseMetadataProvenance(inp string) MetadataProvenanceMode {
}
return MetadataProvenanceModeMin
}

// MetadataWarningsEnabled returns whether metadata warnings are enabled from
// BUILDX_METADATA_WARNINGS environment variable (default false)
func MetadataWarningsEnabled() bool {
if ok, err := strconv.ParseBool(os.Getenv("BUILDX_METADATA_WARNINGS")); err == nil {
return ok
}
return false
}

0 comments on commit eca95f6

Please sign in to comment.