forked from containerd/nerdctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added fix for container prune and volume prune(issue containerd#648)
Signed-off-by: Raymond Mathew <[email protected]>
- Loading branch information
1 parent
8385be4
commit 7bc67f2
Showing
4 changed files
with
195 additions
and
0 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
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 | ||
} |
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,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 | ||
} |