Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1149,10 +1149,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