Skip to content

Commit

Permalink
Add rootless IPv6 support
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Nov 15, 2022
1 parent 1bc44d4 commit b221534
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ func Run(ctx context.Context, cfg cmds.Agent) error {
}

if cfg.Rootless && !cfg.RootlessAlreadyUnshared {
if err := rootless.Rootless(cfg.DataDir); err != nil {
dualNode, err := utilsnet.IsDualStackIPStrings(cfg.NodeIP)
if err != nil {
return err
}
if err := rootless.Rootless(cfg.DataDir, dualNode); err != nil {
return err
}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
}
cfg.DataDir = dataDir
if !cfg.DisableAgent {
if err := rootless.Rootless(dataDir); err != nil {
dualNode, err := utilsnet.IsDualStackIPStrings(cmds.AgentConfig.NodeIP)
if err != nil {
return err
}
if err := rootless.Rootless(dataDir, dualNode); err != nil {
return err
}
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/rootless/rootless.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ var (

mtuEnv = "K3S_ROOTLESS_MTU"
cidrEnv = "K3S_ROOTLESS_CIDR"
enableIPv6Env = "K3S_ROOTLESS_ENABLE_IPV6"
portDriverEnv = "K3S_ROOTLESS_PORT_DRIVER"
disableLoopbackEnv = "K3S_ROOTLESS_DISABLE_HOST_LOOPBACK"
)

func Rootless(stateDir string) error {
func Rootless(stateDir string, enableIPv6 bool) error {
defer func() {
os.Unsetenv(pipeFD)
os.Unsetenv(childEnv)
Expand Down Expand Up @@ -66,7 +67,7 @@ func Rootless(stateDir string) error {
if err := validateSysctl(); err != nil {
logrus.Fatal(err)
}
parentOpt, err := createParentOpt(driver, rootlessDir)
parentOpt, err := createParentOpt(driver, rootlessDir, enableIPv6)
if err != nil {
logrus.Fatal(err)
}
Expand Down Expand Up @@ -127,7 +128,7 @@ func parseCIDR(s string) (*net.IPNet, error) {
return ipnet, nil
}

func createParentOpt(driver portDriver, stateDir string) (*parent.Opt, error) {
func createParentOpt(driver portDriver, stateDir string, enableIPv6 bool) (*parent.Opt, error) {
if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to mkdir %s", stateDir)
}
Expand Down Expand Up @@ -180,6 +181,14 @@ func createParentOpt(driver portDriver, stateDir string) (*parent.Opt, error) {
}
}

if val := os.Getenv(enableIPv6Env); val != "" {
if v, err := strconv.ParseBool(val); err != nil {
logrus.Warn("Failed to parse rootless enable-ipv6 value; using default")
} else {
enableIPv6 = v
}
}

cidr := "10.41.0.0/16"
if val := os.Getenv(cidrEnv); val != "" {
cidr = val
Expand All @@ -193,7 +202,7 @@ func createParentOpt(driver portDriver, stateDir string) (*parent.Opt, error) {
if _, err := exec.LookPath(binary); err != nil {
return nil, err
}
opt.NetworkDriver, err = slirp4netns.NewParentDriver(driver.LogWriter(), binary, mtu, ipnet, "tap0", disableHostLoopback, driver.APISocketPath(), false, false, false)
opt.NetworkDriver, err = slirp4netns.NewParentDriver(driver.LogWriter(), binary, mtu, ipnet, "tap0", disableHostLoopback, driver.APISocketPath(), false, false, enableIPv6)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b221534

Please sign in to comment.