|
| 1 | +/* |
| 2 | + * Copyright 2025 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package backend |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "net/url" |
| 26 | + "path/filepath" |
| 27 | + |
| 28 | + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" |
| 29 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 30 | + "golang.org/x/sync/errgroup" |
| 31 | + "oras.land/oras-go/v2/registry/remote" |
| 32 | + "oras.land/oras-go/v2/registry/remote/auth" |
| 33 | + "oras.land/oras-go/v2/registry/remote/credentials" |
| 34 | + "oras.land/oras-go/v2/registry/remote/retry" |
| 35 | + |
| 36 | + internalpb "github.com/CloudNativeAI/modctl/internal/pb" |
| 37 | + "github.com/CloudNativeAI/modctl/pkg/config" |
| 38 | +) |
| 39 | + |
| 40 | +// Fetch fetches partial files to the output. |
| 41 | +func (b *backend) Fetch(ctx context.Context, target string, cfg *config.Fetch) error { |
| 42 | + // parse the repository and tag from the target. |
| 43 | + ref, err := ParseReference(target) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to parse the target: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + _, tag := ref.Repository(), ref.Tag() |
| 49 | + |
| 50 | + // create the src storage from the remote repository. |
| 51 | + src, err := remote.NewRepository(target) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to create remote repository: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // gets the credentials store. |
| 57 | + credStore, err := credentials.NewStoreFromDocker(credentials.StoreOptions{AllowPlaintextPut: true}) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to create credential store: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + // create the http client. |
| 63 | + httpClient := &http.Client{} |
| 64 | + if cfg.Proxy != "" { |
| 65 | + proxyURL, err := url.Parse(cfg.Proxy) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to parse the proxy URL: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + httpClient.Transport = retry.NewTransport(&http.Transport{ |
| 71 | + Proxy: http.ProxyURL(proxyURL), |
| 72 | + TLSClientConfig: &tls.Config{ |
| 73 | + InsecureSkipVerify: cfg.Insecure, |
| 74 | + }, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + src.Client = &auth.Client{ |
| 79 | + Cache: auth.NewCache(), |
| 80 | + Credential: credentials.Credential(credStore), |
| 81 | + Client: httpClient, |
| 82 | + } |
| 83 | + |
| 84 | + if cfg.PlainHTTP { |
| 85 | + src.PlainHTTP = true |
| 86 | + } |
| 87 | + |
| 88 | + _, manifestReader, err := src.Manifests().FetchReference(ctx, tag) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to fetch the manifest: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + defer manifestReader.Close() |
| 94 | + |
| 95 | + var manifest ocispec.Manifest |
| 96 | + if err := json.NewDecoder(manifestReader).Decode(&manifest); err != nil { |
| 97 | + return fmt.Errorf("failed to decode the manifest: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + layers := []ocispec.Descriptor{} |
| 101 | + // filter the layers by patterns. |
| 102 | + for _, layer := range manifest.Layers { |
| 103 | + for _, pattern := range cfg.Patterns { |
| 104 | + if anno := layer.Annotations; anno != nil { |
| 105 | + matched, err := filepath.Match(pattern, anno[modelspec.AnnotationFilepath]) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("failed to match pattern: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + if matched { |
| 111 | + layers = append(layers, layer) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if len(layers) == 0 { |
| 118 | + return fmt.Errorf("no layers matched the patterns") |
| 119 | + } |
| 120 | + |
| 121 | + pb := internalpb.NewProgressBar() |
| 122 | + pb.Start() |
| 123 | + defer pb.Stop() |
| 124 | + |
| 125 | + g := &errgroup.Group{} |
| 126 | + g.SetLimit(cfg.Concurrency) |
| 127 | + |
| 128 | + for _, layer := range layers { |
| 129 | + g.Go(func() error { |
| 130 | + return pullAndExtractFromRemote(ctx, pb, internalpb.NormalizePrompt("Fetching blob"), src, cfg.Output, layer) |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + return g.Wait() |
| 135 | +} |
0 commit comments