Repository navigation
fix(down): keep a volume the project does not own on down -v - #14374
Conversation
There was a problem hiding this comment.
The fix works as described: I reproduced #14373 on a real engine (down -v in the second project removes the first project's volume on main, skips it with this PR). Two changes requested inline: unlabeled volumes, and failing closed when the inspect fails.
Suggestion: add an e2e scenario (NewScenario, see pkg/e2e/SCENARIO.md) with two projects sharing a volume name:, checking that down -v in the second one keeps the first one's volume, and also an unlabeled one. It captures the intent and validates the behavior on a real engine rather than on mocks.
For reference, up already applies exactly this rule (ownedConfigHash in observed_state.go): a volume whose project label differs from the current project, or is missing, is reused untouched and never recreated. down -v should follow the same rule.
| // A volume declared with an explicit name can be owned by another project | ||
| // sharing that name: up only reuses it (see warnUnmanagedVolumes), so down | ||
| // must not remove it either. | ||
| if owner := res.Volume.Labels[api.ProjectLabel]; err == nil && owner != "" && owner != projectName { |
There was a problem hiding this comment.
The point of this PR is that Compose never removes a volume it did not create, so a volume without the project label should be skipped too, not only one labeled for another project: it carries no ownership either. up already treats it as unmanaged (already exists but was not created by Docker Compose) and reuses it untouched, so down -v removing it is inconsistent. I checked on a real engine: up warns, then down -v removes the volume.
So the rule becomes owner != projectName, with a message that tells the two cases apart, e.g. Volume belongs to project "x" vs Volume was not created by Docker Compose (no project label). The unit test should then expect unlabeled-data to be skipped as well.
There was a problem hiding this comment.
Done in 8ebcae8: the rule is now owner != projectName, so a volume without the project label is skipped too. The warning tells the two cases apart: Volume belongs to project "x" vs Volume was not created by Docker Compose. The unit test now expects unlabeled-data to be skipped, and TestDownRemoveVolumes returns a labeled volume from its inspect mock.
| resource := "Volume " + id | ||
|
|
||
| _, err := s.apiClient().VolumeInspect(ctx, id, client.VolumeInspectOptions{}) | ||
| res, err := s.apiClient().VolumeInspect(ctx, id, client.VolumeInspectOptions{}) |
There was a problem hiding this comment.
If the inspect fails with anything but NotFound, err is dropped and the volume is removed without any ownership check. The check should fail closed: return the error (or at least emit an error event, like removeNetwork does) rather than remove a volume we could not verify. A test case for it would be welcome.
There was a problem hiding this comment.
Done in 8d6fae0: an inspect error other than NotFound is now returned (failed to inspect volume <name>: <err>) and the volume is left in place. As with removeNetwork, this fails down. TestEnsureVolumesDown_KeepsVolumeWhenInspectFails covers it.
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
a02c604 to
bd7684a
Compare
|
@ndeloof
A scenario drives a single project, so the scenario creates the "other project" volume with |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
What this PR does
down -vno longer removes a named volume the project does not own: one labeled for another project, or one created outside Compose (no project label). Such a volume is skipped with a warning. This is the ruleupalready applies (ownedConfigHash): it reuses such a volume untouched. If the volume cannot be inspected,downnow fails instead of removing a volume it could not check.Related issue
Fixes #14373
Changes made
removeVolume(pkg/compose/down.go) checks thecom.docker.compose.projectlabel on the volume it already inspects, and skips the volume unless it belongs to this project. TheSkippedwarning tells the two cases apart:Volume belongs to project "x"vsVolume was not created by Docker Compose.failed to inspect volume <name>: <err>) and the volume is kept, the same wayremoveNetworkfails on an inspect error.TestEnsureVolumesDown_SkipsVolumeNotOwnedByProjectandTestEnsureVolumesDown_KeepsVolumeWhenInspectFails.TestDownRemoveVolumesnow returns a labeled volume from its inspect mock.TestDownVolumesKeepsVolumesNotOwned, plusVolumeExists/VolumeAbsentchecks inpkg/e2e/checks.go.Testing done
make testmake e2e-composeormake e2e-compose-standalonemake lintmake fmtAI tool used (if applicable)
AI Tool:
Additional context
(not mandatory) A picture of a cute animal, if possible in relation to what you did