2016-09-23 07:50:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-13 17:07:58 +00:00
|
|
|
"log"
|
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"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
err = StopContainer(client, context.String("id"))
|
|
|
|
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-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 != "" {
|
|
|
|
config.Metadata.Name = &opts.name
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2016-10-14 19:05:47 +00:00
|
|
|
PodSandboxId: &opts.podID,
|
2016-09-23 07:50:05 +00:00
|
|
|
Config: config,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(*r.ContainerId)
|
|
|
|
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{
|
|
|
|
ContainerId: &ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopContainer sends a StopContainerRequest to the server, and parses
|
|
|
|
// the returned StopContainerResponse.
|
|
|
|
func StopContainer(client pb.RuntimeServiceClient, ID string) error {
|
|
|
|
if ID == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
}
|
|
|
|
_, err := client.StopContainer(context.Background(), &pb.StopContainerRequest{
|
|
|
|
ContainerId: &ID,
|
|
|
|
})
|
|
|
|
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{
|
|
|
|
ContainerId: &ID,
|
|
|
|
})
|
|
|
|
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{
|
|
|
|
ContainerId: &ID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("ID: %s\n", *r.Status.Id)
|
2016-10-26 11:32:59 +00:00
|
|
|
if r.Status.Metadata != nil {
|
|
|
|
if r.Status.Metadata.Name != nil {
|
|
|
|
fmt.Printf("Name: %s\n", *r.Status.Metadata.Name)
|
|
|
|
}
|
|
|
|
if r.Status.Metadata.Attempt != nil {
|
|
|
|
fmt.Printf("Attempt: %v\n", *r.Status.Metadata.Attempt)
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 07:50:05 +00:00
|
|
|
if r.Status.State != nil {
|
|
|
|
fmt.Printf("Status: %s\n", r.Status.State)
|
|
|
|
}
|
|
|
|
if r.Status.CreatedAt != nil {
|
2016-11-16 07:36:32 +00:00
|
|
|
ctm := time.Unix(0, *r.Status.CreatedAt)
|
2016-09-23 07:50:05 +00:00
|
|
|
fmt.Printf("Created: %v\n", ctm)
|
|
|
|
}
|
|
|
|
if r.Status.StartedAt != nil {
|
2016-11-16 07:36:32 +00:00
|
|
|
stm := time.Unix(0, *r.Status.StartedAt)
|
2016-09-23 07:50:05 +00:00
|
|
|
fmt.Printf("Started: %v\n", stm)
|
|
|
|
}
|
|
|
|
if r.Status.FinishedAt != nil {
|
2016-11-16 07:36:32 +00:00
|
|
|
ftm := time.Unix(0, *r.Status.FinishedAt)
|
2016-09-23 07:50:05 +00:00
|
|
|
fmt.Printf("Finished: %v\n", ftm)
|
|
|
|
}
|
|
|
|
if r.Status.ExitCode != nil {
|
|
|
|
fmt.Printf("Exit Code: %v\n", *r.Status.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
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 != "" {
|
|
|
|
filter.Id = &opts.id
|
2016-10-13 17:07:58 +00:00
|
|
|
}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.podID != "" {
|
|
|
|
filter.PodSandboxId = &opts.podID
|
2016-10-13 17:07:58 +00:00
|
|
|
}
|
2016-10-14 00:02:02 +00:00
|
|
|
if opts.state != "" {
|
2016-10-13 17:07:58 +00:00
|
|
|
st := pb.ContainerState_UNKNOWN
|
2016-10-14 00:02:02 +00:00
|
|
|
switch opts.state {
|
2016-10-13 17:07:58 +00:00
|
|
|
case "created":
|
|
|
|
st = pb.ContainerState_CREATED
|
|
|
|
filter.State = &st
|
|
|
|
case "running":
|
|
|
|
st = pb.ContainerState_RUNNING
|
|
|
|
filter.State = &st
|
|
|
|
case "stopped":
|
|
|
|
st = pb.ContainerState_EXITED
|
|
|
|
filter.State = &st
|
|
|
|
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 {
|
2016-10-08 12:30:29 +00:00
|
|
|
fmt.Println(*c.Id)
|
|
|
|
continue
|
|
|
|
}
|
2016-09-27 22:17:56 +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 {
|
|
|
|
if c.Metadata.Name != nil {
|
|
|
|
fmt.Printf("Name: %s\n", *c.Metadata.Name)
|
|
|
|
}
|
|
|
|
if c.Metadata.Attempt != nil {
|
|
|
|
fmt.Printf("Attempt: %v\n", *c.Metadata.Attempt)
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 22:17:56 +00:00
|
|
|
if c.State != nil {
|
|
|
|
fmt.Printf("Status: %s\n", *c.State)
|
|
|
|
}
|
|
|
|
if c.CreatedAt != nil {
|
2016-11-16 07:36:32 +00:00
|
|
|
ctm := time.Unix(0, *c.CreatedAt)
|
2016-09-27 22:17:56 +00:00
|
|
|
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
|
|
|
|
}
|