diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/namespaces/namespaces.go | 26 | ||||
-rw-r--r-- | pkg/specgen/generate/namespaces.go | 7 | ||||
-rw-r--r-- | pkg/specgen/namespaces.go | 24 |
3 files changed, 51 insertions, 6 deletions
diff --git a/pkg/namespaces/namespaces.go b/pkg/namespaces/namespaces.go index 2ffbde977..b4ec04699 100644 --- a/pkg/namespaces/namespaces.go +++ b/pkg/namespaces/namespaces.go @@ -17,7 +17,9 @@ const ( nsType = "ns" podType = "pod" privateType = "private" + rlkFwdType = "port_handler=rootlesskit" shareableType = "shareable" + slirpFwdType = "port_handler=slirp4netns" slirpType = "slirp4netns" ) @@ -385,7 +387,29 @@ func (n NetworkMode) IsBridge() bool { // IsSlirp4netns indicates if we are running a rootless network stack func (n NetworkMode) IsSlirp4netns() bool { - return n == slirpType + return n == slirpType || strings.HasPrefix(string(n), slirpType+":") +} + +// IsPortForwardViaRootlessKit indicates if we are doing rootless port-forwarding via rootlesskit/rootlessport +func (n NetworkMode) IsPortForwardViaRootlessKit() bool { + if !n.IsSlirp4netns() { + return false + } + parts := strings.SplitN(string(n), ":", 2) + if len(parts) == 2 { + return parts[1] == rlkFwdType + } + return true +} + +// IsPortForwardViaSlirpHostFwd indicates if we are doing rootless port-forwarding via slirp4netns add_hostfwd() +func (n NetworkMode) IsPortForwardViaSlirpHostFwd() bool { + if !n.IsSlirp4netns() { + return false + } + // below here, implied IsSlirp4netns() == true + parts := strings.SplitN(string(n), ":", 2) + return len(parts) > 1 && parts[1] == slirpFwdType } // IsNS indicates a network namespace passed in by path (ns:<path>) diff --git a/pkg/specgen/generate/namespaces.go b/pkg/specgen/generate/namespaces.go index 09d6ba445..e038fc84e 100644 --- a/pkg/specgen/generate/namespaces.go +++ b/pkg/specgen/generate/namespaces.go @@ -2,6 +2,7 @@ package generate import ( "context" + "fmt" "os" "strings" @@ -226,7 +227,11 @@ func namespaceOptions(ctx context.Context, s *specgen.SpecGenerator, rt *libpod. if err != nil { return nil, err } - toReturn = append(toReturn, libpod.WithNetNS(portMappings, postConfigureNetNS, "slirp4netns", nil)) + val := "slirp4netns" + if s.NetNS.Value != "" { + val = fmt.Sprintf("slirp4netns:%s", s.NetNS.Value) + } + toReturn = append(toReturn, libpod.WithNetNS(portMappings, postConfigureNetNS, val, nil)) case specgen.Bridge: portMappings, err := createPortMappings(ctx, s, img) if err != nil { diff --git a/pkg/specgen/namespaces.go b/pkg/specgen/namespaces.go index 5f56b242b..e62c8b709 100644 --- a/pkg/specgen/namespaces.go +++ b/pkg/specgen/namespaces.go @@ -108,7 +108,19 @@ func validateNetNS(n *Namespace) error { return nil } switch n.NSMode { - case "", Default, Host, Path, FromContainer, FromPod, Private, NoNetwork, Bridge, Slirp: + case Slirp: + if n.Value != "" { + parts := strings.Split(n.Value, ",") + for _, p := range parts { + switch p { + case "port_handler=slirp4netns", "port_handler=rootlesskit": + default: + return errors.Errorf("invalid value for slirp %q", n.Value) + } + } + } + break + case "", Default, Host, Path, FromContainer, FromPod, Private, NoNetwork, Bridge: break default: return errors.Errorf("invalid network %q", n.NSMode) @@ -119,8 +131,8 @@ func validateNetNS(n *Namespace) error { if len(n.Value) < 1 { return errors.Errorf("namespace mode %s requires a value", n.NSMode) } - } else { - // All others must NOT set a string value + } else if n.NSMode != Slirp { + // All others except must NOT set a string value if len(n.Value) > 0 { return errors.Errorf("namespace value %s cannot be provided with namespace mode %s", n.Value, n.NSMode) } @@ -250,8 +262,12 @@ func ParseNetworkNamespace(ns string) (Namespace, []string, error) { var cniNetworks []string // Net defaults to Slirp on rootless switch { - case ns == "slirp4netns": + case ns == "slirp4netns", strings.HasPrefix(ns, "slirp4netns:"): + split := strings.SplitN(ns, ":", 2) toReturn.NSMode = Slirp + if len(split) > 1 { + toReturn.Value = split[1] + } case ns == "pod": toReturn.NSMode = FromPod case ns == "": |