From 025a6e83525d2a6276a4768554af5a5b48fd6bbc Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Mon, 13 Jan 2025 11:35:30 +0800 Subject: [PATCH] Clean up the built-in alias --- cmd/crproxy/cluster/gateway/gateway.go | 16 ++------------- cmd/crproxy/sync/sync.go | 4 ---- internal/utils/utils.go | 18 +++++++++++++++++ sync/sync.go | 28 ++++---------------------- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/cmd/crproxy/cluster/gateway/gateway.go b/cmd/crproxy/cluster/gateway/gateway.go index bee6560..d4e0b15 100644 --- a/cmd/crproxy/cluster/gateway/gateway.go +++ b/cmd/crproxy/cluster/gateway/gateway.go @@ -8,13 +8,13 @@ import ( "net/http" "net/url" "os" - "strings" "time" "github.com/daocloud/crproxy/cache" "github.com/daocloud/crproxy/gateway" "github.com/daocloud/crproxy/internal/pki" "github.com/daocloud/crproxy/internal/server" + "github.com/daocloud/crproxy/internal/utils" "github.com/daocloud/crproxy/signing" "github.com/daocloud/crproxy/storage" "github.com/daocloud/crproxy/token" @@ -125,19 +125,7 @@ func runE(ctx context.Context, flags *flagpole) error { } } - if info.Host == "docker.io" { - info.Host = "registry-1.docker.io" - } else if info.Host == "ollama.ai" { - info.Host = "registry.ollama.ai" - } - - // docker.io/busybox => docker.io/library/busybox - if info.Host == "registry-1.docker.io" && !strings.Contains(info.Name, "/") { - info.Name = "library/" + info.Name - } - if info.Host == "registry.ollama.ai" && !strings.Contains(info.Name, "/") { - info.Name = "library/" + info.Name - } + info.Host, info.Name = utils.CorrectImage(info.Host, info.Name) return info }), gateway.WithDisableTagsList(flags.DisableTagsList), diff --git a/cmd/crproxy/sync/sync.go b/cmd/crproxy/sync/sync.go index 9be6873..d622e56 100644 --- a/cmd/crproxy/sync/sync.go +++ b/cmd/crproxy/sync/sync.go @@ -95,10 +95,6 @@ func runE(ctx context.Context, flags *flagpole) error { opts = append(opts, csync.WithCaches(caches...), - csync.WithDomainAlias(map[string]string{ - "docker.io": "registry-1.docker.io", - "ollama.ai": "registry.ollama.ai", - }), csync.WithDeep(flags.Deep), csync.WithTransport(tp), csync.WithLogger(logger), diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 5516160..08a427c 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/http" + "strings" "github.com/docker/distribution/registry/api/errcode" ) @@ -68,3 +69,20 @@ func ServeError(rw http.ResponseWriter, r *http.Request, err error, sc int) erro } return json.NewEncoder(rw).Encode(err) } + +func CorrectImage(host, name string) (string, string) { + if host == "docker.io" { + host = "registry-1.docker.io" + } else if host == "ollama.ai" { + host = "registry.ollama.ai" + } + + // docker.io/busybox => docker.io/library/busybox + if host == "registry-1.docker.io" && !strings.Contains(name, "/") { + name = "library/" + name + } + if host == "registry.ollama.ai" && !strings.Contains(name, "/") { + name = "library/" + name + } + return host, name +} diff --git a/sync/sync.go b/sync/sync.go index 2bba320..821fbcf 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -38,27 +38,15 @@ func newNameWithoutDomain(named reference.Named, name string) reference.Named { } type SyncManager struct { - transport http.RoundTripper - caches []*cache.Cache - logger *slog.Logger - domainAlias map[string]string - deep bool + transport http.RoundTripper + caches []*cache.Cache + logger *slog.Logger + deep bool excludeTags []*regexp.Regexp filterPlatform func(pf manifestlist.PlatformSpec) bool } -func (c *SyncManager) getDomainAlias(host string) string { - if c.domainAlias == nil { - return host - } - h, ok := c.domainAlias[host] - if !ok { - return host - } - return h -} - type Option func(*SyncManager) func WithDeep(deep bool) Option { @@ -67,12 +55,6 @@ func WithDeep(deep bool) Option { } } -func WithDomainAlias(domainAlias map[string]string) Option { - return func(c *SyncManager) { - c.domainAlias = domainAlias - } -} - func WithLogger(logger *slog.Logger) Option { return func(c *SyncManager) { c.logger = logger @@ -152,8 +134,6 @@ func (c *SyncManager) Image(ctx context.Context, image string) error { path := reference.Path(named) - host = c.getDomainAlias(host) - name := newNameWithoutDomain(named, path) repo, err := client.NewRepository(name, "https://"+host, c.transport)