Skip to content

Commit 15fedb7

Browse files
committed
fix: add containers response in network inspect
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
1 parent 356ed67 commit 15fedb7

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,5 @@ replace cyphar.com/go-pathrs => github.com/cyphar/libpathrs/go-pathrs v0.2.1
206206

207207
// Pin docker cli version to v28.5.2+incompatible until we migrate to moby/moby/v2
208208
replace github.com/docker/cli => github.com/docker/cli v28.5.2+incompatible
209+
210+
replace github.com/containerd/nerdctl/v2 => github.com/coderbirju/nerdctl/v2 v2.3.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ github.com/cilium/ebpf v0.20.0 h1:atwWj9d3NffHyPZzVlx3hmw1on5CLe9eljR8VuHTwhM=
3333
github.com/cilium/ebpf v0.20.0/go.mod h1:pzLjFymM+uZPLk/IXZUL63xdx5VXEo+enTzxkZXdycw=
3434
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
3535
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
36+
github.com/coderbirju/nerdctl/v2 v2.3.1 h1:26poRroA+iGMjHbviqcWSk36LrPN0GYklVkEXCG7GJY=
37+
github.com/coderbirju/nerdctl/v2 v2.3.1/go.mod h1:Ogh1WVvN6G5GhFgGZxDeGtV0XKi1RiN44aNbJDHia4M=
3638
github.com/compose-spec/compose-go/v2 v2.10.0 h1:K2C5LQ3KXvkYpy5N/SG6kIYB90iiAirA9btoTh/gB0Y=
3739
github.com/compose-spec/compose-go/v2 v2.10.0/go.mod h1:Ohac1SzhO/4fXXrzWIztIVB6ckmKBv1Nt5Z5mGVESUg=
3840
github.com/containerd/accelerated-container-image v1.3.0 h1:sFbTgSuMboeKHa9f7MY11hWF1XxVWjFoiTsXYtOtvdU=
@@ -63,8 +65,6 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
6365
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
6466
github.com/containerd/nerdctl/mod/tigron v0.0.0-20250720235051-d775a8c42fbb h1:qzxCinSHfPvhBoiMGCDl00KxQpnvFPzfW4JpYZfJ7mo=
6567
github.com/containerd/nerdctl/mod/tigron v0.0.0-20250720235051-d775a8c42fbb/go.mod h1:gmUZh2wUVxr/msGogKUi6v9eJbP5ASO4fVYEPzHH4iI=
66-
github.com/containerd/nerdctl/v2 v2.2.1 h1:eXBu4HddMEXtLmZsJncfvOiJ6a8DemOzWhOxypNIEG0=
67-
github.com/containerd/nerdctl/v2 v2.2.1/go.mod h1:Ogh1WVvN6G5GhFgGZxDeGtV0XKi1RiN44aNbJDHia4M=
6868
github.com/containerd/nydus-snapshotter v0.15.10 h1:hphjuKOqSHLGznNJiAvmsOWkdu4qFXjf4DzGrWSuIsM=
6969
github.com/containerd/nydus-snapshotter v0.15.10/go.mod h1:EWRd/QJ0b6UKHAqYgiV5gHlqLC2qq5cQiSlXEdVovrA=
7070
github.com/containerd/platforms v1.0.0-rc.2 h1:0SPgaNZPVWGEi4grZdV8VRYQn78y+nm6acgLGv/QzE4=

internal/backend/containerd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ContainerdClient interface {
3333
GetClient() *containerd.Client
3434
GetContainerStatus(ctx context.Context, c containerd.Container) containerd.ProcessStatus
3535
SearchContainer(ctx context.Context, searchText string) (containers []containerd.Container, err error)
36+
GetContainers(ctx context.Context, filters ...string) (containers []containerd.Container, err error)
3637
GetImage(ctx context.Context, ref string) (containerd.Image, error)
3738
SearchImage(ctx context.Context, searchText string) ([]images.Image, error)
3839
ParsePlatform(platform string) (ocispec.Platform, error)
@@ -110,6 +111,12 @@ func (w *ContainerdClientWrapper) SearchContainer(ctx context.Context, searchTex
110111
return containers, err
111112
}
112113

114+
// GetContainers returns the list of containers that match the given filters.
115+
func (w *ContainerdClientWrapper) GetContainers(ctx context.Context, filters ...string) (containers []containerd.Container, err error) {
116+
containers, err = w.client.Containers(ctx, filters...)
117+
return containers, err
118+
}
119+
113120
// GetImage returns an image with given reference.
114121
func (w *ContainerdClientWrapper) GetImage(ctx context.Context, ref string) (containerd.Image, error) {
115122
return w.client.GetImage(ctx, ref)

internal/backend/network.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ package backend
66
import (
77
"context"
88
"encoding/json"
9+
"fmt"
910

11+
containerd "github.com/containerd/containerd/v2/client"
1012
"github.com/containerd/nerdctl/v2/pkg/api/types"
13+
"github.com/containerd/nerdctl/v2/pkg/containerinspector"
1114
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
1215
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
16+
"github.com/containerd/nerdctl/v2/pkg/labels"
1317
"github.com/containerd/nerdctl/v2/pkg/netutil"
1418
"github.com/containernetworking/cni/libcni"
1519
cnitypes "github.com/containernetworking/cni/pkg/types"
@@ -54,11 +58,18 @@ func (w *NerdctlWrapper) RemoveNetwork(networkConfig *netutil.NetworkConfig) err
5458
}
5559

5660
func (w *NerdctlWrapper) InspectNetwork(ctx context.Context, networkConfig *netutil.NetworkConfig) (*dockercompat.Network, error) {
61+
// Get containers associated with this network
62+
containers, err := getContainersFromNetConfig(ctx, networkConfig, w.clientWrapper)
63+
if err != nil {
64+
return nil, fmt.Errorf("failed to get containers for network: %w", err)
65+
}
66+
5767
network := &native.Network{
5868
CNI: json.RawMessage(networkConfig.Bytes),
5969
NerdctlID: networkConfig.NerdctlID,
6070
NerdctlLabels: networkConfig.NerdctlLabels,
6171
File: networkConfig.File,
72+
Containers: containers,
6273
}
6374
return dockercompat.NetworkFromNative(network)
6475
}
@@ -74,3 +85,27 @@ func (w *NerdctlWrapper) NetconfPath() string {
7485
func (w *NerdctlWrapper) Namespace() string {
7586
return w.netClient.Namespace
7687
}
88+
89+
// getContainersFromNetConfig returns containers associated with the given network
90+
func getContainersFromNetConfig(ctx context.Context, networkConfig *netutil.NetworkConfig, client ContainerdClient) ([]*native.Container, error) {
91+
filters := []string{fmt.Sprintf(`labels.%q~="\\\"%s\\\""`, labels.Networks, networkConfig.Name)}
92+
filteredContainers, err := client.GetContainers(ctx, filters...)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var containers []*native.Container
98+
for _, container := range filteredContainers {
99+
nativeContainer, err := containerinspector.Inspect(ctx, container)
100+
if err != nil {
101+
continue
102+
}
103+
if nativeContainer.Process == nil || nativeContainer.Process.Status.Status != containerd.Running {
104+
continue
105+
}
106+
107+
containers = append(containers, nativeContainer)
108+
}
109+
110+
return containers, nil
111+
}

0 commit comments

Comments
 (0)