package main import ( "fmt" "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, }, } 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", }, }, 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") } // Test RuntimeServiceClient.CreateContainer err = CreateContainer(client, context.String("pod"), context.String("config")) 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 }, } // CreateContainer sends a CreateContainerRequest to the server, and parses // the returned CreateContainerResponse. func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string) error { config, err := loadContainerConfig(path) if err != nil { return err } r, err := client.CreateContainer(context.Background(), &pb.CreateContainerRequest{ PodSandboxId: &sandbox, 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) if r.Status.State != nil { fmt.Printf("Status: %s\n", r.Status.State) } if r.Status.CreatedAt != nil { ctm := time.Unix(*r.Status.CreatedAt, 0) fmt.Printf("Created: %v\n", ctm) } if r.Status.StartedAt != nil { stm := time.Unix(*r.Status.StartedAt, 0) fmt.Printf("Started: %v\n", stm) } if r.Status.FinishedAt != nil { ftm := time.Unix(*r.Status.FinishedAt, 0) fmt.Printf("Finished: %v\n", ftm) } if r.Status.ExitCode != nil { fmt.Printf("Exit Code: %v\n", *r.Status.ExitCode) } return nil }