github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/volume/rm.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package volume
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  
    24  	"github.com/containerd/containerd"
    25  	"github.com/containerd/nerdctl/v2/pkg/api/types"
    26  	"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
    27  	"github.com/containerd/nerdctl/v2/pkg/labels"
    28  	"github.com/containerd/nerdctl/v2/pkg/mountutil"
    29  )
    30  
    31  func Remove(ctx context.Context, client *containerd.Client, volumes []string, options types.VolumeRemoveOptions) error {
    32  	containers, err := client.Containers(ctx)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	volStore, err := Store(options.GOptions.Namespace, options.GOptions.DataRoot, options.GOptions.Address)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	usedVolumes, err := usedVolumes(ctx, containers)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	var volumenames []string // nolint: prealloc
    46  	for _, name := range volumes {
    47  		volume, err := volStore.Get(name, false)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		if _, ok := usedVolumes[volume.Name]; ok {
    52  			return fmt.Errorf("volume %q is in use", name)
    53  		}
    54  		volumenames = append(volumenames, name)
    55  	}
    56  	removedNames, err := volStore.Remove(volumenames)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	for _, name := range removedNames {
    61  		fmt.Fprintln(options.Stdout, name)
    62  	}
    63  	return err
    64  }
    65  
    66  func usedVolumes(ctx context.Context, containers []containerd.Container) (map[string]struct{}, error) {
    67  	usedVolumes := make(map[string]struct{})
    68  	for _, c := range containers {
    69  		l, err := c.Labels(ctx)
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  		mountsJSON, ok := l[labels.Mounts]
    74  		if !ok {
    75  			continue
    76  		}
    77  
    78  		var mounts []dockercompat.MountPoint
    79  		err = json.Unmarshal([]byte(mountsJSON), &mounts)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  		for _, m := range mounts {
    84  			if m.Type == mountutil.Volume {
    85  				usedVolumes[m.Name] = struct{}{}
    86  			}
    87  		}
    88  	}
    89  	return usedVolumes, nil
    90  }