Add support for adding labels at ctr create time

Signed-off-by: Mrunal Patel <mpatel@redhat.com>
This commit is contained in:
Mrunal Patel 2016-10-14 12:05:47 -07:00
parent 24bb1523cf
commit 52dc8e4865

View file

@ -24,6 +24,17 @@ var containerCommand = cli.Command{
}, },
} }
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
}
var createContainerCommand = cli.Command{ var createContainerCommand = cli.Command{
Name: "create", Name: "create",
Usage: "create a container", Usage: "create a container",
@ -42,6 +53,10 @@ var createContainerCommand = cli.Command{
Value: "", Value: "",
Usage: "the name of the container", Usage: "the name of the container",
}, },
cli.StringSliceFlag{
Name: "label",
Usage: "add key=value labels to the container",
},
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
// Set up a connection to the server. // Set up a connection to the server.
@ -55,8 +70,24 @@ var createContainerCommand = cli.Command{
if !context.IsSet("pod") { if !context.IsSet("pod") {
return fmt.Errorf("Please specify the id of the pod sandbox to which the container belongs via the --pod option") return fmt.Errorf("Please specify the id of the pod sandbox to which the container belongs via the --pod option")
} }
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]
}
// Test RuntimeServiceClient.CreateContainer // Test RuntimeServiceClient.CreateContainer
err = CreateContainer(client, context.String("pod"), context.String("config"), context.String("name")) err = CreateContainer(client, opts)
if err != nil { if err != nil {
return fmt.Errorf("Creating container failed: %v", err) return fmt.Errorf("Creating container failed: %v", err)
} }
@ -247,19 +278,23 @@ var listContainersCommand = cli.Command{
// CreateContainer sends a CreateContainerRequest to the server, and parses // CreateContainer sends a CreateContainerRequest to the server, and parses
// the returned CreateContainerResponse. // the returned CreateContainerResponse.
func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string, name string) error { func CreateContainer(client pb.RuntimeServiceClient, opts createOptions) error {
config, err := loadContainerConfig(path) config, err := loadContainerConfig(opts.configPath)
if err != nil { if err != nil {
return err return err
} }
// Override the name by the one specified through CLI // Override the name by the one specified through CLI
if name != "" { if opts.name != "" {
config.Metadata.Name = &name config.Metadata.Name = &opts.name
}
for k, v := range opts.labels {
config.Labels[k] = v
} }
r, err := client.CreateContainer(context.Background(), &pb.CreateContainerRequest{ r, err := client.CreateContainer(context.Background(), &pb.CreateContainerRequest{
PodSandboxId: &sandbox, PodSandboxId: &opts.podID,
Config: config, Config: config,
}) })
if err != nil { if err != nil {