Skip to content

feat: use stricter regex to filter network labels on the container. #4319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions pkg/cmd/network/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"

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

network := netList[0]

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

isNetworkMember, err := isContainerInNetwork(ctx, container, network.Name)
if err != nil {
return err
}
if isNetworkMember {
containers = append(containers, nativeContainer)
}
containers = append(containers, nativeContainer)
}

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

return err
}

func isContainerInNetwork(ctx context.Context, container containerd.Container, networkName string) (bool, error) {
info, err := container.Info(ctx)
if err != nil {
return false, err
}
networkLabels, ok := info.Labels[labels.Networks]
if !ok {
return false, nil
}

var containerNetworks []string
if err := json.Unmarshal([]byte(networkLabels), &containerNetworks); err != nil {
return false, err
}
return slices.Contains(containerNetworks, networkName), nil
}
Loading