Skip to content

Commit

Permalink
Support using sandbox name for sandbox operating
Browse files Browse the repository at this point in the history
Signed-off-by: Yanqiang Miao <[email protected]>
  • Loading branch information
Yanqiang Miao committed Nov 2, 2017
1 parent be5121b commit b7a07c9
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -62,17 +62,28 @@ 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)
}

if err := getRuntimeClient(context); err != nil {
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)
}
Expand All @@ -84,17 +95,28 @@ 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)
}

if err := getRuntimeClient(context); err != nil {
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)
}
Expand All @@ -111,18 +133,27 @@ 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)
}

if err := getRuntimeClient(context); err != nil {
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)
}
Expand All @@ -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",
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

0 comments on commit b7a07c9

Please sign in to comment.