Implement CreateContainer

Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
Haiyan Meng 2016-08-01 13:39:42 -04:00 committed by Mrunal Patel
parent e3a34aa26d
commit c2ee13d187
8 changed files with 567 additions and 12 deletions

View file

@ -31,14 +31,22 @@ func getClientConnection() (*grpc.ClientConn, error) {
return conn, nil
}
func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, error) {
func openFile(path string) (*os.File, error) {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("pod sandbox config at %s not found", path)
return nil, fmt.Errorf("config at %s not found", path)
}
return nil, err
}
return f, nil
}
func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, error) {
f, err := openFile(path)
if err != nil {
return nil, err
}
defer f.Close()
var config pb.PodSandboxConfig
@ -48,6 +56,20 @@ func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, error) {
return &config, nil
}
func loadContainerConfig(path string) (*pb.ContainerConfig, error) {
f, err := openFile(path)
if err != nil {
return nil, err
}
defer f.Close()
var config pb.ContainerConfig
if err := json.NewDecoder(f).Decode(&config); err != nil {
return nil, err
}
return &config, nil
}
// CreatePodSandbox sends a CreatePodSandboxRequest to the server, and parses
// the returned CreatePodSandboxResponse.
func CreatePodSandbox(client pb.RuntimeServiceClient, path string) error {
@ -64,6 +86,25 @@ func CreatePodSandbox(client pb.RuntimeServiceClient, path string) error {
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)
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})
@ -82,6 +123,7 @@ func main() {
app.Commands = []cli.Command{
runtimeVersionCommand,
createPodSandboxCommand,
createContainerCommand,
pullImageCommand,
}
@ -168,3 +210,38 @@ var createPodSandboxCommand = cli.Command{
return nil
},
}
var createContainerCommand = cli.Command{
Name: "createcontainer",
Usage: "create a container",
Flags: []cli.Flag{
cli.StringFlag{
Name: "sandbox",
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()
if err != nil {
return fmt.Errorf("Failed to connect: %v", err)
}
defer conn.Close()
client := pb.NewRuntimeServiceClient(conn)
if !context.IsSet("sandbox") {
return fmt.Errorf("Please specify the id of the pod sandbox to which the container belongs via the --sandbox option")
}
// Test RuntimeServiceClient.CreateContainer
err = CreateContainer(client, context.String("sandbox"), context.String("config"))
if err != nil {
return fmt.Errorf("Creating the pod sandbox failed: %v", err)
}
return nil
},
}

View file

@ -1,54 +0,0 @@
{
"name": "podsandbox1",
"hostname": "ocic_host",
"log_directory": ".",
"dns_options": {
"servers": [
"server1.redhat.com",
"server2.redhat.com"
],
"searches": [
"8.8.8.8"
]
},
"port_mappings": [
{
"name": "port_map1",
"protocol": 1,
"container_port": 80,
"host_port": 4888,
"host_ip": "192.168.0.33"
},
{
"name": "port_map2",
"protocol": 2,
"container_port": 81,
"host_port": 4889,
"host_ip": "192.168.0.33"
}
],
"resources": {
"cpu": {
"limits": 3,
"requests": 2
},
"memory": {
"limits": 50000000,
"requests": 2000000
}
},
"labels": {
"group": "test"
},
"annotations": {
"owner": "hmeng"
},
"linux": {
"cgroup_parent": "/sys/fs/cgroup/test",
"namespace_options": {
"host_network": true,
"host_pid": true,
"host_ipc": true
}
}
}