Skip to content

Commit

Permalink
fix the empty label
Browse files Browse the repository at this point in the history
Signed-off-by: JoJo <1043708974@qq.com>
  • Loading branch information
Flsorescence authored and fuweid committed May 6, 2019
1 parent 461d7d9 commit 2146adc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
19 changes: 8 additions & 11 deletions apis/opts/labels.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package opts

import (
"fmt"
"strings"
)

// ParseLabels parses the labels params of container.
func ParseLabels(labels []string) (map[string]string, error) {
func ParseLabels(labels []string) map[string]string {
results := make(map[string]string)
for _, label := range labels {
fields, err := parseLabel(label)
if err != nil {
return nil, err
}
fields := parseLabel(label)
k, v := fields[0], fields[1]
results[k] = v
}
return results, nil
return results
}

func parseLabel(label string) ([]string, error) {
func parseLabel(label string) []string {
fields := strings.SplitN(label, "=", 2)
if len(fields) != 2 {
return nil, fmt.Errorf("invalid label %s: label must be in format of key=value", label)
// Only input key without value
if len(fields) == 1 {
fields = append(fields, "")
}
return fields, nil
return fields
}
45 changes: 35 additions & 10 deletions apis/opts/labels_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package opts

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,8 +9,8 @@ import (
func TestParseLabels(t *testing.T) {
type result struct {
labels map[string]string
err error
}

type TestCase struct {
input []string
expected result
Expand All @@ -24,7 +23,6 @@ func TestParseLabels(t *testing.T) {
labels: map[string]string{
"a": "b",
},
err: nil,
},
},
{
Expand All @@ -33,7 +31,6 @@ func TestParseLabels(t *testing.T) {
labels: map[string]string{
"a": "b",
},
err: nil,
},
},
{
Expand All @@ -43,21 +40,49 @@ func TestParseLabels(t *testing.T) {
labels: map[string]string{
"a": "bb",
},
err: nil,
},
},
// only input key
{
input: []string{"a"},
expected: result{
labels: map[string]string{
"a": "",
},
},
},
{
input: []string{"a="},
expected: result{
labels: map[string]string{
"a": "",
},
},
},
{
input: []string{"a=b=c"},
expected: result{
labels: map[string]string{
"a": "b=c",
},
},
},
{
input: []string{},
expected: result{
labels: map[string]string{},
},
},
{
input: []string{"ThisIsALableWithoutEqualMark"},
input: nil,
expected: result{
labels: nil,
err: fmt.Errorf("invalid label ThisIsALableWithoutEqualMark: label must be in format of key=value"),
labels: map[string]string{},
},
},
}

for _, testCase := range testCases {
labels, err := ParseLabels(testCase.input)
assert.Equal(t, testCase.expected.err, err)
labels := ParseLabels(testCase.input)
assert.Equal(t, testCase.expected.labels, labels)
}
}
5 changes: 1 addition & 4 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ type container struct {
}

func (c *container) config() (*types.ContainerCreateConfig, error) {
labels, err := opts.ParseLabels(c.labels)
if err != nil {
return nil, err
}
labels := opts.ParseLabels(c.labels)

memory, err := opts.ParseMemory(c.memory)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,10 +1155,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
// but ContainerConfig.Labels is map[string]string
if len(config.Label) != 0 {
// support remove some labels
newLabels, err := opts.ParseLabels(config.Label)
if err != nil {
return errors.Wrapf(err, "failed to parse labels")
}
newLabels := opts.ParseLabels(config.Label)

for k, v := range newLabels {
if v == "" {
Expand Down

0 comments on commit 2146adc

Please sign in to comment.