Skip to content

Commit

Permalink
Clean up the built-in alias
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jan 13, 2025
1 parent 97aa265 commit 025a6e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
16 changes: 2 additions & 14 deletions cmd/crproxy/cluster/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 0 additions & 4 deletions cmd/crproxy/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 18 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/http"
"strings"

"github.com/docker/distribution/registry/api/errcode"
)
Expand Down Expand Up @@ -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
}
28 changes: 4 additions & 24 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 025a6e8

Please sign in to comment.