Skip to content

Commit 4fcb9ac

Browse files
author
Aaron Lehmann
committed
Improve documentation and golint compliance of registry package
* Add godoc documentation where it was missing * Change identifier names that don't match Go style, such as INDEX_NAME * Rename RegistryInfo to PingResult, which more accurately describes what this structure is for. It also has the benefit of making the name not stutter if used outside the package. Updates #14756 Signed-off-by: Aaron Lehmann <[email protected]>
1 parent 3a07e7d commit 4fcb9ac

17 files changed

Lines changed: 242 additions & 152 deletions

api/client/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
//
2323
// Usage: docker login SERVER
2424
func (cli *DockerCli) CmdLogin(args ...string) error {
25-
cmd := Cli.Subcmd("login", []string{"[SERVER]"}, "Register or log in to a Docker registry server, if no server is\nspecified \""+registry.INDEXSERVER+"\" is the default.", true)
25+
cmd := Cli.Subcmd("login", []string{"[SERVER]"}, "Register or log in to a Docker registry server, if no server is\nspecified \""+registry.IndexServer+"\" is the default.", true)
2626
cmd.Require(flag.Max, 1)
2727

2828
var username, password, email string
@@ -33,7 +33,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
3333

3434
cmd.ParseFlags(args, true)
3535

36-
serverAddress := registry.INDEXSERVER
36+
serverAddress := registry.IndexServer
3737
if len(cmd.Args()) > 0 {
3838
serverAddress = cmd.Arg(0)
3939
}

api/client/logout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
//
1515
// Usage: docker logout [SERVER]
1616
func (cli *DockerCli) CmdLogout(args ...string) error {
17-
cmd := Cli.Subcmd("logout", []string{"[SERVER]"}, "Log out from a Docker registry, if no server is\nspecified \""+registry.INDEXSERVER+"\" is the default.", true)
17+
cmd := Cli.Subcmd("logout", []string{"[SERVER]"}, "Log out from a Docker registry, if no server is\nspecified \""+registry.IndexServer+"\" is the default.", true)
1818
cmd.Require(flag.Max, 1)
1919

2020
cmd.ParseFlags(args, true)
2121

22-
serverAddress := registry.INDEXSERVER
22+
serverAddress := registry.IndexServer
2323
if len(cmd.Args()) > 0 {
2424
serverAddress = cmd.Arg(0)
2525
}

daemon/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
7474
NEventsListener: daemon.EventsService.SubscribersCount(),
7575
KernelVersion: kernelVersion,
7676
OperatingSystem: operatingSystem,
77-
IndexServerAddress: registry.INDEXSERVER,
77+
IndexServerAddress: registry.IndexServer,
7878
RegistryConfig: daemon.RegistryService.Config,
7979
InitSha1: dockerversion.INITSHA1,
8080
InitPath: initPath,

graph/pull_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (p *v1Puller) Pull(tag string) (fallback bool, err error) {
3333
return true, registry.ErrNoSupport{errors.New("Cannot pull by digest with v1 registry")}
3434
}
3535

36-
tlsConfig, err := p.registryService.TlsConfig(p.repoInfo.Index.Name)
36+
tlsConfig, err := p.registryService.TLSConfig(p.repoInfo.Index.Name)
3737
if err != nil {
3838
return false, err
3939
}

graph/push_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type v1Pusher struct {
2929
}
3030

3131
func (p *v1Pusher) Push() (fallback bool, err error) {
32-
tlsConfig, err := p.registryService.TlsConfig(p.repoInfo.Index.Name)
32+
tlsConfig, err := p.registryService.TLSConfig(p.repoInfo.Index.Name)
3333
if err != nil {
3434
return false, err
3535
}

hack/make/validate-lint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ packages=(
3636
pkg/tlsconfig
3737
pkg/urlutil
3838
pkg/version
39+
registry
3940
utils
4041
)
4142

registry/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func loginV1(authConfig *cliconfig.AuthConfig, registryEndpoint *Endpoint) (stri
3636
return "", fmt.Errorf("Server Error: Server Address not set.")
3737
}
3838

39-
loginAgainstOfficialIndex := serverAddress == INDEXSERVER
39+
loginAgainstOfficialIndex := serverAddress == IndexServer
4040

4141
// to avoid sending the server address to the server it should be removed before being marshalled
4242
authCopy := *authConfig
@@ -220,7 +220,7 @@ func tryV2TokenAuthLogin(authConfig *cliconfig.AuthConfig, params map[string]str
220220
return nil
221221
}
222222

223-
// this method matches a auth configuration to a server address or a url
223+
// ResolveAuthConfig matches an auth configuration to a server address or a URL
224224
func ResolveAuthConfig(config *cliconfig.ConfigFile, index *IndexInfo) cliconfig.AuthConfig {
225225
configKey := index.GetAuthConfigKey()
226226
// First try the happy case

registry/auth_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
3737
root = filepath.Join(root, cliconfig.ConfigFileName)
3838
configFile := cliconfig.NewConfigFile(root)
3939

40-
for _, registry := range []string{"testIndex", INDEXSERVER} {
40+
for _, registry := range []string{"testIndex", IndexServer} {
4141
configFile.AuthConfigs[registry] = cliconfig.AuthConfig{
4242
Username: "docker-user",
4343
Password: "docker-pass",
@@ -82,7 +82,7 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
8282
}
8383
defer os.RemoveAll(configFile.Filename())
8484

85-
indexConfig := configFile.AuthConfigs[INDEXSERVER]
85+
indexConfig := configFile.AuthConfigs[IndexServer]
8686

8787
officialIndex := &IndexInfo{
8888
Official: true,
@@ -92,10 +92,10 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
9292
}
9393

9494
resolved := ResolveAuthConfig(configFile, officialIndex)
95-
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return INDEXSERVER")
95+
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
9696

9797
resolved = ResolveAuthConfig(configFile, privateIndex)
98-
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return INDEXSERVER")
98+
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
9999
}
100100

101101
func TestResolveAuthConfigFullURL(t *testing.T) {
@@ -120,7 +120,7 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
120120
Password: "baz-pass",
121121
122122
}
123-
configFile.AuthConfigs[INDEXSERVER] = officialAuth
123+
configFile.AuthConfigs[IndexServer] = officialAuth
124124

125125
expectedAuths := map[string]cliconfig.AuthConfig{
126126
"registry.example.com": registryAuth,

registry/config.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,33 @@ type Options struct {
2121
}
2222

2323
const (
24-
DEFAULT_NAMESPACE = "docker.io"
25-
DEFAULT_V2_REGISTRY = "https://registry-1.docker.io"
26-
DEFAULT_REGISTRY_VERSION_HEADER = "Docker-Distribution-Api-Version"
27-
DEFAULT_V1_REGISTRY = "https://index.docker.io"
28-
29-
CERTS_DIR = "/etc/docker/certs.d"
30-
31-
// Only used for user auth + account creation
32-
REGISTRYSERVER = DEFAULT_V2_REGISTRY
33-
INDEXSERVER = DEFAULT_V1_REGISTRY + "/v1/"
34-
INDEXNAME = "docker.io"
35-
36-
// INDEXSERVER = "https://registry-stage.hub.docker.com/v1/"
24+
// DefaultNamespace is the default namespace
25+
DefaultNamespace = "docker.io"
26+
// DefaultV2Registry is the URI of the default v2 registry
27+
DefaultV2Registry = "https://registry-1.docker.io"
28+
// DefaultRegistryVersionHeader is the name of the default HTTP header
29+
// that carries Registry version info
30+
DefaultRegistryVersionHeader = "Docker-Distribution-Api-Version"
31+
// DefaultV1Registry is the URI of the default v1 registry
32+
DefaultV1Registry = "https://index.docker.io"
33+
34+
// CertsDir is the directory where certificates are stored
35+
CertsDir = "/etc/docker/certs.d"
36+
37+
// IndexServer is the v1 registry server used for user auth + account creation
38+
IndexServer = DefaultV1Registry + "/v1/"
39+
// IndexName is the name of the index
40+
IndexName = "docker.io"
41+
42+
// IndexServer = "https://registry-stage.hub.docker.com/v1/"
3743
)
3844

3945
var (
46+
// ErrInvalidRepositoryName is an error returned if the repository name did
47+
// not have the correct form
4048
ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
41-
emptyServiceConfig = NewServiceConfig(nil)
49+
50+
emptyServiceConfig = NewServiceConfig(nil)
4251
)
4352

4453
// InstallFlags adds command-line options to the top-level flag parser for
@@ -116,8 +125,8 @@ func NewServiceConfig(options *Options) *ServiceConfig {
116125
}
117126

118127
// Configure public registry.
119-
config.IndexConfigs[INDEXNAME] = &IndexInfo{
120-
Name: INDEXNAME,
128+
config.IndexConfigs[IndexName] = &IndexInfo{
129+
Name: IndexName,
121130
Mirrors: config.Mirrors,
122131
Secure: true,
123132
Official: true,
@@ -196,8 +205,8 @@ func ValidateMirror(val string) (string, error) {
196205
// ValidateIndexName validates an index name.
197206
func ValidateIndexName(val string) (string, error) {
198207
// 'index.docker.io' => 'docker.io'
199-
if val == "index."+INDEXNAME {
200-
val = INDEXNAME
208+
if val == "index."+IndexName {
209+
val = IndexName
201210
}
202211
if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
203212
return "", fmt.Errorf("Invalid index name (%s). Cannot begin or end with a hyphen.", val)
@@ -267,7 +276,7 @@ func (config *ServiceConfig) NewIndexInfo(indexName string) (*IndexInfo, error)
267276
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
268277
func (index *IndexInfo) GetAuthConfigKey() string {
269278
if index.Official {
270-
return INDEXSERVER
279+
return IndexServer
271280
}
272281
return index.Name
273282
}
@@ -280,7 +289,7 @@ func splitReposName(reposName string) (string, string) {
280289
!strings.Contains(nameParts[0], ":") && nameParts[0] != "localhost") {
281290
// This is a Docker Index repos (ex: samalba/hipache or ubuntu)
282291
// 'docker.io'
283-
indexName = INDEXNAME
292+
indexName = IndexName
284293
remoteName = reposName
285294
} else {
286295
indexName = nameParts[0]

registry/endpoint.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func newEndpoint(address string, tlsConfig *tls.Config, metaHeaders http.Header)
111111
return endpoint, nil
112112
}
113113

114+
// GetEndpoint returns a new endpoint with the specified headers
114115
func (repoInfo *RepositoryInfo) GetEndpoint(metaHeaders http.Header) (*Endpoint, error) {
115116
return NewEndpoint(repoInfo.Index, metaHeaders)
116117
}
@@ -142,7 +143,10 @@ func (e *Endpoint) Path(path string) string {
142143
return fmt.Sprintf("%s/v%d/%s", e.URL, e.Version, path)
143144
}
144145

145-
func (e *Endpoint) Ping() (RegistryInfo, error) {
146+
// Ping pings the remote endpoint with v2 and v1 pings to determine the API
147+
// version. It returns a PingResult containing the discovered version. The
148+
// PingResult also indicates whether the registry is standalone or not.
149+
func (e *Endpoint) Ping() (PingResult, error) {
146150
// The ping logic to use is determined by the registry endpoint version.
147151
switch e.Version {
148152
case APIVersion1:
@@ -167,49 +171,49 @@ func (e *Endpoint) Ping() (RegistryInfo, error) {
167171
}
168172

169173
e.Version = APIVersionUnknown
170-
return RegistryInfo{}, fmt.Errorf("unable to ping registry endpoint %s\nv2 ping attempt failed with error: %s\n v1 ping attempt failed with error: %s", e, errV2, errV1)
174+
return PingResult{}, fmt.Errorf("unable to ping registry endpoint %s\nv2 ping attempt failed with error: %s\n v1 ping attempt failed with error: %s", e, errV2, errV1)
171175
}
172176

173-
func (e *Endpoint) pingV1() (RegistryInfo, error) {
177+
func (e *Endpoint) pingV1() (PingResult, error) {
174178
logrus.Debugf("attempting v1 ping for registry endpoint %s", e)
175179

176-
if e.String() == INDEXSERVER {
180+
if e.String() == IndexServer {
177181
// Skip the check, we know this one is valid
178182
// (and we never want to fallback to http in case of error)
179-
return RegistryInfo{Standalone: false}, nil
183+
return PingResult{Standalone: false}, nil
180184
}
181185

182186
req, err := http.NewRequest("GET", e.Path("_ping"), nil)
183187
if err != nil {
184-
return RegistryInfo{Standalone: false}, err
188+
return PingResult{Standalone: false}, err
185189
}
186190

187191
resp, err := e.client.Do(req)
188192
if err != nil {
189-
return RegistryInfo{Standalone: false}, err
193+
return PingResult{Standalone: false}, err
190194
}
191195

192196
defer resp.Body.Close()
193197

194198
jsonString, err := ioutil.ReadAll(resp.Body)
195199
if err != nil {
196-
return RegistryInfo{Standalone: false}, fmt.Errorf("error while reading the http response: %s", err)
200+
return PingResult{Standalone: false}, fmt.Errorf("error while reading the http response: %s", err)
197201
}
198202

199203
// If the header is absent, we assume true for compatibility with earlier
200204
// versions of the registry. default to true
201-
info := RegistryInfo{
205+
info := PingResult{
202206
Standalone: true,
203207
}
204208
if err := json.Unmarshal(jsonString, &info); err != nil {
205-
logrus.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err)
209+
logrus.Debugf("Error unmarshalling the _ping PingResult: %s", err)
206210
// don't stop here. Just assume sane defaults
207211
}
208212
if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
209213
logrus.Debugf("Registry version header: '%s'", hdr)
210214
info.Version = hdr
211215
}
212-
logrus.Debugf("RegistryInfo.Version: %q", info.Version)
216+
logrus.Debugf("PingResult.Version: %q", info.Version)
213217

214218
standalone := resp.Header.Get("X-Docker-Registry-Standalone")
215219
logrus.Debugf("Registry standalone header: '%s'", standalone)
@@ -220,21 +224,21 @@ func (e *Endpoint) pingV1() (RegistryInfo, error) {
220224
// there is a header set, and it is not "true" or "1", so assume fails
221225
info.Standalone = false
222226
}
223-
logrus.Debugf("RegistryInfo.Standalone: %t", info.Standalone)
227+
logrus.Debugf("PingResult.Standalone: %t", info.Standalone)
224228
return info, nil
225229
}
226230

227-
func (e *Endpoint) pingV2() (RegistryInfo, error) {
231+
func (e *Endpoint) pingV2() (PingResult, error) {
228232
logrus.Debugf("attempting v2 ping for registry endpoint %s", e)
229233

230234
req, err := http.NewRequest("GET", e.Path(""), nil)
231235
if err != nil {
232-
return RegistryInfo{}, err
236+
return PingResult{}, err
233237
}
234238

235239
resp, err := e.client.Do(req)
236240
if err != nil {
237-
return RegistryInfo{}, err
241+
return PingResult{}, err
238242
}
239243
defer resp.Body.Close()
240244

@@ -253,21 +257,21 @@ HeaderLoop:
253257
}
254258

255259
if !supportsV2 {
256-
return RegistryInfo{}, fmt.Errorf("%s does not appear to be a v2 registry endpoint", e)
260+
return PingResult{}, fmt.Errorf("%s does not appear to be a v2 registry endpoint", e)
257261
}
258262

259263
if resp.StatusCode == http.StatusOK {
260264
// It would seem that no authentication/authorization is required.
261265
// So we don't need to parse/add any authorization schemes.
262-
return RegistryInfo{Standalone: true}, nil
266+
return PingResult{Standalone: true}, nil
263267
}
264268

265269
if resp.StatusCode == http.StatusUnauthorized {
266270
// Parse the WWW-Authenticate Header and store the challenges
267271
// on this endpoint object.
268272
e.AuthChallenges = parseAuthHeader(resp.Header)
269-
return RegistryInfo{}, nil
273+
return PingResult{}, nil
270274
}
271275

272-
return RegistryInfo{}, fmt.Errorf("v2 registry endpoint returned status %d: %q", resp.StatusCode, http.StatusText(resp.StatusCode))
276+
return PingResult{}, fmt.Errorf("v2 registry endpoint returned status %d: %q", resp.StatusCode, http.StatusText(resp.StatusCode))
273277
}

0 commit comments

Comments
 (0)