Skip to content

Commit 4fe116f

Browse files
committed
ctr-enc: Synchronize with more recent version of ctr from containerd v2.3.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
1 parent b6aeb69 commit 4fe116f

25 files changed

Lines changed: 1708 additions & 438 deletions

cmd/ctr/app/main.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"fmt"
2121
"io"
2222

23+
"github.com/containerd/log"
24+
"github.com/urfave/cli/v2"
25+
"google.golang.org/grpc/grpclog"
26+
2327
"github.com/containerd/containerd/v2/cmd/ctr/commands/content"
2428
"github.com/containerd/containerd/v2/cmd/ctr/commands/events"
2529
"github.com/containerd/containerd/v2/cmd/ctr/commands/install"
@@ -37,9 +41,6 @@ import (
3741
"github.com/containerd/imgcrypt/cmd/ctr/commands/containers"
3842
"github.com/containerd/imgcrypt/cmd/ctr/commands/images"
3943
"github.com/containerd/imgcrypt/cmd/ctr/commands/run"
40-
"github.com/sirupsen/logrus"
41-
"github.com/urfave/cli/v2"
42-
"google.golang.org/grpc/grpclog"
4344
)
4445

4546
var extraCmds = []*cli.Command{}
@@ -48,8 +49,25 @@ func init() {
4849
// Discard grpc logs so that they don't mess with our stdio
4950
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))
5051

51-
cli.VersionPrinter = func(c *cli.Context) {
52-
fmt.Println(c.App.Name, version.Package, c.App.Version)
52+
cli.VersionPrinter = func(cliContext *cli.Context) {
53+
fmt.Println(cliContext.App.Name, version.Package, cliContext.App.Version)
54+
}
55+
56+
// Override the default flag descriptions for '--version' and '--help'
57+
// to align with other flags and start with uppercase.
58+
cli.VersionFlag = &cli.BoolFlag{
59+
Name: "version",
60+
Aliases: []string{"v"},
61+
Usage: "Print the version",
62+
63+
DisableDefaultText: true,
64+
}
65+
cli.HelpFlag = &cli.BoolFlag{
66+
Name: "help",
67+
Aliases: []string{"h"},
68+
Usage: "Show help",
69+
70+
DisableDefaultText: true,
5371
}
5472
}
5573

@@ -77,27 +95,27 @@ containerd CLI
7795
app.Flags = []cli.Flag{
7896
&cli.BoolFlag{
7997
Name: "debug",
80-
Usage: "enable debug output in logs",
98+
Usage: "Enable debug output in logs",
8199
},
82100
&cli.StringFlag{
83101
Name: "address",
84102
Aliases: []string{"a"},
85-
Usage: "address for containerd's GRPC server",
103+
Usage: "Address for containerd's GRPC server",
86104
Value: defaults.DefaultAddress,
87105
EnvVars: []string{"CONTAINERD_ADDRESS"},
88106
},
89107
&cli.DurationFlag{
90108
Name: "timeout",
91-
Usage: "total timeout for ctr commands",
109+
Usage: "Total timeout for ctr commands",
92110
},
93111
&cli.DurationFlag{
94112
Name: "connect-timeout",
95-
Usage: "timeout for connecting to containerd",
113+
Usage: "Timeout for connecting to containerd",
96114
},
97115
&cli.StringFlag{
98116
Name: "namespace",
99117
Aliases: []string{"n"},
100-
Usage: "namespace to use with commands",
118+
Usage: "Namespace to use with commands",
101119
Value: namespaces.Default,
102120
EnvVars: []string{namespaces.NamespaceEnvVar},
103121
},
@@ -118,9 +136,9 @@ containerd CLI
118136
install.Command,
119137
ociCmd.Command,
120138
}, extraCmds...)
121-
app.Before = func(context *cli.Context) error {
122-
if context.Bool("debug") {
123-
logrus.SetLevel(logrus.DebugLevel)
139+
app.Before = func(cliContext *cli.Context) error {
140+
if cliContext.Bool("debug") {
141+
return log.SetLevel("debug")
124142
}
125143
return nil
126144
}

cmd/ctr/app/main_unix.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !windows
2-
// +build !windows
32

43
/*
54
Copyright The containerd Authors.

cmd/ctr/commands/commands.go

Lines changed: 213 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,219 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26+
"github.com/containerd/containerd/v2/defaults"
2627
"github.com/containerd/containerd/v2/pkg/atomicfile"
2728

2829
"github.com/urfave/cli/v2"
2930
)
3031

32+
var (
33+
// SnapshotterFlags are cli flags specifying snapshotter names
34+
SnapshotterFlags = []cli.Flag{
35+
&cli.StringFlag{
36+
Name: "snapshotter",
37+
Usage: "Snapshotter name. Empty value stands for the default value.",
38+
EnvVars: []string{"CONTAINERD_SNAPSHOTTER"},
39+
},
40+
}
41+
42+
// SnapshotterLabels are cli flags specifying labels which will be added to the new snapshot for container.
43+
SnapshotterLabels = &cli.StringSliceFlag{
44+
Name: "snapshotter-label",
45+
Usage: "Labels added to the new snapshot for this container.",
46+
}
47+
48+
// LabelFlag is a cli flag specifying labels
49+
LabelFlag = &cli.StringSliceFlag{
50+
Name: "label",
51+
Usage: "Labels to attach to the image",
52+
}
53+
54+
// RegistryFlags are cli flags specifying registry options
55+
RegistryFlags = []cli.Flag{
56+
&cli.BoolFlag{
57+
Name: "skip-verify",
58+
Aliases: []string{"k"},
59+
Usage: "Skip SSL certificate validation",
60+
},
61+
&cli.BoolFlag{
62+
Name: "plain-http",
63+
Usage: "Allow connections using plain HTTP",
64+
},
65+
&cli.StringFlag{
66+
Name: "user",
67+
Aliases: []string{"u"},
68+
Usage: "User[:password] Registry user and password",
69+
},
70+
&cli.StringFlag{
71+
Name: "refresh",
72+
Usage: "Refresh token for authorization server",
73+
},
74+
&cli.StringFlag{
75+
Name: "hosts-dir",
76+
// compatible with "/etc/docker/certs.d"
77+
Usage: "Custom hosts configuration directory",
78+
},
79+
&cli.StringFlag{
80+
Name: "tlscacert",
81+
Usage: "Path to TLS root CA",
82+
},
83+
&cli.StringFlag{
84+
Name: "tlscert",
85+
Usage: "Path to TLS client certificate",
86+
},
87+
&cli.StringFlag{
88+
Name: "tlskey",
89+
Usage: "Path to TLS client key",
90+
},
91+
&cli.BoolFlag{
92+
Name: "http-dump",
93+
Usage: "Dump all HTTP request/responses when interacting with container registry",
94+
},
95+
&cli.BoolFlag{
96+
Name: "http-trace",
97+
Usage: "Enable HTTP tracing for registry interactions",
98+
},
99+
}
100+
101+
// RuntimeFlags are cli flags specifying runtime
102+
RuntimeFlags = []cli.Flag{
103+
&cli.StringFlag{
104+
Name: "runtime",
105+
Usage: "Runtime name or absolute path to runtime binary",
106+
Value: defaults.DefaultRuntime,
107+
},
108+
&cli.StringFlag{
109+
Name: "runtime-config-path",
110+
Usage: "Optional runtime config path",
111+
},
112+
}
113+
114+
// ContainerFlags are cli flags specifying container options
115+
ContainerFlags = []cli.Flag{
116+
&cli.StringFlag{
117+
Name: "config",
118+
Aliases: []string{"c"},
119+
Usage: "Path to the runtime-specific spec config file",
120+
},
121+
&cli.StringFlag{
122+
Name: "cwd",
123+
Usage: "Specify the working directory of the process",
124+
},
125+
&cli.StringSliceFlag{
126+
Name: "env",
127+
Usage: "Specify additional container environment variables (e.g. FOO=bar)",
128+
},
129+
&cli.StringFlag{
130+
Name: "env-file",
131+
Usage: "Specify additional container environment variables in a file(e.g. FOO=bar, one per line)",
132+
},
133+
&cli.StringSliceFlag{
134+
Name: "label",
135+
Usage: "Specify additional labels (e.g. foo=bar)",
136+
},
137+
&cli.StringSliceFlag{
138+
Name: "annotation",
139+
Usage: "Specify additional OCI annotations (e.g. foo=bar)",
140+
},
141+
&cli.StringSliceFlag{
142+
Name: "mount",
143+
Usage: "Specify additional container mount (e.g. type=bind,src=/tmp,dst=/host,options=rbind:ro)",
144+
},
145+
&cli.BoolFlag{
146+
Name: "net-host",
147+
Usage: "Enable host networking for the container",
148+
},
149+
&cli.BoolFlag{
150+
Name: "privileged",
151+
Usage: "Run privileged container",
152+
},
153+
&cli.BoolFlag{
154+
Name: "read-only",
155+
Usage: "Set the containers filesystem as readonly",
156+
},
157+
&cli.StringFlag{
158+
Name: "sandbox",
159+
Usage: "Create the container in the given sandbox",
160+
},
161+
&cli.BoolFlag{
162+
Name: "tty",
163+
Aliases: []string{"t"},
164+
Usage: "Allocate a TTY for the container",
165+
},
166+
&cli.StringSliceFlag{
167+
Name: "with-ns",
168+
Usage: "Specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')",
169+
},
170+
&cli.StringFlag{
171+
Name: "pid-file",
172+
Usage: "File path to write the task's pid",
173+
},
174+
&cli.IntSliceFlag{
175+
Name: "gpus",
176+
Usage: "Add gpus to the container",
177+
},
178+
&cli.BoolFlag{
179+
Name: "allow-new-privs",
180+
Usage: "Turn off OCI spec's NoNewPrivileges feature flag",
181+
},
182+
&cli.Uint64Flag{
183+
Name: "memory-limit",
184+
Usage: "Memory limit (in bytes) for the container",
185+
},
186+
&cli.StringSliceFlag{
187+
Name: "cap-add",
188+
Usage: "Add Linux capabilities (Set capabilities with 'CAP_' prefix)",
189+
},
190+
&cli.StringSliceFlag{
191+
Name: "cap-drop",
192+
Usage: "Drop Linux capabilities (Set capabilities with 'CAP_' prefix)",
193+
},
194+
&cli.BoolFlag{
195+
Name: "seccomp",
196+
Usage: "Enable the default seccomp profile",
197+
},
198+
&cli.StringFlag{
199+
Name: "seccomp-profile",
200+
Usage: "File path to custom seccomp profile. seccomp must be set to true, before using seccomp-profile",
201+
},
202+
&cli.StringFlag{
203+
Name: "apparmor-default-profile",
204+
Usage: "Enable AppArmor with the default profile with the specified name, e.g. \"cri-containerd.apparmor.d\"",
205+
},
206+
&cli.StringFlag{
207+
Name: "apparmor-profile",
208+
Usage: "Enable AppArmor with an existing custom profile",
209+
},
210+
&cli.StringFlag{
211+
Name: "blockio-config-file",
212+
Usage: "File path to blockio class definitions. By default class definitions are not loaded.",
213+
},
214+
&cli.StringFlag{
215+
Name: "blockio-class",
216+
Usage: "Name of the blockio class to associate the container with",
217+
},
218+
&cli.StringFlag{
219+
Name: "rdt-class",
220+
Usage: "Name of the RDT class to associate the container with. Specifies a Class of Service (CLOS) for cache and memory bandwidth management.",
221+
},
222+
&cli.StringFlag{
223+
Name: "hostname",
224+
Usage: "Set the container's host name",
225+
},
226+
&cli.StringFlag{
227+
Name: "user",
228+
Aliases: []string{"u"},
229+
Usage: "Username or user id, group optional (format: <name|uid>[:<group|gid>])",
230+
},
231+
}
232+
)
233+
31234
// ObjectWithLabelArgs returns the first arg and a LabelArgs object
32-
func ObjectWithLabelArgs(clicontext *cli.Context) (string, map[string]string) {
235+
func ObjectWithLabelArgs(cliContext *cli.Context) (string, map[string]string) {
33236
var (
34-
first = clicontext.Args().First()
35-
labelStrings = clicontext.Args().Tail()
237+
first = cliContext.Args().First()
238+
labelStrings = cliContext.Args().Tail()
36239
)
37240

38241
return first, LabelArgs(labelStrings)
@@ -42,13 +245,10 @@ func ObjectWithLabelArgs(clicontext *cli.Context) (string, map[string]string) {
42245
func LabelArgs(labelStrings []string) map[string]string {
43246
labels := make(map[string]string, len(labelStrings))
44247
for _, label := range labelStrings {
45-
parts := strings.SplitN(label, "=", 2)
46-
key := parts[0]
47-
value := "true"
48-
if len(parts) > 1 {
49-
value = parts[1]
248+
key, value, ok := strings.Cut(label, "=")
249+
if !ok {
250+
value = "true"
50251
}
51-
52252
labels[key] = value
53253
}
54254

@@ -59,17 +259,17 @@ func LabelArgs(labelStrings []string) map[string]string {
59259
func AnnotationArgs(annoStrings []string) (map[string]string, error) {
60260
annotations := make(map[string]string, len(annoStrings))
61261
for _, anno := range annoStrings {
62-
parts := strings.SplitN(anno, "=", 2)
63-
if len(parts) != 2 {
262+
key, value, ok := strings.Cut(anno, "=")
263+
if !ok {
64264
return nil, fmt.Errorf("invalid key=value format annotation: %v", anno)
65265
}
66-
annotations[parts[0]] = parts[1]
266+
annotations[key] = value
67267
}
68268
return annotations, nil
69269
}
70270

71271
// PrintAsJSON prints input in JSON format
72-
func PrintAsJSON(x interface{}) {
272+
func PrintAsJSON(x any) {
73273
b, err := json.MarshalIndent(x, "", " ")
74274
if err != nil {
75275
fmt.Fprintf(os.Stderr, "can't marshal %+v as a JSON string: %v\n", x, err)

0 commit comments

Comments
 (0)