|
| 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 | + "fmt" |
| 22 | + |
| 23 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 24 | + |
| 25 | + "github.com/containerd/containerd/v2/core/images" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/manifeststore" |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/manifestutil" |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/referenceutil" |
| 31 | +) |
| 32 | + |
| 33 | +// Create creates a local manifest list/index |
| 34 | +func Create(ctx context.Context, listRef string, manifestRefs []string, options types.ManifestCreateOptions) (string, error) { |
| 35 | + parsedListRef, err := referenceutil.Parse(listRef) |
| 36 | + if err != nil { |
| 37 | + return "", fmt.Errorf("failed to parse list reference: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + manifestStore, err := manifeststore.NewStore(options.GOptions.DataRoot) |
| 41 | + if err != nil { |
| 42 | + return "", fmt.Errorf("failed to create manifest store: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + existingManifests, err := manifestStore.GetList(parsedListRef) |
| 46 | + if err == nil && len(existingManifests) > 0 && !options.Amend { |
| 47 | + return "", fmt.Errorf("refusing to amend an existing manifest list with no --amend flag") |
| 48 | + } |
| 49 | + |
| 50 | + for _, manifestRef := range manifestRefs { |
| 51 | + parsedRef, err := referenceutil.Parse(manifestRef) |
| 52 | + if err != nil { |
| 53 | + return "", fmt.Errorf("failed to parse manifest reference %s: %w", manifestRef, err) |
| 54 | + } |
| 55 | + |
| 56 | + manifest, desc, rawData, err := manifestutil.GetManifest(ctx, parsedRef, options.GOptions, options.Insecure) |
| 57 | + if err != nil { |
| 58 | + return "", fmt.Errorf("failed to fetch manifest %s: %w", manifestRef, err) |
| 59 | + } |
| 60 | + |
| 61 | + // Check if the manifest is manifest list |
| 62 | + if desc.MediaType == images.MediaTypeDockerSchema2ManifestList || desc.MediaType == ocispec.MediaTypeImageIndex { |
| 63 | + return "", fmt.Errorf("%s is a manifest list", manifestRef) |
| 64 | + } |
| 65 | + |
| 66 | + imageManifest, err := manifestutil.CreateManifestEntry(parsedRef, desc, rawData) |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("failed to create manifest entry for %s: %w", manifestRef, err) |
| 69 | + } |
| 70 | + |
| 71 | + // Get platform information from config |
| 72 | + if desc.MediaType == ocispec.MediaTypeImageManifest || desc.MediaType == images.MediaTypeDockerSchema2Manifest { |
| 73 | + platform, err := manifestutil.GetPlatform(ctx, parsedRef.Domain, options.GOptions, options.Insecure, manifestRef, manifest) |
| 74 | + if err != nil { |
| 75 | + return "", fmt.Errorf("failed to extract platform for %s: %w", manifestRef, err) |
| 76 | + } |
| 77 | + imageManifest.Descriptor.Platform = platform |
| 78 | + } |
| 79 | + |
| 80 | + if err := manifestStore.Save(parsedListRef, parsedRef, &imageManifest); err != nil { |
| 81 | + return "", fmt.Errorf("failed to store manifest %s: %w", manifestRef, err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return listRef, nil |
| 86 | +} |
0 commit comments