libpod: Re-work the container's network state to help code sharing

This replaces the NetworkJail string field with a struct pointer named
NetNS. This does not try to emulate the complete NetNS interface but does
help to re-use code that just refers to c.state.NetNS.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
This commit is contained in:
Doug Rabson 2022-09-07 15:35:48 +01:00
parent 36cfd05a7d
commit c5f64d9f58
3 changed files with 32 additions and 15 deletions

View File

@ -6,12 +6,20 @@ package libpod
// replaceNetNS handle network namespace transitions after updating a
// container's state.
func replaceNetNS(netNSPath string, ctr *Container, newState *ContainerState) error {
// On FreeBSD, we just record the network jail's name in our state.
newState.NetworkJail = netNSPath
if netNSPath != "" {
// On FreeBSD, we just record the network jail's name in our state.
newState.NetNS = &jailNetNS{Name: netNSPath}
} else {
newState.NetNS = nil
}
return nil
}
// getNetNSPath retrieves the netns path to be stored in the database
func getNetNSPath(ctr *Container) string {
return ctr.state.NetworkJail
if ctr.state.NetNS != nil {
return ctr.state.NetNS.Name
} else {
return ""
}
}

View File

@ -4,11 +4,20 @@
package libpod
type containerPlatformState struct {
// NetworkJail is the name of the container's network VNET
// NetNS is the name of the container's network VNET
// jail. Will only be set if config.CreateNetNS is true, or
// the container was told to join another container's network
// namespace.
NetworkJail string `json:"-"`
NetNS *jailNetNS `json:"-"`
}
type jailNetNS struct {
Name string `json:"-"`
}
func (ns *jailNetNS) Path() string {
// The jail name approximately corresponds to the Linux netns path
return ns.Name
}
func networkDisabled(c *Container) (bool, error) {
@ -16,7 +25,7 @@ func networkDisabled(c *Container) (bool, error) {
return false, nil
}
if !c.config.PostConfigureNetNS {
return c.state.NetworkJail == "", nil
return c.state.NetNS != nil, nil
}
return false, nil
}

View File

@ -26,8 +26,8 @@ var (
// Network stubs to decouple container_internal_freebsd.go from
// networking_freebsd.go so they can be reviewed separately.
func (r *Runtime) createNetNS(ctr *Container) (netJail string, q map[string]types.StatusBlock, retErr error) {
return "", nil, errors.New("not implemented (*Runtime) createNetNS")
func (r *Runtime) createNetNS(ctr *Container) (netJail *jailNetNS, q map[string]types.StatusBlock, retErr error) {
return nil, nil, errors.New("not implemented (*Runtime) createNetNS")
}
func (r *Runtime) teardownNetNS(ctr *Container) error {
@ -51,7 +51,7 @@ func (c *Container) unmountSHM(path string) error {
func (c *Container) prepare() error {
var (
wg sync.WaitGroup
jailName string
ctrNS *jailNetNS
networkStatus map[string]types.StatusBlock
createNetNSErr, mountStorageErr error
mountPoint string
@ -63,9 +63,9 @@ func (c *Container) prepare() error {
go func() {
defer wg.Done()
// Set up network namespace if not already set up
noNetNS := c.state.NetworkJail == ""
noNetNS := c.state.NetNS == nil
if c.config.CreateNetNS && noNetNS && !c.config.PostConfigureNetNS {
jailName, networkStatus, createNetNSErr = c.runtime.createNetNS(c)
ctrNS, networkStatus, createNetNSErr = c.runtime.createNetNS(c)
if createNetNSErr != nil {
return
}
@ -74,7 +74,7 @@ func (c *Container) prepare() error {
defer tmpStateLock.Unlock()
// Assign NetNS attributes to container
c.state.NetworkJail = jailName
c.state.NetNS = ctrNS
c.state.NetworkStatus = networkStatus
}
}()
@ -164,7 +164,7 @@ func (c *Container) addNetworkContainer(g *generate.Generator, ctr string) error
if err != nil {
return fmt.Errorf("retrieving dependency %s of container %s from state: %w", ctr, c.ID(), err)
}
g.AddAnnotation("org.freebsd.parentJail", nsCtr.state.NetworkJail)
g.AddAnnotation("org.freebsd.parentJail", nsCtr.state.NetNS.Name)
return nil
}
@ -187,7 +187,7 @@ func openDirectory(path string) (fd int, err error) {
func (c *Container) addNetworkNamespace(g *generate.Generator) error {
if c.config.CreateNetNS {
g.AddAnnotation("org.freebsd.parentJail", c.state.NetworkJail)
g.AddAnnotation("org.freebsd.parentJail", c.state.NetNS.Name)
}
return nil
}
@ -272,7 +272,7 @@ func (c *Container) isSlirp4netnsIPv6() (bool, error) {
// check for net=none
func (c *Container) hasNetNone() bool {
return c.state.NetworkJail == ""
return c.state.NetNS == nil
}
func setVolumeAtime(mountPoint string, st os.FileInfo) error {