From 3f8604e54902218634318a8ac76b9c8ea89683b9 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Fri, 14 Sep 2018 11:01:55 +0800 Subject: [PATCH 1/2] climc: k8s-deployment-create support labels --- pkg/mcclient/options/k8s/deployment.go | 36 +++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/mcclient/options/k8s/deployment.go b/pkg/mcclient/options/k8s/deployment.go index 7d69632601..d68813f4bc 100644 --- a/pkg/mcclient/options/k8s/deployment.go +++ b/pkg/mcclient/options/k8s/deployment.go @@ -17,7 +17,7 @@ type DeploymentCreateOptions struct { Image string `help:"The image for the container to run"` Replicas int64 `help:"Number of replicas for pods in this deployment"` RunAsPrivileged bool `help:"Whether to run the container as privileged user"` - Labels string `help:"Comma separated labels to apply to the pod(s)"` + Labels string `help:"Comma separated labels to apply to the pod(s), e.g. --labels='app=hazelcast,env=prod'"` Env []string `help:"Environment variables to set in container"` Port []string `help:"Port for the service that is created, format is :: e.g. tcp:80:3000"` Net string `help:"Network config, e.g. net1, net1:10.168.222.171"` @@ -62,6 +62,13 @@ func (o DeploymentCreateOptions) Params() (*jsonutils.JSONDict, error) { } params.Add(net, "networkConfig") } + if o.Labels != "" { + labels, err := parseLabels(o.Labels) + if err != nil { + return nil, err + } + params.Add(labels, "labels") + } return params, nil } @@ -145,3 +152,30 @@ func (o DeploymentCreateFromFileOptions) Params() (*jsonutils.JSONDict, error) { params.Add(jsonutils.NewString(string(content)), "content") return params, nil } + +func parseLabels(labelStr string) (*jsonutils.JSONArray, error) { + labelsStrs := strings.Split(labelStr, ",") + ret := jsonutils.NewArray() + if len(labelsStrs) == 0 { + return ret, nil + } + for _, str := range labelsStrs { + label, err := parseLabel(str) + if err != nil { + return nil, err + } + ret.Add(label) + } + return ret, nil +} + +func parseLabel(str string) (jsonutils.JSONObject, error) { + parts := strings.Split(str, "=") + if len(parts) != 2 { + return nil, fmt.Errorf("Invalid label string: %s", str) + } + label := jsonutils.NewDict() + label.Add(jsonutils.NewString(parts[0]), "key") + label.Add(jsonutils.NewString(parts[1]), "value") + return label, nil +} From a70fee75301c9c71cc4aed7db1faa3e42f9cf183 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Fri, 14 Sep 2018 11:34:09 +0800 Subject: [PATCH 2/2] climc: add name filter to k8s list resource --- pkg/mcclient/options/k8s/base.go | 8 ++++++-- pkg/mcclient/options/k8s/deployment.go | 26 ++++++-------------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/pkg/mcclient/options/k8s/base.go b/pkg/mcclient/options/k8s/base.go index 384c027251..4a45cc869c 100644 --- a/pkg/mcclient/options/k8s/base.go +++ b/pkg/mcclient/options/k8s/base.go @@ -15,8 +15,9 @@ func (o ClusterBaseOptions) Params() *jsonutils.JSONDict { } type BaseListOptions struct { - Limit int `default:"20" help:"Page limit"` - Offset int `default:"0" help:"page offset"` + Limit int `default:"20" help:"Page limit"` + Offset int `default:"0" help:"Page offset"` + Name string `help:"Search by name"` } func (o BaseListOptions) Params() *jsonutils.JSONDict { @@ -27,6 +28,9 @@ func (o BaseListOptions) Params() *jsonutils.JSONDict { if o.Offset > 0 { params.Add(jsonutils.NewInt(int64(o.Offset)), "offset") } + if o.Name != "" { + params.Add(jsonutils.NewString(o.Name), "name") + } return params } diff --git a/pkg/mcclient/options/k8s/deployment.go b/pkg/mcclient/options/k8s/deployment.go index d68813f4bc..4f9f1fe8ef 100644 --- a/pkg/mcclient/options/k8s/deployment.go +++ b/pkg/mcclient/options/k8s/deployment.go @@ -17,7 +17,7 @@ type DeploymentCreateOptions struct { Image string `help:"The image for the container to run"` Replicas int64 `help:"Number of replicas for pods in this deployment"` RunAsPrivileged bool `help:"Whether to run the container as privileged user"` - Labels string `help:"Comma separated labels to apply to the pod(s), e.g. --labels='app=hazelcast,env=prod'"` + Label []string `help:"Labels to apply to the pod(s), e.g. 'env=prod'"` Env []string `help:"Environment variables to set in container"` Port []string `help:"Port for the service that is created, format is :: e.g. tcp:80:3000"` Net string `help:"Network config, e.g. net1, net1:10.168.222.171"` @@ -62,13 +62,15 @@ func (o DeploymentCreateOptions) Params() (*jsonutils.JSONDict, error) { } params.Add(net, "networkConfig") } - if o.Labels != "" { - labels, err := parseLabels(o.Labels) + labels := jsonutils.NewArray() + for _, label := range o.Label { + label, err := parseLabel(label) if err != nil { return nil, err } - params.Add(labels, "labels") + labels.Add(label) } + params.Add(labels, "labels") return params, nil } @@ -153,22 +155,6 @@ func (o DeploymentCreateFromFileOptions) Params() (*jsonutils.JSONDict, error) { return params, nil } -func parseLabels(labelStr string) (*jsonutils.JSONArray, error) { - labelsStrs := strings.Split(labelStr, ",") - ret := jsonutils.NewArray() - if len(labelsStrs) == 0 { - return ret, nil - } - for _, str := range labelsStrs { - label, err := parseLabel(str) - if err != nil { - return nil, err - } - ret.Add(label) - } - return ret, nil -} - func parseLabel(str string) (jsonutils.JSONObject, error) { parts := strings.Split(str, "=") if len(parts) != 2 {