Skip to content

Commit b2f3b6c

Browse files
committed
feat: use stricter regex to filter network labels on the container.
This allows us to skip double checking the network names on the container with an additional helper function. Signed-off-by: Tushar Gupta <[email protected]>
1 parent 190a713 commit b2f3b6c

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

pkg/cmd/network/inspect.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24-
"slices"
2524

2625
containerd "github.com/containerd/containerd/v2/client"
2726
"github.com/containerd/log"
@@ -60,7 +59,7 @@ func Inspect(ctx context.Context, client *containerd.Client, options types.Netwo
6059

6160
network := netList[0]
6261

63-
var filters = []string{fmt.Sprintf("labels.%q~=%q", labels.Networks, network.Name)}
62+
var filters = []string{fmt.Sprintf(`labels.%q~="\\\"%s\\\""`, labels.Networks, network.Name)}
6463
filteredContainers, err := client.Containers(ctx, filters...)
6564
if err != nil {
6665
return err
@@ -76,13 +75,7 @@ func Inspect(ctx context.Context, client *containerd.Client, options types.Netwo
7675
continue
7776
}
7877

79-
isNetworkMember, err := isContainerInNetwork(ctx, container, network.Name)
80-
if err != nil {
81-
return err
82-
}
83-
if isNetworkMember {
84-
containers = append(containers, nativeContainer)
85-
}
78+
containers = append(containers, nativeContainer)
8679
}
8780

8881
r := &native.Network{
@@ -119,20 +112,3 @@ func Inspect(ctx context.Context, client *containerd.Client, options types.Netwo
119112

120113
return err
121114
}
122-
123-
func isContainerInNetwork(ctx context.Context, container containerd.Container, networkName string) (bool, error) {
124-
info, err := container.Info(ctx)
125-
if err != nil {
126-
return false, err
127-
}
128-
networkLabels, ok := info.Labels[labels.Networks]
129-
if !ok {
130-
return false, nil
131-
}
132-
133-
var containerNetworks []string
134-
if err := json.Unmarshal([]byte(networkLabels), &containerNetworks); err != nil {
135-
return false, err
136-
}
137-
return slices.Contains(containerNetworks, networkName), nil
138-
}

0 commit comments

Comments
 (0)