Move IndexInfo and ServiceConfig types to api/types/registry/registry.go

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-12-11 18:14:52 -08:00 committed by Daniel Nephin
parent 6fc54d049b
commit aead731d54
9 changed files with 87 additions and 141 deletions

View file

@ -8,7 +8,8 @@ import (
"strings" "strings"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/cliconfig" "github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
) )
// Login tries to register/login to the registry server. // Login tries to register/login to the registry server.
@ -221,8 +222,8 @@ func tryV2TokenAuthLogin(authConfig *types.AuthConfig, params map[string]string,
} }
// ResolveAuthConfig matches an auth configuration to a server address or a URL // ResolveAuthConfig matches an auth configuration to a server address or a URL
func ResolveAuthConfig(authConfigs map[string]types.AuthConfig, index *IndexInfo) types.AuthConfig { func ResolveAuthConfig(authConfigs map[string]types.AuthConfig, index *registrytypes.IndexInfo) types.AuthConfig {
configKey := index.GetAuthConfigKey() configKey := GetAuthConfigKey(index)
// First try the happy case // First try the happy case
if c, found := authConfigs[configKey]; found || index.Official { if c, found := authConfigs[configKey]; found || index.Official {
return c return c

View file

@ -3,6 +3,8 @@ package registry
import ( import (
"testing" "testing"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/cliconfig" "github.com/docker/docker/cliconfig"
) )
@ -26,8 +28,8 @@ func TestEncodeAuth(t *testing.T) {
} }
} }
func buildAuthConfigs() map[string]cliconfig.AuthConfig { func buildAuthConfigs() map[string]types.AuthConfig {
authConfigs := map[string]cliconfig.AuthConfig{} authConfigs := map[string]types.AuthConfig{}
for _, registry := range []string{"testIndex", IndexServer} { for _, registry := range []string{"testIndex", IndexServer} {
authConfigs[registry] = types.AuthConfig{ authConfigs[registry] = types.AuthConfig{
@ -61,10 +63,10 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
authConfigs := buildAuthConfigs() authConfigs := buildAuthConfigs()
indexConfig := authConfigs[IndexServer] indexConfig := authConfigs[IndexServer]
officialIndex := &IndexInfo{ officialIndex := &registrytypes.IndexInfo{
Official: true, Official: true,
} }
privateIndex := &IndexInfo{ privateIndex := &registrytypes.IndexInfo{
Official: false, Official: false,
} }
@ -127,7 +129,7 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
if !ok || configured.Email == "" { if !ok || configured.Email == "" {
t.Fail() t.Fail()
} }
index := &IndexInfo{ index := &registrytypes.IndexInfo{
Name: configKey, Name: configKey,
} }
for _, registry := range registries { for _, registry := range registries {

View file

@ -1,7 +1,6 @@
package registry package registry
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"net" "net"
@ -9,6 +8,7 @@ import (
"strings" "strings"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/image/v1" "github.com/docker/docker/image/v1"
"github.com/docker/docker/opts" "github.com/docker/docker/opts"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
@ -60,32 +60,8 @@ func (options *Options) InstallFlags(cmd *flag.FlagSet, usageFn func(string) str
cmd.BoolVar(&V2Only, []string{"-disable-legacy-registry"}, false, "Do not contact legacy registries") cmd.BoolVar(&V2Only, []string{"-disable-legacy-registry"}, false, "Do not contact legacy registries")
} }
type netIPNet net.IPNet
func (ipnet *netIPNet) MarshalJSON() ([]byte, error) {
return json.Marshal((*net.IPNet)(ipnet).String())
}
func (ipnet *netIPNet) UnmarshalJSON(b []byte) (err error) {
var ipnetStr string
if err = json.Unmarshal(b, &ipnetStr); err == nil {
var cidr *net.IPNet
if _, cidr, err = net.ParseCIDR(ipnetStr); err == nil {
*ipnet = netIPNet(*cidr)
}
}
return
}
// ServiceConfig stores daemon registry services configuration.
type ServiceConfig struct {
InsecureRegistryCIDRs []*netIPNet `json:"InsecureRegistryCIDRs"`
IndexConfigs map[string]*IndexInfo `json:"IndexConfigs"`
Mirrors []string
}
// NewServiceConfig returns a new instance of ServiceConfig // NewServiceConfig returns a new instance of ServiceConfig
func NewServiceConfig(options *Options) *ServiceConfig { func NewServiceConfig(options *Options) *registrytypes.ServiceConfig {
if options == nil { if options == nil {
options = &Options{ options = &Options{
Mirrors: opts.NewListOpts(nil), Mirrors: opts.NewListOpts(nil),
@ -100,9 +76,9 @@ func NewServiceConfig(options *Options) *ServiceConfig {
// daemon flags on boot2docker? // daemon flags on boot2docker?
options.InsecureRegistries.Set("127.0.0.0/8") options.InsecureRegistries.Set("127.0.0.0/8")
config := &ServiceConfig{ config := &registrytypes.ServiceConfig{
InsecureRegistryCIDRs: make([]*netIPNet, 0), InsecureRegistryCIDRs: make([]*registrytypes.NetIPNet, 0),
IndexConfigs: make(map[string]*IndexInfo, 0), IndexConfigs: make(map[string]*registrytypes.IndexInfo, 0),
// Hack: Bypass setting the mirrors to IndexConfigs since they are going away // Hack: Bypass setting the mirrors to IndexConfigs since they are going away
// and Mirrors are only for the official registry anyways. // and Mirrors are only for the official registry anyways.
Mirrors: options.Mirrors.GetAll(), Mirrors: options.Mirrors.GetAll(),
@ -113,10 +89,10 @@ func NewServiceConfig(options *Options) *ServiceConfig {
_, ipnet, err := net.ParseCIDR(r) _, ipnet, err := net.ParseCIDR(r)
if err == nil { if err == nil {
// Valid CIDR. // Valid CIDR.
config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, (*netIPNet)(ipnet)) config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, (*registrytypes.NetIPNet)(ipnet))
} else { } else {
// Assume `host:port` if not CIDR. // Assume `host:port` if not CIDR.
config.IndexConfigs[r] = &IndexInfo{ config.IndexConfigs[r] = &registrytypes.IndexInfo{
Name: r, Name: r,
Mirrors: make([]string, 0), Mirrors: make([]string, 0),
Secure: false, Secure: false,
@ -126,7 +102,7 @@ func NewServiceConfig(options *Options) *ServiceConfig {
} }
// Configure public registry. // Configure public registry.
config.IndexConfigs[IndexName] = &IndexInfo{ config.IndexConfigs[IndexName] = &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Mirrors: config.Mirrors, Mirrors: config.Mirrors,
Secure: true, Secure: true,
@ -147,9 +123,9 @@ func NewServiceConfig(options *Options) *ServiceConfig {
// or an IP address. If it is a domain name, then it will be resolved in order to check if the IP is contained // or an IP address. If it is a domain name, then it will be resolved in order to check if the IP is contained
// in a subnet. If the resolving is not successful, isSecureIndex will only try to match hostname to any element // in a subnet. If the resolving is not successful, isSecureIndex will only try to match hostname to any element
// of insecureRegistries. // of insecureRegistries.
func (config *ServiceConfig) isSecureIndex(indexName string) bool { func isSecureIndex(config *registrytypes.ServiceConfig, indexName string) bool {
// Check for configured index, first. This is needed in case isSecureIndex // Check for configured index, first. This is needed in case isSecureIndex
// is called from anything besides NewIndexInfo, in order to honor per-index configurations. // is called from anything besides newIndexInfo, in order to honor per-index configurations.
if index, ok := config.IndexConfigs[indexName]; ok { if index, ok := config.IndexConfigs[indexName]; ok {
return index.Secure return index.Secure
} }
@ -258,8 +234,8 @@ func loadRepositoryName(reposName reference.Named) (string, reference.Named, err
return indexName, remoteName, nil return indexName, remoteName, nil
} }
// NewIndexInfo returns IndexInfo configuration from indexName // newIndexInfo returns IndexInfo configuration from indexName
func (config *ServiceConfig) NewIndexInfo(indexName string) (*IndexInfo, error) { func newIndexInfo(config *registrytypes.ServiceConfig, indexName string) (*registrytypes.IndexInfo, error) {
var err error var err error
indexName, err = ValidateIndexName(indexName) indexName, err = ValidateIndexName(indexName)
if err != nil { if err != nil {
@ -272,18 +248,18 @@ func (config *ServiceConfig) NewIndexInfo(indexName string) (*IndexInfo, error)
} }
// Construct a non-configured index info. // Construct a non-configured index info.
index := &IndexInfo{ index := &registrytypes.IndexInfo{
Name: indexName, Name: indexName,
Mirrors: make([]string, 0), Mirrors: make([]string, 0),
Official: false, Official: false,
} }
index.Secure = config.isSecureIndex(indexName) index.Secure = isSecureIndex(config, indexName)
return index, nil return index, nil
} }
// GetAuthConfigKey special-cases using the full index address of the official // GetAuthConfigKey special-cases using the full index address of the official
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes. // index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
func (index *IndexInfo) GetAuthConfigKey() string { func GetAuthConfigKey(index *registrytypes.IndexInfo) string {
if index.Official { if index.Official {
return IndexServer return IndexServer
} }
@ -306,8 +282,8 @@ func splitReposName(reposName reference.Named) (indexName string, remoteName ref
return return
} }
// NewRepositoryInfo validates and breaks down a repository name into a RepositoryInfo // newRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
func (config *ServiceConfig) NewRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) { func newRepositoryInfo(config *registrytypes.ServiceConfig, reposName reference.Named) (*RepositoryInfo, error) {
if err := validateNoSchema(reposName.Name()); err != nil { if err := validateNoSchema(reposName.Name()); err != nil {
return nil, err return nil, err
} }
@ -323,7 +299,7 @@ func (config *ServiceConfig) NewRepositoryInfo(reposName reference.Named) (*Repo
return nil, err return nil, err
} }
repoInfo.Index, err = config.NewIndexInfo(indexName) repoInfo.Index, err = newIndexInfo(config, indexName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -364,14 +340,14 @@ func (config *ServiceConfig) NewRepositoryInfo(reposName reference.Named) (*Repo
// ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but // ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but
// lacks registry configuration. // lacks registry configuration.
func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) { func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) {
return emptyServiceConfig.NewRepositoryInfo(reposName) return newRepositoryInfo(emptyServiceConfig, reposName)
} }
// ParseSearchIndexInfo will use repository name to get back an indexInfo. // ParseSearchIndexInfo will use repository name to get back an indexInfo.
func ParseSearchIndexInfo(reposName string) (*IndexInfo, error) { func ParseSearchIndexInfo(reposName string) (*registrytypes.IndexInfo, error) {
indexName, _ := splitReposSearchTerm(reposName) indexName, _ := splitReposSearchTerm(reposName)
indexInfo, err := emptyServiceConfig.NewIndexInfo(indexName) indexInfo, err := newIndexInfo(emptyServiceConfig, indexName)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -13,6 +13,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/distribution/registry/api/v2" "github.com/docker/distribution/registry/api/v2"
"github.com/docker/distribution/registry/client/transport" "github.com/docker/distribution/registry/client/transport"
registrytypes "github.com/docker/docker/api/types/registry"
) )
// for mocking in unit tests // for mocking in unit tests
@ -44,12 +45,12 @@ func scanForAPIVersion(address string) (string, APIVersion) {
// NewEndpoint parses the given address to return a registry endpoint. v can be used to // NewEndpoint parses the given address to return a registry endpoint. v can be used to
// specify a specific endpoint version // specify a specific endpoint version
func NewEndpoint(index *IndexInfo, metaHeaders http.Header, v APIVersion) (*Endpoint, error) { func NewEndpoint(index *registrytypes.IndexInfo, metaHeaders http.Header, v APIVersion) (*Endpoint, error) {
tlsConfig, err := newTLSConfig(index.Name, index.Secure) tlsConfig, err := newTLSConfig(index.Name, index.Secure)
if err != nil { if err != nil {
return nil, err return nil, err
} }
endpoint, err := newEndpoint(index.GetAuthConfigKey(), tlsConfig, metaHeaders) endpoint, err := newEndpoint(GetAuthConfigKey(index), tlsConfig, metaHeaders)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -16,6 +16,7 @@ import (
"time" "time"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/opts" "github.com/docker/docker/opts"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -150,22 +151,22 @@ func makeHTTPSURL(req string) string {
return testHTTPSServer.URL + req return testHTTPSServer.URL + req
} }
func makeIndex(req string) *IndexInfo { func makeIndex(req string) *registrytypes.IndexInfo {
index := &IndexInfo{ index := &registrytypes.IndexInfo{
Name: makeURL(req), Name: makeURL(req),
} }
return index return index
} }
func makeHTTPSIndex(req string) *IndexInfo { func makeHTTPSIndex(req string) *registrytypes.IndexInfo {
index := &IndexInfo{ index := &registrytypes.IndexInfo{
Name: makeHTTPSURL(req), Name: makeHTTPSURL(req),
} }
return index return index
} }
func makePublicIndex() *IndexInfo { func makePublicIndex() *registrytypes.IndexInfo {
index := &IndexInfo{ index := &registrytypes.IndexInfo{
Name: IndexServer, Name: IndexServer,
Secure: true, Secure: true,
Official: true, Official: true,
@ -173,7 +174,7 @@ func makePublicIndex() *IndexInfo {
return index return index
} }
func makeServiceConfig(mirrors []string, insecureRegistries []string) *ServiceConfig { func makeServiceConfig(mirrors []string, insecureRegistries []string) *registrytypes.ServiceConfig {
options := &Options{ options := &Options{
Mirrors: opts.NewListOpts(nil), Mirrors: opts.NewListOpts(nil),
InsecureRegistries: opts.NewListOpts(nil), InsecureRegistries: opts.NewListOpts(nil),

View file

@ -10,6 +10,8 @@ import (
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/distribution/registry/client/transport" "github.com/docker/distribution/registry/client/transport"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
) )
var ( var (
@ -49,7 +51,7 @@ func spawnTestRegistrySession(t *testing.T) *Session {
} }
func TestPingRegistryEndpoint(t *testing.T) { func TestPingRegistryEndpoint(t *testing.T) {
testPing := func(index *IndexInfo, expectedStandalone bool, assertMessage string) { testPing := func(index *registrytypes.IndexInfo, expectedStandalone bool, assertMessage string) {
ep, err := NewEndpoint(index, nil, APIVersionUnknown) ep, err := NewEndpoint(index, nil, APIVersionUnknown)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -69,7 +71,7 @@ func TestPingRegistryEndpoint(t *testing.T) {
func TestEndpoint(t *testing.T) { func TestEndpoint(t *testing.T) {
// Simple wrapper to fail test if err != nil // Simple wrapper to fail test if err != nil
expandEndpoint := func(index *IndexInfo) *Endpoint { expandEndpoint := func(index *registrytypes.IndexInfo) *Endpoint {
endpoint, err := NewEndpoint(index, nil, APIVersionUnknown) endpoint, err := NewEndpoint(index, nil, APIVersionUnknown)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -77,7 +79,7 @@ func TestEndpoint(t *testing.T) {
return endpoint return endpoint
} }
assertInsecureIndex := func(index *IndexInfo) { assertInsecureIndex := func(index *registrytypes.IndexInfo) {
index.Secure = true index.Secure = true
_, err := NewEndpoint(index, nil, APIVersionUnknown) _, err := NewEndpoint(index, nil, APIVersionUnknown)
assertNotEqual(t, err, nil, index.Name+": Expected error for insecure index") assertNotEqual(t, err, nil, index.Name+": Expected error for insecure index")
@ -85,7 +87,7 @@ func TestEndpoint(t *testing.T) {
index.Secure = false index.Secure = false
} }
assertSecureIndex := func(index *IndexInfo) { assertSecureIndex := func(index *registrytypes.IndexInfo) {
index.Secure = true index.Secure = true
_, err := NewEndpoint(index, nil, APIVersionUnknown) _, err := NewEndpoint(index, nil, APIVersionUnknown)
assertNotEqual(t, err, nil, index.Name+": Expected cert error for secure index") assertNotEqual(t, err, nil, index.Name+": Expected cert error for secure index")
@ -93,7 +95,7 @@ func TestEndpoint(t *testing.T) {
index.Secure = false index.Secure = false
} }
index := &IndexInfo{} index := &registrytypes.IndexInfo{}
index.Name = makeURL("/v1/") index.Name = makeURL("/v1/")
endpoint := expandEndpoint(index) endpoint := expandEndpoint(index)
assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name) assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
@ -363,7 +365,7 @@ func TestParseRepositoryInfo(t *testing.T) {
expectedRepoInfos := map[string]RepositoryInfo{ expectedRepoInfos := map[string]RepositoryInfo{
"fooo/bar": { "fooo/bar": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -373,7 +375,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"library/ubuntu": { "library/ubuntu": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -383,7 +385,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: true, Official: true,
}, },
"nonlibrary/ubuntu": { "nonlibrary/ubuntu": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -393,7 +395,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"ubuntu": { "ubuntu": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -403,7 +405,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: true, Official: true,
}, },
"other/library": { "other/library": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -413,7 +415,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"127.0.0.1:8000/private/moonbase": { "127.0.0.1:8000/private/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "127.0.0.1:8000", Name: "127.0.0.1:8000",
Official: false, Official: false,
}, },
@ -423,7 +425,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"127.0.0.1:8000/privatebase": { "127.0.0.1:8000/privatebase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "127.0.0.1:8000", Name: "127.0.0.1:8000",
Official: false, Official: false,
}, },
@ -433,7 +435,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"localhost:8000/private/moonbase": { "localhost:8000/private/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "localhost:8000", Name: "localhost:8000",
Official: false, Official: false,
}, },
@ -443,7 +445,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"localhost:8000/privatebase": { "localhost:8000/privatebase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "localhost:8000", Name: "localhost:8000",
Official: false, Official: false,
}, },
@ -453,7 +455,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"example.com/private/moonbase": { "example.com/private/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "example.com", Name: "example.com",
Official: false, Official: false,
}, },
@ -463,7 +465,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"example.com/privatebase": { "example.com/privatebase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "example.com", Name: "example.com",
Official: false, Official: false,
}, },
@ -473,7 +475,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"example.com:8000/private/moonbase": { "example.com:8000/private/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "example.com:8000", Name: "example.com:8000",
Official: false, Official: false,
}, },
@ -483,7 +485,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"example.com:8000/privatebase": { "example.com:8000/privatebase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "example.com:8000", Name: "example.com:8000",
Official: false, Official: false,
}, },
@ -493,7 +495,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"localhost/private/moonbase": { "localhost/private/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "localhost", Name: "localhost",
Official: false, Official: false,
}, },
@ -503,7 +505,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"localhost/privatebase": { "localhost/privatebase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: "localhost", Name: "localhost",
Official: false, Official: false,
}, },
@ -513,7 +515,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
IndexName + "/public/moonbase": { IndexName + "/public/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -523,7 +525,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"index." + IndexName + "/public/moonbase": { "index." + IndexName + "/public/moonbase": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -533,7 +535,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: false, Official: false,
}, },
"ubuntu-12.04-base": { "ubuntu-12.04-base": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -543,7 +545,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: true, Official: true,
}, },
IndexName + "/ubuntu-12.04-base": { IndexName + "/ubuntu-12.04-base": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -553,7 +555,7 @@ func TestParseRepositoryInfo(t *testing.T) {
Official: true, Official: true,
}, },
"index." + IndexName + "/ubuntu-12.04-base": { "index." + IndexName + "/ubuntu-12.04-base": {
Index: &IndexInfo{ Index: &registrytypes.IndexInfo{
Name: IndexName, Name: IndexName,
Official: true, Official: true,
}, },
@ -585,9 +587,9 @@ func TestParseRepositoryInfo(t *testing.T) {
} }
func TestNewIndexInfo(t *testing.T) { func TestNewIndexInfo(t *testing.T) {
testIndexInfo := func(config *ServiceConfig, expectedIndexInfos map[string]*IndexInfo) { testIndexInfo := func(config *registrytypes.ServiceConfig, expectedIndexInfos map[string]*registrytypes.IndexInfo) {
for indexName, expectedIndexInfo := range expectedIndexInfos { for indexName, expectedIndexInfo := range expectedIndexInfos {
index, err := config.NewIndexInfo(indexName) index, err := newIndexInfo(config, indexName)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
@ -601,7 +603,7 @@ func TestNewIndexInfo(t *testing.T) {
config := NewServiceConfig(nil) config := NewServiceConfig(nil)
noMirrors := []string{} noMirrors := []string{}
expectedIndexInfos := map[string]*IndexInfo{ expectedIndexInfos := map[string]*registrytypes.IndexInfo{
IndexName: { IndexName: {
Name: IndexName, Name: IndexName,
Official: true, Official: true,
@ -632,7 +634,7 @@ func TestNewIndexInfo(t *testing.T) {
publicMirrors := []string{"http://mirror1.local", "http://mirror2.local"} publicMirrors := []string{"http://mirror1.local", "http://mirror2.local"}
config = makeServiceConfig(publicMirrors, []string{"example.com"}) config = makeServiceConfig(publicMirrors, []string{"example.com"})
expectedIndexInfos = map[string]*IndexInfo{ expectedIndexInfos = map[string]*registrytypes.IndexInfo{
IndexName: { IndexName: {
Name: IndexName, Name: IndexName,
Official: true, Official: true,
@ -679,7 +681,7 @@ func TestNewIndexInfo(t *testing.T) {
testIndexInfo(config, expectedIndexInfos) testIndexInfo(config, expectedIndexInfos)
config = makeServiceConfig(nil, []string{"42.42.0.0/16"}) config = makeServiceConfig(nil, []string{"42.42.0.0/16"})
expectedIndexInfos = map[string]*IndexInfo{ expectedIndexInfos = map[string]*registrytypes.IndexInfo{
"example.com": { "example.com": {
Name: "example.com", Name: "example.com",
Official: false, Official: false,
@ -981,7 +983,7 @@ func TestIsSecureIndex(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
config := makeServiceConfig(nil, tt.insecureRegistries) config := makeServiceConfig(nil, tt.insecureRegistries)
if sec := config.isSecureIndex(tt.addr); sec != tt.expected { if sec := isSecureIndex(config, tt.addr); sec != tt.expected {
t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec) t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
} }
} }

View file

@ -8,12 +8,14 @@ import (
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/distribution/registry/client/auth" "github.com/docker/distribution/registry/client/auth"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
) )
// Service is a registry service. It tracks configuration data such as a list // Service is a registry service. It tracks configuration data such as a list
// of mirrors. // of mirrors.
type Service struct { type Service struct {
Config *ServiceConfig Config *registrytypes.ServiceConfig
} }
// NewService returns a new instance of Service ready to be // NewService returns a new instance of Service ready to be
@ -78,7 +80,7 @@ func (s *Service) Search(term string, authConfig *types.AuthConfig, headers map[
indexName, remoteName := splitReposSearchTerm(term) indexName, remoteName := splitReposSearchTerm(term)
index, err := s.Config.NewIndexInfo(indexName) index, err := newIndexInfo(s.Config, indexName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -109,12 +111,12 @@ func (s *Service) Search(term string, authConfig *types.AuthConfig, headers map[
// ResolveRepository splits a repository name into its components // ResolveRepository splits a repository name into its components
// and configuration of the associated registry. // and configuration of the associated registry.
func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) { func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) {
return s.Config.NewRepositoryInfo(name) return newRepositoryInfo(s.Config, name)
} }
// ResolveIndex takes indexName and returns index info // ResolveIndex takes indexName and returns index info
func (s *Service) ResolveIndex(name string) (*IndexInfo, error) { func (s *Service) ResolveIndex(name string) (*registrytypes.IndexInfo, error) {
return s.Config.NewIndexInfo(name) return newIndexInfo(s.Config, name)
} }
// APIEndpoint represents a remote API endpoint // APIEndpoint represents a remote API endpoint
@ -136,7 +138,7 @@ func (e APIEndpoint) ToV1Endpoint(metaHeaders http.Header) (*Endpoint, error) {
// TLSConfig constructs a client TLS configuration based on server defaults // TLSConfig constructs a client TLS configuration based on server defaults
func (s *Service) TLSConfig(hostname string) (*tls.Config, error) { func (s *Service) TLSConfig(hostname string) (*tls.Config, error) {
return newTLSConfig(hostname, s.Config.isSecureIndex(hostname)) return newTLSConfig(hostname, isSecureIndex(s.Config, hostname))
} }
func (s *Service) tlsConfigForMirror(mirror string) (*tls.Config, error) { func (s *Service) tlsConfigForMirror(mirror string) (*tls.Config, error) {

View file

@ -20,6 +20,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/httputils" "github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"

View file

@ -2,6 +2,7 @@ package registry
import ( import (
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
registrytypes "github.com/docker/docker/api/types/registry"
) )
// SearchResult describes a search result returned from a registry // SearchResult describes a search result returned from a registry
@ -83,51 +84,10 @@ const (
APIVersion2 APIVersion2
) )
// IndexInfo contains information about a registry
//
// RepositoryInfo Examples:
// {
// "Index" : {
// "Name" : "docker.io",
// "Mirrors" : ["https://registry-2.docker.io/v1/", "https://registry-3.docker.io/v1/"],
// "Secure" : true,
// "Official" : true,
// },
// "RemoteName" : "library/debian",
// "LocalName" : "debian",
// "CanonicalName" : "docker.io/debian"
// "Official" : true,
// }
//
// {
// "Index" : {
// "Name" : "127.0.0.1:5000",
// "Mirrors" : [],
// "Secure" : false,
// "Official" : false,
// },
// "RemoteName" : "user/repo",
// "LocalName" : "127.0.0.1:5000/user/repo",
// "CanonicalName" : "127.0.0.1:5000/user/repo",
// "Official" : false,
// }
type IndexInfo struct {
// Name is the name of the registry, such as "docker.io"
Name string
// Mirrors is a list of mirrors, expressed as URIs
Mirrors []string
// Secure is set to false if the registry is part of the list of
// insecure registries. Insecure registries accept HTTP and/or accept
// HTTPS with certificates from unknown CAs.
Secure bool
// Official indicates whether this is an official registry
Official bool
}
// RepositoryInfo describes a repository // RepositoryInfo describes a repository
type RepositoryInfo struct { type RepositoryInfo struct {
// Index points to registry information // Index points to registry information
Index *IndexInfo Index *registrytypes.IndexInfo
// RemoteName is the remote name of the repository, such as // RemoteName is the remote name of the repository, such as
// "library/ubuntu-12.04-base" // "library/ubuntu-12.04-base"
RemoteName reference.Named RemoteName reference.Named