From 9f43ed7e77be7c0e55053070cefa9b2f25a6912b Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 7 Oct 2024 18:43:43 -0700 Subject: [PATCH] llb: use buildkit user-agent for HTTP source Signed-off-by: Tonis Tiigi --- frontend/dockerfile/dockerfile_test.go | 12 ++++++++++++ source/http/source.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/frontend/dockerfile/dockerfile_test.go b/frontend/dockerfile/dockerfile_test.go index 5893d9e021e1..640415aa426e 100644 --- a/frontend/dockerfile/dockerfile_test.go +++ b/frontend/dockerfile/dockerfile_test.go @@ -2757,6 +2757,11 @@ ADD %s /dest/ require.NoError(t, err) require.Equal(t, []byte("content1"), dt) + // run again to test HEAD request + cmd = sb.Cmd(args) + err = cmd.Run() + require.NoError(t, err) + // test the default properties dockerfile = []byte(fmt.Sprintf(` FROM scratch @@ -2785,6 +2790,13 @@ ADD %s /dest/ fi, err := os.Stat(destFile) require.NoError(t, err) require.Equal(t, modTime.Format(http.TimeFormat), fi.ModTime().Format(http.TimeFormat)) + + stats := server.Stats("/foo") + require.Len(t, stats.Requests, 2) + require.Equal(t, "GET", stats.Requests[0].Method) + require.Contains(t, stats.Requests[0].Header.Get("User-Agent"), "buildkit/v") + require.Equal(t, "HEAD", stats.Requests[1].Method) + require.Contains(t, stats.Requests[1].Header.Get("User-Agent"), "buildkit/v") } func testDockerfileAddArchive(t *testing.T, sb integration.Sandbox) { diff --git a/source/http/source.go b/source/http/source.go index 82d26eb34a59..a7d7ae49d749 100644 --- a/source/http/source.go +++ b/source/http/source.go @@ -26,6 +26,7 @@ import ( srctypes "github.com/moby/buildkit/source/types" "github.com/moby/buildkit/util/bklog" "github.com/moby/buildkit/util/tracing" + "github.com/moby/buildkit/version" digest "github.com/opencontainers/go-digest" "github.com/pkg/errors" ) @@ -181,6 +182,7 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde return "", "", nil, false, err } req = req.WithContext(ctx) + req.Header.Add("User-Agent", version.UserAgent()) m := map[string]cacheRefMetadata{} // If we request a single ETag in 'If-None-Match', some servers omit the