diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go index 7f2359f55cb..a8e010108b4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go @@ -250,14 +250,14 @@ func processJSONPathInput(jsonPathInput []string) (string, string, error) { if err != nil { return "", "", err } - if len(jsonPathInput) > 1 { - jsonPathValue := jsonPathInput[1] - if jsonPathValue == "" { - return "", "", errors.New("jsonpath wait value cannot be empty") - } - return relaxedJSONPathExp, strings.Trim(jsonPathValue, `'"`), nil + if len(jsonPathInput) == 1 { + return relaxedJSONPathExp, "", nil } - return relaxedJSONPathExp, "", nil + jsonPathValue := strings.Trim(jsonPathInput[1], `'"`) + if jsonPathValue == "" { + return "", "", errors.New("jsonpath wait has to have a value after equal sign, like --for=jsonpath='{.status.readyReplicas}'=3") + } + return relaxedJSONPathExp, jsonPathValue, nil } // ResourceLocation holds the location of a resource diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go index 2923b7bc3e9..2d01cce5b76 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go @@ -1556,13 +1556,13 @@ func TestWaitForJSONPathBadConditionParsing(t *testing.T) { { name: "undefined value", condition: "jsonpath={.metadata.name}=", - expectedErr: "jsonpath wait value cannot be empty", + expectedErr: "jsonpath wait has to have a value after equal sign, like --for=jsonpath='{.status.readyReplicas}'=3", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { _, err := conditionFuncFor(test.condition, io.Discard) - if err == nil { + if err == nil && test.expectedErr != "" { t.Fatalf("expected %q, got empty", test.expectedErr) } if !strings.Contains(err.Error(), test.expectedErr) { diff --git a/test/cmd/wait.sh b/test/cmd/wait.sh index 242ae435ea2..d33c44d3e61 100644 --- a/test/cmd/wait.sh +++ b/test/cmd/wait.sh @@ -94,6 +94,21 @@ EOF # Clean deployment kubectl delete deployment dtest + # create test data + kubectl create deployment test-3 --image=busybox + + # wait with jsonpath without value to succeed + set +o errexit + # Command: Wait with jsonpath without value + output_message=$(kubectl wait --for=jsonpath='{.status.replicas}' deploy/test-3 2>&1) + set -o errexit + + # Post-Condition: Wait succeed + kube::test::if_has_string "${output_message}" 'deployment.apps/test-3 condition met' + + # Clean deployment + kubectl delete deployment test-3 + set +o nounset set +o errexit }