diff --git a/dockerversion/useragent.go b/dockerversion/useragent.go index 232a28acc9..7013a4543d 100644 --- a/dockerversion/useragent.go +++ b/dockerversion/useragent.go @@ -17,8 +17,8 @@ type UAStringKey struct{} // In accordance with RFC 7231 (5.5.3) is of the form: // // [docker client's UA] UpstreamClient([upstream client's UA]) -func DockerUserAgent(ctx context.Context) string { - ua := getDaemonUserAgent() +func DockerUserAgent(ctx context.Context, extraVersions ...useragent.VersionInfo) string { + ua := useragent.AppendVersions(getDaemonUserAgent(), extraVersions...) if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" { ua += " " + upstreamUA } diff --git a/dockerversion/useragent_test.go b/dockerversion/useragent_test.go index 86d5004fb7..b9fe3d2dfa 100644 --- a/dockerversion/useragent_test.go +++ b/dockerversion/useragent_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/docker/docker/pkg/useragent" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) @@ -15,10 +16,23 @@ func TestDockerUserAgent(t *testing.T) { assert.Check(t, is.Equal(ua, expected)) }) + t.Run("daemon user-agent custom metadata", func(t *testing.T) { + ua := DockerUserAgent(context.TODO(), useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"}) + expected := getDaemonUserAgent() + ` hello/world foo/bar` + assert.Check(t, is.Equal(ua, expected)) + }) + t.Run("daemon user-agent with upstream", func(t *testing.T) { ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)") ua := DockerUserAgent(ctx) expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))` assert.Check(t, is.Equal(ua, expected)) }) + + t.Run("daemon user-agent with upstream and custom metadata", func(t *testing.T) { + ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)") + ua := DockerUserAgent(ctx, useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"}) + expected := getDaemonUserAgent() + ` hello/world foo/bar UpstreamClient(Magic-Client/1.2.3 \(linux\))` + assert.Check(t, is.Equal(ua, expected)) + }) }