(ctx context.Context, client *containerd.Client, options types.NetworkRemoveOptions)
| 29 | ) |
| 30 | |
| 31 | func Remove(ctx context.Context, client *containerd.Client, options types.NetworkRemoveOptions) error { |
| 32 | cniEnv, err := netutil.NewCNIEnv(options.GOptions.CNIPath, options.GOptions.CNINetConfPath, netutil.WithNamespace(options.GOptions.Namespace)) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | usedNetworkInfo, err := netutil.UsedNetworks(ctx, client) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | var result []string |
| 43 | netLists, errs := cniEnv.ListNetworksMatch(options.Networks, false) |
| 44 | |
| 45 | for req, netList := range netLists { |
| 46 | if len(netList) > 1 { |
| 47 | errs = append(errs, fmt.Errorf("multiple IDs found with provided prefix: %s", req)) |
| 48 | continue |
| 49 | } |
| 50 | if len(netList) == 0 { |
| 51 | errs = append(errs, fmt.Errorf("no network found matching: %s", req)) |
| 52 | continue |
| 53 | } |
| 54 | network := netList[0] |
| 55 | if value, ok := usedNetworkInfo[network.Name]; ok { |
| 56 | errs = append(errs, fmt.Errorf("network %q is in use by container %q", req, value)) |
| 57 | continue |
| 58 | } |
| 59 | if network.Name == "bridge" { |
| 60 | errs = append(errs, errors.New("cannot remove pre-defined network bridge")) |
| 61 | continue |
| 62 | } |
| 63 | if network.File == "" { |
| 64 | errs = append(errs, fmt.Errorf("%s is a pre-defined network and cannot be removed", req)) |
| 65 | continue |
| 66 | } |
| 67 | if network.NerdctlID == nil { |
| 68 | errs = append(errs, fmt.Errorf("%s is managed outside nerdctl and cannot be removed", req)) |
| 69 | continue |
| 70 | } |
| 71 | if err := cniEnv.RemoveNetwork(network); err != nil { |
| 72 | errs = append(errs, err) |
| 73 | } else { |
| 74 | result = append(result, req) |
| 75 | } |
| 76 | } |
| 77 | for _, unErr := range errs { |
| 78 | log.G(ctx).Error(unErr) |
| 79 | } |
| 80 | if len(result) > 0 { |
| 81 | for _, id := range result { |
| 82 | fmt.Fprintln(options.Stdout, id) |
| 83 | } |
| 84 | err = nil |
| 85 | } else { |
| 86 | err = errors.New("no network could be removed") |
| 87 | } |
| 88 |
no test coverage detected
searching dependent graphs…