(ctx context.Context, client *containerd.Client, options types.NetworkInspectOptions)
| 35 | ) |
| 36 | |
| 37 | func Inspect(ctx context.Context, client *containerd.Client, options types.NetworkInspectOptions) error { |
| 38 | if options.Mode != "native" && options.Mode != "dockercompat" { |
| 39 | return fmt.Errorf("unknown mode %q", options.Mode) |
| 40 | } |
| 41 | |
| 42 | cniEnv, err := netutil.NewCNIEnv(options.GOptions.CNIPath, options.GOptions.CNINetConfPath, netutil.WithNamespace(options.GOptions.Namespace)) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | var result []interface{} |
| 48 | netLists, errs := cniEnv.ListNetworksMatch(options.Networks, true) |
| 49 | |
| 50 | for req, netList := range netLists { |
| 51 | if len(netList) > 1 { |
| 52 | errs = append(errs, fmt.Errorf("multiple IDs found with provided prefix: %s", req)) |
| 53 | continue |
| 54 | } |
| 55 | if len(netList) == 0 { |
| 56 | errs = append(errs, fmt.Errorf("no network found matching: %s", req)) |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | network := netList[0] |
| 61 | |
| 62 | var filters = []string{fmt.Sprintf(`labels.%q~="\\\"%s\\\""`, labels.Networks, network.Name)} |
| 63 | filteredContainers, err := client.Containers(ctx, filters...) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | var containers []*native.Container |
| 69 | for _, container := range filteredContainers { |
| 70 | nativeContainer, err := containerinspector.Inspect(ctx, container) |
| 71 | if err != nil { |
| 72 | continue |
| 73 | } |
| 74 | if nativeContainer.Process == nil || nativeContainer.Process.Status.Status != containerd.Running { |
| 75 | continue |
| 76 | } |
| 77 | |
| 78 | containers = append(containers, nativeContainer) |
| 79 | } |
| 80 | |
| 81 | r := &native.Network{ |
| 82 | CNI: json.RawMessage(network.Bytes), |
| 83 | NerdctlID: network.NerdctlID, |
| 84 | NerdctlLabels: network.NerdctlLabels, |
| 85 | File: network.File, |
| 86 | Containers: containers, |
| 87 | } |
| 88 | switch options.Mode { |
| 89 | case "native": |
| 90 | result = append(result, r) |
| 91 | case "dockercompat": |
| 92 | compat, err := dockercompat.NetworkFromNative(r) |
| 93 | if err != nil { |
| 94 | return err |
no test coverage detected
searching dependent graphs…