Merge pull request #3288 from Ace-Tang/easy-shim
ctr: make ctr shim command easy to use
This commit is contained in:
commit
b99a66c267
@ -23,9 +23,12 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/containerd/runtime/v2/shim"
|
||||
"github.com/containerd/containerd/runtime/v2/task"
|
||||
"github.com/containerd/ttrpc"
|
||||
"github.com/containerd/typeurl"
|
||||
@ -61,8 +64,8 @@ var Command = cli.Command{
|
||||
Usage: "interact with a shim directly",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "socket",
|
||||
Usage: "socket on which to connect to the shim",
|
||||
Name: "id",
|
||||
Usage: "container id",
|
||||
},
|
||||
},
|
||||
Subcommands: []cli.Command{
|
||||
@ -116,7 +119,7 @@ var stateCommand = cli.Command{
|
||||
return err
|
||||
}
|
||||
r, err := service.State(gocontext.Background(), &task.StateRequest{
|
||||
ID: context.Args().First(),
|
||||
ID: context.GlobalString("id"),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@ -226,20 +229,30 @@ var execCommand = cli.Command{
|
||||
}
|
||||
|
||||
func getTaskService(context *cli.Context) (task.TaskService, error) {
|
||||
bindSocket := context.GlobalString("socket")
|
||||
if bindSocket == "" {
|
||||
return nil, errors.New("socket path must be specified")
|
||||
id := context.GlobalString("id")
|
||||
if id == "" {
|
||||
return nil, fmt.Errorf("container id must be specified")
|
||||
}
|
||||
ns := context.GlobalString("namespace")
|
||||
|
||||
conn, err := net.Dial("unix", "\x00"+bindSocket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// /containerd-shim/ns/id/shim.sock is the old way to generate shim socket,
|
||||
// compatible it
|
||||
s1 := filepath.Join(string(filepath.Separator), "containerd-shim", ns, id, "shim.sock")
|
||||
// this should not error, ctr always get a default ns
|
||||
ctx := namespaces.WithNamespace(gocontext.Background(), ns)
|
||||
s2, _ := shim.SocketAddress(ctx, id)
|
||||
|
||||
for _, socket := range []string{s1, s2} {
|
||||
conn, err := net.Dial("unix", "\x00"+socket)
|
||||
if err == nil {
|
||||
client := ttrpc.NewClient(conn)
|
||||
|
||||
// TODO(stevvooe): This actually leaks the connection. We were leaking it
|
||||
// before, so may not be a huge deal.
|
||||
|
||||
return task.NewTaskClient(client), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("fail to connect to container %s's shim", id)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user