diff --git a/pkg/kubectl/cmd/annotate_test.go b/pkg/kubectl/cmd/annotate_test.go index 7f8a2e24538..e1b6ca7d6dd 100644 --- a/pkg/kubectl/cmd/annotate_test.go +++ b/pkg/kubectl/cmd/annotate_test.go @@ -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") }, diff --git a/pkg/kubectl/cmd/label_test.go b/pkg/kubectl/cmd/label_test.go index e92dda131bd..5140ca2bcf2 100644 --- a/pkg/kubectl/cmd/label_test.go +++ b/pkg/kubectl/cmd/label_test.go @@ -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") }, diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index 885cd8754d7..bf322293beb 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -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 {