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

feat: Add "Containers" propery in the "nerdctl network inspect" command. #4052

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions cmd/nerdctl/network/network_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
"github.com/containerd/nerdctl/v2/pkg/cmd/network"
)

Expand Down Expand Up @@ -59,13 +60,22 @@ func inspectAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return network.Inspect(cmd.Context(), types.NetworkInspectOptions{

options := types.NetworkInspectOptions{
GOptions: globalOptions,
Mode: mode,
Format: format,
Networks: args,
Stdout: cmd.OutOrStdout(),
})
}

client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
if err != nil {
return err
}
defer cancel()

return network.Inspect(ctx, client, options)
}

func networkInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
37 changes: 37 additions & 0 deletions cmd/nerdctl/network/network_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

Expand Down Expand Up @@ -278,6 +279,42 @@ func TestNetworkInspect(t *testing.T) {
}
},
},
{
Description: "Verify that only active containers appear in the network inspect output",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("network", "create", data.Identifier("nginx-network-1"))
helpers.Ensure("network", "create", data.Identifier("nginx-network-2"))
helpers.Ensure("create", "--name", data.Identifier("nginx-container-1"), "--network", data.Identifier("nginx-network-1"), testutil.NginxAlpineImage)
helpers.Ensure("create", "--name", data.Identifier("nginx-container-2"), "--network", data.Identifier("nginx-network-1"), testutil.NginxAlpineImage)
helpers.Ensure("create", "--name", data.Identifier("nginx-container-on-diff-network"), "--network", data.Identifier("nginx-network-2"), testutil.NginxAlpineImage)
helpers.Ensure("start", data.Identifier("nginx-container-1"), data.Identifier("nginx-container-on-diff-network"))
data.Set("nginx-container-1-id", strings.Trim(helpers.Capture("inspect", data.Identifier("nginx-container-1"), "--format", "{{.Id}}"), "\n"))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier("nginx-container-1"))
helpers.Anyhow("rm", "-f", data.Identifier("nginx-container-2"))
helpers.Anyhow("rm", "-f", data.Identifier("nginx-container-on-diff-network"))
helpers.Anyhow("network", "remove", data.Identifier("nginx-network-1"))
helpers.Anyhow("network", "remove", data.Identifier("nginx-network-2"))
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("network", "inspect", data.Identifier("nginx-network-1"))
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: func(stdout string, info string, t *testing.T) {
var dc []dockercompat.Network
err := json.Unmarshal([]byte(stdout), &dc)
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results\n"+info)
assert.Equal(t, dc[0].Name, data.Identifier("nginx-network-1"))
// Assert only the "running" containers on the same network are returned.
assert.Equal(t, 1, len(dc[0].Containers), "Expected a single container as per configuration, but got multiple.")
assert.Equal(t, data.Identifier("nginx-container-1"), dc[0].Containers[data.Get("nginx-container-1-id")].Name)
},
}
},
},
}

testCase.Run(t)
Expand Down
28 changes: 27 additions & 1 deletion pkg/cmd/network/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ import (
"errors"
"fmt"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/log"

"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/containerinspector"
"github.com/containerd/nerdctl/v2/pkg/formatter"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/netutil"
)

func Inspect(ctx context.Context, options types.NetworkInspectOptions) error {
func Inspect(ctx context.Context, client *containerd.Client, options types.NetworkInspectOptions) error {
if options.Mode != "native" && options.Mode != "dockercompat" {
return fmt.Errorf("unknown mode %q", options.Mode)
}
Expand All @@ -53,12 +56,35 @@ func Inspect(ctx context.Context, options types.NetworkInspectOptions) error {
errs = append(errs, fmt.Errorf("no network found matching: %s", req))
continue
}

network := netList[0]
var filters = []string{fmt.Sprintf("labels.%q==%q", labels.Networks, []string{network.Name})}

filteredContainers, err := client.Containers(ctx, filters...)

if err != nil {
return err
}

var containers []*native.Container

for _, container := range filteredContainers {
nativeContainer, err := containerinspector.Inspect(ctx, container)
if err != nil {
continue
}
if nativeContainer.Process == nil || nativeContainer.Process.Status.Status != containerd.Running {
continue
}
containers = append(containers, nativeContainer)
}

r := &native.Network{
CNI: json.RawMessage(network.Bytes),
NerdctlID: network.NerdctlID,
NerdctlLabels: network.NerdctlLabels,
File: network.File,
Containers: containers,
}
switch options.Mode {
case "native":
Expand Down
28 changes: 24 additions & 4 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,13 +884,22 @@ type IPAM struct {
// Network mimics a `docker network inspect` object.
// From https://github.com/moby/moby/blob/v20.10.7/api/types/types.go#L430-L448
type Network struct {
Name string `json:"Name"`
ID string `json:"Id,omitempty"` // optional in nerdctl
IPAM IPAM `json:"IPAM,omitempty"`
Labels map[string]string `json:"Labels"`
Name string `json:"Name"`
ID string `json:"Id,omitempty"` // optional in nerdctl
IPAM IPAM `json:"IPAM,omitempty"`
Labels map[string]string `json:"Labels"`
Containers map[string]EndpointResource `json:"Containers"` // Containers contains endpoints belonging to the network
// Scope, Driver, etc. are omitted
}

type EndpointResource struct {
Name string
EndpointID string
MacAddress string
IPv4Address string
IPv6Address string
}

type structuredCNI struct {
Name string `json:"name"`
Plugins []struct {
Expand Down Expand Up @@ -930,6 +939,17 @@ func NetworkFromNative(n *native.Network) (*Network, error) {
res.Labels = *n.NerdctlLabels
}

res.Containers = make(map[string]EndpointResource)
for _, container := range n.Containers {
res.Containers[container.ID] = EndpointResource{
Name: container.Labels[labels.Name],
// EndpointID: container.EndpointID,
// MacAddress: container.MacAddress,
// IPv4Address: container.IPv4Address,
// IPv6Address: container.IPv6Address,
}
}

return &res, nil
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/inspecttypes/native/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package native

import "encoding/json"
import (
"encoding/json"
)

// Network corresponds to pkg/netutil.NetworkConfigList
// Network corresponds to pkg/netutil.NetworkConfig
type Network struct {
CNI json.RawMessage `json:"CNI,omitempty"`
NerdctlID *string `json:"NerdctlID"`
NerdctlLabels *map[string]string `json:"NerdctlLabels,omitempty"`
File string `json:"File,omitempty"`
Containers []*Container `json:"Containers"`
}
Loading