-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ye Sijun <[email protected]>
- Loading branch information
Showing
9 changed files
with
234 additions
and
23 deletions.
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.