Merge pull request #46760 from xilabao/fix-parse-pairs
Automatic merge from submit-queue (batch tested with PRs 49524, 46760, 50206, 50166, 49603) fix parse pairs **What this PR does / why we need it**: add check to `-` ``` # kubectl label pod foo - error: at least one label update is required See 'kubectl label -h' for help and examples. ``` **Which issue this PR fixes**: **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
		@@ -156,6 +156,18 @@ func TestParseAnnotations(t *testing.T) {
 | 
			
		||||
			scenario:    "incorrect annotation input (missing =value)",
 | 
			
		||||
			expectErr:   true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			annotations: []string{"-"},
 | 
			
		||||
			expectedErr: "invalid annotation format: -",
 | 
			
		||||
			scenario:    "incorrect annotation input (missing key)",
 | 
			
		||||
			expectErr:   true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			annotations: []string{"=bar"},
 | 
			
		||||
			expectedErr: "invalid annotation format: =bar",
 | 
			
		||||
			scenario:    "incorrect annotation input (missing key)",
 | 
			
		||||
			expectErr:   true,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		annotations, remove, err := parseAnnotations(test.annotations)
 | 
			
		||||
@@ -380,6 +392,18 @@ func TestAnnotateErrors(t *testing.T) {
 | 
			
		||||
				return strings.Contains(err.Error(), "at least one annotation update is required")
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		"wrong annotations": {
 | 
			
		||||
			args: []string{"pods", "-"},
 | 
			
		||||
			errFn: func(err error) bool {
 | 
			
		||||
				return strings.Contains(err.Error(), "at least one annotation update is required")
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		"wrong annotations 2": {
 | 
			
		||||
			args: []string{"pods", "=bar"},
 | 
			
		||||
			errFn: func(err error) bool {
 | 
			
		||||
				return strings.Contains(err.Error(), "at least one annotation update is required")
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		"no resources remove annotations": {
 | 
			
		||||
			args:  []string{"pods-"},
 | 
			
		||||
			errFn: func(err error) bool { return strings.Contains(err.Error(), "one or more resources must be specified") },
 | 
			
		||||
 
 | 
			
		||||
@@ -289,6 +289,14 @@ func TestLabelErrors(t *testing.T) {
 | 
			
		||||
			args:  []string{"pods"},
 | 
			
		||||
			errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
 | 
			
		||||
		},
 | 
			
		||||
		"wrong labels": {
 | 
			
		||||
			args:  []string{"pods", "-"},
 | 
			
		||||
			errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
 | 
			
		||||
		},
 | 
			
		||||
		"wrong labels 2": {
 | 
			
		||||
			args:  []string{"pods", "=bar"},
 | 
			
		||||
			errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
 | 
			
		||||
		},
 | 
			
		||||
		"no resources": {
 | 
			
		||||
			args:  []string{"pods-"},
 | 
			
		||||
			errFn: func(err error) bool { return strings.Contains(err.Error(), "one or more resources must be specified") },
 | 
			
		||||
 
 | 
			
		||||
@@ -606,7 +606,7 @@ func AddInclude3rdPartyVarFlags(cmd *cobra.Command, include3rdParty *bool) {
 | 
			
		||||
func GetResourcesAndPairs(args []string, pairType string) (resources []string, pairArgs []string, err error) {
 | 
			
		||||
	foundPair := false
 | 
			
		||||
	for _, s := range args {
 | 
			
		||||
		nonResource := strings.Contains(s, "=") || strings.HasSuffix(s, "-")
 | 
			
		||||
		nonResource := (strings.Contains(s, "=") && s[0] != '=') || (strings.HasSuffix(s, "-") && s != "-")
 | 
			
		||||
		switch {
 | 
			
		||||
		case !foundPair && nonResource:
 | 
			
		||||
			foundPair = true
 | 
			
		||||
@@ -632,7 +632,7 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair
 | 
			
		||||
	var invalidBuf bytes.Buffer
 | 
			
		||||
	var invalidBufNonEmpty bool
 | 
			
		||||
	for _, pairArg := range pairArgs {
 | 
			
		||||
		if strings.Contains(pairArg, "=") {
 | 
			
		||||
		if strings.Contains(pairArg, "=") && pairArg[0] != '=' {
 | 
			
		||||
			parts := strings.SplitN(pairArg, "=", 2)
 | 
			
		||||
			if len(parts) != 2 {
 | 
			
		||||
				if invalidBufNonEmpty {
 | 
			
		||||
@@ -643,7 +643,7 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair
 | 
			
		||||
			} else {
 | 
			
		||||
				newPairs[parts[0]] = parts[1]
 | 
			
		||||
			}
 | 
			
		||||
		} else if supportRemove && strings.HasSuffix(pairArg, "-") {
 | 
			
		||||
		} else if supportRemove && strings.HasSuffix(pairArg, "-") && pairArg != "-" {
 | 
			
		||||
			removePairs = append(removePairs, pairArg[:len(pairArg)-1])
 | 
			
		||||
		} else {
 | 
			
		||||
			if invalidBufNonEmpty {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user