X Tutup
The Wayback Machine - https://web.archive.org/web/20200929025930/https://github.com/moby/moby/pull/41449
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

Added simple logic for dry run on prune containers #41449

Open
wants to merge 1 commit into
base: master
from

Conversation

@kaushik94
Copy link

kaushik94 commented Sep 14, 2020

changed:
daemon/prune.go

TODO: Add tests

Signed-off-by: Kaushik Varanasi kaushik.varanasi1@gmail.com

- What I did
Implemented a simple dry run mode for containers

- How I did it
When checking for containers that are stale, moby does a daemon.ContainerRm. In dry run mode, I simply disabled this line and returned the container id and size to be removed.

- How to verify it
do docker run hello-world
docker containers prune --dry-run
use this branch for cli: docker/cli#2698

- Description for the changelog

changed:
daemon/prune.go

TODO: Add tests

- A picture of a cute animal (not mandatory but encouraged)
cute-cat-photos-1593441022

changed:
daemon/prune.go

TODO: Add tests

Signed-off-by: Kaushik Varanasi <kaushik.varanasi1@gmail.com>
@@ -26,6 +26,7 @@ var (
"label": true,
"label!": true,
"until": true,
"dryRun": true,

This comment has been minimized.

@cpuguy83

cpuguy83 Sep 14, 2020 Contributor

I would probably promote dryRun to a full option rather than a filter argument.

This comment has been minimized.

@kaushik94

kaushik94 Sep 14, 2020 Author

@cpuguy83 when I remove this line, it is complaining me invalid parameters.

This comment has been minimized.

@cpuguy83

cpuguy83 Sep 14, 2020 Contributor

This is probably because you are passing dryRun as a filter rather than a configuration option.
It looks like we don't currently accept any options on prune other than filter.

This would need to be added at the API level.

This comment has been minimized.

@kaushik94

kaushik94 Sep 14, 2020 Author

@cpuguy83 can you give an example of any top level options. Like --all? Can you show me the file to add this.

This comment has been minimized.

@cpuguy83

cpuguy83 Sep 14, 2020 Contributor

It will need to be decoded here:

func (s *containerRouter) postContainersPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
if err != nil {
return errdefs.InvalidParameter(err)
}
pruneReport, err := s.backend.ContainersPrune(ctx, pruneFilters)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
}

The backend interface definition will need to be updated here:

ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error)

The options will need to be encoded here:

func (cli *Client) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
var report types.ContainersPruneReport
if err := cli.NewVersionError("1.25", "container prune"); err != nil {
return report, err
}
query, err := getFiltersQuery(pruneFilters)
if err != nil {
return report, err
}
serverResp, err := cli.post(ctx, "/containers/prune", query, nil, nil)
defer ensureReaderClosed(serverResp)
if err != nil {
return report, err
}
if err := json.NewDecoder(serverResp.body).Decode(&report); err != nil {
return report, fmt.Errorf("Error retrieving disk usage: %v", err)
}
return report, nil
}

}
if cSize > 0 {
rep.SpaceReclaimed += uint64(cSize)
if (!dryRunMode) {

This comment has been minimized.

@cpuguy83

cpuguy83 Sep 14, 2020 Contributor

This is duplicated in the else block.
I think this can be:

Suggested change
if (!dryRunMode) {
if !dryRunMode {
if err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{}); err != nil {
logrus.Warnf("failed to prune container %s: %v", c.ID, err)
continue
}
}
if cSize > 0 {
rep.SpaceReclaimed += uint64(cSize)
}
rep.ContainersDeleted = append(rep.ContainersDeleted, c.ID)

Note: Don't use GH's UI to accept the change, as the commit won't be signed and it will be rejected by CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.
X Tutup