kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.89k stars 923 forks source link

kubectl wait --for=create doesn't work with selectors #1675

Open aaronmondal opened 3 weeks ago

aaronmondal commented 3 weeks ago

What happened?

This works:

kubectl wait --for=create someresourcetype/resource

But for generated resources where we need to filter, this immediately exits with error: no matching resources found:

kubectl wait --for=create someresourcetype -l somelabel

What did you expect to happen?

I'd expect kubectl wait --for=create to still wait when used with -l. Otherwise we're back to hacky sleeps and bash loops in cases where resource names are unknown (i.e. generated).

How can we reproduce it (as minimally and precisely as possible)?

#!/bin/bash
set -xeuo pipefail

# This works as expected.
(
sleep 1 && cat <<EOF | kubectl create -f - >/dev/null 2>&1
apiVersion: v1
kind: ConfigMap
metadata:
  name: unlabeled-cm
data:
  key: value
EOF
) & pid=$!

kubectl wait --for=create configmap/unlabeled-cm --timeout=10s || true

wait $pid

# This fails, since the filter failure seems to cause an early exit.
(
sleep 1 && cat <<EOF | kubectl create -f - >/dev/null 2>&1
apiVersion: v1
kind: ConfigMap
metadata:
  name: labeled-cm
  labels:
    hello: world
data:
  key: value
EOF
) & pid=$!

kubectl wait --for=create configmap -l hello=world --timeout=10s || true

wait $pid

# This works again, since the configmap now exists.
kubectl wait --for=create configmap -l hello=world --timeout=10s || true

kubectl delete configmap unlabeled-cm labeled-cm

Anything else we need to know?

I didn't test anything other than -l, but other filtering mechanisms might be broken with --for=create as well.

This issue is especially noticeable when working with generated resources. For instance, you might have some trigger after a while to invoke e.g. a Tekton PipelineRun. These PipelineRuns have predictable labels but unpredictable names, making name-based waiting impossible.

Kubernetes version

```console $ kubectl version Client Version: v1.31.0 Kustomize Version: v5.4.2 Server Version: v1.31.0 ```

Cloud provider

Local kind cluster, v0.24.0

OS version

```console # On Linux: $ cat /etc/os-release NAME=Gentoo ID=gentoo PRETTY_NAME="Gentoo Linux" ANSI_COLOR="1;32" HOME_URL="https://www.gentoo.org/" SUPPORT_URL="https://www.gentoo.org/support/" BUG_REPORT_URL="https://bugs.gentoo.org/" VERSION_ID="2.15" $ uname -a Linux 6.9.2-gentoo #1 SMP PREEMPT_DYNAMIC Mon May 27 01:25:56 CEST 2024 x86_64 GNU/Linux # On Windows: C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture # paste output here ```

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, ...) and versions (if applicable)

ardaguclu commented 2 weeks ago

Yes, you are right and this is a bug. ResourceFinder seems to be returning error nil when no resource has been found with the given label selector. /triage accepted /priority important-soon

/cc @soltysh

omerap12 commented 2 weeks ago

Yes, you are right and this is a bug. ResourceFinder seems to be returning error nil when no resource has been found with the given label selector. /triage accepted /priority important-soon

/cc @soltysh

I'm glad to take a look at that bug if that's alright with you.

ardaguclu commented 2 weeks ago

@omerap12 sounds good. Thank you.

omerap12 commented 2 weeks ago

/assign