diff --git a/libpod/networking_common.go b/libpod/networking_common.go index 851d836659..0da0a84413 100644 --- a/libpod/networking_common.go +++ b/libpod/networking_common.go @@ -162,10 +162,20 @@ func (r *Runtime) reloadContainerNetwork(ctr *Container) (map[string]types.Statu } logrus.Infof("Going to reload container %s network", ctr.ID()) + // store the old status before unsetting it + netStatus := ctr.getNetworkStatus() err := r.teardownNetwork(ctr) if err != nil { logrus.Error(err) } + // We must unset the network status here so + ctr.state.NetworkStatus = nil + + // always save even when there was an error + err = ctr.save() + if err != nil { + return nil, fmt.Errorf("failed to save container status after network teardown: %w", err) + } networkOpts, err := ctr.networks() if err != nil { @@ -173,7 +183,6 @@ func (r *Runtime) reloadContainerNetwork(ctr *Container) (map[string]types.Statu } // Set the same network settings as before.. - netStatus := ctr.getNetworkStatus() newNetworkOpts := make([]types.NamedPerNetworkOptions, 0, len(networkOpts)) for _, network := range networkOpts { for name, netInt := range netStatus[network.Name].Interfaces { @@ -190,7 +199,7 @@ func (r *Runtime) reloadContainerNetwork(ctr *Container) (map[string]types.Statu } ctr.perNetworkOpts = newNetworkOpts - return r.configureNetNS(ctr, ctr.state.NetNS) + return r.configureNetNS(ctr, ctr.state.NetNS, true) } // Produce an InspectNetworkSettings containing information on the container diff --git a/libpod/networking_freebsd.go b/libpod/networking_freebsd.go index e296f996f0..69abc5207d 100644 --- a/libpod/networking_freebsd.go +++ b/libpod/networking_freebsd.go @@ -47,14 +47,14 @@ type NetstatAddress struct { // started. We can use this to initialise the container's vnet when we don't // have a separate vnet jail (which is the case in FreeBSD 13.3 and later). func (r *Runtime) setupNetNS(ctr *Container) error { - networkStatus, err := r.configureNetNS(ctr, ctr.ID()) + networkStatus, err := r.configureNetNS(ctr, ctr.ID(), false) ctr.state.NetNS = ctr.ID() ctr.state.NetworkStatus = networkStatus return err } // Create and configure a new network namespace for a container -func (r *Runtime) configureNetNS(ctr *Container, ctrNS string) (status map[string]types.StatusBlock, rerr error) { +func (r *Runtime) configureNetNS(ctr *Container, ctrNS string, _ bool) (status map[string]types.StatusBlock, rerr error) { if err := r.exposeMachinePorts(ctr.config.PortMappings); err != nil { return nil, err } @@ -112,7 +112,7 @@ func (r *Runtime) createNetNS(ctr *Container) (n string, q map[string]types.Stat logrus.Debugf("Created vnet jail %s for container %s", netns, ctr.ID()) var networkStatus map[string]types.StatusBlock - networkStatus, err = r.configureNetNS(ctr, netns) + networkStatus, err = r.configureNetNS(ctr, netns, false) if err != nil { jconf := jail.NewConfig() jconf.Set("persist", false) diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index 4845227f0a..67b2343acd 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -18,7 +18,7 @@ import ( ) // Create and configure a new network namespace for a container -func (r *Runtime) configureNetNS(ctr *Container, ctrNS string) (status map[string]types.StatusBlock, rerr error) { +func (r *Runtime) configureNetNS(ctr *Container, ctrNS string, reload bool) (status map[string]types.StatusBlock, rerr error) { if err := r.exposeMachinePorts(ctr.config.PortMappings); err != nil { return nil, err } @@ -68,7 +68,7 @@ func (r *Runtime) configureNetNS(ctr *Container, ctrNS string) (status map[strin case config.RootlessPortForwarderPasta: // Handled by container-libs netavark Setup() case config.RootlessPortForwarderRootlessport, "": - if ctr.getNetworkStatus() == nil { + if !reload { err = r.setupRootlessPortMappingViaRLK(ctr, ctrNS, netStatus) } default: @@ -99,7 +99,7 @@ func (r *Runtime) createNetNS(ctr *Container) (n string, q map[string]types.Stat logrus.Debugf("Made network namespace at %s for container %s", ctrNS.Path(), ctr.ID()) var networkStatus map[string]types.StatusBlock - networkStatus, err = r.configureNetNS(ctr, ctrNS.Path()) + networkStatus, err = r.configureNetNS(ctr, ctrNS.Path(), false) return ctrNS.Path(), networkStatus, err } @@ -111,7 +111,7 @@ func (r *Runtime) setupNetNS(ctr *Container) error { return err } - networkStatus, err := r.configureNetNS(ctr, nsPath) + networkStatus, err := r.configureNetNS(ctr, nsPath, false) // Assign NetNS attributes to container ctr.state.NetNS = nsPath