@@ -23,9 +23,12 @@ import (
2323 "fmt"
2424 "io/ioutil"
2525 "net"
26+ "path/filepath"
2627
2728 "github.com/containerd/console"
2829 "github.com/containerd/containerd/cmd/ctr/commands"
30+ "github.com/containerd/containerd/namespaces"
31+ "github.com/containerd/containerd/runtime/v2/shim"
2932 "github.com/containerd/containerd/runtime/v2/task"
3033 "github.com/containerd/ttrpc"
3134 "github.com/containerd/typeurl"
@@ -61,8 +64,8 @@ var Command = cli.Command{
6164 Usage : "interact with a shim directly" ,
6265 Flags : []cli.Flag {
6366 cli.StringFlag {
64- Name : "socket " ,
65- Usage : "socket on which to connect to the shim " ,
67+ Name : "id " ,
68+ Usage : "container id " ,
6669 },
6770 },
6871 Subcommands : []cli.Command {
@@ -116,7 +119,7 @@ var stateCommand = cli.Command{
116119 return err
117120 }
118121 r , err := service .State (gocontext .Background (), & task.StateRequest {
119- ID : context .Args (). First ( ),
122+ ID : context .GlobalString ( "id" ),
120123 })
121124 if err != nil {
122125 return err
@@ -226,20 +229,30 @@ var execCommand = cli.Command{
226229}
227230
228231func getTaskService (context * cli.Context ) (task.TaskService , error ) {
229- bindSocket := context .GlobalString ("socket " )
230- if bindSocket == "" {
231- return nil , errors . New ( "socket path must be specified" )
232+ id := context .GlobalString ("id " )
233+ if id == "" {
234+ return nil , fmt . Errorf ( "container id must be specified" )
232235 }
236+ ns := context .GlobalString ("namespace" )
233237
234- conn , err := net .Dial ("unix" , "\x00 " + bindSocket )
235- if err != nil {
236- return nil , err
237- }
238+ // /containerd-shim/ns/id/shim.sock is the old way to generate shim socket,
239+ // compatible it
240+ s1 := filepath .Join (string (filepath .Separator ), "containerd-shim" , ns , id , "shim.sock" )
241+ // this should not error, ctr always get a default ns
242+ ctx := namespaces .WithNamespace (gocontext .Background (), ns )
243+ s2 , _ := shim .SocketAddress (ctx , id )
244+
245+ for _ , socket := range []string {s1 , s2 } {
246+ conn , err := net .Dial ("unix" , "\x00 " + socket )
247+ if err == nil {
248+ client := ttrpc .NewClient (conn )
238249
239- client := ttrpc .NewClient (conn )
250+ // TODO(stevvooe): This actually leaks the connection. We were leaking it
251+ // before, so may not be a huge deal.
240252
241- // TODO(stevvooe): This actually leaks the connection. We were leaking it
242- // before, so may not be a huge deal.
253+ return task .NewTaskClient (client ), nil
254+ }
255+ }
243256
244- return task . NewTaskClient ( client ), nil
257+ return nil , fmt . Errorf ( "fail to connect to container %s's shim" , id )
245258}
0 commit comments