Skip to content

kubectl apply with "--selector" fails if an unrelated label is set to Null #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fjolublar opened this issue May 8, 2025 · 11 comments
Closed
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@fjolublar
Copy link

fjolublar commented May 8, 2025

What happened:

If you have a resource with 2+ labels and one of the labels is set to Null or is empty kubectl apply works with no issues.

If you try to apply the resource using a selector -l the apply command fails with not so useful error message:

error: no objects passed to apply

What you expected to happen:

The filtering should work.

The existing error message caused confusion because the label that we are searching for is actually set and we would expect that the filtering would find that resource.

How to reproduce it (as minimally and precisely as possible):

Create a test_file.yaml with a CM resource definition.

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
  labels:
    label_a: null
    label_b: "bar"
    app.kubernetes.io/version: "v1.0.0"
data:
  config_key_1: "value1"
>> kubectl apply -f test_file.yaml
configmap/test-config created

If you try to apply the resource using a selector -l the apply command fails with not so useful error message:

>> kubectl apply -f test_file.yaml -l='label_b=bar'
error: no objects passed to apply

The label_a being Null/missing is causing issues with the -l label selector, even though label_b is being filtered.

Anything else we need to know?:

Environment:

  • Kubernetes client and server versions (use kubectl version): Client Version: v1.30.5, Server Version: v1.31.4
  • Cloud provider or hardware configuration: n/a
  • OS (e.g: cat /etc/os-release): Darwin 24.4.0 Darwin Kernel Version 24.4.0:
@fjolublar fjolublar added the kind/bug Categorizes issue or PR as related to a bug. label May 8, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label May 8, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ardaguclu
Copy link
Member

Do you mean this kubectl apply -f test_file.yaml -l='label_b=bar' works if test_file.yaml does not contain label_a: null?.

I'm not sure that kubectl apply supports setting a label on command line (i.e. -l='label_b=bar') while you are passing a file to be applied (i.e. -f test_file.yaml).

@fjolublar
Copy link
Author

fjolublar commented May 12, 2025

Do you mean this kubectl apply -f test_file.yaml -l='label_b=bar' works if test_file.yaml does not contain label_a: null?.

Yes. If the file does not contain label_a: null the apply with using the label selector works.

If the resource contains label_a: null the kubectl apply -f test_file.yaml -l='label_b=bar' fails even though it should work.
As long as kubectl apply -f test_file.yaml works why shouldn't kubectl apply -f test_file.yaml -l='label_b=bar' work.

I'm not sure that kubectl apply supports setting a label on command line

The -l argument is not used to set a label but to filter on the label: https://kubernetes.io/docs/reference/kubectl/generated/kubectl_apply/#options, i.e. select the resources that contain such label.

@ardaguclu
Copy link
Member

Did you try kubectl apply -l='label_b=bar' -f test_file.yaml

@fjolublar
Copy link
Author

Did you try kubectl apply -l='label_b=bar' -f test_file.yaml

I don't think the order of the arguments matters 🤔. I tried it nevertheless and still fails.

@ardaguclu
Copy link
Member

Thank you for testing. Could you please try kubectl apply -l='label_b=bar' --server-side -f test_file.yaml?

@fjolublar
Copy link
Author

> kubectl apply -l='label_b=bar' --server-side -f test_file.yaml
error: no objects passed to apply

@ardaguclu
Copy link
Member

I tried these steps and it didn't fail;

$ cat test_cm.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
  labels:
    label_a: null
    label_b: "bar"
    app.kubernetes.io/version: "v1.0.0"
data:
  config_key_1: "value1"

firstly create resource;

$ kubectl apply --server-side -f test_cm.yaml 
configmap/test-config serverside-applied

After that I re-applied;

$ kubectl apply -f test_cm.yaml -l='label_b=bar' --server-side
configmap/test-config serverside-applied

I've used kubectl that is built from master branch.

@fjolublar
Copy link
Author

fjolublar commented May 12, 2025

I just tried it with v1.33.0 and it works. Maybe there was a change in the newer versions of kubectl that fixes the problem!

kubectl version --client
Client Version: v1.33.0

client-side apply:

> kubectl apply -f test_cm.yaml -l='label_b=bar'
configmap/test-config created

> k get cm test-config -oyaml 
apiVersion: v1
data:
  config_key_1: value1
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"config_key_1":"value1"},"kind":"ConfigMap","metadata":{"annotations":{},"labels":{"app.kubernetes.io/version":"v1.0.0","label_a":null,"label_b":"bar"},"name":"test-config","namespace":"default"}}
  creationTimestamp: "2025-05-12T10:06:08Z"
  labels:
    app.kubernetes.io/version: v1.0.0
    label_b: bar
  name: test-config
  namespace: default
  ...

server-side apply

>kubectl apply -f test_cm.yaml -l='label_b=bar' --server-side
configmap/test-config serverside-applied

>k get cm test-config -oyaml
apiVersion: v1
data:
  config_key_1: value1
kind: ConfigMap
metadata:
  creationTimestamp: "2025-05-12T10:07:28Z"
  labels:
    app.kubernetes.io/version: v1.0.0
    label_a: ""
    label_b: bar
  name: test-config
  namespace: default
  ...

For server-side the resource contains label_a set explicitly to empty string "" and in the client-side is completely missing.... but the original point was not with regards to differences to client-side vs server-side.

As far as I am concerned, it seems that the newer versions of kubectl do not show the issue with the label selector anymore and I just need to update my version to the latest.

Thank you for checking and your help @ardaguclu

@ardaguclu
Copy link
Member

Thank you for your collaboration @fjolublar

I think we can close this issue. WDYT?

@fjolublar
Copy link
Author

I think we can close this issue. WDYT?

Yes. Closing this issue. Thank you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

3 participants