(ctx context.Context, client *containerd.Client, options types.NetworkPruneOptions)
| 29 | ) |
| 30 | |
| 31 | func Prune(ctx context.Context, client *containerd.Client, options types.NetworkPruneOptions) error { |
| 32 | e, err := netutil.NewCNIEnv(options.GOptions.CNIPath, options.GOptions.CNINetConfPath, netutil.WithNamespace(options.GOptions.Namespace)) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | usedNetworks, err := netutil.UsedNetworks(ctx, client) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | networkConfigs, err := e.NetworkList() |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | var removedNetworks []string // nolint: prealloc |
| 48 | for _, net := range networkConfigs { |
| 49 | if strutil.InStringSlice(options.NetworkDriversToKeep, net.Name) { |
| 50 | continue |
| 51 | } |
| 52 | if net.NerdctlID == nil || net.File == "" { |
| 53 | continue |
| 54 | } |
| 55 | if _, ok := usedNetworks[net.Name]; ok { |
| 56 | continue |
| 57 | } |
| 58 | if err := e.RemoveNetwork(net); err != nil { |
| 59 | log.G(ctx).WithError(err).Errorf("failed to remove network %s", net.Name) |
| 60 | continue |
| 61 | } |
| 62 | removedNetworks = append(removedNetworks, net.Name) |
| 63 | } |
| 64 | |
| 65 | if len(removedNetworks) > 0 { |
| 66 | fmt.Fprintln(options.Stdout, "Deleted Networks:") |
| 67 | for _, name := range removedNetworks { |
| 68 | fmt.Fprintln(options.Stdout, name) |
| 69 | } |
| 70 | fmt.Fprintln(options.Stdout, "") |
| 71 | } |
| 72 | return nil |
| 73 | } |
no test coverage detected
searching dependent graphs…