From ee73b07ad0dc980936fc711eb32c212b9443af4a Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Fri, 16 Aug 2024 14:49:02 +0000 Subject: [PATCH] WIP: Drop driverConfiguresNet in favour of ChildDriverInfo() I'm not sure what I'm doing and it doesn't work yet... Signed-off-by: Stefano Brivio --- cmd/rootlesskit/main.go | 2 -- pkg/api/api.go | 6 ++++++ pkg/child/child.go | 11 ++++++----- pkg/network/lxcusernic/lxcusernic.go | 6 ++++++ pkg/network/network.go | 6 ++++++ pkg/network/pasta/pasta.go | 11 +++++++++++ pkg/network/slirp4netns/slirp4netns.go | 6 ++++++ pkg/network/vpnkit/vpnkit.go | 6 ++++++ 8 files changed, 47 insertions(+), 7 deletions(-) diff --git a/cmd/rootlesskit/main.go b/cmd/rootlesskit/main.go index ba9dde00..be7d7f29 100644 --- a/cmd/rootlesskit/main.go +++ b/cmd/rootlesskit/main.go @@ -604,7 +604,6 @@ func createChildOpt(clicontext *cli.Context) (child.Opt, error) { DetachNetNS: detachNetNS, Propagation: clicontext.String("propagation"), EvacuateCgroup2: clicontext.String("evacuate-cgroup2") != "", - ConfigureNet: false, } switch reaperStr := clicontext.String("reaper"); reaperStr { case "auto": @@ -626,7 +625,6 @@ func createChildOpt(clicontext *cli.Context) (child.Opt, error) { // NOP case "pasta": opt.NetworkDriver = pasta.NewChildDriver() - opt.ConfigureNet = true case "slirp4netns": opt.NetworkDriver = slirp4netns.NewChildDriver() case "vpnkit": diff --git a/pkg/api/api.go b/pkg/api/api.go index d310e6d3..b0397da8 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -16,6 +16,7 @@ type Info struct { ChildPID int `json:"childPID"` NetworkDriver *NetworkDriverInfo `json:"networkDriver,omitempty"` PortDriver *PortDriverInfo `json:"portDriver,omitempty"` + ChildDriver *ChildDriverInfo `json:"childDriver,omitempty"` } // NetworkDriverInfo in Info @@ -32,3 +33,8 @@ type PortDriverInfo struct { Protos []string `json:"protos"` DisallowLoopbackChildIP bool `json:"disallowLoopbackChildIP,omitempty"` // since API v1.1.1 } + +type ChildDriverInfo struct { + ConfiguresInterface bool `json:"configuresInterface"` +} + diff --git a/pkg/child/child.go b/pkg/child/child.go index db1e5f38..2e86b8b9 100644 --- a/pkg/child/child.go +++ b/pkg/child/child.go @@ -187,7 +187,7 @@ func setupCopyDir(driver copyup.ChildDriver, dirs []string) (bool, error) { // setupNet sets up the network driver. // // NOTE: msg is altered during calling driver.ConfigureNetworkChild -func setupNet(stateDir string, msg *messages.ParentInitNetworkDriverCompleted, etcWasCopied bool, driver network.ChildDriver, driverConfiguresNet bool, detachedNetNSPath string) error { +func setupNet(stateDir string, msg *messages.ParentInitNetworkDriverCompleted, etcWasCopied bool, driver network.ChildDriver, detachedNetNSPath string) error { // HostNetwork if driver == nil { return nil @@ -215,7 +215,8 @@ func setupNet(stateDir string, msg *messages.ParentInitNetworkDriverCompleted, e if err := os.WriteFile(stateDirResolvConf, generateResolvConf(msg.DNS), 0644); err != nil { return fmt.Errorf("writing %s: %w", stateDirResolvConf, err) } - if !driverConfiguresNet { + Info, _ := driver.ChildDriverInfo() + if !Info.ConfiguresInterface { if err := activateDev(dev, msg.IP, msg.Netmask, msg.Gateway, msg.MTU); err != nil { return err } @@ -257,7 +258,8 @@ func setupNet(stateDir string, msg *messages.ParentInitNetworkDriverCompleted, e return fmt.Errorf("writing %s: %w", stateDirResolvConf, err) } if err := ns.WithNetNSPath(detachedNetNSPath, func(_ ns.NetNS) error { - if !driverConfiguresNet { + Info, _ := driver.ChildDriverInfo() + if !Info.ConfiguresInterface { return activateDev(dev, msg.IP, msg.Netmask, msg.Gateway, msg.MTU) } return nil @@ -283,7 +285,6 @@ type Opt struct { Propagation string // mount propagation type Reaper bool EvacuateCgroup2 bool // needs to correspond to parent.Opt.EvacuateCgroup2 is set - ConfigureNet bool // driver configures network interface by itself } // statPIDNS is from https://github.com/containerd/containerd/blob/v1.7.2/services/introspection/pidns_linux.go#L25-L36 @@ -464,7 +465,7 @@ func Child(opt Opt) error { return err } } - if err := setupNet(stateDir, netMsg, etcWasCopied, opt.NetworkDriver, opt.ConfigureNet, detachedNetNSPath); err != nil { + if err := setupNet(stateDir, netMsg, etcWasCopied, opt.NetworkDriver, detachedNetNSPath); err != nil { return err } portQuitCh := make(chan struct{}) diff --git a/pkg/network/lxcusernic/lxcusernic.go b/pkg/network/lxcusernic/lxcusernic.go index 3ec643e1..47749ff9 100644 --- a/pkg/network/lxcusernic/lxcusernic.go +++ b/pkg/network/lxcusernic/lxcusernic.go @@ -148,6 +148,12 @@ func exchangeDHCP(c *client4.Client, dev string, detachedNetNSPath string) (*dhc return ack, nil } +func (d *childDriver) ChildDriverInfo() (*api.ChildDriverInfo, error) { + return &api.ChildDriverInfo { + ConfiguresInterface: false, + }, nil +} + func (d *childDriver) ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDriverCompleted, detachedNetNSPath string) (string, error) { dev := netmsg.Dev if dev == "" { diff --git a/pkg/network/network.go b/pkg/network/network.go index 74238787..a6e4f4b9 100644 --- a/pkg/network/network.go +++ b/pkg/network/network.go @@ -17,6 +17,10 @@ type ParentDriver interface { ConfigureNetwork(childPID int, stateDir, detachedNetNSPath string) (netmsg *messages.ParentInitNetworkDriverCompleted, cleanup func() error, err error) } +type childDriverInfo struct { + ConfiguresInterface bool +} + // ChildDriver is called from the child namespace type ChildDriver interface { // ConfigureNetworkChild is executed in the child's namespaces, excluding detached-netns. @@ -24,4 +28,6 @@ type ChildDriver interface { // netmsg MAY be modified. // devName is like "tap" or "eth0" ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDriverCompleted, detachedNetNSPath string) (devName string, err error) + + ChildDriverInfo() (*api.ChildDriverInfo, error) } diff --git a/pkg/network/pasta/pasta.go b/pkg/network/pasta/pasta.go index b9b9a2a6..091fef32 100644 --- a/pkg/network/pasta/pasta.go +++ b/pkg/network/pasta/pasta.go @@ -179,7 +179,18 @@ func NewChildDriver() network.ChildDriver { return &childDriver{} } +//type childDriverInfo struct { + //ConfiguresInterface bool +//} + type childDriver struct { + info func() *api.ChildDriverInfo +} + +func (d *childDriver) ChildDriverInfo() (*api.ChildDriverInfo, error) { + return &api.ChildDriverInfo { + ConfiguresInterface: true, + }, nil } func (d *childDriver) ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDriverCompleted, detachedNetNSPath string) (string, error) { diff --git a/pkg/network/slirp4netns/slirp4netns.go b/pkg/network/slirp4netns/slirp4netns.go index d69717e2..151cc184 100644 --- a/pkg/network/slirp4netns/slirp4netns.go +++ b/pkg/network/slirp4netns/slirp4netns.go @@ -337,6 +337,12 @@ func NewChildDriver() network.ChildDriver { type childDriver struct { } +func (d *childDriver) ChildDriverInfo() (*api.ChildDriverInfo, error) { + return &api.ChildDriverInfo { + ConfiguresInterface: false, + }, nil +} + func (d *childDriver) ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDriverCompleted, detachedNetNSPath string) (string, error) { tap := netmsg.Dev if tap == "" { diff --git a/pkg/network/vpnkit/vpnkit.go b/pkg/network/vpnkit/vpnkit.go index 6b5db535..d90c1e5a 100644 --- a/pkg/network/vpnkit/vpnkit.go +++ b/pkg/network/vpnkit/vpnkit.go @@ -172,6 +172,12 @@ func NewChildDriver() network.ChildDriver { type childDriver struct { } +func (d *childDriver) ChildDriverInfo() (*api.ChildDriverInfo, error) { + return &api.ChildDriverInfo { + ConfiguresInterface: false, + }, nil +} + func (d *childDriver) ConfigureNetworkChild(netmsg *messages.ParentInitNetworkDriverCompleted, detachedNetNSPath string) (tap string, err error) { tapName := netmsg.Dev if tapName == "" {