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 <omerap12@gmail.com>

* Add unit test

Signed-off-by: Omer Aplatony <omerap12@gmail.com>

* add integration test

Signed-off-by: Omer Aplatony <omerap12@gmail.com>

* Increase wait time to 40 seconds

Signed-off-by: Omer Aplatony <omerap12@gmail.com>

---------

Signed-off-by: Omer Aplatony <omerap12@gmail.com>
This commit is contained in:
Omer Aplatony 2024-12-12 04:57:28 +02:00 committed by GitHub
parent a892f0fd80
commit 0cc9262200
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 1 deletions

View File

@ -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

View File

@ -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 <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-label
labels:
app: test-label
spec:
replicas: 1
selector:
matchLabels:
app: test-label
template:
metadata:
labels:
app: test-label
spec:
containers:
- name: bb
image: busybox
command: ["/bin/sh", "-c", "sleep infinity"]
EOF
) &
# Command: Wait for labeled deployment to be created
output_message=$(kubectl wait --for=create deploy -l app=test-label --timeout=40s)
# Post-Condition: Wait was successful
kube::test::if_has_string "${output_message}" 'test-label condition met'
# Clean up
kubectl delete deployment test-label
# create test data to test timeout error is occurred in correct time
kubectl apply -f - <<EOF
apiVersion: apps/v1