|
| 1 | +/* |
| 2 | + Copyright The containerd 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 manifest |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/base64" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/opencontainers/go-digest" |
| 27 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 | + |
| 29 | + "github.com/containerd/containerd/v2/core/images" |
| 30 | + "github.com/containerd/containerd/v2/core/remotes" |
| 31 | + "github.com/containerd/errdefs" |
| 32 | + |
| 33 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 34 | + "github.com/containerd/nerdctl/v2/pkg/manifeststore" |
| 35 | + "github.com/containerd/nerdctl/v2/pkg/manifesttypes" |
| 36 | + "github.com/containerd/nerdctl/v2/pkg/manifestutil" |
| 37 | + "github.com/containerd/nerdctl/v2/pkg/referenceutil" |
| 38 | +) |
| 39 | + |
| 40 | +func Push(ctx context.Context, listRef string, options types.ManifestPushOptions) error { |
| 41 | + parsedTargetRef, err := referenceutil.Parse(listRef) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to parse target reference %s: %w", listRef, err) |
| 44 | + } |
| 45 | + |
| 46 | + manifestStore, err := manifeststore.NewStore(options.GOptions.DataRoot) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to create manifest store: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + manifests, err := manifestStore.GetList(parsedTargetRef) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to get manifests: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + if len(manifests) == 0 { |
| 57 | + return fmt.Errorf("no manifests found for %s", listRef) |
| 58 | + } |
| 59 | + |
| 60 | + resolver, err := manifestutil.CreateResolver(ctx, parsedTargetRef.Domain, options.GOptions, options.Insecure) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to create resolver: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := pushIndividualManifests(ctx, resolver, manifests, parsedTargetRef, options); err != nil { |
| 66 | + return fmt.Errorf("failed to push individual manifests: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + manifestList, err := buildManifestList(manifests) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to build manifest list: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + digest, err := pushManifestList(ctx, resolver, parsedTargetRef, manifestList) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to push manifest list: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Fprintln(options.Stdout, digest) |
| 80 | + |
| 81 | + if options.Purge { |
| 82 | + if err := manifestStore.Remove(parsedTargetRef); err != nil { |
| 83 | + return fmt.Errorf("failed to remove manifest list from store: %w", err) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func buildManifestList(manifests []*manifesttypes.DockerManifestEntry) (manifesttypes.DockerManifestList, error) { |
| 91 | + if len(manifests) == 0 { |
| 92 | + return manifesttypes.DockerManifestList{}, fmt.Errorf("no manifests to build list from") |
| 93 | + } |
| 94 | + |
| 95 | + var descriptors []manifesttypes.DockerManifestDescriptor |
| 96 | + useOCIIndex := false |
| 97 | + |
| 98 | + for _, manifest := range manifests { |
| 99 | + if manifest.Descriptor.Platform == nil || |
| 100 | + manifest.Descriptor.Platform.Architecture == "" || |
| 101 | + manifest.Descriptor.Platform.OS == "" { |
| 102 | + return manifesttypes.DockerManifestList{}, fmt.Errorf("manifest %s must have an OS and Architecture to be pushed to a registry", manifest.Ref) |
| 103 | + } |
| 104 | + |
| 105 | + if manifest.Descriptor.MediaType == ocispec.MediaTypeImageManifest { |
| 106 | + useOCIIndex = true |
| 107 | + } |
| 108 | + |
| 109 | + descriptors = append(descriptors, manifesttypes.DockerManifestDescriptor{ |
| 110 | + MediaType: manifest.Descriptor.MediaType, |
| 111 | + Size: manifest.Descriptor.Size, |
| 112 | + Digest: manifest.Descriptor.Digest, |
| 113 | + Platform: *manifest.Descriptor.Platform, |
| 114 | + }) |
| 115 | + } |
| 116 | + manifestList := manifesttypes.DockerManifestList{ |
| 117 | + SchemaVersion: 2, |
| 118 | + MediaType: images.MediaTypeDockerSchema2ManifestList, |
| 119 | + Manifests: descriptors, |
| 120 | + } |
| 121 | + if useOCIIndex { |
| 122 | + manifestList.MediaType = ocispec.MediaTypeImageIndex |
| 123 | + } |
| 124 | + |
| 125 | + return manifestList, nil |
| 126 | +} |
| 127 | + |
| 128 | +func pushIndividualManifests(ctx context.Context, resolver remotes.Resolver, manifests []*manifesttypes.DockerManifestEntry, targetRef *referenceutil.ImageReference, options types.ManifestPushOptions) error { |
| 129 | + targetDomain := targetRef.Domain |
| 130 | + targetRepo := targetRef.Path |
| 131 | + |
| 132 | + for _, manifest := range manifests { |
| 133 | + manifestRef, err := referenceutil.Parse(manifest.Ref) |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("failed to parse manifest reference %s: %w", manifest.Ref, err) |
| 136 | + } |
| 137 | + |
| 138 | + var targetManifestRef string |
| 139 | + if manifestRef.Domain != targetDomain { |
| 140 | + targetManifestRef = fmt.Sprintf("%s/%s@%s", targetDomain, manifestRef.Path, manifest.Descriptor.Digest) |
| 141 | + } else { |
| 142 | + targetManifestRef = fmt.Sprintf("%s/%s@%s", targetDomain, targetRepo, manifest.Descriptor.Digest) |
| 143 | + } |
| 144 | + |
| 145 | + if err := pushManifest(ctx, resolver, targetManifestRef, manifest); err != nil { |
| 146 | + return fmt.Errorf("failed to push manifest %s: %w", targetManifestRef, err) |
| 147 | + } |
| 148 | + |
| 149 | + fmt.Fprintf(options.Stdout, "Pushed ref %s with digest: %s\n", targetManifestRef, manifest.Descriptor.Digest) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func pushManifest(ctx context.Context, resolver remotes.Resolver, ref string, manifest *manifesttypes.DockerManifestEntry) error { |
| 156 | + rawData, err := base64.StdEncoding.DecodeString(manifest.Raw) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("failed to decode manifest data: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + pusher, err := resolver.Pusher(ctx, ref) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("failed to create pusher: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + writer, err := pusher.Push(ctx, manifest.Descriptor) |
| 167 | + if err != nil { |
| 168 | + if errdefs.IsAlreadyExists(err) || strings.Contains(err.Error(), "already exists") { |
| 169 | + return nil |
| 170 | + } |
| 171 | + return fmt.Errorf("failed to create content writer: %w", err) |
| 172 | + } |
| 173 | + defer writer.Close() |
| 174 | + |
| 175 | + if _, err := writer.Write(rawData); err != nil { |
| 176 | + return fmt.Errorf("failed to write manifest data: %w", err) |
| 177 | + } |
| 178 | + |
| 179 | + if err := writer.Commit(ctx, manifest.Descriptor.Size, manifest.Descriptor.Digest); err != nil { |
| 180 | + if errdefs.IsAlreadyExists(err) || strings.Contains(err.Error(), "already exists") { |
| 181 | + return nil |
| 182 | + } |
| 183 | + return fmt.Errorf("failed to commit manifest: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func pushManifestList(ctx context.Context, resolver remotes.Resolver, targetRef *referenceutil.ImageReference, manifestList manifesttypes.DockerManifestList) (digest.Digest, error) { |
| 190 | + data, err := json.MarshalIndent(manifestList, "", " ") |
| 191 | + if err != nil { |
| 192 | + return "", fmt.Errorf("failed to marshal manifest list: %w", err) |
| 193 | + } |
| 194 | + |
| 195 | + dgst := digest.FromBytes(data) |
| 196 | + |
| 197 | + desc := ocispec.Descriptor{ |
| 198 | + MediaType: manifestList.MediaType, |
| 199 | + Size: int64(len(data)), |
| 200 | + Digest: dgst, |
| 201 | + } |
| 202 | + |
| 203 | + pusher, err := resolver.Pusher(ctx, targetRef.String()) |
| 204 | + if err != nil { |
| 205 | + return "", fmt.Errorf("failed to create pusher: %w", err) |
| 206 | + } |
| 207 | + |
| 208 | + writer, err := pusher.Push(ctx, desc) |
| 209 | + if err != nil { |
| 210 | + if errdefs.IsAlreadyExists(err) || strings.Contains(err.Error(), "already exists") { |
| 211 | + return dgst, nil |
| 212 | + } |
| 213 | + return "", fmt.Errorf("failed to create content writer: %w", err) |
| 214 | + } |
| 215 | + defer writer.Close() |
| 216 | + |
| 217 | + if _, err := writer.Write(data); err != nil { |
| 218 | + return "", fmt.Errorf("failed to write manifest list data: %w", err) |
| 219 | + } |
| 220 | + |
| 221 | + if err := writer.Commit(ctx, desc.Size, desc.Digest); err != nil { |
| 222 | + if errdefs.IsAlreadyExists(err) || strings.Contains(err.Error(), "already exists") { |
| 223 | + return dgst, nil |
| 224 | + } |
| 225 | + return "", fmt.Errorf("failed to commit manifest list: %w", err) |
| 226 | + } |
| 227 | + |
| 228 | + return dgst, nil |
| 229 | +} |
0 commit comments