-
Notifications
You must be signed in to change notification settings - Fork 634
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
support system prune #1264
Merged
Merged
support system prune #1264
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newSystemPruneCommand() *cobra.Command { | ||
systemPruneCommand := &cobra.Command{ | ||
Use: "prune [flags]", | ||
Short: "Remove unused data", | ||
Args: cobra.NoArgs, | ||
RunE: systemPruneAction, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
systemPruneCommand.Flags().BoolP("all", "a", false, "Remove all unused images, not just dangling ones") | ||
systemPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation") | ||
systemPruneCommand.Flags().Bool("volumes", false, "Prune volumes") | ||
return systemPruneCommand | ||
} | ||
|
||
func systemPruneAction(cmd *cobra.Command, args []string) error { | ||
all, err := cmd.Flags().GetBool("all") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !all { | ||
logrus.Warn("Currently, `nerdctl system prune` requires --all to be specified. Skip pruning.") | ||
// NOP | ||
return nil | ||
} | ||
|
||
vFlag, err := cmd.Flags().GetBool("volumes") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
force, err := cmd.Flags().GetBool("force") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !force { | ||
var confirm string | ||
msg := `This will remove: | ||
- all stopped containers | ||
- all networks not used by at least one container` | ||
if vFlag { | ||
msg += ` | ||
- all volumes not used by at least one container` | ||
} | ||
msg += ` | ||
- all images without at least one container associated to them | ||
` | ||
msg += "\nAre you sure you want to continue? [y/N] " | ||
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg) | ||
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) | ||
|
||
if strings.ToLower(confirm) != "y" { | ||
return nil | ||
} | ||
} | ||
|
||
client, ctx, cancel, err := newClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
if err := containerPrune(cmd, client, ctx); err != nil { | ||
return err | ||
} | ||
if err := networkPrune(cmd, client, ctx); err != nil { | ||
return err | ||
} | ||
if vFlag { | ||
if err := volumePrune(cmd, client, ctx); err != nil { | ||
return err | ||
} | ||
} | ||
return imagePrune(cmd, client, ctx) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
) | ||
|
||
func TestSystemPrune(t *testing.T) { | ||
base := testutil.NewBase(t) | ||
base.Cmd("container", "prune", "-f").AssertOK() | ||
base.Cmd("network", "prune", "-f").AssertOK() | ||
base.Cmd("volume", "prune", "-f").AssertOK() | ||
base.Cmd("image", "prune", "-f", "--all").AssertOK() | ||
|
||
nID := testutil.Identifier(t) | ||
base.Cmd("network", "create", nID).AssertOK() | ||
defer base.Cmd("network", "rm", nID).Run() | ||
|
||
vID := testutil.Identifier(t) | ||
base.Cmd("volume", "create", vID).AssertOK() | ||
defer base.Cmd("volume", "rm", vID).Run() | ||
|
||
tID := testutil.Identifier(t) | ||
base.Cmd("run", "-v", fmt.Sprintf("%s:/volume", vID), "--net", nID, | ||
"--name", tID, testutil.CommonImage).AssertOK() | ||
defer base.Cmd("rm", "-f", tID).Run() | ||
|
||
base.Cmd("ps", "-a").AssertOutContains(tID) | ||
base.Cmd("images").AssertOutContains("alpine") | ||
|
||
base.Cmd("system", "prune", "-f", "--volumes", "--all").AssertOK() | ||
base.Cmd("volume", "ls").AssertNoOut(vID) | ||
base.Cmd("ps", "-a").AssertNoOut(tID) | ||
base.Cmd("network", "ls").AssertNoOut(nID) | ||
base.Cmd("images").AssertNoOut("alpine") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we prune builder cache too?
Can be another PR though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I will make another PR for it.