Browse Source

registry: remove ErrInvalidRepositoryName

This error was only returned in a single location, and not used anywhere
as a specific type.

The error returned by `validateNoScheme()` also appeared to only be used in
one case; in all other cases, the error itself was ignored, and replaced with
a custom error. Because of this, this patch also replace `validateNoScheme()`
with a `hasScheme()` function that returns a boolean, to better match how it's
used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 years ago
parent
commit
98202c86ad
2 changed files with 6 additions and 15 deletions
  1. 4 13
      registry/config.go
  2. 2 2
      registry/service.go

+ 4 - 13
registry/config.go

@@ -10,7 +10,6 @@ import (
 
 	"github.com/docker/distribution/reference"
 	registrytypes "github.com/docker/docker/api/types/registry"
-	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 )
 
@@ -58,10 +57,6 @@ var (
 		Host:   DefaultRegistryHost,
 	}
 
-	// ErrInvalidRepositoryName is an error returned if the repository name did
-	// not have the correct form
-	ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
-
 	emptyServiceConfig, _ = newServiceConfig(ServiceOptions{})
 	validHostPortRegex    = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
 
@@ -101,7 +96,7 @@ func (config *serviceConfig) loadAllowNondistributableArtifacts(registries []str
 		if _, err := ValidateIndexName(r); err != nil {
 			return err
 		}
-		if validateNoScheme(r) != nil {
+		if hasScheme(r) {
 			return fmt.Errorf("allow-nondistributable-artifacts registry %s should not contain '://'", r)
 		}
 
@@ -188,7 +183,7 @@ skip:
 		} else if strings.HasPrefix(strings.ToLower(r), "https://") {
 			logrus.Warnf("insecure registry %s should not contain 'https://' and 'https://' has been removed from the insecure registry config", r)
 			r = r[8:]
-		} else if validateNoScheme(r) != nil {
+		} else if hasScheme(r) {
 			// Insecure registry should not contain '://'
 			// before returning err, roll back to original data
 			config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
@@ -343,12 +338,8 @@ func ValidateIndexName(val string) (string, error) {
 	return val, nil
 }
 
-func validateNoScheme(reposName string) error {
-	if strings.Contains(reposName, "://") {
-		// It cannot contain a scheme!
-		return ErrInvalidRepositoryName
-	}
-	return nil
+func hasScheme(reposName string) bool {
+	return strings.Contains(reposName, "://")
 }
 
 func validateHostPort(s string) error {

+ 2 - 2
registry/service.go

@@ -161,8 +161,8 @@ func splitReposSearchTerm(reposName string) (string, string) {
 // search terms, and returns the results.
 func (s *defaultService) Search(ctx context.Context, term string, limit int, authConfig *types.AuthConfig, userAgent string, headers map[string][]string) (*registrytypes.SearchResults, error) {
 	// TODO Use ctx when searching for repositories
-	if err := validateNoScheme(term); err != nil {
-		return nil, err
+	if hasScheme(term) {
+		return nil, errors.New(`invalid repository name (ex: "registry.domain.tld/myrepos")`)
 	}
 
 	indexName, remoteName := splitReposSearchTerm(term)