From b7a07c98ad6d2901e2dd613842bd4be11933252f Mon Sep 17 00:00:00 2001 From: Yanqiang Miao Date: Thu, 2 Nov 2017 13:27:26 +0800 Subject: [PATCH] Support using sandbox name for sandbox operating Signed-off-by: Yanqiang Miao --- cmd/crictl/sandbox.go | 83 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 84112cd075..81c72a65c2 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -52,7 +52,7 @@ var runPodSandboxCommand = cli.Command{ // Test RuntimeServiceClient.RunPodSandbox err = RunPodSandbox(runtimeClient, podSandboxConfig) if err != nil { - return fmt.Errorf("Run pod sandbox failed: %v", err) + return fmt.Errorf("run pod sandbox failed: %v", err) } return nil }, @@ -62,9 +62,16 @@ var stopPodSandboxCommand = cli.Command{ Name: "stops", Usage: "Stop a running sandbox", ArgsUsage: "SANDBOX", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace, n", + Value: "default", + Usage: "Specify namespace", + }, + }, Action: func(context *cli.Context) error { - id := context.Args().First() - if id == "" { + ref := context.Args().First() + if ref == "" { return cli.ShowSubcommandHelp(context) } @@ -72,7 +79,11 @@ var stopPodSandboxCommand = cli.Command{ return err } - err := StopPodSandbox(runtimeClient, id) + id, err := getPodIdFromRef(runtimeClient, ref, context.String("namespace")) + if err != nil { + return fmt.Errorf("error: %v", err) + } + err = StopPodSandbox(runtimeClient, id) if err != nil { return fmt.Errorf("stopping the pod sandbox failed: %v", err) } @@ -84,9 +95,16 @@ var removePodSandboxCommand = cli.Command{ Name: "rms", Usage: "Remove a sandbox", ArgsUsage: "SANDBOX", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace, n", + Value: "default", + Usage: "Specify namespace", + }, + }, Action: func(context *cli.Context) error { - id := context.Args().First() - if id == "" { + ref := context.Args().First() + if ref == "" { return cli.ShowSubcommandHelp(context) } @@ -94,7 +112,11 @@ var removePodSandboxCommand = cli.Command{ return err } - err := RemovePodSandbox(runtimeClient, id) + id, err := getPodIdFromRef(runtimeClient, ref, context.String("namespace")) + if err != nil { + return fmt.Errorf("error: %v", err) + } + err = RemovePodSandbox(runtimeClient, id) if err != nil { return fmt.Errorf("removing the pod sandbox failed: %v", err) } @@ -111,10 +133,15 @@ var podSandboxStatusCommand = cli.Command{ Name: "output, o", Usage: "Output format, One of: json|yaml|table", }, + cli.StringFlag{ + Name: "namespace, n", + Value: "default", + Usage: "Specify namespace", + }, }, Action: func(context *cli.Context) error { - id := context.Args().First() - if id == "" { + ref := context.Args().First() + if ref == "" { return cli.ShowSubcommandHelp(context) } @@ -122,7 +149,11 @@ var podSandboxStatusCommand = cli.Command{ return err } - err := PodSandboxStatus(runtimeClient, id, context.String("output")) + id, err := getPodIdFromRef(runtimeClient, ref, context.String("namespace")) + if err != nil { + return fmt.Errorf("error: %v", err) + } + err = PodSandboxStatus(runtimeClient, id, context.String("output")) if err != nil { return fmt.Errorf("getting the pod sandbox status failed: %v", err) } @@ -137,24 +168,24 @@ var listPodSandboxCommand = cli.Command{ cli.StringFlag{ Name: "id", Value: "", - Usage: "filter by pod sandbox id", + Usage: "Filter by pod sandbox id", }, cli.StringFlag{ Name: "state,s", Value: "", - Usage: "filter by pod sandbox state", + Usage: "Filter by pod sandbox state", }, cli.StringSliceFlag{ Name: "label,l", - Usage: "filter by key=value label", + Usage: "Filter by key=value label", }, cli.BoolFlag{ Name: "verbose, v", - Usage: "show verbose info for sandboxes", + Usage: "Show verbose info for sandboxes", }, cli.BoolFlag{ Name: "quiet", - Usage: "list only sandbox IDs", + Usage: "List only sandbox IDs", }, cli.StringFlag{ Name: "output, o", @@ -351,10 +382,10 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { if !opts.verbose { if printHeader { printHeader = false - fmt.Fprintln(w, "SANDBOX ID\tNAME\tSTATE") + fmt.Fprintln(w, "SANDBOX ID\tNAME\tSTATE\tNAMESPACE") } - fmt.Fprintf(w, "%s\t%s\t%s\n", pod.Id, pod.Metadata.Name, pod.State) + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pod.Id, pod.Metadata.Name, pod.State, pod.Metadata.Namespace) continue } @@ -394,3 +425,21 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error { w.Flush() return nil } + +func getPodIdFromRef(client pb.RuntimeServiceClient, ref, namespace string) (string, error) { + r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{}) + if err != nil { + return "", err + } + + for _, pod := range r.Items { + if pod.Id == ref { + return pod.Id, nil + } + if pod.Metadata.Namespace == namespace && pod.Metadata.Name == ref { + return pod.Id, nil + } + } + + return "", fmt.Errorf("no such sandbox: %q", ref) +}