Skip to content

Commit 07e2ded

Browse files
committed
Merge pull request moby#19057 from dnephin/remove_version_from_registry_pkg
Remove dockerversion from registry package
2 parents c5380f9 + 61a49bb commit 07e2ded

File tree

13 files changed

+63
-53
lines changed

13 files changed

+63
-53
lines changed

api/client/trust.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/docker/distribution/registry/client/transport"
2222
"github.com/docker/docker/cliconfig"
2323
"github.com/docker/docker/distribution"
24+
"github.com/docker/docker/dockerversion"
2425
"github.com/docker/docker/pkg/jsonmessage"
2526
flag "github.com/docker/docker/pkg/mflag"
2627
"github.com/docker/docker/reference"
@@ -139,7 +140,7 @@ func (cli *DockerCli) getNotaryRepository(repoInfo *registry.RepositoryInfo, aut
139140
}
140141

141142
// Skip configuration headers since request is not going to Docker daemon
142-
modifiers := registry.DockerHeaders(http.Header{})
143+
modifiers := registry.DockerHeaders(dockerversion.DockerUserAgent(), http.Header{})
143144
authTransport := transport.NewTransport(base, modifiers...)
144145
pingClient := &http.Client{
145146
Transport: authTransport,

daemon/daemon.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/docker/docker/distribution"
4242
dmetadata "github.com/docker/docker/distribution/metadata"
4343
"github.com/docker/docker/distribution/xfer"
44+
"github.com/docker/docker/dockerversion"
4445
derr "github.com/docker/docker/errors"
4546
"github.com/docker/docker/image"
4647
"github.com/docker/docker/image/tarexport"
@@ -1444,15 +1445,15 @@ func configureVolumes(config *Config, rootUID, rootGID int) (*store.VolumeStore,
14441445

14451446
// AuthenticateToRegistry checks the validity of credentials in authConfig
14461447
func (daemon *Daemon) AuthenticateToRegistry(authConfig *types.AuthConfig) (string, error) {
1447-
return daemon.RegistryService.Auth(authConfig)
1448+
return daemon.RegistryService.Auth(authConfig, dockerversion.DockerUserAgent())
14481449
}
14491450

14501451
// SearchRegistryForImages queries the registry for images matching
14511452
// term. authConfig is used to login.
14521453
func (daemon *Daemon) SearchRegistryForImages(term string,
14531454
authConfig *types.AuthConfig,
14541455
headers map[string][]string) (*registrytypes.SearchResults, error) {
1455-
return daemon.RegistryService.Search(term, authConfig, headers)
1456+
return daemon.RegistryService.Search(term, authConfig, dockerversion.DockerUserAgent(), headers)
14561457
}
14571458

14581459
// IsShuttingDown tells whether the daemon is shutting down or not

distribution/pull.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
// ImagePullConfig stores pull configuration.
2020
type ImagePullConfig struct {
2121
// MetaHeaders stores HTTP headers with metadata about the image
22-
// (DockerHeaders with prefix X-Meta- in the request).
2322
MetaHeaders map[string][]string
2423
// AuthConfig holds authentication credentials for authenticating with
2524
// the registry.

distribution/pull_v1.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/distribution/registry/client/transport"
1515
"github.com/docker/docker/distribution/metadata"
1616
"github.com/docker/docker/distribution/xfer"
17+
"github.com/docker/docker/dockerversion"
1718
"github.com/docker/docker/image"
1819
"github.com/docker/docker/image/v1"
1920
"github.com/docker/docker/layer"
@@ -47,10 +48,10 @@ func (p *v1Puller) Pull(ctx context.Context, ref reference.Named) error {
4748
tr := transport.NewTransport(
4849
// TODO(tiborvass): was ReceiveTimeout
4950
registry.NewTransport(tlsConfig),
50-
registry.DockerHeaders(p.config.MetaHeaders)...,
51+
registry.DockerHeaders(dockerversion.DockerUserAgent(), p.config.MetaHeaders)...,
5152
)
5253
client := registry.HTTPClient(tr)
53-
v1Endpoint, err := p.endpoint.ToV1Endpoint(p.config.MetaHeaders)
54+
v1Endpoint, err := p.endpoint.ToV1Endpoint(dockerversion.DockerUserAgent(), p.config.MetaHeaders)
5455
if err != nil {
5556
logrus.Debugf("Could not get v1 endpoint: %v", err)
5657
return fallbackError{err: err}

distribution/push.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
// ImagePushConfig stores push configuration.
2323
type ImagePushConfig struct {
2424
// MetaHeaders store HTTP headers with metadata about the image
25-
// (DockerHeaders with prefix X-Meta- in the request).
2625
MetaHeaders map[string][]string
2726
// AuthConfig holds authentication credentials for authenticating with
2827
// the registry.

distribution/push_v1.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/distribution/digest"
99
"github.com/docker/distribution/registry/client/transport"
1010
"github.com/docker/docker/distribution/metadata"
11+
"github.com/docker/docker/dockerversion"
1112
"github.com/docker/docker/image"
1213
"github.com/docker/docker/image/v1"
1314
"github.com/docker/docker/layer"
@@ -38,10 +39,10 @@ func (p *v1Pusher) Push(ctx context.Context) error {
3839
tr := transport.NewTransport(
3940
// TODO(tiborvass): was NoTimeout
4041
registry.NewTransport(tlsConfig),
41-
registry.DockerHeaders(p.config.MetaHeaders)...,
42+
registry.DockerHeaders(dockerversion.DockerUserAgent(), p.config.MetaHeaders)...,
4243
)
4344
client := registry.HTTPClient(tr)
44-
v1Endpoint, err := p.endpoint.ToV1Endpoint(p.config.MetaHeaders)
45+
v1Endpoint, err := p.endpoint.ToV1Endpoint(dockerversion.DockerUserAgent(), p.config.MetaHeaders)
4546
if err != nil {
4647
logrus.Debugf("Could not get v1 endpoint: %v", err)
4748
return fallbackError{err: err}

distribution/registry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/distribution/registry/client/auth"
1616
"github.com/docker/distribution/registry/client/transport"
1717
"github.com/docker/docker/distribution/xfer"
18+
"github.com/docker/docker/dockerversion"
1819
"github.com/docker/docker/registry"
1920
"github.com/docker/engine-api/types"
2021
"golang.org/x/net/context"
@@ -68,7 +69,7 @@ func NewV2Repository(ctx context.Context, repoInfo *registry.RepositoryInfo, end
6869
DisableKeepAlives: true,
6970
}
7071

71-
modifiers := registry.DockerHeaders(metaHeaders)
72+
modifiers := registry.DockerHeaders(dockerversion.DockerUserAgent(), metaHeaders)
7273
authTransport := transport.NewTransport(base, modifiers...)
7374
pingClient := &http.Client{
7475
Transport: authTransport,

dockerversion/useragent.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dockerversion
2+
3+
import (
4+
"runtime"
5+
6+
"github.com/docker/docker/pkg/parsers/kernel"
7+
"github.com/docker/docker/pkg/useragent"
8+
)
9+
10+
// DockerUserAgent is the User-Agent the Docker client uses to identify itself.
11+
// It is populated from version information of different components.
12+
func DockerUserAgent() string {
13+
httpVersion := make([]useragent.VersionInfo, 0, 6)
14+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "docker", Version: Version})
15+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "go", Version: runtime.Version()})
16+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "git-commit", Version: GitCommit})
17+
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
18+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "kernel", Version: kernelVersion.String()})
19+
}
20+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "os", Version: runtime.GOOS})
21+
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "arch", Version: runtime.GOARCH})
22+
23+
return useragent.AppendVersions("", httpVersion...)
24+
}

registry/endpoint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func scanForAPIVersion(address string) (string, APIVersion) {
4545

4646
// NewEndpoint parses the given address to return a registry endpoint. v can be used to
4747
// specify a specific endpoint version
48-
func NewEndpoint(index *registrytypes.IndexInfo, metaHeaders http.Header, v APIVersion) (*Endpoint, error) {
48+
func NewEndpoint(index *registrytypes.IndexInfo, userAgent string, metaHeaders http.Header, v APIVersion) (*Endpoint, error) {
4949
tlsConfig, err := newTLSConfig(index.Name, index.Secure)
5050
if err != nil {
5151
return nil, err
5252
}
53-
endpoint, err := newEndpoint(GetAuthConfigKey(index), tlsConfig, metaHeaders)
53+
endpoint, err := newEndpoint(GetAuthConfigKey(index), tlsConfig, userAgent, metaHeaders)
5454
if err != nil {
5555
return nil, err
5656
}
@@ -91,7 +91,7 @@ func validateEndpoint(endpoint *Endpoint) error {
9191
return nil
9292
}
9393

94-
func newEndpoint(address string, tlsConfig *tls.Config, metaHeaders http.Header) (*Endpoint, error) {
94+
func newEndpoint(address string, tlsConfig *tls.Config, userAgent string, metaHeaders http.Header) (*Endpoint, error) {
9595
var (
9696
endpoint = new(Endpoint)
9797
trimmedAddress string
@@ -112,7 +112,7 @@ func newEndpoint(address string, tlsConfig *tls.Config, metaHeaders http.Header)
112112

113113
// TODO(tiborvass): make sure a ConnectTimeout transport is used
114114
tr := NewTransport(tlsConfig)
115-
endpoint.client = HTTPClient(transport.NewTransport(tr, DockerHeaders(metaHeaders)...))
115+
endpoint.client = HTTPClient(transport.NewTransport(tr, DockerHeaders(userAgent, metaHeaders)...))
116116
return endpoint, nil
117117
}
118118

registry/endpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestEndpointParse(t *testing.T) {
1919
{"0.0.0.0:5000", "https://0.0.0.0:5000/v0/"},
2020
}
2121
for _, td := range testData {
22-
e, err := newEndpoint(td.str, nil, nil)
22+
e, err := newEndpoint(td.str, nil, "", nil)
2323
if err != nil {
2424
t.Errorf("%q: %s", td.str, err)
2525
}

registry/registry.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import (
2121
"github.com/docker/distribution/registry/api/v2"
2222
"github.com/docker/distribution/registry/client"
2323
"github.com/docker/distribution/registry/client/transport"
24-
"github.com/docker/docker/dockerversion"
25-
"github.com/docker/docker/pkg/parsers/kernel"
26-
"github.com/docker/docker/pkg/useragent"
2724
"github.com/docker/go-connections/tlsconfig"
2825
)
2926

@@ -34,23 +31,7 @@ var (
3431
errLoginRequired = errors.New("Authentication is required.")
3532
)
3633

37-
// dockerUserAgent is the User-Agent the Docker client uses to identify itself.
38-
// It is populated on init(), comprising version information of different components.
39-
var dockerUserAgent string
40-
4134
func init() {
42-
httpVersion := make([]useragent.VersionInfo, 0, 6)
43-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "docker", Version: dockerversion.Version})
44-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "go", Version: runtime.Version()})
45-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "git-commit", Version: dockerversion.GitCommit})
46-
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
47-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "kernel", Version: kernelVersion.String()})
48-
}
49-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "os", Version: runtime.GOOS})
50-
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "arch", Version: runtime.GOARCH})
51-
52-
dockerUserAgent = useragent.AppendVersions("", httpVersion...)
53-
5435
if runtime.GOOS != "linux" {
5536
V2Only = true
5637
}
@@ -130,12 +111,13 @@ func ReadCertsDirectory(tlsConfig *tls.Config, directory string) error {
130111
return nil
131112
}
132113

133-
// DockerHeaders returns request modifiers that ensure requests have
134-
// the User-Agent header set to dockerUserAgent and that metaHeaders
135-
// are added.
136-
func DockerHeaders(metaHeaders http.Header) []transport.RequestModifier {
137-
modifiers := []transport.RequestModifier{
138-
transport.NewHeaderRequestModifier(http.Header{"User-Agent": []string{dockerUserAgent}}),
114+
// DockerHeaders returns request modifiers with a User-Agent and metaHeaders
115+
func DockerHeaders(userAgent string, metaHeaders http.Header) []transport.RequestModifier {
116+
modifiers := []transport.RequestModifier{}
117+
if userAgent != "" {
118+
modifiers = append(modifiers, transport.NewHeaderRequestModifier(http.Header{
119+
"User-Agent": []string{userAgent},
120+
}))
139121
}
140122
if metaHeaders != nil {
141123
modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders))

registry/registry_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ const (
2525

2626
func spawnTestRegistrySession(t *testing.T) *Session {
2727
authConfig := &types.AuthConfig{}
28-
endpoint, err := NewEndpoint(makeIndex("/v1/"), nil, APIVersionUnknown)
28+
endpoint, err := NewEndpoint(makeIndex("/v1/"), "", nil, APIVersionUnknown)
2929
if err != nil {
3030
t.Fatal(err)
3131
}
32+
userAgent := "docker test client"
3233
var tr http.RoundTripper = debugTransport{NewTransport(nil), t.Log}
33-
tr = transport.NewTransport(AuthTransport(tr, authConfig, false), DockerHeaders(nil)...)
34+
tr = transport.NewTransport(AuthTransport(tr, authConfig, false), DockerHeaders(userAgent, nil)...)
3435
client := HTTPClient(tr)
3536
r, err := NewSession(client, authConfig, endpoint)
3637
if err != nil {
@@ -52,7 +53,7 @@ func spawnTestRegistrySession(t *testing.T) *Session {
5253

5354
func TestPingRegistryEndpoint(t *testing.T) {
5455
testPing := func(index *registrytypes.IndexInfo, expectedStandalone bool, assertMessage string) {
55-
ep, err := NewEndpoint(index, nil, APIVersionUnknown)
56+
ep, err := NewEndpoint(index, "", nil, APIVersionUnknown)
5657
if err != nil {
5758
t.Fatal(err)
5859
}
@@ -72,7 +73,7 @@ func TestPingRegistryEndpoint(t *testing.T) {
7273
func TestEndpoint(t *testing.T) {
7374
// Simple wrapper to fail test if err != nil
7475
expandEndpoint := func(index *registrytypes.IndexInfo) *Endpoint {
75-
endpoint, err := NewEndpoint(index, nil, APIVersionUnknown)
76+
endpoint, err := NewEndpoint(index, "", nil, APIVersionUnknown)
7677
if err != nil {
7778
t.Fatal(err)
7879
}
@@ -81,15 +82,15 @@ func TestEndpoint(t *testing.T) {
8182

8283
assertInsecureIndex := func(index *registrytypes.IndexInfo) {
8384
index.Secure = true
84-
_, err := NewEndpoint(index, nil, APIVersionUnknown)
85+
_, err := NewEndpoint(index, "", nil, APIVersionUnknown)
8586
assertNotEqual(t, err, nil, index.Name+": Expected error for insecure index")
8687
assertEqual(t, strings.Contains(err.Error(), "insecure-registry"), true, index.Name+": Expected insecure-registry error for insecure index")
8788
index.Secure = false
8889
}
8990

9091
assertSecureIndex := func(index *registrytypes.IndexInfo) {
9192
index.Secure = true
92-
_, err := NewEndpoint(index, nil, APIVersionUnknown)
93+
_, err := NewEndpoint(index, "", nil, APIVersionUnknown)
9394
assertNotEqual(t, err, nil, index.Name+": Expected cert error for secure index")
9495
assertEqual(t, strings.Contains(err.Error(), "certificate signed by unknown authority"), true, index.Name+": Expected cert error for secure index")
9596
index.Secure = false
@@ -155,7 +156,7 @@ func TestEndpoint(t *testing.T) {
155156
}
156157
for _, address := range badEndpoints {
157158
index.Name = address
158-
_, err := NewEndpoint(index, nil, APIVersionUnknown)
159+
_, err := NewEndpoint(index, "", nil, APIVersionUnknown)
159160
checkNotEqual(t, err, nil, "Expected error while expanding bad endpoint")
160161
}
161162
}

registry/service.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewService(options *Options) *Service {
2828
// Auth contacts the public registry with the provided credentials,
2929
// and returns OK if authentication was successful.
3030
// It can be used to verify the validity of a client's credentials.
31-
func (s *Service) Auth(authConfig *types.AuthConfig) (string, error) {
31+
func (s *Service) Auth(authConfig *types.AuthConfig, userAgent string) (string, error) {
3232
addr := authConfig.ServerAddress
3333
if addr == "" {
3434
// Use the official registry address if not specified.
@@ -45,7 +45,7 @@ func (s *Service) Auth(authConfig *types.AuthConfig) (string, error) {
4545
endpointVersion = APIVersion2
4646
}
4747

48-
endpoint, err := NewEndpoint(index, nil, endpointVersion)
48+
endpoint, err := NewEndpoint(index, userAgent, nil, endpointVersion)
4949
if err != nil {
5050
return "", err
5151
}
@@ -72,7 +72,7 @@ func splitReposSearchTerm(reposName string) (string, string) {
7272

7373
// Search queries the public registry for images matching the specified
7474
// search terms, and returns the results.
75-
func (s *Service) Search(term string, authConfig *types.AuthConfig, headers map[string][]string) (*registrytypes.SearchResults, error) {
75+
func (s *Service) Search(term string, authConfig *types.AuthConfig, userAgent string, headers map[string][]string) (*registrytypes.SearchResults, error) {
7676
if err := validateNoSchema(term); err != nil {
7777
return nil, err
7878
}
@@ -85,7 +85,7 @@ func (s *Service) Search(term string, authConfig *types.AuthConfig, headers map[
8585
}
8686

8787
// *TODO: Search multiple indexes.
88-
endpoint, err := NewEndpoint(index, http.Header(headers), APIVersionUnknown)
88+
endpoint, err := NewEndpoint(index, userAgent, http.Header(headers), APIVersionUnknown)
8989
if err != nil {
9090
return nil, err
9191
}
@@ -129,8 +129,8 @@ type APIEndpoint struct {
129129
}
130130

131131
// ToV1Endpoint returns a V1 API endpoint based on the APIEndpoint
132-
func (e APIEndpoint) ToV1Endpoint(metaHeaders http.Header) (*Endpoint, error) {
133-
return newEndpoint(e.URL, e.TLSConfig, metaHeaders)
132+
func (e APIEndpoint) ToV1Endpoint(userAgent string, metaHeaders http.Header) (*Endpoint, error) {
133+
return newEndpoint(e.URL, e.TLSConfig, userAgent, metaHeaders)
134134
}
135135

136136
// TLSConfig constructs a client TLS configuration based on server defaults

0 commit comments

Comments
 (0)