|
| 1 | +// Copyright 2023 CUE Labs AG |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package modmux |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "cuelabs.dev/go/oci/ociregistry" |
| 24 | + |
| 25 | + "cuelang.org/go/internal/mod/modresolve" |
| 26 | +) |
| 27 | + |
| 28 | +// New returns a registry implementation that uses the given |
| 29 | +// resolver to multiplex between different registries. |
| 30 | +// |
| 31 | +// The newRegistry function will be used to create the |
| 32 | +// registries for the hosts in the [modresolver.Location] values |
| 33 | +// returned by the resolver. |
| 34 | +// |
| 35 | +// The returned registry always returns an error for Repositories and MountBlob |
| 36 | +// (neither of these capabilities are required or used by the module fetching/pushing |
| 37 | +// logic). |
| 38 | +func New(resolver modresolve.Resolver, newRegistry func(host string, insecure bool) (ociregistry.Interface, error)) ociregistry.Interface { |
| 39 | + return ®istry{ |
| 40 | + resolver: resolver, |
| 41 | + newRegistry: newRegistry, |
| 42 | + repos: make(map[string]ociregistry.Interface), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +type registry struct { |
| 47 | + *ociregistry.Funcs |
| 48 | + resolver modresolve.Resolver |
| 49 | + newRegistry func(host string, insecure bool) (ociregistry.Interface, error) |
| 50 | + |
| 51 | + mu sync.Mutex |
| 52 | + repos map[string]ociregistry.Interface |
| 53 | +} |
| 54 | + |
| 55 | +func (r *registry) GetBlob(ctx context.Context, repo string, digest ociregistry.Digest) (ociregistry.BlobReader, error) { |
| 56 | + cr, repo, err := r.resolve(repo) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return cr.GetBlob(ctx, repo, digest) |
| 61 | +} |
| 62 | + |
| 63 | +func (r *registry) GetBlobRange(ctx context.Context, repo string, digest ociregistry.Digest, offset0, offset1 int64) (ociregistry.BlobReader, error) { |
| 64 | + cr, repo, err := r.resolve(repo) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + return cr.GetBlobRange(ctx, repo, digest, offset0, offset1) |
| 69 | +} |
| 70 | + |
| 71 | +func (r *registry) GetManifest(ctx context.Context, repo string, digest ociregistry.Digest) (ociregistry.BlobReader, error) { |
| 72 | + cr, repo, err := r.resolve(repo) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return cr.GetManifest(ctx, repo, digest) |
| 77 | +} |
| 78 | + |
| 79 | +func (r *registry) GetTag(ctx context.Context, repo string, tagName string) (ociregistry.BlobReader, error) { |
| 80 | + cr, repo, err := r.resolve(repo) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + return cr.GetTag(ctx, repo, tagName) |
| 85 | +} |
| 86 | + |
| 87 | +func (r *registry) ResolveBlob(ctx context.Context, repo string, digest ociregistry.Digest) (ociregistry.Descriptor, error) { |
| 88 | + cr, repo, err := r.resolve(repo) |
| 89 | + if err != nil { |
| 90 | + return ociregistry.Descriptor{}, err |
| 91 | + } |
| 92 | + return cr.ResolveBlob(ctx, repo, digest) |
| 93 | +} |
| 94 | + |
| 95 | +func (r *registry) ResolveManifest(ctx context.Context, repo string, digest ociregistry.Digest) (ociregistry.Descriptor, error) { |
| 96 | + cr, repo, err := r.resolve(repo) |
| 97 | + if err != nil { |
| 98 | + return ociregistry.Descriptor{}, err |
| 99 | + } |
| 100 | + return cr.ResolveManifest(ctx, repo, digest) |
| 101 | +} |
| 102 | + |
| 103 | +func (r *registry) ResolveTag(ctx context.Context, repo string, tagName string) (ociregistry.Descriptor, error) { |
| 104 | + cr, repo, err := r.resolve(repo) |
| 105 | + if err != nil { |
| 106 | + return ociregistry.Descriptor{}, err |
| 107 | + } |
| 108 | + return cr.ResolveTag(ctx, repo, tagName) |
| 109 | +} |
| 110 | + |
| 111 | +func (r *registry) PushBlob(ctx context.Context, repo string, desc ociregistry.Descriptor, rd io.Reader) (ociregistry.Descriptor, error) { |
| 112 | + cr, repo, err := r.resolve(repo) |
| 113 | + if err != nil { |
| 114 | + return ociregistry.Descriptor{}, err |
| 115 | + } |
| 116 | + return cr.PushBlob(ctx, repo, desc, rd) |
| 117 | +} |
| 118 | + |
| 119 | +func (r *registry) PushBlobChunked(ctx context.Context, repo string, id string, chunkSize int) (ociregistry.BlobWriter, error) { |
| 120 | + cr, repo, err := r.resolve(repo) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + return cr.PushBlobChunked(ctx, repo, id, chunkSize) |
| 125 | +} |
| 126 | + |
| 127 | +func (r *registry) MountBlob(ctx context.Context, fromRepo, toRepo string, digest ociregistry.Digest) (ociregistry.Descriptor, error) { |
| 128 | + return ociregistry.Descriptor{}, ociregistry.ErrUnsupported |
| 129 | +} |
| 130 | + |
| 131 | +func (r *registry) PushManifest(ctx context.Context, repo string, tag string, contents []byte, mediaType string) (ociregistry.Descriptor, error) { |
| 132 | + cr, repo, err := r.resolve(repo) |
| 133 | + if err != nil { |
| 134 | + return ociregistry.Descriptor{}, err |
| 135 | + } |
| 136 | + return cr.PushManifest(ctx, repo, tag, contents, mediaType) |
| 137 | +} |
| 138 | + |
| 139 | +func (r *registry) DeleteBlob(ctx context.Context, repo string, digest ociregistry.Digest) error { |
| 140 | + cr, repo, err := r.resolve(repo) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + return cr.DeleteBlob(ctx, repo, digest) |
| 145 | +} |
| 146 | + |
| 147 | +func (r *registry) DeleteManifest(ctx context.Context, repo string, digest ociregistry.Digest) error { |
| 148 | + cr, repo, err := r.resolve(repo) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + return cr.DeleteManifest(ctx, repo, digest) |
| 153 | +} |
| 154 | + |
| 155 | +func (r *registry) DeleteTag(ctx context.Context, repo string, name string) error { |
| 156 | + cr, repo, err := r.resolve(repo) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + return cr.DeleteTag(ctx, repo, name) |
| 161 | +} |
| 162 | + |
| 163 | +func (r *registry) Repositories(ctx context.Context) ociregistry.Iter[string] { |
| 164 | + return ociregistry.ErrorIter[string](ociregistry.ErrUnsupported) |
| 165 | +} |
| 166 | + |
| 167 | +func (r *registry) Tags(ctx context.Context, repo string) ociregistry.Iter[string] { |
| 168 | + cr, repo, err := r.resolve(repo) |
| 169 | + if err != nil { |
| 170 | + return ociregistry.ErrorIter[string](err) |
| 171 | + } |
| 172 | + return cr.Tags(ctx, repo) |
| 173 | +} |
| 174 | + |
| 175 | +func (r *registry) Referrers(ctx context.Context, repo string, digest ociregistry.Digest, artifactType string) ociregistry.Iter[ociregistry.Descriptor] { |
| 176 | + cr, repo, err := r.resolve(repo) |
| 177 | + if err != nil { |
| 178 | + return ociregistry.ErrorIter[ociregistry.Descriptor](err) |
| 179 | + } |
| 180 | + return cr.Referrers(ctx, repo, digest, artifactType) |
| 181 | +} |
| 182 | + |
| 183 | +func (r *registry) resolve(repo string) (reg ociregistry.Interface, repo1 string, err error) { |
| 184 | + loc := r.resolver.Resolve(repo) |
| 185 | + r.mu.Lock() |
| 186 | + defer r.mu.Unlock() |
| 187 | + reg = r.repos[loc.Host] |
| 188 | + if reg == nil { |
| 189 | + reg1, err := r.newRegistry(loc.Host, loc.Insecure) |
| 190 | + if err != nil { |
| 191 | + return nil, "", fmt.Errorf("cannot make client: %v", err) |
| 192 | + } |
| 193 | + r.repos[loc.Host] = reg1 |
| 194 | + reg = reg1 |
| 195 | + } |
| 196 | + return reg, join(loc.Prefix, repo), nil |
| 197 | +} |
| 198 | + |
| 199 | +// join is similar to path.Join but doesn't Clean the result, because |
| 200 | +// that's not appropriate in this scenario. |
| 201 | +func join(prefix, repo string) string { |
| 202 | + if prefix == "" { |
| 203 | + return repo |
| 204 | + } |
| 205 | + if repo == "" { |
| 206 | + return prefix |
| 207 | + } |
| 208 | + return prefix + "/" + repo |
| 209 | +} |
0 commit comments