Skip to content
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

Support selectors for kubectl patch #121673

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tnozicka
Copy link
Contributor

@tnozicka tnozicka commented Nov 1, 2023

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR adds 3 selector flags for kubectl patch that are common on other commands:

  • --label to select object using labels
  • --field-selector to select objects by fields
  • --all to select all objects within a namespace

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#909

Special notes for your reviewer:

Does this PR introduce a user-facing change?

`kubectl patch` now support `--all`, `--label` and `--field-selector`, similarly to other existing `kubectl` commands.

/sig cli
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. sig/cli Categorizes an issue or PR as relevant to SIG CLI. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 1, 2023
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. area/kubectl labels Nov 1, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tnozicka
Once this PR has been reviewed and has the lgtm label, please assign seans3 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tnozicka
Copy link
Contributor Author

tnozicka commented Nov 1, 2023

Job execution failed: Pod got deleted unexpectedly
/retest

@tnozicka
Copy link
Contributor Author

@ardaguclu @mpuckett159 gentle ping

@@ -235,7 +240,9 @@ func (o *PatchOptions) RunPatch() error {
NamespaceParam(o.namespace).DefaultNamespace().
FilenameParam(o.enforceNamespace, &o.FilenameOptions).
Subresource(o.Subresource).
ResourceTypeOrNameArgs(false, o.args...).
ResourceTypeOrNameArgs(o.All, o.args...).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field and label selectors in patch command sounds reasonable to me but I'm strongly reluctant to add all in here. Because this flag already creates confusions to users in other commands and I don't think, we'd want to add more.

In addition to that all flag is also mutually exclusive with the other selectors

b.errs = append(b.errs, fmt.Errorf("setting 'all' parameter but found a non empty selector. "))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed don't want to add all because it is highly confusing to users as it does not actually mean "all."

Copy link
Contributor Author

@tnozicka tnozicka Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it means "select all instances" so you don't have to create dummy selector when you want to select all instances of the object. Would you prefer the flag name to be different? If so which one and how'd you see the consistency with other commands and reusing the builders?

@@ -146,6 +148,9 @@ func NewCmdPatch(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobra
cmd.Flags().BoolVar(&o.Local, "local", o.Local, "If true, patch will operate on the content of the file, not the server-side resource.")
cmdutil.AddFieldManagerFlagVar(cmd, &o.fieldManager, "kubectl-patch")
cmdutil.AddSubresourceFlags(cmd, &o.Subresource, "If specified, patch will operate on the subresource of the requested object.", supportedSubresources...)
cmdutil.AddLabelSelectorFlagVar(cmd, &o.LabelSelector)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add unit tests for these 2 flags in here

and integration tests preferably in

kubectl patch "${kube_flags[@]}" pod pod-with-precision -p='{"metadata":{"annotations":{"patchkey": "patchvalue"}}}'

@mpuckett159
Copy link
Contributor

I am hesitant to approve this. SIG-CLI generally does not prefer to add more flags to existing imperative commands as it encourages people to continue using them for use cases they are not designed for. Especially with these particular flags, because they encourage users to make sweeping changes to large amounts of objects at once, when these commands are only meant to be introductory tools, or for quick testing and proof of concept scenarios. We have been trying to curb the expansion of these flags for some time. I don't want to close this immediately to allow for some discussions.

@tnozicka
Copy link
Contributor Author

Especially with these particular flags, because they encourage users to make sweeping changes to large amounts of objects at once, when these commands are only meant to be introductory tools, or for quick testing and proof of concept scenarios.

I have to admit I am a bit surprised to hear kubectl patch being called an "introductory tool". To my understanding kubectl patch along with kubectl create, kubectl replace, kubectl delete and the rest of them mirror the REST API methods (with some variations) and are essential for kubectl and for both developers and administrators when operating a Kubernetes cluster.

because they encourage users to make sweeping changes to large amounts of objects at once

I don't think it's about encouraging modifying multiple objects at once but more about operating a real clusters where there is a lot of instances of those objects and sometimes you need to make changes that aren't exposed in higher level objects or are maintenance based. Obviously, there are some commands that allow avoiding doing the patches manually, like kubectl label, for a small subset of common tasks, but that hardly diminishes the importance of kubectl patch. Especially, with CRDs there is no way kubectl can have dedicated commands which makes the generic ones even more important. Here are some examples where kubectl patch is used for operating procedures:

@mpuckett159
Copy link
Contributor

Sorry I meant to add this link to my previous comment. Please see this blog post for SIG-CLI's position regarding patch and other commands like it.

https://kubernetes.io/docs/concepts/overview/working-with-objects/object-management/

As well as this presentation that some of the SIG-CLI leads and myself gave regarding this issue at the most recent KubeCon.

https://youtu.be/RggqaCSdOGA?si=tjj9g_vfA9M5NNjq&t=558

I know that this is not generally what people like to hear, but unfortunately it is very common for a lot of reasons, and there isn't much we can do to prevent people from using these commands in the ways you've described above without removing the commands entirely from the code base, which is not something that project is willing to do.

Thank you for providing the links and examples though, this is helpful for us to find more pain points that lead to people relying on imperative commands rather than using the preferred declarative workflow for production systems.

@tnozicka
Copy link
Contributor Author

tnozicka commented Nov 30, 2023

Thanks for the links, now I know where the view is coming from. While I am the first person to tell people to use GitOps and prefer kubectl apply --server-side it isn't, and likely never will be, "one fits all" cases. It's generally meant for .spec + .metadata and only the resources you manage / define. I'll list a few examples to show that point:

  • Objects that aren't created by the user but he needs to edit the spec, add annotations, labels, or something else. That includes Nodes, default ConfigMaps or Secrets, Namespaces, global CRDs created by operators, and others. Unless all controllers would use SSA (they don't) this won't work.
  • Object that are created by other Objects and you don't define them in your Object. Even better, they have variable instance count so storing a manifest for each isn't conceptionaly doable and is often one shot edit. A concrete case is e.g. PVCs for StatefulSets.
  • Editing .status. (I don't see a meaningful way to use apply there unless we want to tel people to replace kubectl patch with kubectl get -o yaml | yq '.make-my-change' | kubectl apply --server-side -f -
  • Development flows where for + kubectl get -o yaml + yq + kubectl apply is far from the effective path.
  • Admins needing to unstuck cluster when they hit a bug or roadblock.

While I agree that we should tell people to deploy apps using kubectl apply --server-side those are not the only actions needing to be done on Kubernetes clusters and kubectl patch is still valuable and maps directly to an API concept that it exposes (contrary to some generator commands) which was the founding base of what kubectl should do.

I suppose mostly you could do bulk patch with kubectl apply like:

for o in $( kubectl get my-resource -o name ); do
  kubectl get my-resource/"${o}" -o yaml | yq 'del(.status.conditions)' | kubectl apply --server-side -f -
done

but that needs extra binary, makes a ton of extra API calls on large clusters (terribly slow) and misses extra concepts like UID preconditions.

/cc @soltysh
(as I saw you on the stage in the referenced KubeCon recording :) )

@k8s-triage-robot
Copy link

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

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

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

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 19, 2024
@tnozicka
Copy link
Contributor Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 19, 2024
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 1, 2024
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

Support patch with selector
5 participants