Skip to content

Commit

Permalink
Added fix for container prune and volume prune(issue containerd#648)
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Mathew <[email protected]>
  • Loading branch information
click2cloud-lamda committed Apr 20, 2022
1 parent 8385be4 commit 7bc67f2
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/nerdctl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func newContainerCommand() *cobra.Command {
newWaitCommand(),
newUnpauseCommand(),
newCommitCommand(),
newContainerPruneCommand(),
)
return containerCommand
}
Expand Down
88 changes: 88 additions & 0 deletions cmd/nerdctl/container_prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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"
"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/containerinspector"
"github.com/containerd/nerdctl/pkg/namestore"
"strings"

"github.com/containerd/containerd/namespaces"
"github.com/spf13/cobra"
)

func newContainerPruneCommand() *cobra.Command {
containerPruneCommand := &cobra.Command{
Use: "prune [flags]",
Short: "Remove all stopped containers",
RunE: containerPruneAction,
SilenceUsage: true,
SilenceErrors: true,
}
containerPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
return containerPruneCommand
}

func containerPruneAction(cmd *cobra.Command, _ []string) error {
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
}
defer cancel()
dataStore, err := getDataStore(cmd)
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
var confirm string
fmt.Fprintf(cmd.OutOrStdout(), "%s", "WARNING! This will remove all stopped containers.\nAre you sure you want to continue? [y/N] ")
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)

if strings.ToLower(confirm) != "y" {
return nil
}
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}

ctx = namespaces.WithNamespace(ctx, ns)

containers, err := client.Containers(ctx)
if err != nil {
return err
}
for _, container := range containers {
cont, _ := containerinspector.Inspect(ctx, container)
if cont.Process == nil || (cont.Process != nil && cont.Process.Status.Status != containerd.Running) {
containerNameStore, err := namestore.New(dataStore, ns)
if err != nil {
return err
}
stateDir, err := getContainerStateDirPath(cmd, dataStore, container.ID())
err = removeContainer(cmd, ctx, client, ns, container.ID(), "", force, dataStore, stateDir, containerNameStore, true)
if err != nil {
return err
}
}
}
return nil
}
1 change: 1 addition & 0 deletions cmd/nerdctl/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func newVolumeCommand() *cobra.Command {
newVolumeInspectCommand(),
newVolumeCreateCommand(),
newVolumeRmCommand(),
newVolumePruneCommand(),
)
return volumeCommand
}
Expand Down
105 changes: 105 additions & 0 deletions cmd/nerdctl/volume_prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 (
"encoding/json"
"fmt"
"github.com/containerd/nerdctl/pkg/containerinspector"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)

func newVolumePruneCommand() *cobra.Command {
volumeRmCommand := &cobra.Command{
Use: "prune",
Short: "Remove one or more volumes",
Long: "NOTE: volume in use is not deleted",
RunE: volumePruneAction,
SilenceUsage: true,
SilenceErrors: true,
}
volumeRmCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
return volumeRmCommand
}

func volumePruneAction(cmd *cobra.Command, args []string) error {
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
}
defer cancel()
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
var confirm string
fmt.Fprintf(cmd.OutOrStdout(), "%s", "WARNING! This will remove all local volumes not used by at least one container.\nAre you sure you want to continue? [y/N] ")
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)

if strings.ToLower(confirm) != "y" {
return nil
}
}
containers, err := client.Containers(ctx)
if err != nil {
return err
}
var mount *specs.Spec
var removednames []string
volumes, err := getVolumes(cmd)
if err != nil {
return err
}
volStore, err := getVolumeStore(cmd)
if err != nil {
return err
}
for name, voldetails := range volumes {
var found bool
for _, container := range containers {
n, err := containerinspector.Inspect(ctx, container)
if err != nil {
return err
}
err = json.Unmarshal(n.Container.Spec.Value, &mount)
if err != nil {
return err
}
if found = checkVolume(&mount.Mounts, voldetails.Mountpoint); found {
found = true
logrus.WithError(fmt.Errorf("Volume %q is in use", name)).Error(fmt.Errorf("Remove Volume: %q failed", name))
break
}
}
if !found {
// volumes that are not mounted to any container
removednames = append(removednames, name)
}
}
if removednames != nil {
rNames, err := volStore.Remove(removednames)
if err != nil {
panic(err)
}
fmt.Fprintln(cmd.OutOrStdout(), rNames)
}
return nil
}

0 comments on commit 7bc67f2

Please sign in to comment.