Skip to content

Commit c6e2f70

Browse files
authored
Merge pull request #4442 from ChengyuZhu6/manifest-annotate
manifest: support nerdctl manifest annotate command
2 parents bd89fee + 7ac8397 commit c6e2f70

File tree

7 files changed

+357
-0
lines changed

7 files changed

+357
-0
lines changed

cmd/nerdctl/manifest/manifest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func Command() *cobra.Command {
3535
cmd.AddCommand(
3636
InspectCommand(),
3737
CreateCommand(),
38+
AnnotateCommand(),
3839
)
3940

4041
return cmd
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
23+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
24+
"github.com/containerd/nerdctl/v2/pkg/api/types"
25+
"github.com/containerd/nerdctl/v2/pkg/cmd/manifest"
26+
)
27+
28+
func AnnotateCommand() *cobra.Command {
29+
var cmd = &cobra.Command{
30+
Use: "annotate INDEX/MANIFESTLIST MANIFEST",
31+
Short: "Add additional information to a local image manifest",
32+
Args: cobra.ExactArgs(2),
33+
RunE: annotateAction,
34+
ValidArgsFunction: annotateShellComplete,
35+
SilenceUsage: true,
36+
SilenceErrors: true,
37+
}
38+
cmd.Flags().String("os", "", "Set operating system")
39+
cmd.Flags().String("arch", "", "Set architecture")
40+
cmd.Flags().String("os-version", "", "Set operating system version")
41+
cmd.Flags().String("variant", "", "Set operating system feature")
42+
cmd.Flags().StringArray("os-features", []string{}, "Set architecture variant")
43+
return cmd
44+
}
45+
46+
func processAnnotateFlags(cmd *cobra.Command) (types.ManifestAnnotateOptions, error) {
47+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
48+
if err != nil {
49+
return types.ManifestAnnotateOptions{}, err
50+
}
51+
52+
os, err := cmd.Flags().GetString("os")
53+
if err != nil {
54+
return types.ManifestAnnotateOptions{}, err
55+
}
56+
arch, err := cmd.Flags().GetString("arch")
57+
if err != nil {
58+
return types.ManifestAnnotateOptions{}, err
59+
}
60+
osVersion, err := cmd.Flags().GetString("os-version")
61+
if err != nil {
62+
return types.ManifestAnnotateOptions{}, err
63+
}
64+
variant, err := cmd.Flags().GetString("variant")
65+
if err != nil {
66+
return types.ManifestAnnotateOptions{}, err
67+
}
68+
osFeatures, err := cmd.Flags().GetStringArray("os-features")
69+
if err != nil {
70+
return types.ManifestAnnotateOptions{}, err
71+
}
72+
73+
return types.ManifestAnnotateOptions{
74+
Stdout: cmd.OutOrStdout(),
75+
GOptions: globalOptions,
76+
Os: os,
77+
Arch: arch,
78+
OsVersion: osVersion,
79+
Variant: variant,
80+
OsFeatures: osFeatures,
81+
}, nil
82+
}
83+
84+
func annotateAction(cmd *cobra.Command, args []string) error {
85+
annotateOptions, err := processAnnotateFlags(cmd)
86+
if err != nil {
87+
return err
88+
}
89+
90+
listRef := args[0]
91+
manifestRef := args[1]
92+
93+
return manifest.Annotate(cmd.Context(), listRef, manifestRef, annotateOptions)
94+
}
95+
96+
func annotateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
97+
return completion.ImageNames(cmd)
98+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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/test"
24+
25+
"github.com/containerd/nerdctl/v2/pkg/testutil"
26+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
27+
)
28+
29+
func TestManifestAnnotateErrors(t *testing.T) {
30+
testCase := nerdtest.Setup()
31+
manifestListName := "test-list:v1"
32+
manifestName := "example.com/alpine:latest"
33+
invalidName := "invalid/name/with/special@chars"
34+
testCase.SubTests = []*test.Case{
35+
{
36+
Description: "too-few-arguments",
37+
Command: test.Command("manifest", "annotate", manifestListName),
38+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
39+
return &test.Expected{
40+
ExitCode: 1,
41+
}
42+
},
43+
},
44+
{
45+
Description: "invalid-list-name",
46+
Command: test.Command("manifest", "annotate", invalidName, manifestName),
47+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
48+
return &test.Expected{
49+
ExitCode: 1,
50+
Errors: []error{errors.New(data.Labels().Get("error"))},
51+
}
52+
},
53+
Data: test.WithLabels(map[string]string{
54+
"error": "invalid reference format",
55+
}),
56+
},
57+
{
58+
Description: "invalid-manifest-reference",
59+
Command: test.Command("manifest", "annotate", manifestListName, invalidName),
60+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
61+
return &test.Expected{
62+
ExitCode: 1,
63+
Errors: []error{errors.New(data.Labels().Get("error"))},
64+
}
65+
},
66+
Data: test.WithLabels(map[string]string{
67+
"error": "invalid reference format",
68+
}),
69+
},
70+
}
71+
72+
testCase.Run(t)
73+
}
74+
75+
func TestManifestAnnotate(t *testing.T) {
76+
testCase := nerdtest.Setup()
77+
manifestListName := "example.com/test-list-annotate:v1"
78+
manifestRef := testutil.GetTestImageWithoutTag("alpine") + "@" + testutil.GetTestImageManifestDigest("alpine", "linux/amd64")
79+
80+
testCase.SubTests = []*test.Case{
81+
{
82+
Description: "annotate-non-existent-manifest",
83+
Setup: func(data test.Data, helpers test.Helpers) {
84+
cmd := helpers.Command("manifest", "create", manifestListName, manifestRef)
85+
cmd.Run(&test.Expected{ExitCode: 0})
86+
},
87+
Command: test.Command("manifest", "annotate", manifestListName, "example.com/fake:0.0"),
88+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
89+
return &test.Expected{
90+
ExitCode: 1,
91+
Errors: []error{errors.New(data.Labels().Get("error"))},
92+
}
93+
},
94+
Data: test.WithLabels(map[string]string{
95+
"error": "manifest for image example.com/fake:0.0 does not exist",
96+
}),
97+
},
98+
{
99+
Description: "annotate-success",
100+
Setup: func(data test.Data, helpers test.Helpers) {
101+
cmd := helpers.Command("manifest", "create", manifestListName+"-success", manifestRef)
102+
cmd.Run(&test.Expected{ExitCode: 0})
103+
},
104+
Command: test.Command("manifest", "annotate",
105+
manifestListName+"-success",
106+
manifestRef,
107+
"--os", "freebsd",
108+
"--arch", "arm",
109+
"--os-version", "1",
110+
"--os-features", "feature1",
111+
"--variant", "v7"),
112+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
113+
return &test.Expected{
114+
ExitCode: 0,
115+
}
116+
},
117+
},
118+
}
119+
120+
testCase.Run(t)
121+
}

docs/command-reference.md

Lines changed: 22 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 annotate](#whale-nerdctl-manifest-annotate)
5556
- [:whale: nerdctl manifest create](#whale-nerdctl-manifest-create)
5657
- [:whale: nerdctl manifest inspect](#whale-nerdctl-manifest-inspect)
5758
- [Registry](#registry)
@@ -1040,6 +1041,27 @@ Flags:
10401041

10411042
## Manifest management
10421043

1044+
### :whale: nerdctl manifest annotate
1045+
1046+
Add additional information to a local image manifest.
1047+
1048+
Usage: `nerdctl manifest annotate [OPTIONS] INDEX/MANIFESTLIST MANIFEST`
1049+
1050+
Flags:
1051+
1052+
- :whale: `--os`: Set operating system (e.g., "linux", "windows", "freebsd")
1053+
- :whale: `--arch`: Set architecture (e.g., "amd64", "arm64", "arm")
1054+
- :whale: `--os-version`: Set operating system version (e.g., "10.0.19041")
1055+
- :whale: `--variant`: Set architecture variant (e.g., "v7", "v8")
1056+
- :whale: `--os-features`: Set operating system features (e.g., "win32k")
1057+
1058+
Examples:
1059+
1060+
```bash
1061+
nerdctl manifest annotate myapp:latest alpine@sha256:eafc1edb577d2e9b458664a15f23ea1c370214193226069eb22921169fc7e43f \
1062+
--os linux --arch arm --variant v7 --os-features feature1,feature2
1063+
```
1064+
10431065
### :whale: nerdctl manifest create
10441066

10451067
Create a local index/manifest list.

pkg/api/types/manifest_types.go

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

1919
import "io"
2020

21+
// ManifestAnnotateOptions specifies options for `nerdctl manifest annotate`.
22+
type ManifestAnnotateOptions struct {
23+
Stdout io.Writer
24+
GOptions GlobalCommandOptions
25+
Os string
26+
Arch string
27+
OsVersion string
28+
Variant string
29+
OsFeatures []string
30+
}
31+
2132
// ManifestCreateOptions specifies options for `nerdctl manifest create`.
2233
type ManifestCreateOptions struct {
2334
Stdout io.Writer

pkg/cmd/manifest/annotate.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
"errors"
22+
"fmt"
23+
24+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/api/types"
27+
"github.com/containerd/nerdctl/v2/pkg/manifeststore"
28+
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
29+
"github.com/containerd/nerdctl/v2/pkg/store"
30+
)
31+
32+
func Annotate(ctx context.Context, listRef string, manifestRef string, options types.ManifestAnnotateOptions) error {
33+
parsedListRef, err := referenceutil.Parse(listRef)
34+
if err != nil {
35+
return fmt.Errorf("failed to parse list reference: %w", err)
36+
}
37+
38+
parsedManifestRef, err := referenceutil.Parse(manifestRef)
39+
if err != nil {
40+
return fmt.Errorf("failed to parse manifest reference: %w", err)
41+
}
42+
43+
manifestStore, err := manifeststore.NewStore(options.GOptions.DataRoot)
44+
if err != nil {
45+
return fmt.Errorf("failed to create manifest store: %w", err)
46+
}
47+
48+
imageManifest, err := manifestStore.Get(parsedListRef, parsedManifestRef)
49+
if err != nil {
50+
if errors.Is(err, store.ErrNotFound) {
51+
return fmt.Errorf("manifest for image %s does not exist in %s", manifestRef, listRef)
52+
}
53+
return fmt.Errorf("failed to get manifest: %w", err)
54+
}
55+
56+
if imageManifest.Descriptor.Platform == nil {
57+
imageManifest.Descriptor.Platform = new(ocispec.Platform)
58+
}
59+
60+
if options.Os != "" {
61+
imageManifest.Descriptor.Platform.OS = options.Os
62+
}
63+
64+
if options.Arch != "" {
65+
imageManifest.Descriptor.Platform.Architecture = options.Arch
66+
}
67+
68+
if options.Variant != "" {
69+
imageManifest.Descriptor.Platform.Variant = options.Variant
70+
}
71+
72+
if options.OsVersion != "" {
73+
imageManifest.Descriptor.Platform.OSVersion = options.OsVersion
74+
}
75+
76+
for _, osFeature := range options.OsFeatures {
77+
imageManifest.Descriptor.Platform.OSFeatures = appendIfUnique(imageManifest.Descriptor.Platform.OSFeatures, osFeature)
78+
}
79+
80+
return manifestStore.Save(parsedListRef, parsedManifestRef, imageManifest)
81+
}
82+
83+
func appendIfUnique(list []string, str string) []string {
84+
for _, s := range list {
85+
if s == str {
86+
return list
87+
}
88+
}
89+
return append(list, str)
90+
}

0 commit comments

Comments
 (0)