Skip to content
Merged
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
19 changes: 2 additions & 17 deletions cmd/nerdctl/run_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"runtime"
"strings"

"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
gocni "github.com/containerd/go-cni"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/containerd/nerdctl/pkg/dnsutil"
"github.com/containerd/nerdctl/pkg/dnsutil/hostsstore"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
Expand Down Expand Up @@ -219,7 +219,7 @@ func generateNetOpts(cmd *cobra.Command, globalOptions types.GlobalCommandOption
hostnamePath := filepath.Join(conStateDir, "hostname")
resolvConfPath := filepath.Join(conStateDir, "resolv.conf")
etcHostsPath := hostsstore.HostsPath(dataStore, ns, containerID)
netNSPath, err := getContainerNetNSPath(ctx, found.Container)
netNSPath, err := containerutil.ContainerNetNSPath(ctx, found.Container)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,21 +251,6 @@ func generateNetOpts(cmd *cobra.Command, globalOptions types.GlobalCommandOption
return opts, netSlice, ipAddress, ports, macAddress, nil
}

func getContainerNetNSPath(ctx context.Context, c containerd.Container) (string, error) {
task, err := c.Task(ctx, nil)
if err != nil {
return "", err
}
status, err := task.Status(ctx)
if err != nil {
return "", err
}
if status.Status != containerd.Running {
return "", fmt.Errorf("invalid target container: %s, should be running", c.ID())
}
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}

func verifyCNINetwork(cmd *cobra.Command, netSlice []string, macAddress string, globalOptions types.GlobalCommandOptions) error {
e, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithDefaultNetwork())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/oci"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/containerd/nerdctl/pkg/errutil"
"github.com/containerd/nerdctl/pkg/formatter"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
Expand Down Expand Up @@ -212,7 +213,7 @@ func reconfigNetContainer(ctx context.Context, c containerd.Container, client *c
if err != nil {
return err
}
netNSPath, err := getContainerNetNSPath(ctx, targetCon)
netNSPath, err := containerutil.ContainerNetNSPath(ctx, targetCon)
if err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ func ContainerStatus(ctx context.Context, c containerd.Container) (containerd.St

return task.Status(ctx)
}

// ContainerNetNSPath returns the netns path of a container.
func ContainerNetNSPath(ctx context.Context, c containerd.Container) (string, error) {
task, err := c.Task(ctx, nil)
if err != nil {
return "", err
}
status, err := task.Status(ctx)
if err != nil {
return "", err
}
if status.Status != containerd.Running {
return "", fmt.Errorf("invalid target container: %s, should be running", c.ID())
}
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}