Skip to content

Commit bd89fee

Browse files
authored
Merge pull request #4439 from ChengyuZhu6/manifest-create
manifest: support nerdctl manifest create command
2 parents 3b616db + 6e23148 commit bd89fee

File tree

9 files changed

+733
-233
lines changed

9 files changed

+733
-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+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
"errors"
21+
"testing"
22+
23+
"github.com/containerd/nerdctl/mod/tigron/expect"
24+
"github.com/containerd/nerdctl/mod/tigron/test"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/testutil"
27+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
28+
)
29+
30+
func TestManifestCreateErrors(t *testing.T) {
31+
testCase := nerdtest.Setup()
32+
manifestListName := "test-list:v1"
33+
manifestName := "example.com/alpine:latest"
34+
invalidName := "invalid/name/with/special@chars"
35+
testCase.SubTests = []*test.Case{
36+
{
37+
Description: "too-few-arguments",
38+
Command: test.Command("manifest", "create", manifestListName),
39+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
40+
return &test.Expected{
41+
ExitCode: 1,
42+
Errors: []error{errors.New(data.Labels().Get("error"))},
43+
}
44+
},
45+
Data: test.WithLabels(map[string]string{
46+
"error": "requires at least 2 arg",
47+
}),
48+
},
49+
{
50+
Description: "invalid-list-name",
51+
Command: test.Command("manifest", "create", invalidName, manifestName),
52+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
53+
return &test.Expected{
54+
ExitCode: 1,
55+
Errors: []error{errors.New(data.Labels().Get("error"))},
56+
}
57+
},
58+
Data: test.WithLabels(map[string]string{
59+
"error": "invalid reference format",
60+
}),
61+
},
62+
{
63+
Description: "invalid-manifest-reference",
64+
Command: test.Command("manifest", "create", manifestListName, invalidName),
65+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
66+
return &test.Expected{
67+
ExitCode: 1,
68+
Errors: []error{errors.New(data.Labels().Get("error"))},
69+
}
70+
},
71+
Data: test.WithLabels(map[string]string{
72+
"error": "invalid reference format",
73+
}),
74+
},
75+
}
76+
77+
testCase.Run(t)
78+
}
79+
80+
func TestManifestCreate(t *testing.T) {
81+
testCase := nerdtest.Setup()
82+
manifestListName := "test-list-create:v1"
83+
manifestRef := testutil.GetTestImageWithoutTag("alpine") + "@" + testutil.GetTestImageManifestDigest("alpine", "linux/amd64")
84+
testCase.SubTests = []*test.Case{
85+
{
86+
Description: "create-manifest-list",
87+
Command: test.Command("manifest", "create", manifestListName, manifestRef),
88+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
89+
return &test.Expected{
90+
ExitCode: 0,
91+
Output: expect.Contains(data.Labels().Get("output")),
92+
}
93+
},
94+
Data: test.WithLabels(map[string]string{
95+
"output": "Created manifest list ",
96+
}),
97+
},
98+
{
99+
Description: "create-existed-manifest-list-without-amend-flag",
100+
Setup: func(data test.Data, helpers test.Helpers) {
101+
cmd := helpers.Command("manifest", "create", manifestListName+"-without-amend-flag", manifestRef)
102+
cmd.Run(&test.Expected{ExitCode: 0})
103+
},
104+
Command: test.Command("manifest", "create", manifestListName+"-without-amend-flag", manifestRef),
105+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
106+
return &test.Expected{
107+
ExitCode: 1,
108+
Errors: []error{errors.New(data.Labels().Get("error"))},
109+
}
110+
},
111+
Data: test.WithLabels(map[string]string{
112+
"error": "refusing to amend an existing manifest list with no --amend flag",
113+
}),
114+
},
115+
{
116+
Description: "create-manifest-list-with-amend-flag",
117+
Setup: func(data test.Data, helpers test.Helpers) {
118+
cmd := helpers.Command("manifest", "create", manifestListName+"-with-amend-flag", manifestRef)
119+
cmd.Run(&test.Expected{ExitCode: 0})
120+
},
121+
Command: test.Command("manifest", "create", "--amend", manifestListName+"-with-amend-flag", manifestRef),
122+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
123+
return &test.Expected{
124+
ExitCode: 0,
125+
Output: expect.Contains(data.Labels().Get("output")),
126+
}
127+
},
128+
Data: test.WithLabels(map[string]string{
129+
"output": "Created manifest list",
130+
}),
131+
},
132+
}
133+
134+
testCase.Run(t)
135+
}

docs/command-reference.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ It does not necessarily mean that the corresponding features are missing in cont
5252
- [:nerd_face: nerdctl image encrypt](#nerd_face-nerdctl-image-encrypt)
5353
- [:nerd_face: nerdctl image decrypt](#nerd_face-nerdctl-image-decrypt)
5454
- [Manifest management](#manifest-management)
55+
- [:whale: nerdctl manifest create](#whale-nerdctl-manifest-create)
5556
- [:whale: nerdctl manifest inspect](#whale-nerdctl-manifest-inspect)
5657
- [Registry](#registry)
5758
- [:whale: nerdctl login](#whale-nerdctl-login)
@@ -1039,6 +1040,23 @@ Flags:
10391040

10401041
## Manifest management
10411042

1043+
### :whale: nerdctl manifest create
1044+
1045+
Create a local index/manifest list.
1046+
1047+
Usage: `nerdctl manifest create [OPTIONS] INDEX/MANIFESTLIST MANIFEST [MANIFEST...]`
1048+
1049+
Flags:
1050+
1051+
- `--amend`: Amend the existing index/manifest list
1052+
- `--insecure`: Allow communication with an insecure registry
1053+
1054+
Example:
1055+
1056+
```bash
1057+
nerdctl manifest create myapp:latest alpine@sha256:eafc1edb577d2e9b458664a15f23ea1c370214193226069eb22921169fc7e43f
1058+
```
1059+
10421060
### :whale: nerdctl manifest inspect
10431061

10441062
Display the contents of a manifest list or manifest.

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)