Procházet zdrojové kódy

registry: return concrete service type

Move interface definitions to the packages which use the registry
service.

https://github.com/golang/go/wiki/CodeReviewComments#interfaces

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider před 2 roky
rodič
revize
7b3acdff5d

+ 1 - 1
daemon/containerd/resolver.go

@@ -23,7 +23,7 @@ func (i *ImageService) newResolverFromAuthConfig(authConfig *registrytypes.AuthC
 	}), tracker
 	}), tracker
 }
 }
 
 
-func hostsWrapper(hostsFn docker.RegistryHosts, authConfig *registrytypes.AuthConfig, regService registry.Service) docker.RegistryHosts {
+func hostsWrapper(hostsFn docker.RegistryHosts, authConfig *registrytypes.AuthConfig, regService RegistryConfigProvider) docker.RegistryHosts {
 	return func(n string) ([]docker.RegistryHost, error) {
 	return func(n string) ([]docker.RegistryHost, error) {
 		hosts, err := hostsFn(n)
 		hosts, err := hostsFn(n)
 		if err != nil {
 		if err != nil {

+ 6 - 3
daemon/containerd/service.go

@@ -14,7 +14,6 @@ import (
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/layer"
 	"github.com/docker/docker/layer"
-	"github.com/docker/docker/registry"
 	"github.com/opencontainers/go-digest"
 	"github.com/opencontainers/go-digest"
 	"github.com/opencontainers/image-spec/identity"
 	"github.com/opencontainers/image-spec/identity"
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -27,15 +26,19 @@ type ImageService struct {
 	containers      container.Store
 	containers      container.Store
 	snapshotter     string
 	snapshotter     string
 	registryHosts   RegistryHostsProvider
 	registryHosts   RegistryHostsProvider
-	registryService registry.Service
+	registryService RegistryConfigProvider
 }
 }
 
 
 type RegistryHostsProvider interface {
 type RegistryHostsProvider interface {
 	RegistryHosts() docker.RegistryHosts
 	RegistryHosts() docker.RegistryHosts
 }
 }
 
 
+type RegistryConfigProvider interface {
+	IsInsecureRegistry(host string) bool
+}
+
 // NewService creates a new ImageService.
 // NewService creates a new ImageService.
-func NewService(c *containerd.Client, containers container.Store, snapshotter string, hostsProvider RegistryHostsProvider, registry registry.Service) *ImageService {
+func NewService(c *containerd.Client, containers container.Store, snapshotter string, hostsProvider RegistryHostsProvider, registry RegistryConfigProvider) *ImageService {
 	return &ImageService{
 	return &ImageService{
 		client:          c,
 		client:          c,
 		containers:      containers,
 		containers:      containers,

+ 2 - 2
daemon/daemon.go

@@ -82,7 +82,7 @@ type Daemon struct {
 	configStore           *config.Config
 	configStore           *config.Config
 	statsCollector        *stats.Collector
 	statsCollector        *stats.Collector
 	defaultLogConfig      containertypes.LogConfig
 	defaultLogConfig      containertypes.LogConfig
-	registryService       registry.Service
+	registryService       *registry.Service
 	EventsService         *events.Events
 	EventsService         *events.Events
 	netController         *libnetwork.Controller
 	netController         *libnetwork.Controller
 	volumes               *volumesservice.VolumesService
 	volumes               *volumesservice.VolumesService
@@ -1460,7 +1460,7 @@ func (daemon *Daemon) ImageService() ImageService {
 }
 }
 
 
 // RegistryService returns the Daemon's RegistryService
 // RegistryService returns the Daemon's RegistryService
-func (daemon *Daemon) RegistryService() registry.Service {
+func (daemon *Daemon) RegistryService() *registry.Service {
 	return daemon.registryService
 	return daemon.registryService
 }
 }
 
 

+ 3 - 3
daemon/images/service.go

@@ -8,12 +8,12 @@ import (
 	"github.com/containerd/containerd/leases"
 	"github.com/containerd/containerd/leases"
 	"github.com/docker/docker/container"
 	"github.com/docker/docker/container"
 	daemonevents "github.com/docker/docker/daemon/events"
 	daemonevents "github.com/docker/docker/daemon/events"
+	"github.com/docker/docker/distribution"
 	"github.com/docker/docker/distribution/metadata"
 	"github.com/docker/docker/distribution/metadata"
 	"github.com/docker/docker/distribution/xfer"
 	"github.com/docker/docker/distribution/xfer"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/layer"
 	"github.com/docker/docker/layer"
 	dockerreference "github.com/docker/docker/reference"
 	dockerreference "github.com/docker/docker/reference"
-	"github.com/docker/docker/registry"
 	"github.com/opencontainers/go-digest"
 	"github.com/opencontainers/go-digest"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 )
 )
@@ -39,7 +39,7 @@ type ImageServiceConfig struct {
 	MaxConcurrentUploads      int
 	MaxConcurrentUploads      int
 	MaxDownloadAttempts       int
 	MaxDownloadAttempts       int
 	ReferenceStore            dockerreference.Store
 	ReferenceStore            dockerreference.Store
-	RegistryService           registry.Service
+	RegistryService           distribution.RegistryResolver
 	ContentStore              content.Store
 	ContentStore              content.Store
 	Leases                    leases.Manager
 	Leases                    leases.Manager
 	ContentNamespace          string
 	ContentNamespace          string
@@ -73,7 +73,7 @@ type ImageService struct {
 	layerStore                layer.Store
 	layerStore                layer.Store
 	pruneRunning              int32
 	pruneRunning              int32
 	referenceStore            dockerreference.Store
 	referenceStore            dockerreference.Store
-	registryService           registry.Service
+	registryService           distribution.RegistryResolver
 	uploadManager             *xfer.LayerUploadManager
 	uploadManager             *xfer.LayerUploadManager
 	leases                    leases.Manager
 	leases                    leases.Manager
 	content                   content.Store
 	content                   content.Store

+ 9 - 1
distribution/config.go

@@ -8,6 +8,7 @@ import (
 
 
 	"github.com/docker/distribution"
 	"github.com/docker/distribution"
 	"github.com/docker/distribution/manifest/schema2"
 	"github.com/docker/distribution/manifest/schema2"
+	"github.com/docker/distribution/reference"
 	"github.com/docker/docker/api/types/registry"
 	"github.com/docker/docker/api/types/registry"
 	"github.com/docker/docker/distribution/metadata"
 	"github.com/docker/docker/distribution/metadata"
 	"github.com/docker/docker/distribution/xfer"
 	"github.com/docker/docker/distribution/xfer"
@@ -35,7 +36,7 @@ type Config struct {
 	ProgressOutput progress.Output
 	ProgressOutput progress.Output
 	// RegistryService is the registry service to use for TLS configuration
 	// RegistryService is the registry service to use for TLS configuration
 	// and endpoint lookup.
 	// and endpoint lookup.
-	RegistryService registrypkg.Service
+	RegistryService RegistryResolver
 	// ImageEventLogger notifies events for a given image
 	// ImageEventLogger notifies events for a given image
 	ImageEventLogger func(id, name, action string)
 	ImageEventLogger func(id, name, action string)
 	// MetadataStore is the storage backend for distribution-specific
 	// MetadataStore is the storage backend for distribution-specific
@@ -75,6 +76,13 @@ type ImagePushConfig struct {
 	UploadManager *xfer.LayerUploadManager
 	UploadManager *xfer.LayerUploadManager
 }
 }
 
 
+// RegistryResolver is used for TLS configuration and endpoint lookup.
+type RegistryResolver interface {
+	LookupPushEndpoints(hostname string) (endpoints []registrypkg.APIEndpoint, err error)
+	LookupPullEndpoints(hostname string) (endpoints []registrypkg.APIEndpoint, err error)
+	ResolveRepository(name reference.Named) (*registrypkg.RepositoryInfo, error)
+}
+
 // ImageConfigStore handles storing and getting image configurations
 // ImageConfigStore handles storing and getting image configurations
 // by digest. Allows getting an image configurations rootfs from the
 // by digest. Allows getting an image configurations rootfs from the
 // configuration.
 // configuration.

+ 1 - 1
registry/registry_test.go

@@ -520,7 +520,7 @@ func TestMirrorEndpointLookup(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
-	s := defaultService{config: cfg}
+	s := Service{config: cfg}
 
 
 	imageName, err := reference.WithName(IndexName + "/test/image")
 	imageName, err := reference.WithName(IndexName + "/test/image")
 	if err != nil {
 	if err != nil {

+ 2 - 2
registry/search.go

@@ -24,7 +24,7 @@ var acceptedSearchFilterTags = map[string]bool{
 
 
 // Search queries the public registry for repositories matching the specified
 // Search queries the public registry for repositories matching the specified
 // search term and filters.
 // search term and filters.
-func (s *defaultService) Search(ctx context.Context, searchFilters filters.Args, term string, limit int, authConfig *registry.AuthConfig, headers map[string][]string) ([]registry.SearchResult, error) {
+func (s *Service) Search(ctx context.Context, searchFilters filters.Args, term string, limit int, authConfig *registry.AuthConfig, headers map[string][]string) ([]registry.SearchResult, error) {
 	if err := searchFilters.Validate(acceptedSearchFilterTags); err != nil {
 	if err := searchFilters.Validate(acceptedSearchFilterTags); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -80,7 +80,7 @@ func (s *defaultService) Search(ctx context.Context, searchFilters filters.Args,
 	return filteredResults, nil
 	return filteredResults, nil
 }
 }
 
 
-func (s *defaultService) searchUnfiltered(ctx context.Context, term string, limit int, authConfig *registry.AuthConfig, userAgent string, headers map[string][]string) (*registry.SearchResults, error) {
+func (s *Service) searchUnfiltered(ctx context.Context, term string, limit int, authConfig *registry.AuthConfig, userAgent string, headers map[string][]string) (*registry.SearchResults, error) {
 	// TODO Use ctx when searching for repositories
 	// TODO Use ctx when searching for repositories
 	if hasScheme(term) {
 	if hasScheme(term) {
 		return nil, invalidParamf("invalid repository name: repository name (%s) should not have a scheme", term)
 		return nil, invalidParamf("invalid repository name: repository name (%s) should not have a scheme", term)

+ 13 - 28
registry/service.go

@@ -8,50 +8,35 @@ import (
 	"sync"
 	"sync"
 
 
 	"github.com/docker/distribution/reference"
 	"github.com/docker/distribution/reference"
-	"github.com/docker/docker/api/types/filters"
 	"github.com/docker/docker/api/types/registry"
 	"github.com/docker/docker/api/types/registry"
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/errdefs"
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 )
 )
 
 
-// Service is the interface defining what a registry service should implement.
-type Service interface {
-	Auth(ctx context.Context, authConfig *registry.AuthConfig, userAgent string) (status, token string, err error)
-	LookupPullEndpoints(hostname string) (endpoints []APIEndpoint, err error)
-	LookupPushEndpoints(hostname string) (endpoints []APIEndpoint, err error)
-	ResolveRepository(name reference.Named) (*RepositoryInfo, error)
-	Search(ctx context.Context, searchFilters filters.Args, term string, limit int, authConfig *registry.AuthConfig, headers map[string][]string) ([]registry.SearchResult, error)
-	ServiceConfig() *registry.ServiceConfig
-	LoadAllowNondistributableArtifacts([]string) error
-	LoadMirrors([]string) error
-	LoadInsecureRegistries([]string) error
-	IsInsecureRegistry(string) bool
-}
-
-// defaultService 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 defaultService struct {
+type Service struct {
 	config *serviceConfig
 	config *serviceConfig
 	mu     sync.RWMutex
 	mu     sync.RWMutex
 }
 }
 
 
 // NewService returns a new instance of defaultService ready to be
 // NewService returns a new instance of defaultService ready to be
 // installed into an engine.
 // installed into an engine.
-func NewService(options ServiceOptions) (Service, error) {
+func NewService(options ServiceOptions) (*Service, error) {
 	config, err := newServiceConfig(options)
 	config, err := newServiceConfig(options)
 
 
-	return &defaultService{config: config}, err
+	return &Service{config: config}, err
 }
 }
 
 
 // ServiceConfig returns a copy of the public registry service's configuration.
 // ServiceConfig returns a copy of the public registry service's configuration.
-func (s *defaultService) ServiceConfig() *registry.ServiceConfig {
+func (s *Service) ServiceConfig() *registry.ServiceConfig {
 	s.mu.RLock()
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	defer s.mu.RUnlock()
 	return s.config.copy()
 	return s.config.copy()
 }
 }
 
 
 // LoadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries for Service.
 // LoadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries for Service.
-func (s *defaultService) LoadAllowNondistributableArtifacts(registries []string) error {
+func (s *Service) LoadAllowNondistributableArtifacts(registries []string) error {
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()
 
 
@@ -59,7 +44,7 @@ func (s *defaultService) LoadAllowNondistributableArtifacts(registries []string)
 }
 }
 
 
 // LoadMirrors loads registry mirrors for Service
 // LoadMirrors loads registry mirrors for Service
-func (s *defaultService) LoadMirrors(mirrors []string) error {
+func (s *Service) LoadMirrors(mirrors []string) error {
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()
 
 
@@ -67,7 +52,7 @@ func (s *defaultService) LoadMirrors(mirrors []string) error {
 }
 }
 
 
 // LoadInsecureRegistries loads insecure registries for Service
 // LoadInsecureRegistries loads insecure registries for Service
-func (s *defaultService) LoadInsecureRegistries(registries []string) error {
+func (s *Service) LoadInsecureRegistries(registries []string) error {
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()
 
 
@@ -77,7 +62,7 @@ func (s *defaultService) LoadInsecureRegistries(registries []string) error {
 // Auth contacts the public registry with the provided credentials,
 // Auth contacts the public registry with the provided credentials,
 // and returns OK if authentication was successful.
 // and returns OK if authentication was successful.
 // It can be used to verify the validity of a client's credentials.
 // It can be used to verify the validity of a client's credentials.
-func (s *defaultService) Auth(ctx context.Context, authConfig *registry.AuthConfig, userAgent string) (status, token string, err error) {
+func (s *Service) Auth(ctx context.Context, authConfig *registry.AuthConfig, userAgent string) (status, token string, err error) {
 	// TODO Use ctx when searching for repositories
 	// TODO Use ctx when searching for repositories
 	var registryHostName = IndexHostname
 	var registryHostName = IndexHostname
 
 
@@ -130,7 +115,7 @@ func splitReposSearchTerm(reposName string) (string, string) {
 
 
 // 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 *defaultService) ResolveRepository(name reference.Named) (*RepositoryInfo, error) {
+func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) {
 	s.mu.RLock()
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	defer s.mu.RUnlock()
 	return newRepositoryInfo(s.config, name)
 	return newRepositoryInfo(s.config, name)
@@ -149,7 +134,7 @@ type APIEndpoint struct {
 
 
 // LookupPullEndpoints creates a list of v2 endpoints to try to pull from, in order of preference.
 // LookupPullEndpoints creates a list of v2 endpoints to try to pull from, in order of preference.
 // It gives preference to mirrors over the actual registry, and HTTPS over plain HTTP.
 // It gives preference to mirrors over the actual registry, and HTTPS over plain HTTP.
-func (s *defaultService) LookupPullEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
+func (s *Service) LookupPullEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
 	s.mu.RLock()
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	defer s.mu.RUnlock()
 
 
@@ -158,7 +143,7 @@ func (s *defaultService) LookupPullEndpoints(hostname string) (endpoints []APIEn
 
 
 // LookupPushEndpoints creates a list of v2 endpoints to try to push to, in order of preference.
 // LookupPushEndpoints creates a list of v2 endpoints to try to push to, in order of preference.
 // It gives preference to HTTPS over plain HTTP. Mirrors are not included.
 // It gives preference to HTTPS over plain HTTP. Mirrors are not included.
-func (s *defaultService) LookupPushEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
+func (s *Service) LookupPushEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
 	s.mu.RLock()
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	defer s.mu.RUnlock()
 
 
@@ -175,7 +160,7 @@ func (s *defaultService) LookupPushEndpoints(hostname string) (endpoints []APIEn
 
 
 // IsInsecureRegistry returns true if the registry at given host is configured as
 // IsInsecureRegistry returns true if the registry at given host is configured as
 // insecure registry.
 // insecure registry.
-func (s *defaultService) IsInsecureRegistry(host string) bool {
+func (s *Service) IsInsecureRegistry(host string) bool {
 	s.mu.RLock()
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	defer s.mu.RUnlock()
 	return !s.config.isSecureIndex(host)
 	return !s.config.isSecureIndex(host)

+ 1 - 1
registry/service_v2.go

@@ -7,7 +7,7 @@ import (
 	"github.com/docker/go-connections/tlsconfig"
 	"github.com/docker/go-connections/tlsconfig"
 )
 )
 
 
-func (s *defaultService) lookupV2Endpoints(hostname string) (endpoints []APIEndpoint, err error) {
+func (s *Service) lookupV2Endpoints(hostname string) (endpoints []APIEndpoint, err error) {
 	ana := s.config.allowNondistributableArtifacts(hostname)
 	ana := s.config.allowNondistributableArtifacts(hostname)
 
 
 	if hostname == DefaultNamespace || hostname == IndexHostname {
 	if hostname == DefaultNamespace || hostname == IndexHostname {