kubernetes / kubectl

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

kubectl label output from file input with multiple objects does not include a document separator #1265

Open DenverJ opened 2 years ago

DenverJ commented 2 years ago

What happened: When running "kubectl label" with file input containing multiple objects no document separator is included in the output. This means only the last object will be picked up to be labelled on the cluster (or passed on to an apply command etc).

What you expected to happen: Multiple objects labelled with a document separator in between.

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

cat <<EOF > /tmp/cm_test.yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: ConfigMap
  metadata:
    name: test1
- apiVersion: v1
  kind: ConfigMap
  metadata:
    name: test2
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
EOF
cat /tmp/cm_test.yaml | kubectl label testlabel=foo -o yaml -f - --local

Output:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    testlabel: foo
  name: test1
apiVersion: v1  # No record separator here
kind: ConfigMap
metadata:
  labels:
    testlabel: foo
  name: test2

Anything else we need to know?: The same process and data but using the "annotate" command instead of "label" works perfectly and includes document separators. as per output below.

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    testlabel: foo
  name: test1
---
apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    testlabel: foo
  name: test2

Environment:

ardaguclu commented 2 years ago

Problem stems from that label initializes new printer for each object https://github.com/kubernetes/kubernetes/blob/10eb7092f854c71122c03752465e868bce23c0b6/staging/src/k8s.io/kubectl/pkg/cmd/label/label.go#L390 unlike to annotate.

/triage accepted /assign

ardaguclu commented 2 years ago

Currently YAMLPrinter only adds document separator(---) if the object count is more than 1 for one YAMLPrinter object https://github.com/kubernetes/kubernetes/blob/e4fca6469022309bc0ace863fce73054a0219464/staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go#L48.

However, for the command label(and for a couple of more commands), there is no one YAMLPrinter, instead to manage the message in printer object, YAMLPrinter is initialized per object https://github.com/kubernetes/kubernetes/blob/10eb7092f854c71122c03752465e868bce23c0b6/staging/src/k8s.io/kubectl/pkg/cmd/label/label.go#L390.

That causes each YAMLPrinter has it's count to 1. Therefore, they are not adding document separator.

This simply works for annotate command because print message is not being changed for annotate command(there is also an issue for that problem https://github.com/kubernetes/kubernetes/issues/110123) and printer is initialized once in completed.

I wonder would it be possible to add document separator in any case without checking the counter in here https://github.com/kubernetes/kubernetes/blob/e4fca6469022309bc0ace863fce73054a0219464/staging/src/k8s.io/cli-runtime/pkg/printers/yaml.go#L48

@eddiezane @brianpursley @soltysh

/unassign

jaehnri commented 2 years ago

Hi! I would like to take a look at this issue and see if I can help with it. /assign

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

ardaguclu commented 2 years ago

/remove-lifecycle stale

jaehnri commented 1 year ago

It looks like PR #110124 is already addressing this issue and has been hardened and reviewed for a while now. I'm not really contributing to sig-cli anymore so I'll not look further into this. Unassigning and closing my PR so another person can tackle it!

/unassign

k8s-triage-robot commented 10 months ago

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted

k8s-triage-robot commented 7 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 6 months ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten