Skip to content

Commit 9a7c3e0

Browse files
committed
manifest: support nerdctl manifest create command
Support `nerdctl manifest create` command Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent b78a05a commit 9a7c3e0

File tree

7 files changed

+580
-233
lines changed

7 files changed

+580
-233
lines changed

cmd/nerdctl/manifest/manifest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Command() *cobra.Command {
3434

3535
cmd.AddCommand(
3636
InspectCommand(),
37+
CreateCommand(),
3738
)
3839

3940
return cmd
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
26+
"github.com/containerd/nerdctl/v2/pkg/api/types"
27+
"github.com/containerd/nerdctl/v2/pkg/cmd/manifest"
28+
)
29+
30+
func CreateCommand() *cobra.Command {
31+
var cmd = &cobra.Command{
32+
Use: "create INDEX/MANIFESTLIST MANIFEST [MANIFEST...]",
33+
Short: "Create a local index/manifest list for annotating and pushing to a registry",
34+
Args: cobra.MinimumNArgs(2),
35+
RunE: createAction,
36+
ValidArgsFunction: createShellComplete,
37+
SilenceUsage: true,
38+
SilenceErrors: true,
39+
}
40+
cmd.Flags().Bool("amend", false, "Amend the existing index/manifest list")
41+
cmd.Flags().Bool("insecure", false, "Allow communication with an insecure registry")
42+
return cmd
43+
}
44+
45+
func processCreateFlags(cmd *cobra.Command) (types.ManifestCreateOptions, error) {
46+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
47+
if err != nil {
48+
return types.ManifestCreateOptions{}, err
49+
}
50+
amend, err := cmd.Flags().GetBool("amend")
51+
if err != nil {
52+
return types.ManifestCreateOptions{}, err
53+
}
54+
insecure, err := cmd.Flags().GetBool("insecure")
55+
if err != nil {
56+
return types.ManifestCreateOptions{}, err
57+
}
58+
return types.ManifestCreateOptions{
59+
Stdout: cmd.OutOrStdout(),
60+
GOptions: globalOptions,
61+
Amend: amend,
62+
Insecure: insecure,
63+
}, nil
64+
}
65+
66+
func createAction(cmd *cobra.Command, args []string) error {
67+
createOptions, err := processCreateFlags(cmd)
68+
if err != nil {
69+
return err
70+
}
71+
72+
listRef := args[0]
73+
manifestRefs := args[1:]
74+
75+
listRef, err = manifest.Create(cmd.Context(), listRef, manifestRefs, createOptions)
76+
if err != nil {
77+
return err
78+
}
79+
80+
fmt.Fprintln(createOptions.Stdout, "Created manifest list", listRef)
81+
82+
return nil
83+
}
84+
85+
func createShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
86+
return completion.ImageNames(cmd)
87+
}

pkg/api/types/manifest_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ package types
1818

1919
import "io"
2020

21+
// ManifestCreateOptions specifies options for `nerdctl manifest create`.
22+
type ManifestCreateOptions struct {
23+
Stdout io.Writer
24+
GOptions GlobalCommandOptions
25+
// Amend an existing manifest list
26+
Amend bool
27+
// Allow communication with an insecure registry
28+
Insecure bool
29+
}
30+
2131
// ManifestInspectOptions specifies options for `nerdctl manifest inspect`.
2232
type ManifestInspectOptions struct {
2333
Stdout io.Writer

pkg/cmd/manifest/create.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)