2016-09-23 07:50:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-13 17:07:58 +00:00
|
|
|
"log"
|
2017-04-20 14:15:56 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2016-10-14 00:02:02 +00:00
|
|
|
"strings"
|
2016-09-23 07:50:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"golang.org/x/net/context"
|
2017-04-20 14:15:56 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
|
2016-09-23 07:50:05 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2017-04-20 14:15:56 +00:00
|
|
|
remotecommandserver "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
|
2016-09-23 07:50:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var containerCommand = cli.Command{
|
|
|
|
Name: "container",
|
|
|
|
Aliases: []string{"ctr"},
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
createContainerCommand,
|
|
|
|
startContainerCommand,
|
|
|
|
stopContainerCommand,
|
|
|
|
removeContainerCommand,
|
|
|
|
containerStatusCommand,
|
2016-09-27 22:17:56 +00:00
|
|
|
listContainersCommand,
|
2016-11-18 00:42:08 +00:00
|
|
|
execSyncCommand,
|
2017-02-08 13:20:38 +00:00
|
|
|
execCommand,
|
2016-09-23 07:50:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-14 19:05:47 +00:00
|
|
|
type createOptions struct {
|
|
|
|
// configPath is path to the config for container
|
|
|
|
configPath string
|
|
|
|
// name sets the container name
|
|
|
|
name string
|
|
|
|
// podID of the container
|
|
|
|
podID string
|
|
|
|
// labels for the container
|
|
|
|
labels map[string]string
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:50:05 +00:00
|
|
|
var createContainerCommand = cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "create a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pod",
|
|
|
|
Usage: "the id of the pod sandbox to which the container belongs",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Value: "config.json",
|
|
|
|
Usage: "the path of a container config file",
|
|
|
|
},
|
2016-10-07 18:42:08 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "the name of the container",
|
|
|
|
},
|
2016-10-14 19:05:47 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "label",
|
|
|
|
Usage: "add key=value labels to the container",
|
|
|
|
},
|
2016-09-23 07:50:05 +00:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
if !context.IsSet("pod") {
|
|
|
|
return fmt.Errorf("Please specify the id of the pod sandbox to which the container belongs via the --pod option")
|
|
|
|
}
|
2016-10-14 19:05:47 +00:00
|
|
|
|
|
|
|
opts := createOptions{
|
|
|
|
configPath: context.String("config"),
|
|
|
|
name: context.String("name"),
|
|
|
|
podID: context.String("pod"),
|
|
|
|
labels: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range context.StringSlice("label") {
|
|
|
|
pair := strings.Split(l, "=")
|
|
|
|
if len(pair) != 2 {
|
|
|
|
return fmt.Errorf("incorrectly specified label: %v", l)
|
|
|
|
}
|
|
|
|
opts.labels[pair[0]] = pair[1]
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:50:05 +00:00
|
|
|
// Test RuntimeServiceClient.CreateContainer
|
2016-10-14 19:05:47 +00:00
|
|
|
err = CreateContainer(client, opts)
|
2016-09-23 07:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Creating container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var startContainerCommand = cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Usage: "start a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
err = StartContainer(client, context.String("id"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Starting the container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var stopContainerCommand = cli.Command{
|
|
|
|
Name: "stop",
|
|
|
|
Usage: "stop a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
2017-05-15 13:27:03 +00:00
|
|
|
err = StopContainer(client, context.String("id"), -1)
|
2016-09-23 07:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Stopping the container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var removeContainerCommand = cli.Command{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
err = RemoveContainer(client, context.String("id"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Removing the container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var containerStatusCommand = cli.Command{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "get the status of a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
err = ContainerStatus(client, context.String("id"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Getting the status of the container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 00:42:08 +00:00
|
|
|
var execSyncCommand = cli.Command{
|
|
|
|
Name: "execsync",
|
|
|
|
Usage: "exec a command synchronously in a container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "timeout",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "timeout for the command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
err = ExecSync(client, context.String("id"), context.Args(), context.Int64("timeout"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("execing command in container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-08 13:20:38 +00:00
|
|
|
var execCommand = cli.Command{
|
|
|
|
Name: "exec",
|
|
|
|
Usage: "prepare a streaming endpoint to execute a command in the container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "id of the container",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tty",
|
|
|
|
Usage: "whether to use tty",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "stdin",
|
|
|
|
Usage: "whether to stream to stdin",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "url",
|
|
|
|
Usage: "do not exec command, just prepare streaming endpoint",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
|
|
|
|
|
|
|
err = Exec(client, context.String("id"), context.Bool("tty"), context.Bool("stdin"), context.Bool("url"), context.Args())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("execing command in container failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:02:02 +00:00
|
|
|
type listOptions struct {
|
|
|
|
// id of the container
|
|
|
|
id string
|
|
|
|
// podID of the container
|
|
|
|
podID string
|
|
|
|
// state of the container
|
|
|
|
state string
|
|
|
|
// quiet is for listing just container IDs
|
|
|
|
quiet bool
|
|
|
|
// labels are selectors for the container
|
|
|
|
labels map[string]string
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:17:56 +00:00
|
|
|
var listContainersCommand = cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "list containers",
|
2016-10-08 12:30:29 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet",
|
|
|
|
Usage: "list only container IDs",
|
|
|
|
},
|
2016-10-13 17:07:58 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "filter by container id",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pod",
|
|
|
|
Value: "",
|
|
|
|
Usage: "filter by container pod id",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "state",
|
|
|
|
Value: "",
|
|
|
|
Usage: "filter by container state",
|
|
|
|
},
|
2016-10-14 00:02:02 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "label",
|
|
|
|
Usage: "filter by key=value label",
|
|
|
|
},
|
2016-10-08 12:30:29 +00:00
|
|
|
},
|
2016-09-27 22:17:56 +00:00
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
// Set up a connection to the server.
|
|
|
|
conn, err := getClientConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewRuntimeServiceClient(conn)
|
2016-10-14 00:02:02 +00:00
|
|
|
opts := listOptions{
|
|
|
|
id: context.String("id"),
|
|
|
|
podID: context.String("pod"),
|
|
|
|
state: context.String("state"),
|
|
|
|
quiet: context.Bool("quiet"),
|
|
|
|
labels: make(map[string]string),
|
|
|
|
}
|
2016-09-27 22:17:56 +00:00
|
|
|
|
2016-10-14 00:02:02 +00:00
|
|
|
for _, l := range context.StringSlice("label") {
|
|
|
|
pair := strings.Split(l, "=")
|
|
|
|
if len(pair) != 2 {
|
|
|
|
return fmt.Errorf("incorrectly specified label: %v", l)
|
|
|
|
}
|
|
|
|
opts.labels[pair[0]] = pair[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ListContainers(client, opts)
|
2016-09-27 22:17:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listing containers failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:50:05 +00:00
|
|
|
// CreateContainer sends a CreateContainerRequest to the server, and parses
|
|
|
|
// the returned CreateContainerResponse.
|
2016-10-14 19:05:47 +00:00
|
|
|
func CreateContainer(client pb.RuntimeServiceClient, opts createOptions) error {
|
|
|
|
config, err := loadContainerConfig(opts.configPath)
|
2016-09-23 07:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:42:08 +00:00
|
|
|
// Override the name by the one specified through CLI
|
2016-10-14 19:05:47 +00:00
|
|
|
if opts.name != "" {
|
2017-02-03 14:41:28 +00:00
|
|
|
config.Metadata.Name = opts.name
|
2016-10-14 19:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range opts.labels {
|
|
|
|
config.Labels[k] = v
|
2016-10-07 18:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 07:50:05 +00:00
|
|
|
r, err := client.CreateContainer(context.Background(), &pb.CreateContainerRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
PodSandboxId: opts.podID,
|
2016-09-23 07:50:05 +00:00
|
|
|
Config: config,
|
2016-11-23 09:41:48 +00:00
|
|
|
// TODO(runcom): this is missing PodSandboxConfig!!!
|
|
|
|
// we should/could find a way to retrieve it from the fs and set it here
|
2016-09-23 07:50:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Println(r.ContainerId)
|
2016-09-23 07:50:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartContainer sends a StartContainerRequest to the server, and parses
|
|
|
|
// the returned StartContainerResponse.
|
|
|
|
func StartContainer(client pb.RuntimeServiceClient, ID string) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
_, err := client.StartContainer(context.Background(), &pb.StartContainerRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
ContainerId: ID,
|
2016-09-23 07:50:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopContainer sends a StopContainerRequest to the server, and parses
|
|
|
|
// the returned StopContainerResponse.
|
2017-05-15 13:27:03 +00:00
|
|
|
func StopContainer(client pb.RuntimeServiceClient, ID string, timeout int64) error {
|
2016-09-23 07:50:05 +00:00
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
_, err := client.StopContainer(context.Background(), &pb.StopContainerRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
ContainerId: ID,
|
2017-05-15 13:27:03 +00:00
|
|
|
Timeout: timeout,
|
2016-09-23 07:50:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveContainer sends a RemoveContainerRequest to the server, and parses
|
|
|
|
// the returned RemoveContainerResponse.
|
|
|
|
func RemoveContainer(client pb.RuntimeServiceClient, ID string) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
_, err := client.RemoveContainer(context.Background(), &pb.RemoveContainerRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
ContainerId: ID,
|
2016-09-23 07:50:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus sends a ContainerStatusRequest to the server, and parses
|
|
|
|
// the returned ContainerStatusResponse.
|
|
|
|
func ContainerStatus(client pb.RuntimeServiceClient, ID string) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
r, err := client.ContainerStatus(context.Background(), &pb.ContainerStatusRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
ContainerId: ID})
|
2016-09-23 07:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("ID: %s\n", r.Status.Id)
|
2016-10-26 11:32:59 +00:00
|
|
|
if r.Status.Metadata != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if r.Status.Metadata.Name != "" {
|
|
|
|
fmt.Printf("Name: %s\n", r.Status.Metadata.Name)
|
2016-10-26 11:32:59 +00:00
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("Attempt: %v\n", r.Status.Metadata.Attempt)
|
2016-09-23 07:50:05 +00:00
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
// TODO(mzylowski): print it prettier
|
|
|
|
fmt.Printf("Status: %s\n", r.Status.State)
|
|
|
|
ctm := time.Unix(0, r.Status.CreatedAt)
|
|
|
|
fmt.Printf("Created: %v\n", ctm)
|
|
|
|
stm := time.Unix(0, r.Status.StartedAt)
|
|
|
|
fmt.Printf("Started: %v\n", stm)
|
|
|
|
ftm := time.Unix(0, r.Status.FinishedAt)
|
|
|
|
fmt.Printf("Finished: %v\n", ftm)
|
|
|
|
fmt.Printf("Exit Code: %v\n", r.Status.ExitCode)
|
2016-09-23 07:50:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-27 22:17:56 +00:00
|
|
|
|
2016-11-18 00:42:08 +00:00
|
|
|
// ExecSync sends an ExecSyncRequest to the server, and parses
|
|
|
|
// the returned ExecSyncResponse.
|
|
|
|
func ExecSync(client pb.RuntimeServiceClient, ID string, cmd []string, timeout int64) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
r, err := client.ExecSync(context.Background(), &pb.ExecSyncRequest{
|
2017-02-03 14:41:28 +00:00
|
|
|
ContainerId: ID,
|
2016-11-18 00:42:08 +00:00
|
|
|
Cmd: cmd,
|
2017-02-03 14:41:28 +00:00
|
|
|
Timeout: timeout,
|
2016-11-18 00:42:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Stdout:")
|
|
|
|
fmt.Println(string(r.Stdout))
|
|
|
|
fmt.Println("Stderr:")
|
|
|
|
fmt.Println(string(r.Stderr))
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("Exit code: %v\n", r.ExitCode)
|
2016-11-18 00:42:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 13:20:38 +00:00
|
|
|
// Exec sends an ExecRequest to the server, and parses
|
|
|
|
// the returned ExecResponse.
|
|
|
|
func Exec(client pb.RuntimeServiceClient, ID string, tty bool, stdin bool, urlOnly bool, cmd []string) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
r, err := client.Exec(context.Background(), &pb.ExecRequest{
|
|
|
|
ContainerId: ID,
|
|
|
|
Cmd: cmd,
|
|
|
|
Tty: tty,
|
|
|
|
Stdin: stdin,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if urlOnly {
|
|
|
|
fmt.Println("URL:")
|
2017-04-20 14:15:56 +00:00
|
|
|
fmt.Println(r.Url)
|
2017-02-08 13:20:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-20 14:15:56 +00:00
|
|
|
execURL, err := url.Parse(r.Url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
streamExec, err := remotecommand.NewExecutor(&restclient.Config{}, "GET", execURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := remotecommand.StreamOptions{
|
|
|
|
SupportedProtocols: remotecommandserver.SupportedStreamingProtocols,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
Tty: tty,
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdin {
|
|
|
|
options.Stdin = os.Stdin
|
|
|
|
}
|
|
|
|
|
|
|
|
return streamExec.Stream(options)
|
2017-02-08 13:20:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 22:17:56 +00:00
|
|
|
// ListContainers sends a ListContainerRequest to the server, and parses
|
|
|
|
// the returned ListContainerResponse.
|
2016-10-14 00:02:02 +00:00
|
|
|
func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
|
2016-10-13 17:07:58 +00:00
|
|
|
filter := &pb.ContainerFilter{}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.id != "" {
|
2017-02-03 14:41:28 +00:00
|
|
|
filter.Id = opts.id
|
2016-10-13 17:07:58 +00:00
|
|
|
}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.podID != "" {
|
2017-02-03 14:41:28 +00:00
|
|
|
filter.PodSandboxId = opts.podID
|
2016-10-13 17:07:58 +00:00
|
|
|
}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.state != "" {
|
2017-02-03 14:41:28 +00:00
|
|
|
st := &pb.ContainerStateValue{}
|
|
|
|
st.State = pb.ContainerState_CONTAINER_UNKNOWN
|
2016-10-14 00:02:02 +00:00
|
|
|
switch opts.state {
|
2016-10-13 17:07:58 +00:00
|
|
|
case "created":
|
2017-02-03 14:41:28 +00:00
|
|
|
st.State = pb.ContainerState_CONTAINER_CREATED
|
|
|
|
filter.State = st
|
2016-10-13 17:07:58 +00:00
|
|
|
case "running":
|
2017-02-03 14:41:28 +00:00
|
|
|
st.State = pb.ContainerState_CONTAINER_RUNNING
|
|
|
|
filter.State = st
|
2016-10-13 17:07:58 +00:00
|
|
|
case "stopped":
|
2017-02-03 14:41:28 +00:00
|
|
|
st.State = pb.ContainerState_CONTAINER_EXITED
|
|
|
|
filter.State = st
|
2016-10-13 17:07:58 +00:00
|
|
|
default:
|
|
|
|
log.Fatalf("--state should be one of created, running or stopped")
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.labels != nil {
|
|
|
|
filter.LabelSelector = opts.labels
|
|
|
|
}
|
2016-10-13 17:07:58 +00:00
|
|
|
r, err := client.ListContainers(context.Background(), &pb.ListContainersRequest{
|
|
|
|
Filter: filter,
|
|
|
|
})
|
2016-09-27 22:17:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, c := range r.GetContainers() {
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.quiet {
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Println(c.Id)
|
2016-10-08 12:30:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("ID: %s\n", c.Id)
|
|
|
|
fmt.Printf("Pod: %s\n", c.PodSandboxId)
|
2016-10-26 11:32:59 +00:00
|
|
|
if c.Metadata != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if c.Metadata.Name != "" {
|
|
|
|
fmt.Printf("Name: %s\n", c.Metadata.Name)
|
2016-10-26 11:32:59 +00:00
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("Attempt: %v\n", c.Metadata.Attempt)
|
2016-10-26 11:32:59 +00:00
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("Status: %s\n", c.State)
|
2016-12-12 10:12:03 +00:00
|
|
|
if c.Image != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
fmt.Printf("Image: %s\n", c.Image.Image)
|
2016-09-27 22:17:56 +00:00
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
ctm := time.Unix(0, c.CreatedAt)
|
|
|
|
fmt.Printf("Created: %v\n", ctm)
|
2016-10-14 00:02:02 +00:00
|
|
|
if c.Labels != nil {
|
|
|
|
fmt.Println("Labels:")
|
2016-11-02 06:36:42 +00:00
|
|
|
for _, k := range getSortedKeys(c.Labels) {
|
|
|
|
fmt.Printf("\t%s -> %s\n", k, c.Labels[k])
|
2016-10-14 00:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Annotations != nil {
|
|
|
|
fmt.Println("Annotations:")
|
2016-11-02 06:36:42 +00:00
|
|
|
for _, k := range getSortedKeys(c.Annotations) {
|
|
|
|
fmt.Printf("\t%s -> %s\n", k, c.Annotations[k])
|
2016-10-14 00:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 11:32:59 +00:00
|
|
|
fmt.Println()
|
2016-09-27 22:17:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|