Add first version of createPodSandbox

Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
Haiyan Meng 2016-07-19 21:30:05 -04:00 committed by Mrunal Patel
parent 0766dfecfe
commit 819d76b6fd
7 changed files with 361 additions and 23 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"fmt"
"log"
"net"
@ -30,6 +31,39 @@ func getClientConnection() (*grpc.ClientConn, error) {
return conn, nil
}
func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, 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, err
}
defer f.Close()
var config pb.PodSandboxConfig
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 {
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)
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})
@ -47,6 +81,7 @@ func main() {
app.Commands = []cli.Command{
runtimeVersionCommand,
createPodSandboxCommand,
pullImageCommand,
}
@ -105,3 +140,31 @@ var runtimeVersionCommand = cli.Command{
return nil
},
}
var createPodSandboxCommand = cli.Command{
Name: "createpodsandbox",
Usage: "create a pod sandbox",
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()
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
},
}

View file

@ -0,0 +1,54 @@
{
"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": 50000000,
"requests": 20000000
},
"memory": {
"limits": 500000000000,
"requests": 200000000000
}
},
"lables": {
"group": "test"
},
"annotations": {
"owner": "hmeng"
},
"linux": {
"cgroup_parent": "/sys/fs/cgroup/test",
"namespace_options": {
"host_network": true,
"host_pid": true,
"host_ipc": true
}
}
}