Commit graph

57 commits

Author SHA1 Message Date
Leoy
39dad5862b kubectl: use %w for error wrapping and remove redundant .Error() calls (#138223)
* kubectl: use %w for error wrapping and remove redundant .Error() calls

Replace fmt.Errorf with %s and err.Error() with idiomatic %w wrapping
in kubectl's polymorphichelpers, wait, and drain packages. This enables
proper error chain inspection via errors.Is/As and follows Go best
practices.

Also fixes a typo: "Statefulset" -> "StatefulSet" in history.go.

* Address review: use %w for error wrapping in extendErrWaitTimeout

Use fmt.Errorf with %w instead of %v to preserve the error chain,
allowing wait.Interrupted() to correctly detect timeout errors.

Signed-off-by: supermario_leo <leo.stack@outlook.com>

* Fix lint: wrap both errors with %w in cordon error path

The linter flags err used with %v when it is an error type. Since
Go 1.20+ supports multiple %w verbs in a single fmt.Errorf call,
wrap both err and patchErr to preserve both error chains.

Signed-off-by: supermario_leo <leo.stack@outlook.com>

---------

Signed-off-by: supermario_leo <leo.stack@outlook.com>

Kubernetes-commit: d916c320deb859cd264354c48f3bd5419f7a2950
2026-04-23 08:37:22 +08:00
Davanum Srinivas
e67d906fc9 kubectl/drain: widen namespace termination retry timeout
TestEvictDuringNamespaceTerminating intentionally exercises the retry path
but only allows 10ms of total time. The production loop sleeps, refreshes
state, and retries under that same deadline, so a single retry plus
scheduler jitter is enough to exhaust the budget under -race or on busy
CI workers.

Keep the retry interval small so the test still covers the retry behavior,
but widen the overall timeout so the assertion measures semantics instead
of machine speed.

Tested:
go test -race ./staging/src/k8s.io/kubectl/pkg/drain -run TestEvictDuringNamespaceTerminating -count=100

Kubernetes-commit: 77f8d7c2a9571dd9a76ee1be8b234a4dddd2ad35
2026-03-14 10:11:26 -04:00
Mads Jensen
3a2bf51acc Lint: Activate modernize/forvar rule in golangci-lint.
Kubernetes-commit: dd32d7c324b97d259b99c1464802d9e3ca706a94
2026-03-05 00:11:40 +01:00
Volker Theile
f60ea778a4 Remove the unnecessary cannot delete from the error message
Related to: https://github.com/kubernetes/kubernetes/issues/135817

Signed-off-by: Volker Theile <vtheile@suse.com>

Kubernetes-commit: 468d3a8406045d4686a3b7e70dcafb9e7f301680
2025-12-18 11:22:52 +01:00
Kirsten Laskoski
8a9690ac95 kubectl: remove context.TODO from daemonSetFilter
Replace the context.TODO with getContext to prefer the Helper's context
when it is set. This is consistent with the rest of the package now.

Since contextual logging or timeouts may be included as part of the
Helper's context, using context.TODO will silently ignore these
features, producing unexpected behaviors.

Signed-off-by: Kirsten Laskoski <klaskosk@redhat.com>

Kubernetes-commit: 7022a626607be602b2e3577cef5882933f42a516
2025-11-29 17:05:12 -05:00
cz21
6575dcbe31 fix(cordonhelper): Avoid mutating local node before API call
Signed-off-by: cz21 <cz21ok@gmail.com>

Kubernetes-commit: c524a5fba28b447ef0a0ef3df3eaef64e8ae8228
2025-09-28 00:21:51 +08:00
Lorain
9e0a26615e fix using stale pod when evict failed and retry (#133461)
* fix using stale pod when evict failed and retry

* simplify pod refresh process

* use activePod at getPodFn

* fix lint check

* add ut

* introduce EvictErrorRetryDelay

Kubernetes-commit: 66fdbe105831e08b588dd01039a7e3130fd2d36f
2025-08-28 04:54:34 +00:00
Aleksey Gavrilov
6323c5bc57 [kubectl] drain daemonSetFilter with other APIVersion
Signed-off-by: Aleksey Gavrilov <alexey.gavrilov@flant.com>

Kubernetes-commit: a52863827bafa64a678c1291f066d2c8b85217af
2024-11-13 10:23:54 +03:00
Omer Aplatony
120105a0a9 kubectl: add test coverage for cordon command (#129202)
* kubectl: add test coverage for cordon command

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

* using cmp.Diff

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

---------

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

Kubernetes-commit: 3ec97a445f036a38bfec6291dee661954138bac9
2024-12-17 06:34:43 +00:00
Omer Aplatony
c329ccff7c Update waitForDelete to use PollUntilContextTimeout
Signed-off-by: Omer Aplatony <omerap12@gmail.com>

Kubernetes-commit: bba055067e6283f94ee05cedeb33dacafe4a1094
2024-10-05 22:07:37 +03:00
tux
bd15ba4612 fix: use the getContext() method for evicting pods
Kubernetes-commit: cc244e8d1d9d57818816e8e35651b78b0c6f9709
2024-08-21 14:08:57 +08:00
Brian Pursley
2687c08c5c Fix kubectl drain error handling bug.
Fixed a bug where kubectl drain would consider a pod as having been deleted if an error occurs while calling the API.

Kubernetes-commit: 4aecb151b8301a6fc69f63c08f640c7b2b4289fc
2024-01-04 08:53:57 -05:00
ithrael
b7a900337d fix(kubectl): delete pod declare no controllor note
Kubernetes-commit: c7549fe340dfa3c038addf84e61f7a923c6e39d5
2023-08-24 23:51:14 +08:00
adil ghaffar
e112354038 adding more callbacks in kubedrain helper
Kubernetes-commit: 0323fe42e23aee176ff14f3385bc15e81216c7bb
2023-04-20 16:16:11 +03:00
W. Trevor King
7ad33aa9fe kubectl/pkg/drain: Include namespace in evictPods return error
And also in the terminating-namespace log output.  This makes it
easier to track down drain-blocking pods, without having to hunt
around in earlier logs for 'evicting pod ...' messages.  Before this
change, caller logs might look like:

  evicting pod {namespace}/{name}
  ...
  error when waiting for pod "{name}" terminating: global timeout reached: 20s

With this change, they will look like:

  evicting pod {namespace}/{name}
  ...
  error when waiting for pod "{name}" in namespace "{namespace}" to terminate: global timeout reached: 20s

Kubernetes-commit: b6318d4e5b9b8eb0d2e2d5a8568df14817733f26
2023-02-07 13:10:30 -08:00
Sean Sullivan
6dde41fea6 Removes unnecessary dry run verifier
Kubernetes-commit: 530f65d6fd5af81c0691143fbf2c00d440476bb9
2022-12-05 16:25:12 -08:00
Kevin Delgado
7e681490ec Generalize dry run verify to arbitrary query param
Kubernetes-commit: d6c83281bc696474a76365a879f2255b8b568786
2022-03-09 14:51:50 +00:00
W. Trevor King
5ff8356c5b kubectl drain: Generic "declare a controller" message
When it initially landed in kubernetes/kubernetes@c6e9ad066e (Initial
node drain implementation for #3885, 2015-08-30,
kubernetes/kubernetes#16698), the drain logic looked in a created-by
annotation for recognized kinds [1], so listing the set of recognized
kinds was a clear approach.

Sometime later, the source moved into ownerReferences, but the
hard-coded set of recognized controller kinds remained.

When kubernetes/kubernetes@2f1108451f (Remove hard-coded
pod-controller check, 2017-12-05, kubernetes/kubernetes#56864) removed
the hard-coded set of recognized controller kinds, it should have also
updated these messages to remove stale references to the previous
hard-coded values.  This commit catches the message strings up with
that commit.

[1]: c6e9ad066e (diff-211259b8a8ec42f105264c10897dad48029badb538684e60e43eaead68c3d219R216)

Kubernetes-commit: 587f4f04cc5fc18f4e85ed6a4a06bbf1bfee0496
2022-02-23 16:26:59 -08:00
Jack
5fb22b2ab8 RunCordonOrUncordon error if drainer has nil Ctx or Client
Kubernetes-commit: ee02f5ddc42a67166c91e539fe98cc6541db668e
2021-09-27 14:20:24 -07:00
Bronson Mirafuentes
1d62bce235 wrap error from RunCordonOrUncordon
Kubernetes-commit: 297432f0e92479cfbb62d182d9d20c1833a3521c
2022-01-25 10:34:47 -08:00
yuzhiquan
c74a1185c3 remove ignore error flag for drain, and set this feature as default
Kubernetes-commit: f54d2a0dd1dd5c143fb32dae5c627dba87d6df7e
2021-10-08 19:44:26 +08:00
Zhou Peng
7454a6489e [k8s.io/kubectl/pkg/drain/drain]: minor typo fixup
Signed-off-by: Zhou Peng <p@ctriple.cn>

Kubernetes-commit: 7e2d6301bf95893ea74116871d2c2f6392217105
2021-08-01 07:07:31 +08:00
Jordan Liggitt
49f840d934 kubectl: send policy/v1 evictions to servers that support it
Kubernetes-commit: f07fc213b07568442b22757b2220b80a5041cced
2021-04-01 22:50:59 -04:00
Katrina Verey
ea308b60b6 Add --chunk-size flag to kubectl drain
Kubernetes-commit: 5200ff86d0983e42c3d2d5c0d830e96495516347
2021-03-19 08:32:37 -07:00
Sagar Muchhal
27325b0861 Adds actual context to node drainer code
Signed-off-by: Sagar Muchhal <muchhals@vmware.com>

Kubernetes-commit: 2e01560fd0c1fc6eec607c5a01022384feda67d6
2021-03-21 18:11:50 -07:00
Katrina Verey
5105e147ad Fix kubectl drain --dry-run=server
Adds missing type meta to PodDelete items

Kubernetes-commit: ff9ada1b550852c697af5250c90e8e4de69110e4
2021-03-12 16:26:48 -08:00
rkilingr
1d4a9f61b6 Provides capacity for make slice
* Providing capacity for slice decreases memory allocations performed

Kubernetes-commit: dfe29ae7f84dccb3a989ed1f5d73bde3f58d4ed8
2021-01-31 20:56:11 +05:30
yuzhiquan
7e8ba7c82e add ignore-errors flag, support none break drain in group
add warning msg

Kubernetes-commit: db264eaa6e2db5f344ec40dc6dd632ece8fe5768
2021-01-20 15:02:00 +08:00
Sagar Muchhal
72f141c577 Passes a context to the drain helper object
Enables the PatchOrReplace call for the cordon helper to accept a
user-supplied context to be used for the client.Patch/Update calls to
alter the node status while cordoning/uncordoning the node.

Signed-off-by: Sagar Muchhal <muchhals@vmware.com>

Kubernetes-commit: 017eaa519de5926fd75a9ddc61bedf2398b69653
2020-12-04 15:13:03 -08:00
Bryan Boreham
40c811e2d9 Comment default behaviour of GracePeriodSeconds
It took me a while to spot this subtlety.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>

Kubernetes-commit: 7c764c8550121b48a6e2678700b6a80fa7d3e2f0
2020-11-23 17:46:14 +00:00
blade
9cc15e588a return success if cordon node by replace
Kubernetes-commit: c825e8ec16482ebbefc3ae96a26f4dcf924e750c
2019-12-12 20:20:29 +08:00
Douglas Schilling Landgraf
56ea9621b7 kubectl: deprecate --delete-local-data
The option --delete-local-data according with users is not clear.
This patch deprecate --delete-local-data in favor of --delete-emptydir-data.

Reference:
https://github.com/kubernetes/kubernetes/issues/80228

Signed-off-by: Douglas Schilling Landgraf <dougsland@redhat.com>

Kubernetes-commit: 625e47aaa2769d221c59e5b9b05b4ac97212719b
2020-09-25 17:33:45 -04:00
Amanda Hager Lopes de Andrade Katz
587d894118 Fixes high CPU usage in kubectl drain
Kubernetes-commit: de9c2c2090bbf66943022e902868a5457410af90
2020-10-02 16:02:12 -03:00
Michael Gugino
a28f944c90 kubectl/drain add support for custom pod filters
Currently, there is no way to supply custom pod filters to
exclude pods meeting arbitrary criteria when using drain
as a library.

This commit adds the ability for developers to add custom
filters to the drain operation when utilizing drain
as a library.

This commit exports certain types that were previously
private to allow for better code reuse.

This commit also adds appropriate unit tests.

Kubernetes-commit: 85004f030dc3dceb9d15f41fdd607545758d5da2
2020-02-18 19:15:07 -05:00
Michael Gugino
e5ddedb459 Handle eviction of pods in deleted namespace
If a pod is already marked deleted, and the eviction
api returns an unauthorized response, ignore that
error since the pod is marked for deletion already.

If the pod is not already marked deleted, retry.

Kubernetes-commit: 8d2a2ffe014ed06b5f8971e7f3dc25ec155e44d4
2020-08-12 16:15:54 -04:00
Ryan Phillips
de06b92a12 drain: eviction creates in a deleting namespace will throw a forbidden error
we can safely ignore this error since a deleting namespace will delete
all pods

Kubernetes-commit: 93f62dad6c58bb41c396c48f54c0a73830779599
2020-03-20 14:43:33 -05:00
David Eads
25d0565d86 update drain message to include namespace for pod in kubectl get compatible format
Kubernetes-commit: 9655953000406ce413772ac7d5082c923e913bb4
2020-08-26 11:58:14 -04:00
Jordan Liggitt
197605389f Fix int->string casts
Kubernetes-commit: 124a5ddf725c4862520d8619017cac9db7a03522
2020-07-24 11:13:07 -04:00
Jordan Liggitt
cfef71236e client-go: update expansions callers
Kubernetes-commit: d8abacba40a63395cc064452b09a244555536fca
2020-03-06 16:29:23 -05:00
Mike Danese
15efe80aa3 more artisanal fixes
Most of these could have been refactored automatically but it wouldn't
have been uglier. The unsophisticated tooling left lots of unnecessary
struct -> pointer -> struct transitions.

Kubernetes-commit: 76f85943787a5901a34a314a935712177edd2db0
2020-03-01 09:34:30 -08:00
Mike Danese
96c320d500 automated refactor
Kubernetes-commit: c58e69ec791e4dd8a1298d5c2cc0649cf9176f67
2020-03-01 09:24:42 -08:00
Julian V. Modesto
f9460c5333 Use --dry-run=client,server in kubectl.
- Support --dry-run=server for subcommands apply, run, create, annotate,
expose, patch, label, autoscale, apply set-last-applied, drain, rollout undo
- Support --dry-run=server for set subcommands
  - image
  - resources
  - serviceaccount
  - selector
  - env
  - subject
- Support --dry-run=server for create subcommands.
  - clusterrole
  - clusterrolebinding
  - configmap
  - cronjob
  - job
  - deployment
  - namespace
  - poddisruptionbudget
  - priorityclass
  - quota
  - role
  - rolebinding
  - service
  - secret
  - serviceaccount
- Remove GetClientSideDryRun

Kubernetes-commit: 13b80b48cd02b8263d910f2423a1f5b92cdf7644
2020-01-30 20:23:33 -05:00
zhouya0
94b91c1d51 fix kubectl drain ignore daemonsets and others
Kubernetes-commit: e3c8add683a095f1bffef1b0ca413f1e42be7fe0
2020-01-19 14:28:53 +08:00
Mike Danese
29cd24b6cc generated: run refactor
Kubernetes-commit: 25651408aeadf38c3df7ea8c760e7519fd37d625
2020-02-08 12:30:21 -05:00
Mike Danese
71d8052cb0 generated: run refactor
Kubernetes-commit: 3aa59f7f3077642592dc8a864fcef8ba98699894
2020-02-07 18:16:47 -08:00
Michael Gugino
a17d91f9f5 kubectl: remove unreachable code
This code cannot be reached and causes some external linters
to fail.

Kubernetes-commit: 46fd8fce45cfbbe5681a44872602da6daea80cf6
2019-10-23 11:20:40 -04:00
Michael Gugino
ebda9f6262 kubectl/drain: add option skip-wait-for-delete-timeout
Currently, some circumstances may cause waitForDelete to
never succeed after the pod has been marked for deletion.
In particular, Nodes that are unresponsive and have
pods with local-storage will not be able to
successfully drain.

We should allow drain to ignore pods that have a
DeletionTimestamp older than a user-provided age.
This will allow controllers utilizing kubectl/drain
to optionally account for a pod that cannot be
removed due to a misbehaving node.

Kubernetes-commit: da53044abdf8c8a9771a5c3dfd861f0c4ec78c40
2019-11-23 20:00:06 -05:00
Sally O'Malley
6d309ed99e When running kubectl drain in dry-run, list warnings and pods that would be deleted.
Kubernetes-commit: 909300b8be909c48a68900c407de03be95555409
2019-09-12 14:31:09 -04:00
Michael Gugino
feccbe3c28 kubectl/drain: add disable-eviction option
Currently, if eviction is supported during a drain operation,
eviction is always used.

This commit allows the user to specify disabling eviction.
This is particularly useful when you wish to ignore
PodDisruptionBudgets after a normal drain has failed for
some time.

Kubernetes-commit: 6c1d587d5142b95e6c169a198a086332a7e4d8ad
2019-11-23 11:18:52 -05:00
Michael Gugino
fe592e7f80 kubectl/drain: Add context support
This commits allows specifying a context.Context
in the Helper type. This context is utilized to
cancel waitForDelete.

Kubernetes-commit: 8682e902f5487e04b893da7230125db0d7ae66b4
2019-11-23 13:58:59 -05:00