Browse Source

daemon: use the registry service config for getting registry hosts

The RegistryHosts lookup function is used by both BuildKit and by the
containerd snapshotter. However, this function differs in behaviour from
the config parser for the RegistryConfig:

- The protocol for insecure registries is treated as significant by
  RegistryHosts, while the RegistryConfig strips this information.
- RegistryConfig validates and deduplicates mirrors.
- RegistryConfig does not parse the insecure-registries as URLs, which
  can lead to parsing opaque URLs as was possible by the RegistryHosts
  function.

This patch updates the lookup function to ensure consistency.

Signed-off-by: Justin Chadwell <me@jedevc.com>
Justin Chadwell 1 year ago
parent
commit
e0b065cc33
1 changed files with 10 additions and 34 deletions
  1. 10 34
      daemon/daemon.go

+ 10 - 34
daemon/daemon.go

@@ -9,12 +9,10 @@ import (
 	"context"
 	"fmt"
 	"net"
-	"net/url"
 	"os"
 	"path"
 	"path/filepath"
 	"runtime"
-	"strings"
 	"sync"
 	"sync/atomic"
 	"time"
@@ -194,42 +192,20 @@ func (daemon *Daemon) UsesSnapshotter() bool {
 // RegistryHosts returns the registry hosts configuration for the host component
 // of a distribution image reference.
 func (daemon *Daemon) RegistryHosts(host string) ([]docker.RegistryHost, error) {
-	var (
-		conf        = daemon.config()
-		registryKey = "docker.io"
-		mirrors     = make([]string, len(conf.Mirrors))
-		m           = map[string]resolverconfig.RegistryConfig{}
-	)
-	// must trim "https://" or "http://" prefix
-	for i, v := range conf.Mirrors {
-		if uri, err := url.Parse(v); err == nil {
-			v = uri.Host
-		}
-		mirrors[i] = v
-	}
-	// set mirrors for default registry
-	m[registryKey] = resolverconfig.RegistryConfig{Mirrors: mirrors}
+	var m = map[string]resolverconfig.RegistryConfig{}
 
-	for _, v := range conf.InsecureRegistries {
-		u, err := url.Parse(v)
-		if err != nil && !strings.HasPrefix(v, "http://") && !strings.HasPrefix(v, "https://") {
-			originalErr := err
-			u, err = url.Parse("http://" + v)
-			if err != nil {
-				err = originalErr
-			}
-		}
+	mirrors := daemon.registryService.ServiceConfig().Mirrors
+	m["docker.io"] = resolverconfig.RegistryConfig{Mirrors: mirrors}
+
+	conf := daemon.registryService.ServiceConfig().IndexConfigs
+	for k, v := range conf {
 		c := resolverconfig.RegistryConfig{}
-		if err == nil {
-			v = u.Host
+		if !v.Secure {
 			t := true
-			if u.Scheme == "http" {
-				c.PlainHTTP = &t
-			} else {
-				c.Insecure = &t
-			}
+			c.PlainHTTP = &t
+			c.Insecure = &t
 		}
-		m[v] = c
+		m[k] = c
 	}
 
 	for k, v := range m {