cmd/client: split commands in multiple files
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
6c5767fb6f
commit
ec173002fd
7 changed files with 539 additions and 497 deletions
|
@ -14,7 +14,7 @@ for d in $(find . -type d -not -iwholename '*.git*' -a -not -iname '.tool' -a -n
|
|||
--exclude='error return value not checked.*(Close|Log|Print).*\(errcheck\)$' \
|
||||
--exclude='.*_test\.go:.*error return value not checked.*\(errcheck\)$' \
|
||||
--exclude='duplicate of.*_test.go.*\(dupl\)$' \
|
||||
--exclude='duplicate of.*main.go.*\(dupl\)$' \
|
||||
--exclude='cmd\/client\/.*\.go.*\(dupl\)$' \
|
||||
--disable=aligncheck \
|
||||
--disable=gotype \
|
||||
--disable=gas \
|
||||
|
|
4
Makefile
4
Makefile
|
@ -17,10 +17,10 @@ conmon:
|
|||
make -C $@
|
||||
|
||||
ocid:
|
||||
go build -o ocid ./cmd/server/main.go
|
||||
go build -o ocid ./cmd/server/
|
||||
|
||||
ocic:
|
||||
go build -o ocic ./cmd/client/main.go
|
||||
go build -o ocic ./cmd/client/
|
||||
|
||||
clean:
|
||||
rm -f ocic ocid
|
||||
|
|
266
cmd/client/container.go
Normal file
266
cmd/client/container.go
Normal file
|
@ -0,0 +1,266 @@
|
|||
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
|
||||
}
|
42
cmd/client/image.go
Normal file
42
cmd/client/image.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"golang.org/x/net/context"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
var imageCommand = cli.Command{
|
||||
Name: "image",
|
||||
Subcommands: []cli.Command{
|
||||
pullImageCommand,
|
||||
},
|
||||
}
|
||||
|
||||
var pullImageCommand = cli.Command{
|
||||
Name: "pull",
|
||||
Usage: "pull an image",
|
||||
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.NewImageServiceClient(conn)
|
||||
|
||||
_, err = PullImage(client, context.Args().Get(0))
|
||||
if err != nil {
|
||||
return fmt.Errorf("pulling image failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// PullImage sends a PullImageRequest to the server, and parses
|
||||
// the returned ContainerStatusResponse.
|
||||
func PullImage(client pb.ImageServiceClient, image string) (*pb.PullImageResponse, error) {
|
||||
return client.PullImage(context.Background(), &pb.PullImageRequest{Image: &pb.ImageSpec{Image: &image}})
|
||||
}
|
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
@ -69,175 +68,6 @@ func loadContainerConfig(path string) (*pb.ContainerConfig, error) {
|
|||
return &config, nil
|
||||
}
|
||||
|
||||
// CreatePodSandbox sends a CreatePodSandboxRequest to the server, and parses
|
||||
// the returned CreatePodSandboxResponse.
|
||||
func CreatePodSandbox(client pb.RuntimeServiceClient, path string) error {
|
||||
config, err := loadPodSandboxConfig(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := client.CreatePodSandbox(context.Background(), &pb.CreatePodSandboxRequest{Config: config})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(*r.PodSandboxId)
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopPodSandbox sends a StopPodSandboxRequest to the server, and parses
|
||||
// the returned StopPodSandboxResponse.
|
||||
func StopPodSandbox(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
_, err := client.StopPodSandbox(context.Background(), &pb.StopPodSandboxRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePodSandbox sends a RemovePodSandboxRequest to the server, and parses
|
||||
// the returned RemovePodSandboxResponse.
|
||||
func RemovePodSandbox(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
_, err := client.RemovePodSandbox(context.Background(), &pb.RemovePodSandboxRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PodSandboxStatus sends a PodSandboxStatusRequest to the server, and parses
|
||||
// the returned PodSandboxStatusResponse.
|
||||
func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
r, err := client.PodSandboxStatus(context.Background(), &pb.PodSandboxStatusRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(r)
|
||||
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
|
||||
}
|
||||
|
||||
// Version sends a VersionRequest to the server, and parses the returned VersionResponse.
|
||||
func Version(client pb.RuntimeServiceClient, version string) error {
|
||||
r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("VersionResponse: Version: %s, RuntimeName: %s, RuntimeVersion: %s, RuntimeApiVersion: %s\n", *r.Version, *r.RuntimeName, *r.RuntimeVersion, *r.RuntimeApiVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "ocic"
|
||||
|
@ -248,7 +78,7 @@ func main() {
|
|||
podSandboxCommand,
|
||||
containerCommand,
|
||||
runtimeVersionCommand,
|
||||
pullImageCommand,
|
||||
imageCommand,
|
||||
}
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
|
@ -263,326 +93,3 @@ func main() {
|
|||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// PullImage sends a PullImageRequest to the server, and parses
|
||||
// the returned ContainerStatusResponse.
|
||||
func PullImage(client pb.ImageServiceClient, image string) (*pb.PullImageResponse, error) {
|
||||
return client.PullImage(context.Background(), &pb.PullImageRequest{Image: &pb.ImageSpec{Image: &image}})
|
||||
}
|
||||
|
||||
// try this with ./ocic pullimage docker://busybox
|
||||
var pullImageCommand = cli.Command{
|
||||
Name: "pullimage",
|
||||
Usage: "pull an image",
|
||||
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.NewImageServiceClient(conn)
|
||||
|
||||
_, err = PullImage(client, context.Args().Get(0))
|
||||
if err != nil {
|
||||
return fmt.Errorf("pulling image failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var runtimeVersionCommand = cli.Command{
|
||||
Name: "runtimeversion",
|
||||
Usage: "get runtime version information",
|
||||
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)
|
||||
|
||||
// Test RuntimeServiceClient.Version
|
||||
version := "v1alpha1"
|
||||
err = Version(client, version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Getting the runtime version failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var podSandboxCommand = cli.Command{
|
||||
Name: "pod",
|
||||
Subcommands: []cli.Command{
|
||||
createPodSandboxCommand,
|
||||
stopPodSandboxCommand,
|
||||
removePodSandboxCommand,
|
||||
podSandboxStatusCommand,
|
||||
},
|
||||
}
|
||||
|
||||
var createPodSandboxCommand = cli.Command{
|
||||
Name: "create",
|
||||
Usage: "create a pod",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "config.json",
|
||||
Usage: "the path of a pod sandbox 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)
|
||||
|
||||
// Test RuntimeServiceClient.CreatePodSandbox
|
||||
err = CreatePodSandbox(client, context.String("config"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Creating the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var stopPodSandboxCommand = cli.Command{
|
||||
Name: "stop",
|
||||
Usage: "stop a pod sandbox",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod sandbox",
|
||||
},
|
||||
},
|
||||
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 = StopPodSandbox(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Stopping the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var removePodSandboxCommand = cli.Command{
|
||||
Name: "remove",
|
||||
Usage: "remove a pod sandbox",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod sandbox",
|
||||
},
|
||||
},
|
||||
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 = RemovePodSandbox(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("removing the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var podSandboxStatusCommand = cli.Command{
|
||||
Name: "status",
|
||||
Usage: "return the status of a pod",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod",
|
||||
},
|
||||
},
|
||||
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 = PodSandboxStatus(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting the pod sandbox status failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
}
|
||||
|
|
186
cmd/client/sandbox.go
Normal file
186
cmd/client/sandbox.go
Normal file
|
@ -0,0 +1,186 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"golang.org/x/net/context"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
var podSandboxCommand = cli.Command{
|
||||
Name: "pod",
|
||||
Subcommands: []cli.Command{
|
||||
createPodSandboxCommand,
|
||||
stopPodSandboxCommand,
|
||||
removePodSandboxCommand,
|
||||
podSandboxStatusCommand,
|
||||
},
|
||||
}
|
||||
|
||||
var createPodSandboxCommand = cli.Command{
|
||||
Name: "create",
|
||||
Usage: "create a pod",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "config.json",
|
||||
Usage: "the path of a pod sandbox 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)
|
||||
|
||||
// Test RuntimeServiceClient.CreatePodSandbox
|
||||
err = CreatePodSandbox(client, context.String("config"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Creating the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var stopPodSandboxCommand = cli.Command{
|
||||
Name: "stop",
|
||||
Usage: "stop a pod sandbox",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod sandbox",
|
||||
},
|
||||
},
|
||||
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 = StopPodSandbox(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Stopping the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var removePodSandboxCommand = cli.Command{
|
||||
Name: "remove",
|
||||
Usage: "remove a pod sandbox",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod sandbox",
|
||||
},
|
||||
},
|
||||
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 = RemovePodSandbox(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("removing the pod sandbox failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var podSandboxStatusCommand = cli.Command{
|
||||
Name: "status",
|
||||
Usage: "return the status of a pod",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "id of the pod",
|
||||
},
|
||||
},
|
||||
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 = PodSandboxStatus(client, context.String("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting the pod sandbox status failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// CreatePodSandbox sends a CreatePodSandboxRequest to the server, and parses
|
||||
// the returned CreatePodSandboxResponse.
|
||||
func CreatePodSandbox(client pb.RuntimeServiceClient, path string) error {
|
||||
config, err := loadPodSandboxConfig(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := client.CreatePodSandbox(context.Background(), &pb.CreatePodSandboxRequest{Config: config})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(*r.PodSandboxId)
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopPodSandbox sends a StopPodSandboxRequest to the server, and parses
|
||||
// the returned StopPodSandboxResponse.
|
||||
func StopPodSandbox(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
_, err := client.StopPodSandbox(context.Background(), &pb.StopPodSandboxRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePodSandbox sends a RemovePodSandboxRequest to the server, and parses
|
||||
// the returned RemovePodSandboxResponse.
|
||||
func RemovePodSandbox(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
_, err := client.RemovePodSandbox(context.Background(), &pb.RemovePodSandboxRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PodSandboxStatus sends a PodSandboxStatusRequest to the server, and parses
|
||||
// the returned PodSandboxStatusResponse.
|
||||
func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error {
|
||||
if ID == "" {
|
||||
return fmt.Errorf("ID cannot be empty")
|
||||
}
|
||||
r, err := client.PodSandboxStatus(context.Background(), &pb.PodSandboxStatusRequest{PodSandboxId: &ID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(r)
|
||||
return nil
|
||||
}
|
41
cmd/client/system.go
Normal file
41
cmd/client/system.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"golang.org/x/net/context"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
var runtimeVersionCommand = cli.Command{
|
||||
Name: "runtimeversion",
|
||||
Usage: "get runtime version information",
|
||||
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)
|
||||
|
||||
// Test RuntimeServiceClient.Version
|
||||
version := "v1alpha1"
|
||||
err = Version(client, version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Getting the runtime version failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// Version sends a VersionRequest to the server, and parses the returned VersionResponse.
|
||||
func Version(client pb.RuntimeServiceClient, version string) error {
|
||||
r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("VersionResponse: Version: %s, RuntimeName: %s, RuntimeVersion: %s, RuntimeApiVersion: %s\n", *r.Version, *r.RuntimeName, *r.RuntimeVersion, *r.RuntimeApiVersion)
|
||||
return nil
|
||||
}
|
Loading…
Reference in a new issue