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

Skip unused volumes in VolumeManager #81163

Merged
merged 3 commits into from
Aug 17, 2019

Conversation

jsafrane
Copy link
Member

@jsafrane jsafrane commented Aug 8, 2019

DesiredStateOfWorldPopulator should skip a volume that is not used in any pod. "Used" means either mounted (via volumeMounts) or used as raw block device (via volumeDevices) in a container in the pod.

Especially when block feature is disabled, a block volume must not get into DesiredStateOfWorld, because it would be formatted and mounted there, potentially overwriting any existing raw block data.

/kind bug
Fixes #76044

Does this PR introduce a user-facing change?:

Volumes specified in a pod but not used in it are no longer unnecessarily formatted, mounted and reported in `node.status.volumesInUse`.

Change of Kubernetes behavior
Previously, all volumes were reported in node.status.volumesInUse and they were mounted / mapped as raw block devices in kubelet. With this PR, only the volumes that are actually used will be reported there and mounted/mapped. In most cases this won't make a difference (why would anyone put a volume to a pod and then not use it in any container?)

cc: @wongma7 @msau42 @bertinatto

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 8, 2019
@k8s-ci-robot k8s-ci-robot requested review from msau42 and rootfs August 8, 2019 14:03
@wongma7
Copy link
Contributor

wongma7 commented Aug 8, 2019

My old attempt to fix this looks extremely convoluted... What happens in kubelet-side WaitForAttachAndMount since it also reads spec.volumes?

for _, podVolume := range pod.Spec.Volumes {

@mattjmcnaughton
Copy link
Contributor

I believe that the pull-kubernetes-bazel-test failure is legit:

 --- FAIL: TestGetMountedVolumesForPodAndGetVolumesInUse (123.00s)
    volume_manager_test.go:80: Expected success: unmounted volumes=[vol1], unattached volumes=[vol1]: timed out waiting for the condition 

My guess is that the other failures are flake... retesting them now to verify.

/test pull-kubernetes-integration
/test pull-kubernetes-kubemark-e2e-gce-big
/test pull-kubernetes-kubemark-e2e-gce-csi-serial

@wongma7
Copy link
Contributor

wongma7 commented Aug 8, 2019

Yes, test failure is legit, @jsafrane volumemanager needs some kind of fix as well since it gets list of volumes to wait for from spec.volumes instead of desired state. I'm sure you can fix it in a nicer way than I did.

@msau42
Copy link
Member

msau42 commented Aug 9, 2019

/assign @jingxu97

@jsafrane jsafrane force-pushed the skip-unused-volumes branch from 7506c53 to fb4c781 Compare August 9, 2019 13:25
@k8s-ci-robot k8s-ci-robot removed the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 9, 2019
@jsafrane
Copy link
Member Author

jsafrane commented Aug 9, 2019

volumemanager needs some kind of fix as well since it gets list of volumes to wait for from spec.volumes instead of desired state. I'm sure you can fix it in a nicer way than I did.

You're right, I reworked getExpectedVolumes a bit. I used the simplest solution I could think of, not sure it's nicer.

In addition, I moved most of the code to processPodVolumes(), the code got a bit cleaner, but then I needed to pass fake BlockVolumePathHandler to NewVolumeManager for unit tests, so the PR grew a lot...

/hold

TODO:

  • Add e2e test with unused volume
  • Squash the commits

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 9, 2019
@jsafrane jsafrane force-pushed the skip-unused-volumes branch from fb4c781 to 46589a0 Compare August 9, 2019 13:42
@jsafrane
Copy link
Member Author

@wongma7, can you please take a look at the current status? I'll squash the commits if this is the way to go.

I could not find any elegant way how to e2e test this. From storage test I can't get the global mount directory or name to check in node.status.volumesInUse easily. I could compare volumesInUse before and after a test pod was created, but that 1) would imply [Serial], 2) it's quite ugly and 3) I am not sure it would work for all plugins. Any ideas?

@wongma7
Copy link
Contributor

wongma7 commented Aug 12, 2019

IMO it doesn't need to work for all plugins. Maybe just gcepd, e.g.

func waitForPDInVolumesInUse(
.

Initially I was thinking to ssh into the node and parse the mounts. Then you could e.g. make sure that the test pod UID has 0 (non secret/token etc.)* mounts.


for _, podVolume := range pod.Spec.Volumes {
expectedVolumes = append(expectedVolumes, podVolume.Name)
for _, container := range pod.Spec.Containers {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about initContainers?

Copy link
Member Author

@jsafrane jsafrane Aug 13, 2019

Choose a reason for hiding this comment

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

Good catch!

I copied the code from VolumeManager and it observes only .containers, not .initContainers. Filed #81343.

I think the best would be to fix makeVolumeMap in #81343 first (to have a small patch), backport it everywhere and then move it to pkg/volume/utils that will be used also here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wrote too early, the issue is reproducible only in this PR.

I extracted GetPodVolumeNames and used it consistently in VolumeManager and DSWP.

pvMode: v1.PersistentVolumeBlock,
podMode: v1.PersistentVolumeFilesystem,
expectMount: false,
expectError: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

where does the error come from, is it a timeout or won't expectedVolumes have len 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not timeout, DSWP has stored error into DSW and WaitForAttachAndMount asynchronously picked it up within ~podAttachAndMountRetryInterval (300ms)

@jsafrane jsafrane force-pushed the skip-unused-volumes branch from 46589a0 to a572ff0 Compare August 13, 2019 10:49
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 13, 2019
@k8s-ci-robot k8s-ci-robot added area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/test sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Aug 13, 2019
@jsafrane jsafrane force-pushed the skip-unused-volumes branch from a572ff0 to 7a9d142 Compare August 13, 2019 11:04
@jsafrane
Copy link
Member Author

Initially I was thinking to ssh into the node and parse the mounts. Then you could e.g. make sure that the test pod UID has 0 (non secret/token etc.)* mounts.

Ha, we mount unused volumes even to pod directory. I can test that.

The function will be handy in subsequent patches. Also change custom maps
into sets.String.
DesiredStateOfWorldPopulator should skip a volume that is not used in any
pod. "Used" means either mounted (via volumeMounts) or used as raw block
device (via volumeDevices).

Especially when block feature is disabled, a block volume must not get into
DesiredStateOfWorld, because it would be formatted and mounted there.
@jsafrane jsafrane force-pushed the skip-unused-volumes branch from 7a9d142 to 2c79ffe Compare August 15, 2019 09:07
@jsafrane
Copy link
Member Author

Added e2e test. Now it should be complete.

@jsafrane
Copy link
Member Author

e2e test added, commits squashed a bit
/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 15, 2019
@jsafrane
Copy link
Member Author

/retest

mounts = sets.NewString()
devices = sets.NewString()

addContainerVolumes(pod.Spec.Containers, mounts, devices)
Copy link
Member

Choose a reason for hiding this comment

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

This may need to be updated again after #59484

@wongma7
Copy link
Contributor

wongma7 commented Aug 15, 2019

/lgtm
beautiful!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 15, 2019
@jsafrane
Copy link
Member Author

/assign @tallclair @derekwaynecarr
for approval from node point of view

Note that there is a small behavior change - before this PR, kubelet mounted / mapped as raw block everything that was in pod.spec.volumes, now it mounts / maps only things that are in pod.spec.containers[*].volumeMounts / volumeDevices.

@derekwaynecarr
Copy link
Member

kubelet changes lgtm.

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: derekwaynecarr, 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 Aug 16, 2019
@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@k8s-ci-robot k8s-ci-robot merged commit e319abf into kubernetes:master Aug 17, 2019
@k8s-ci-robot k8s-ci-robot added this to the v1.16 milestone Aug 17, 2019
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. area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/kubelet area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block PV may be mounted if kubelet feature gate disabled
9 participants