Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tighten label parsing #6674

Merged
merged 1 commit into from Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions pkg/kubectl/generate.go
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/golang/glog"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -81,19 +80,18 @@ func MakeLabels(labels map[string]string) string {
}

// ParseLabels turns a string representation of a label set into a map[string]string
func ParseLabels(labelString string) map[string]string {
func ParseLabels(labelString string) (map[string]string, error) {
if len(labelString) == 0 {
return nil
return nil, fmt.Errorf("no label spec passed")
}
labels := map[string]string{}
labelSpecs := strings.Split(labelString, ",")
for ix := range labelSpecs {
labelSpec := strings.Split(labelSpecs[ix], "=")
if len(labelSpec) != 2 {
glog.Errorf("unexpected label spec: %s", labelSpecs[ix])
continue
return nil, fmt.Errorf("unexpected label spec: %s", labelSpecs[ix])
}
labels[labelSpec[0]] = labelSpec[1]
}
return labels
return labels, nil
}
6 changes: 5 additions & 1 deletion pkg/kubectl/run.go
Expand Up @@ -39,8 +39,12 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
// TODO: extract this flag to a central location.
labelString, found := params["labels"]
var labels map[string]string
var err error
if found && len(labelString) > 0 {
labels = ParseLabels(labelString)
labels, err = ParseLabels(labelString)
if err != nil {
return nil, err
}
} else {
labels = map[string]string{
"run-container": params["name"],
Expand Down
10 changes: 8 additions & 2 deletions pkg/kubectl/service.go
Expand Up @@ -45,12 +45,18 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
if !found || len(selectorString) == 0 {
return nil, fmt.Errorf("'selector' is a required parameter.")
}
selector := ParseLabels(selectorString)
selector, err := ParseLabels(selectorString)
if err != nil {
return nil, err
}

labelsString, found := params["labels"]
var labels map[string]string
if found && len(labelsString) > 0 {
labels = ParseLabels(labelsString)
labels, err = ParseLabels(labelsString)
if err != nil {
return nil, err
}
}

name, found := params["name"]
Expand Down