Skip to content

Commit 1597270

Browse files
Merge pull request #2579 from lifubang/ctrrun
fix when --config provided, don't need Image/RootFS
2 parents 12c877f + 48fe635 commit 1597270

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

cmd/ctr/commands/containers/containers.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ var createCommand = cli.Command{
5353
Flags: append(commands.SnapshotterFlags, commands.ContainerFlags...),
5454
Action: func(context *cli.Context) error {
5555
var (
56-
id = context.Args().Get(1)
57-
ref = context.Args().First()
56+
id string
57+
ref string
58+
config = context.IsSet("config")
5859
)
59-
if ref == "" {
60-
return errors.New("image ref must be provided")
60+
61+
if config {
62+
id = context.Args().First()
63+
if context.NArg() > 1 {
64+
return errors.New("with spec config file, only container id should be provided")
65+
}
66+
} else {
67+
id = context.Args().Get(1)
68+
ref = context.Args().First()
69+
if ref == "" {
70+
return errors.New("image ref must be provided")
71+
}
6172
}
6273
if id == "" {
6374
return errors.New("container id must be provided")

cmd/ctr/commands/run/run.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,26 @@ var Command = cli.Command{
110110
Action: func(context *cli.Context) error {
111111
var (
112112
err error
113+
id string
114+
ref string
113115

114-
id = context.Args().Get(1)
115-
ref = context.Args().First()
116116
tty = context.Bool("tty")
117117
detach = context.Bool("detach")
118+
config = context.IsSet("config")
118119
)
119120

120-
if ref == "" {
121-
return errors.New("image ref must be provided")
121+
if config {
122+
id = context.Args().First()
123+
if context.NArg() > 1 {
124+
return errors.New("with spec config file, only container id should be provided")
125+
}
126+
} else {
127+
id = context.Args().Get(1)
128+
ref = context.Args().First()
129+
130+
if ref == "" {
131+
return errors.New("image ref must be provided")
132+
}
122133
}
123134
if id == "" {
124135
return errors.New("container id must be provided")

cmd/ctr/commands/run/run_unix.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ import (
3535
// NewContainer creates a new container
3636
func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
3737
var (
38-
ref = context.Args().First()
39-
id = context.Args().Get(1)
40-
args = context.Args()[2:]
38+
id string
39+
config = context.IsSet("config")
4140
)
41+
if config {
42+
id = context.Args().First()
43+
} else {
44+
id = context.Args().Get(1)
45+
}
4246

4347
if raw := context.String("checkpoint"); raw != "" {
4448
im, err := client.GetImage(ctx, raw)
@@ -54,9 +58,14 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
5458
spec containerd.NewContainerOpts
5559
)
5660

57-
if context.IsSet("config") {
61+
if config {
5862
opts = append(opts, oci.WithSpecFromFile(context.String("config")))
5963
} else {
64+
var (
65+
ref = context.Args().First()
66+
//for container's id is Args[1]
67+
args = context.Args()[2:]
68+
)
6069
opts = append(opts, oci.WithDefaultSpec(), oci.WithDefaultUnixDevices)
6170
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
6271
opts = append(opts, withMounts(context))

0 commit comments

Comments
 (0)