From 1694591f6c8ebd3936845c5f4c0bf2e0aa038963 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 17 Oct 2016 12:33:48 -0700 Subject: [PATCH] Add support for adding labels at pod creation time Signed-off-by: Mrunal Patel --- cmd/client/sandbox.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd/client/sandbox.go b/cmd/client/sandbox.go index 96a53e42..7722a2b7 100644 --- a/cmd/client/sandbox.go +++ b/cmd/client/sandbox.go @@ -36,6 +36,10 @@ var runPodSandboxCommand = cli.Command{ Value: "", Usage: "the name of the pod sandbox", }, + 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. @@ -46,8 +50,22 @@ var runPodSandboxCommand = cli.Command{ defer conn.Close() client := pb.NewRuntimeServiceClient(conn) + opts := createOptions{ + configPath: context.String("config"), + name: context.String("name"), + 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.RunPodSandbox - err = RunPodSandbox(client, context.String("config"), context.String("name")) + err = RunPodSandbox(client, opts) if err != nil { return fmt.Errorf("Creating the pod sandbox failed: %v", err) } @@ -193,15 +211,19 @@ var listPodSandboxCommand = cli.Command{ // RunPodSandbox sends a RunPodSandboxRequest to the server, and parses // the returned RunPodSandboxResponse. -func RunPodSandbox(client pb.RuntimeServiceClient, path string, name string) error { - config, err := loadPodSandboxConfig(path) +func RunPodSandbox(client pb.RuntimeServiceClient, opts createOptions) error { + config, err := loadPodSandboxConfig(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.RunPodSandbox(context.Background(), &pb.RunPodSandboxRequest{Config: config})