From 6efda0959f7b56e14d933eebe174ea3bfe0a14f7 Mon Sep 17 00:00:00 2001 From: Dmitry Chepurovskiy Date: Sun, 2 Aug 2020 19:27:57 +0300 Subject: [PATCH] Improve label parsing code Signed-off-by: Dmitry Chepurovskiy --- pkg/action/action.go | 16 ++++++++++++++++ pkg/action/action_test.go | 17 +++++++++++++++++ pkg/action/install.go | 7 ++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index 3c47cc6df..ec575ed49 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -417,3 +417,19 @@ func (c *Configuration) Init(getter genericclioptions.RESTClientGetter, namespac return nil } + +func parseLabels(labels string) (map[string]string, error) { + if len(labels) == 0 { + return map[string]string{}, nil + } + + labelsMap := make(map[string]string) + for _, label := range strings.Split(labels, ",") { + parts := strings.Split(label, "=") + if len(parts) != 2 { + return nil, fmt.Errorf("Can't parse label: %s", label) + } + labelsMap[parts[0]] = parts[1] + } + return labelsMap, nil +} diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 0cbdb162b..36a3ae543 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -353,3 +353,20 @@ func TestValidName(t *testing.T) { } } } + +func TestParseLabels(t *testing.T) { + names := map[string]bool{ + "": true, + "foo": false, + "foo=bar": true, + "foo=bar1,foo2=bar": true, + "foo=bar1=bar2": false, + } + for input, expectPass := range names { + _, err := parseLabels(input) + passed := err == nil + if passed != expectPass { + t.Errorf("Expected %q to %s", input, st) + } + } +} diff --git a/pkg/action/install.go b/pkg/action/install.go index 55e7db960..ebdb8817d 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -236,11 +236,8 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. rel := i.createRelease(chrt, vals) - // FIXME: Rework me - rel.Labels = make(map[string]string) - for _, label := range strings.Split(i.Labels, ",") { - parts := strings.Split(label, "=") - rel.Labels[parts[0]] = parts[1] + if rel.Labels, err = parseLabels(i.Labels); err != nil { + return nil, err } var manifestDoc *bytes.Buffer