Skip to content
Merged
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
}