Skip to content

Commit 5dd22a2

Browse files
committed
Move ContainerFlags to "commands" package
Commit 0551328 exposed the "rootfs" and "no-pivot" flags for the "containers" command, but it accidentally removed them for "run" since package-level variables are initialized before package-level init functions in golang. Hoisting these flags to a package imported by both commands solves the problem. Signed-off-by: Felix Abecassis <[email protected]>
1 parent c1e1f3d commit 5dd22a2

5 files changed

Lines changed: 97 additions & 74 deletions

File tree

cmd/ctr/commands/commands.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"path/filepath"
24+
"runtime"
2425
"strings"
2526

2627
"github.com/containerd/containerd"
@@ -63,6 +64,67 @@ var (
6364
Usage: "refresh token for authorization server",
6465
},
6566
}
67+
68+
// ContainerFlags are cli flags specifying container options
69+
ContainerFlags = []cli.Flag{
70+
cli.StringFlag{
71+
Name: "config,c",
72+
Usage: "path to the runtime-specific spec config file",
73+
},
74+
cli.StringFlag{
75+
Name: "checkpoint",
76+
Usage: "provide the checkpoint digest to restore the container",
77+
},
78+
cli.StringFlag{
79+
Name: "cwd",
80+
Usage: "specify the working directory of the process",
81+
},
82+
cli.StringSliceFlag{
83+
Name: "env",
84+
Usage: "specify additional container environment variables (i.e. FOO=bar)",
85+
},
86+
cli.StringSliceFlag{
87+
Name: "label",
88+
Usage: "specify additional labels (i.e. foo=bar)",
89+
},
90+
cli.StringSliceFlag{
91+
Name: "mount",
92+
Usage: "specify additional container mount (ex: type=bind,src=/tmp,dst=/host,options=rbind:ro)",
93+
},
94+
cli.BoolFlag{
95+
Name: "net-host",
96+
Usage: "enable host networking for the container",
97+
},
98+
cli.BoolFlag{
99+
Name: "privileged",
100+
Usage: "run privileged container",
101+
},
102+
cli.BoolFlag{
103+
Name: "read-only",
104+
Usage: "set the containers filesystem as readonly",
105+
},
106+
cli.StringFlag{
107+
Name: "runtime",
108+
Usage: "runtime name (io.containerd.runtime.v1.linux, io.containerd.runtime.v1.windows, io.containerd.runtime.v1.com.vmware.linux)",
109+
Value: fmt.Sprintf("io.containerd.runtime.v1.%s", runtime.GOOS),
110+
},
111+
cli.BoolFlag{
112+
Name: "tty,t",
113+
Usage: "allocate a TTY for the container",
114+
},
115+
cli.StringSliceFlag{
116+
Name: "with-ns",
117+
Usage: "specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')",
118+
},
119+
cli.StringFlag{
120+
Name: "pid-file",
121+
Usage: "file path to write the task's pid",
122+
},
123+
cli.IntFlag{
124+
Name: "gpus",
125+
Usage: "add gpus to the container",
126+
},
127+
}
66128
)
67129

68130
// ObjectWithLabelArgs returns the first arg and a LabelArgs object

cmd/ctr/commands/commands_unix.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// +build !windows
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package commands
20+
21+
import (
22+
"github.com/urfave/cli"
23+
)
24+
25+
func init() {
26+
ContainerFlags = append(ContainerFlags, cli.BoolFlag{
27+
Name: "rootfs",
28+
Usage: "use custom rootfs that is not managed by containerd snapshotter",
29+
}, cli.BoolFlag{
30+
Name: "no-pivot",
31+
Usage: "disable use of pivot-root (linux only)",
32+
})
33+
}

cmd/ctr/commands/containers/containers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var createCommand = cli.Command{
4949
Name: "create",
5050
Usage: "create container",
5151
ArgsUsage: "[flags] Image|RootFS CONTAINER",
52-
Flags: append(commands.SnapshotterFlags, run.ContainerFlags...),
52+
Flags: append(commands.SnapshotterFlags, commands.ContainerFlags...),
5353
Action: func(context *cli.Context) error {
5454
var (
5555
id = context.Args().Get(1)

cmd/ctr/commands/run/run.go

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"io/ioutil"
25-
"runtime"
2625
"strings"
2726

2827
"github.com/containerd/console"
@@ -38,67 +37,6 @@ import (
3837
"github.com/urfave/cli"
3938
)
4039

41-
// ContainerFlags are cli flags specifying container options
42-
var ContainerFlags = []cli.Flag{
43-
cli.StringFlag{
44-
Name: "config,c",
45-
Usage: "path to the runtime-specific spec config file",
46-
},
47-
cli.StringFlag{
48-
Name: "checkpoint",
49-
Usage: "provide the checkpoint digest to restore the container",
50-
},
51-
cli.StringFlag{
52-
Name: "cwd",
53-
Usage: "specify the working directory of the process",
54-
},
55-
cli.StringSliceFlag{
56-
Name: "env",
57-
Usage: "specify additional container environment variables (i.e. FOO=bar)",
58-
},
59-
cli.StringSliceFlag{
60-
Name: "label",
61-
Usage: "specify additional labels (i.e. foo=bar)",
62-
},
63-
cli.StringSliceFlag{
64-
Name: "mount",
65-
Usage: "specify additional container mount (ex: type=bind,src=/tmp,dst=/host,options=rbind:ro)",
66-
},
67-
cli.BoolFlag{
68-
Name: "net-host",
69-
Usage: "enable host networking for the container",
70-
},
71-
cli.BoolFlag{
72-
Name: "privileged",
73-
Usage: "run privileged container",
74-
},
75-
cli.BoolFlag{
76-
Name: "read-only",
77-
Usage: "set the containers filesystem as readonly",
78-
},
79-
cli.StringFlag{
80-
Name: "runtime",
81-
Usage: "runtime name (io.containerd.runtime.v1.linux, io.containerd.runtime.v1.windows, io.containerd.runtime.v1.com.vmware.linux)",
82-
Value: fmt.Sprintf("io.containerd.runtime.v1.%s", runtime.GOOS),
83-
},
84-
cli.BoolFlag{
85-
Name: "tty,t",
86-
Usage: "allocate a TTY for the container",
87-
},
88-
cli.StringSliceFlag{
89-
Name: "with-ns",
90-
Usage: "specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')",
91-
},
92-
cli.StringFlag{
93-
Name: "pid-file",
94-
Usage: "file path to write the task's pid",
95-
},
96-
cli.IntFlag{
97-
Name: "gpus",
98-
Usage: "add gpus to the container",
99-
},
100-
}
101-
10240
func loadSpec(path string, s *specs.Spec) error {
10341
raw, err := ioutil.ReadFile(path)
10442
if err != nil {
@@ -181,7 +119,7 @@ var Command = cli.Command{
181119
Name: "fifo-dir",
182120
Usage: "directory used for storing IO FIFOs",
183121
},
184-
}, append(commands.SnapshotterFlags, ContainerFlags...)...),
122+
}, append(commands.SnapshotterFlags, commands.ContainerFlags...)...),
185123
Action: func(context *cli.Context) error {
186124
var (
187125
err error

cmd/ctr/commands/run/run_unix.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ import (
3131
"github.com/urfave/cli"
3232
)
3333

34-
func init() {
35-
ContainerFlags = append(ContainerFlags, cli.BoolFlag{
36-
Name: "rootfs",
37-
Usage: "use custom rootfs that is not managed by containerd snapshotter",
38-
}, cli.BoolFlag{
39-
Name: "no-pivot",
40-
Usage: "disable use of pivot-root (linux only)",
41-
})
42-
}
43-
4434
// NewContainer creates a new container
4535
func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
4636
var (

0 commit comments

Comments
 (0)