From 0cc926220067cd814cecd8f3d0c3b9235e6a68db Mon Sep 17 00:00:00 2001 From: Omer Aplatony Date: Thu, 12 Dec 2024 04:57:28 +0200 Subject: [PATCH] kubectl: fix wait --for=create to work correctly with label selectors (#128662) * kubectl: fix wait --for=create to work correctly with label selectors Signed-off-by: Omer Aplatony * Add unit test Signed-off-by: Omer Aplatony * add integration test Signed-off-by: Omer Aplatony * Increase wait time to 40 seconds Signed-off-by: Omer Aplatony --------- Signed-off-by: Omer Aplatony --- .../src/k8s.io/kubectl/pkg/cmd/wait/wait.go | 4 +- test/cmd/wait.sh | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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 bc3a5c8d5ce..ab37c953f0c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go @@ -329,7 +329,9 @@ func (o *WaitOptions) RunWait() error { // or functions from ResourceBuilder for parsing those. Lastly, this poll // should be replaced with a ListWatch cache. if err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, o.Timeout, true, func(context.Context) (done bool, err error) { + foundResource := false visitErr := o.ResourceFinder.Do().Visit(func(info *resource.Info, err error) error { + foundResource = true return nil }) if apierrors.IsNotFound(visitErr) { @@ -338,7 +340,7 @@ func (o *WaitOptions) RunWait() error { if visitErr != nil { return false, visitErr } - return true, nil + return foundResource, nil }); err != nil { if errors.Is(err, context.DeadlineExceeded) { return fmt.Errorf("%s", wait.ErrWaitTimeout.Error()) // nolint:staticcheck // SA1019 diff --git a/test/cmd/wait.sh b/test/cmd/wait.sh index 2578d4bd6fd..01820b66778 100644 --- a/test/cmd/wait.sh +++ b/test/cmd/wait.sh @@ -67,6 +67,49 @@ run_wait_tests() { kube::test::if_has_string "${output_message}" 'test-1 condition met' kube::test::if_has_string "${output_message}" 'test-2 condition met' + set +o errexit + # Command: Wait with label selector before resource exists + output_message=$(kubectl wait --for=create deploy -l app=test-label --timeout=1s 2>&1) + set -o errexit + + # Post-Condition: Should get timeout, not "no resources found" + kube::test::if_has_string "${output_message}" 'timed out waiting for the condition' + + # Create deployment with label selector in the background + + ( sleep 2 && cat <