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

fix: the volume is not detached after the pod and PVC objects are deleted #116138

Merged
merged 1 commit into from
Apr 11, 2023

Conversation

cvvz
Copy link
Member

@cvvz cvvz commented Feb 28, 2023

What type of PR is this?

/kind bug

What this PR does / why we need it:

When vol_data.json file does not exist, skip UnmountDevice since it must succeed before, no need to do UnmountDevice multiple times.

Which issue(s) this PR fixes:

Fixes #114207

Special notes for your reviewer:

Does this PR introduce a user-facing change?

fix 1.24 regression: the volume is not detached after the pod and PVC objects are deleted

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@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/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 28, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @cvvz. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. sig/storage Categorizes an issue or PR as relevant to SIG Storage. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 28, 2023
@cvvz
Copy link
Member Author

cvvz commented Mar 1, 2023

cc @andyzhangx

@Chaunceyctx
Copy link

Chaunceyctx commented Mar 1, 2023

Is there any better solution to fix a TOCTOU bug in unmountDetachDevices. In other words, kubelet will perform unmountDevice operation only once. @jingxu97 @cvvz

Copy link
Member

@andyzhangx andyzhangx left a comment

Choose a reason for hiding this comment

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

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 1, 2023
@cvvz
Copy link
Member Author

cvvz commented Mar 1, 2023

Is there any better solution to fix a TOCTOU bug in unmountDetachDevices. In other words, kubelet will perform unmountDevice operation only once. @jingxu97 @cvvz

@Chaunceyctx Thanks for you comment. After analyzing the logs and code, I do find there could be a TOCTOU bug in unmountDetachDevices process. I will explain more specificly later.

Anyway, I think it is worth to take a look at this PR first, since after there is no volume name in the global mount path, getDriverAndVolNameFromDeviceMountPath is useless and we can just rely on vol_data.json. The only risk will be vol_data.json file is corrupted for some reason, which should never happen IMO.

@cvvz
Copy link
Member Author

cvvz commented Mar 1, 2023

Here is the time-series chart, the left side is the reconcile loop, the right side is the async UnmountDevice operation, the red box indicates the first time of unmountDetachDevices operation, and the green box indicates the second time of unmountDetachDevices operation, the timestamp is from the logs provided by @Chaunceyctx.

Hopefully I can make things clear.

time-series

@jingxu97
Copy link
Contributor

jingxu97 commented Mar 2, 2023

@cvvz thanks a lot for the diagram. Yes, this is the issue I was thinking about. There could be multiple places for fix this issue,

  • Avoid operation are called multiple times (by fixing the TOCTOU). However I don't think we can make a guarantee on this since operation might timeout etc.
  • Make sure operation is idempotent. I think this is necessary because previous one has no guarantee. There are two related operations here, one is the kubelet call for UnmountDevice, the other is CSI driver side to trigger UnstageVolume.

Should we consider add fixes for all those places?

/cc @msau42 @gnufied @jsafrane

@msau42
Copy link
Member

msau42 commented Mar 6, 2023

Thanks for putting together the diagram! What if after checking IsOperationPending, we try to get a fresh volume state? Will that solve the TOCTOU?

cc @sunnylovestiramisu

@@ -595,14 +595,13 @@ func (c *csiAttacher) UnmountDevice(deviceMountPath string) error {
driverName = data[volDataKey.driverName]
volID = data[volDataKey.volHandle]
} else {
klog.Error(log("UnmountDevice failed to load volume data file [%s]: %v", dataDir, err))

// The volume might have been mounted by old CSI volume plugin. Fall back to the old behavior: read PV from API server
Copy link
Member

Choose a reason for hiding this comment

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

Was this logic to handle a scenario where someone didn't drain the node before upgrading? Is this safe to remove now? cc @mattcary @saikat-royc

Copy link
Member Author

@cvvz cvvz Mar 8, 2023

Choose a reason for hiding this comment

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

I think vol_data.json was brought in #64882 5 years ago, so now no need to fall back to the old behavior when it does not exist anymore, it should be safe to remove.

Copy link
Member

Choose a reason for hiding this comment

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

shall we move this PR forward since it really solves the problem per our large scale test, there is no disk detach issue any more. @mattcary @saikat-royc @msau42

Copy link
Contributor

Choose a reason for hiding this comment

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

#107065 is the PR where the new global path is introduced (1.24). The PR states that the node need to be drained explicitly in its release note. Change node staging path for csi driver to use a PV agnostic path. Nodes must be drained before updating the kubelet with this change.

So I think if vol_data.json does not exist, we can skip the else part of the code to parse the path. [Note: this also means that this PR can be cherry-picked to >= 1.24, since we already have a dependency to drain node when upgrading from < 1.24 to 1.24]. So +1 from my side to remove the fallback mountpath parsing

@cvvz
Copy link
Member Author

cvvz commented Mar 8, 2023

Thanks for putting together the diagram! What if after checking IsOperationPending, we try to get a fresh volume state? Will that solve the TOCTOU?

How about making deviceMountState as a pointer? So we won't get the stale state.

@sunnylovestiramisu
Copy link
Contributor

Thanks for the diagram. Is removing the vol_data.json file last step of unmount? Will there be any failures after that step?

@cvvz
Copy link
Member Author

cvvz commented Mar 9, 2023

Yes it is. kubelet will only remove vol_data.json after NodeUnstageVolume succeed.https://github.com/kubernetes/kubernetes/blob/master/pkg/volume/csi/csi_attacher.go#L635-L646

@andyzhangx
Copy link
Member

andyzhangx commented Mar 13, 2023

This issue was consistently reproducible after ~5 runs of a 1K disk attach/detach load test. However, with this PR, 100 test runs of the same load test were conducted in k8s 1.25.5 and the issue no longer occurred.

@andyzhangx
Copy link
Member

/priority important-soon
/sig storage

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyzhangx, cvvz, jsafrane

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 30, 2023
@andyzhangx
Copy link
Member

@jsafrane shall we merge this PR in milestone v1.27?

@jsafrane
Copy link
Member

jsafrane commented Apr 3, 2023

I'd wait for 1.27 GA and then open cherry-picks.

@k8s-ci-robot k8s-ci-robot merged commit de10997 into kubernetes:master Apr 11, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.28 milestone Apr 11, 2023
@ialidzhikov
Copy link
Contributor

Can we now proceed with the backports? Thanks in advance!

@ialidzhikov
Copy link
Contributor

@cvvz can we backport to all affected releases? Currently I see only a backport for the release-1.27 branch.

@cvvz
Copy link
Member Author

cvvz commented Apr 14, 2023

I will backport to 1.25 and 1.26

@humblec
Copy link
Contributor

humblec commented Apr 14, 2023

@cvvz can we have the 1.24 backport too ?

@dguendisch
Copy link

While I cannot answer if you "have to", I can only second that we see this issue massively on 1.24 clusters and a back port would be super welcomed 😊

@cvvz
Copy link
Member Author

cvvz commented Apr 14, 2023

sure, backport to 1.24

@humblec
Copy link
Contributor

humblec commented Apr 14, 2023

While I cannot answer if you "have to", I can only second that we see this issue massively on 1.24 clusters and a back port would be super welcomed 😊

Thanks for the info! , I was looking into 1.24 code and was trying to confirm :)

Thanks @cvvz for filing it ! 👍

k8s-ci-robot added a commit that referenced this pull request May 4, 2023
…-origin-release-1.24

Automated cherry pick of #116138: fix: the volume is not detached after the pod and PVC objects
k8s-ci-robot added a commit that referenced this pull request May 4, 2023
…-origin-release-1.26

Automated cherry pick of #116138: fix: the volume is not detached after the pod and PVC objects
k8s-ci-robot added a commit that referenced this pull request May 4, 2023
…-origin-release-1.25

Automated cherry pick of #116138: fix: the volume is not detached after the pod and PVC objects
k8s-ci-robot added a commit that referenced this pull request May 4, 2023
…-origin-release-1.27

Automated cherry pick of #116138: fix: the volume is not detached after the pod and PVC objects
@andyzhangx
Copy link
Member

FYI. this issue is fixed in 1.24.14, 1.25.10, 1.26.5, and has been fixed in v1.27.0

@shadowdsp
Copy link

@cvvz can we have the 1.19 backport? Thanks

@mattcary
Copy link
Contributor

@cvvz can we have the 1.19 backport? Thanks

It's only possible to backport to active k8s releases. Branches for end-of-life versions have been frozen in my understanding. So we can't backport past 1.24: https://kubernetes.io/releases/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/storage Categorizes an issue or PR as relevant to SIG Storage. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the volume is not detached after the pod and PVC objects are deleted