Skip to content

Commit

Permalink
Added volume remove fixes(issue #971)
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Mathew <raymond.mathew@click2cloud.net>
  • Loading branch information
click2cloud-lamda committed Apr 11, 2022
1 parent 53bc54e commit cfdeb60
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions cmd/nerdctl/volume_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
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"
)

Expand All @@ -39,14 +43,51 @@ func newVolumeRmCommand() *cobra.Command {
}

func volumeRmAction(cmd *cobra.Command, args []string) error {
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
}
defer cancel()
containers, _ := client.Containers(ctx)
var mount *specs.Spec
var removednames []string
volStore, err := getVolumeStore(cmd)
if err != nil {
return err
}
names := args
removedNames, err := volStore.Remove(names)
for _, removed := range removedNames {
fmt.Fprintln(cmd.OutOrStdout(), removed)

for _, name := range names {
var found bool
for _, container := range containers {
n, err := containerinspector.Inspect(ctx, container)
if err != nil {
panic(err)
}
err = json.Unmarshal(n.Container.Spec.Value, &mount)
if err != nil {
return err
}
volGet, _ := volStore.Get(name)
if found = checkVolume(&mount.Mounts, volGet.Mountpoint); found {
found = true
var verr error
verr = fmt.Errorf("%s %q %s", "Volume", name, "is in use")
logrus.WithError(verr).Error(fmt.Errorf("%s %q %s", "Remove Volume:", name, "failed"))
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 err
}
Expand All @@ -55,3 +96,13 @@ func volumeRmShellComplete(cmd *cobra.Command, args []string, toComplete string)
// show volume names
return shellCompleteVolumeNames(cmd)
}

// checkVolume checks whether the container mount path and the given volume mount point are same or not.
func checkVolume(mounts *[]specs.Mount, volmountpoint string) bool {
for _, mount := range *mounts {
if mount.Source == volmountpoint {
return true
}
}
return false
}

0 comments on commit cfdeb60

Please sign in to comment.