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

Add UpstreamClientUserAgent to dockerclient #1678

Merged
merged 2 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -188,7 +188,7 @@ func run(
if err != nil {
return err
}
client, err := bufplugindocker.NewClient(container.Logger())
client, err := bufplugindocker.NewClient(container.Logger(), bufcli.Version)
if err != nil {
return err
}
Expand Down
18 changes: 16 additions & 2 deletions private/bufpkg/bufplugin/bufplugindocker/docker.go
Expand Up @@ -35,6 +35,15 @@ import (
const (
// PluginsImagePrefix is used to prefix all image names with the correct path for pushing to the OCI registry.
PluginsImagePrefix = "plugins."

// Setting this value on the buf docker client allows us to propagate a custom
// value to the OCI registry. This is a useful property that enables registries
// to differentiate between the buf cli vs other tools like docker cli.
// Note, this does not override the final User-Agent entirely, but instead adds
// the value to the final outgoing User-Agent value in the form: [docker client's UA] UpstreamClient(buf-cli-1.11.0)
//
// Example: User-Agent = [docker/20.10.21 go/go1.18.7 git-commit/3056208 kernel/5.15.49-linuxkit os/linux arch/arm64 UpstreamClient(buf-cli-1.11.0)]
BufUpstreamClientUserAgentPrefix = "buf-cli-"
)

// Client is a small abstraction over a Docker API client, providing the basic APIs we need to build plugins.
Expand Down Expand Up @@ -203,15 +212,20 @@ func (d *dockerAPIClient) Close() error {
}

// NewClient creates a new Client to use to build Docker plugins.
func NewClient(logger *zap.Logger, options ...ClientOption) (Client, error) {
func NewClient(logger *zap.Logger, cliVersion string, options ...ClientOption) (Client, error) {
if logger == nil {
return nil, errors.New("logger required")
}
opts := &clientOptions{}
for _, option := range options {
option(opts)
}
dockerClientOpts := []client.Opt{client.FromEnv}
dockerClientOpts := []client.Opt{
client.FromEnv,
client.WithHTTPHeaders(map[string]string{
"User-Agent": BufUpstreamClientUserAgentPrefix + cliVersion,
}),
}
if len(opts.host) > 0 {
dockerClientOpts = append(dockerClientOpts, client.WithHost(opts.host))
}
Expand Down
2 changes: 1 addition & 1 deletion private/bufpkg/bufplugin/bufplugindocker/docker_test.go
Expand Up @@ -113,7 +113,7 @@ func createClient(t testing.TB, options ...ClientOption) Client {
t.Helper()
logger, err := zap.NewDevelopment()
require.Nilf(t, err, "failed to create zap logger")
dockerClient, err := NewClient(logger, options...)
dockerClient, err := NewClient(logger, "buf-cli-1.11.0", options...)
require.Nilf(t, err, "failed to create client")
t.Cleanup(func() {
if err := dockerClient.Close(); err != nil {
Expand Down