Change:
<!-- This form is for bug reports and feature requests ONLY! If you're looking for help check and the . --Is this a BUG REPORT or FEATURE REQUEST?: /kind bug What happened: construct a string for labels, and let consume it. For example, suppose label string is , then this function will return a map which indicates a label . construct a string for portProtocols, and let consume it. For example, suppose portProtocols string is , then this function will return a map which indicates a pair . What you expected to happen: I assume the key of a label should not be an empty string since it makes no sense. I assume neither the nor the of pair should not be an empty string since it makes no sense. How to reproduce it (as minimally and precisely as possible): Anything else we need to know?: Environment: Kubernetes version (use ): Cloud provider or hardware configuration**: OS (e.g. from /etc/os-release): Kernel (e.g. ): Install tools: Others: /area kubectl
If it's a bug, I can help fix it.
There are no sig labels on this issue. Please by: a sig: e.g., to notify the contributor experience sig, OR the label manually: e.g., to apply the label Note: Method 1 will trigger an email to the group. You can find the group list and label list . The in the method 1 has to be replaced with one of these: bugs, feature-requests, pr-reviews, test-failures, proposals
/sig cli

Discussion:
if len(portProtocol) != 2 { return nil, fmt.Errorf("unexpected port protocol mapping: %s", protocolsSlice[ix]) } <ins> if len(portProtocol[0]) == 0 { return nil, fmt.Errorf("unexpected empty port") } if len(portProtocol[1]) == 0 { return nil, fmt.Errorf("unexpected empty protocol") } </ins> portProtocolMap[portProtocol[0]] = portProtocol[1] } return portProtocolMap, nil

if len(labelSpec) != 2 { return nil, fmt.Errorf("unexpected label spec: %s", labelSpecs[ix]) } <ins> if len(labelSpec[0]) == 0 { return nil, fmt.Errorf("unexpected empty label key") } </ins> labels[labelSpec[0]] = labelSpec[1] } return labels, nil

t.Errorf("nexpected:n%vnsaw:n%v", expected, params) } } <ins> func TestGetBool(t *testing.T) { testCases := []struct { name         string parameters   map[string]string key          string defaultValue bool expected     bool expectError  bool }{ { name: "found key in parameters, default value is different from key value", parameters: map[string]string{ "foo": "false", }, key:          "foo", defaultValue: false, expected:     false, expectError:  false, }, { name: "found key in parameters, default value is same with key value", parameters: map[string]string{ "foo": "true", }, key:          "foo", defaultValue: true, expected:     true, expectError:  false, }, { name: "key not found in parameters, default value is true", parameters: map[string]string{ "foo": "true", "far": "false", }, key:          "bar", defaultValue: true, expected:     true, expectError:  false, }, { name: "key not found in parameters, default value is false", parameters: map[string]string{ "foo": "true", "far": "false", }, key:          "bar", defaultValue: false, expected:     false, expectError:  false, }, { name:         "parameters is empty", parameters:   map[string]string{}, key:          "foo", defaultValue: true, expected:     true, expectError:  false, }, { name: "parameters key is not a valid bool value", parameters: map[string]string{ "foo": "error", }, key:          "foo", defaultValue: true, expected:     false, expectError:  true, }, } for _, test := range testCases { got, err := GetBool(test.parameters, test.key, test.defaultValue) if err != nil && test.expectError == false { t.Errorf("%s: unexpected error: %v", test.name, err) } if err == nil && test.expectError == true { t.Errorf("%s: expect error, got nil", test.name) } if got != test.expected { t.Errorf("%s: expect %v, got %v", test.name, test.expected, got) } } } func TestMakeParseLabels(t *testing.T) { successCases := []struct { labels   map[string]string expected map[string]string }{ { labels: map[string]string{ "foo": "false", }, expected: map[string]string{ "foo": "false", }, }, { labels: map[string]string{ "foo": "true", "bar": "123", }, expected: map[string]string{ "foo": "true", "bar": "123", }, }, } for _, test := range successCases { labelString := MakeLabels(test.labels) got, err := ParseLabels(labelString) if err != nil { t.Errorf("unexpected error :%v", err) } if !reflect.DeepEqual(test.expected, got) { t.Errorf("nexpected:n%vngot:n%v", test.expected, got) } } errorCases := []struct { name   string labels interface{} }{ { name:   "non-string", labels: 123, }, { name:   "empty string", labels: "", }, { name:   "error format", labels: "abc=456;bcd=789", }, { name:   "error format", labels: "abc=456.bcd=789", }, { name:   "error format", labels: "abc,789", }, { name:   "error format", labels: "abc", }, { name:   "error format", labels: "=abc", }, } for _, test := range errorCases { _, err := ParseLabels(test.labels) if err == nil { t.Errorf("labels %s expect error, reason: %s, got nil", test.labels, test.name) } } } func TestMakeParseProtocols(t *testing.T) { successCases := []struct { protocols map[string]string expected  map[string]string }{ { protocols: map[string]string{ "101": "TCP", }, expected: map[string]string{ "101": "TCP", }, }, { protocols: map[string]string{ "102": "UDP", "101": "TCP", }, expected: map[string]string{ "102": "UDP", "101": "TCP", }, }, } for _, test := range successCases { protocolString := MakeProtocols(test.protocols) got, err := ParseProtocols(protocolString) if err != nil { t.Errorf("unexpected error :%v", err) } if !reflect.DeepEqual(test.expected, got) { t.Errorf("nexpected:n%vngot:n%v", test.expected, got) } } errorCases := []struct { name      string protocols interface{} }{ { name:      "non-string", protocols: 123, }, { name:      "empty string", protocols: "", }, { name:      "error format", protocols: "123/TCP;456/UDP", }, { name:      "error format", protocols: "123/TCP.456/UDP", }, { name:      "error format", protocols: "123=456", }, { name:      "error format", protocols: "123", }, { name:      "error format", protocols: "123=", }, { name:      "error format", protocols: "=TCP", }, } for _, test := range errorCases { _, err := ParseProtocols(test.protocols) if err == nil { t.Errorf("protocols %s expect error, reason: %s, got nil", test.protocols, test.name) } } } </ins>