Skip to content

Commit

Permalink
Support sync with multi cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Dec 20, 2024
1 parent f44ea30 commit ea3fe05
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 65 deletions.
47 changes: 22 additions & 25 deletions cmd/crproxy/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import (
"strings"

"github.com/daocloud/crproxy/cache"
"github.com/daocloud/crproxy/storage"
csync "github.com/daocloud/crproxy/sync"
"github.com/daocloud/crproxy/transport"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/registry/storage/driver/factory"
"github.com/spf13/cobra"
)

type flagpole struct {
StorageDriver string
StorageParameters map[string]string
Deep bool
List []string
ListFromFile string
Platform []string
MaxWarn int
Userpass []string
StorageURL []string
Deep bool
List []string
ListFromFile string
Platform []string
MaxWarn int
Userpass []string
}

func NewCommand() *cobra.Command {
Expand All @@ -44,8 +43,8 @@ func NewCommand() *cobra.Command {
return runE(cmd.Context(), flags)
},
}
cmd.Flags().StringVar(&flags.StorageDriver, "storage-driver", flags.StorageDriver, "Storage driver")
cmd.Flags().StringToStringVar(&flags.StorageParameters, "storage-parameters", flags.StorageParameters, "Storage parameters")

cmd.Flags().StringSliceVar(&flags.StorageURL, "storage-url", flags.StorageURL, "Storage driver url")
cmd.Flags().BoolVar(&flags.Deep, "deep", flags.Deep, "Deep sync")
cmd.Flags().StringSliceVar(&flags.List, "list", flags.List, "List")
cmd.Flags().StringVar(&flags.ListFromFile, "list-from-file", flags.ListFromFile, "List from file")
Expand All @@ -61,21 +60,19 @@ func runE(ctx context.Context, flags *flagpole) error {

logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))

cacheOpts := []cache.Option{}
var caches []*cache.Cache
for _, s := range flags.StorageURL {
sd, err := storage.NewStorage(s)
if err != nil {
return fmt.Errorf("create storage driver failed: %w", err)
}

parameters := map[string]interface{}{}
for k, v := range flags.StorageParameters {
parameters[k] = v
}
sd, err := factory.Create(flags.StorageDriver, parameters)
if err != nil {
return fmt.Errorf("create storage driver failed: %w", err)
}
cacheOpts = append(cacheOpts, cache.WithStorageDriver(sd))
cache, err := cache.NewCache(cache.WithStorageDriver(sd))
if err != nil {
return fmt.Errorf("create cache failed: %w", err)
}

cache, err := cache.NewCache(cacheOpts...)
if err != nil {
return fmt.Errorf("create cache failed: %w", err)
caches = append(caches, cache)
}

transportOpts := []transport.Option{
Expand All @@ -92,7 +89,7 @@ func runE(ctx context.Context, flags *flagpole) error {
}

opts = append(opts,
csync.WithCache(cache),
csync.WithCaches(caches...),
csync.WithDomainAlias(map[string]string{
"docker.io": "registry-1.docker.io",
"ollama.ai": "registry.ollama.ai",
Expand Down
46 changes: 46 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package storage

import (
"fmt"
"net/url"
"strings"

"github.com/docker/distribution/registry/storage/driver"
"github.com/docker/distribution/registry/storage/driver/factory"
)

type StorageDriver = driver.StorageDriver

func NewStorage(uri string) (StorageDriver, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}

parameters := map[string]interface{}{}
query := u.Query()
for k := range query {
parameters[k] = query.Get(k)
}

if u.User != nil {
parameters["accesskeyid"] = u.User.Username()
parameters["accesskeysecret"], _ = u.User.Password()
}

if u.Host != "" {
part := strings.Split(u.Host, ".")
if len(part) != 2 {
return nil, fmt.Errorf("invalid host %q", u.Host)
}

parameters["bucket"] = part[0]
parameters["region"] = part[1]
}

sd, err := factory.Create(u.Scheme, parameters)
if err != nil {
return nil, fmt.Errorf("create storage driver failed: %w", err)
}
return sd, nil
}
Loading

0 comments on commit ea3fe05

Please sign in to comment.