From 52dc8e48650594f51ac143d5d38c0a63ef7c36a8 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Fri, 14 Oct 2016 12:05:47 -0700 Subject: [PATCH 1/2] Add support for adding labels at ctr create time Signed-off-by: Mrunal Patel --- cmd/client/container.go | 47 +++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/cmd/client/container.go b/cmd/client/container.go index 3e833bc1..ac10c570 100644 --- a/cmd/client/container.go +++ b/cmd/client/container.go @@ -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{ Name: "create", Usage: "create a container", @@ -42,6 +53,10 @@ var createContainerCommand = cli.Command{ Value: "", Usage: "the name of the container", }, + cli.StringSliceFlag{ + Name: "label", + Usage: "add key=value labels to the container", + }, }, Action: func(context *cli.Context) error { // Set up a connection to the server. @@ -55,8 +70,24 @@ var createContainerCommand = cli.Command{ if !context.IsSet("pod") { 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 - err = CreateContainer(client, context.String("pod"), context.String("config"), context.String("name")) + err = CreateContainer(client, opts) if err != nil { 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 // the returned CreateContainerResponse. -func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string, name string) error { - config, err := loadContainerConfig(path) +func CreateContainer(client pb.RuntimeServiceClient, opts createOptions) error { + config, err := loadContainerConfig(opts.configPath) if err != nil { return err } // Override the name by the one specified through CLI - if name != "" { - config.Metadata.Name = &name + if opts.name != "" { + config.Metadata.Name = &opts.name + } + + for k, v := range opts.labels { + config.Labels[k] = v } r, err := client.CreateContainer(context.Background(), &pb.CreateContainerRequest{ - PodSandboxId: &sandbox, + PodSandboxId: &opts.podID, Config: config, }) if err != nil { From e1a4b71478776a0496e3715c07b799a719788270 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Fri, 14 Oct 2016 12:06:10 -0700 Subject: [PATCH 2/2] Add more tests for label filtering Signed-off-by: Mrunal Patel --- test/ctr.bats | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/ctr.bats b/test/ctr.bats index 3f80c0a2..f6101988 100644 --- a/test/ctr.bats +++ b/test/ctr.bats @@ -251,11 +251,19 @@ function teardown() { echo "$output" [ "$status" -eq 0 ] pod_id="$output" - run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id" + run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id" --name ctr1 --label "a=b" --label "c=d" --label "e=f" echo "$output" [ "$status" -eq 0 ] ctr1_id="$output" - run ocic ctr list --label "tier=backend" --quiet + run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id" --name ctr2 --label "a=b" --label "c=d" + echo "$output" + [ "$status" -eq 0 ] + ctr2_id="$output" + run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id" --name ctr3 --label "a=b" + echo "$output" + [ "$status" -eq 0 ] + ctr3_id="$output" + run ocic ctr list --label "tier=backend" --label "a=b" --label "c=d" --label "e=f" --quiet echo "$output" [ "$status" -eq 0 ] [[ "$output" != "" ]] @@ -264,6 +272,19 @@ function teardown() { echo "$output" [ "$status" -eq 0 ] [[ "$output" == "" ]] + run ocic ctr list --label "a=b" --label "c=d" --quiet + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" != "" ]] + [[ "$output" =~ "$ctr1_id" ]] + [[ "$output" =~ "$ctr2_id" ]] + run ocic ctr list --label "a=b" --quiet + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" != "" ]] + [[ "$output" =~ "$ctr1_id" ]] + [[ "$output" =~ "$ctr2_id" ]] + [[ "$output" =~ "$ctr3_id" ]] run ocic pod remove --id "$pod_id" echo "$output" [ "$status" -eq 0 ]