Skip to content

Commit 8a29d0a

Browse files
committed
manifest: support nerdctl manifest annotate command
Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent bd89fee commit 8a29d0a

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-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+
}

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+
}

pkg/manifeststore/manifeststore.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
type Store interface {
31+
Get(listRef *referenceutil.ImageReference, manifestRef *referenceutil.ImageReference) (*manifesttypes.DockerManifestEntry, error)
3132
// GetList returns all the local manifests for a index or manifest list
3233
GetList(listRef *referenceutil.ImageReference) ([]*manifesttypes.DockerManifestEntry, error)
3334
// Save saves a manifest as part of a index or local manifest list
@@ -47,6 +48,19 @@ func NewStore(dataRoot string) (Store, error) {
4748
return &manifestStore{store: st}, nil
4849
}
4950

51+
func (s *manifestStore) Get(listRef *referenceutil.ImageReference, manifestRef *referenceutil.ImageReference) (*manifesttypes.DockerManifestEntry, error) {
52+
var manifest *manifesttypes.DockerManifestEntry
53+
err := s.store.WithLock(func() error {
54+
listPath := makeFilesafeName(listRef.String())
55+
manifestPath := makeFilesafeName(manifestRef.String())
56+
57+
var err error
58+
manifest, err = s.getManifestFromPath(listPath, manifestPath)
59+
return err
60+
})
61+
return manifest, err
62+
}
63+
5064
func (s *manifestStore) GetList(listRef *referenceutil.ImageReference) ([]*manifesttypes.DockerManifestEntry, error) {
5165
listPath := makeFilesafeName(listRef.String())
5266

0 commit comments

Comments
 (0)