libnetwork: fix sandbox restore

The method to restore a network namespace takes a collection of
interfaces to restore with the options to apply. The interface names are
structured data, tuples of (SrcName, DstPrefix) but for whatever reason
are being passed into Restore() serialized to strings. A refactor,
f0be4d126d, accidentally broke the
serialization by dropping the delimiter. Rather than fix the
serialization and leave the time-bomb for someone else to trip over,
pass the interface names as structured data.

Signed-off-by: Cory Snider <csnider@mirantis.com>
(cherry picked from commit 50eb2d2782)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-05-30 12:27:59 -04:00
parent fec801a103
commit 3452a76589
3 changed files with 12 additions and 14 deletions

View file

@ -470,16 +470,10 @@ func (n *networkNamespace) Destroy() error {
}
// Restore restore the network namespace
func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
func (n *networkNamespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
// restore interfaces
for name, opts := range ifsopt {
if !strings.Contains(name, "+") {
return fmt.Errorf("wrong iface name in restore osl sandbox interface: %s", name)
}
seps := strings.Split(name, "+")
srcName := seps[0]
dstPrefix := seps[1]
i := &nwIface{srcName: srcName, dstName: dstPrefix, ns: n}
i := &nwIface{srcName: name.SrcName, dstName: name.DstPrefix, ns: n}
i.processInterfaceOptions(opts...)
if i.master != "" {
i.dstMaster = n.findDst(i.master, true)
@ -531,7 +525,7 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty
}
var index int
indexStr := strings.TrimPrefix(i.dstName, dstPrefix)
indexStr := strings.TrimPrefix(i.dstName, name.DstPrefix)
if indexStr != "" {
index, err = strconv.Atoi(indexStr)
if err != nil {
@ -540,8 +534,8 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty
}
index++
n.Lock()
if index > n.nextIfIndex[dstPrefix] {
n.nextIfIndex[dstPrefix] = index
if index > n.nextIfIndex[name.DstPrefix] {
n.nextIfIndex[name.DstPrefix] = index
}
n.iFaces = append(n.iFaces, i)
n.Unlock()

View file

@ -17,6 +17,10 @@ const (
SandboxTypeLoadBalancer = iota
)
type Iface struct {
SrcName, DstPrefix string
}
// IfaceOption is a function option type to set interface options.
type IfaceOption func(i *nwIface)
@ -89,7 +93,7 @@ type Sandbox interface {
Destroy() error
// Restore restores the sandbox.
Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
// ApplyOSTweaks applies operating system specific knobs on the sandbox.
ApplyOSTweaks([]SandboxType)

View file

@ -765,7 +765,7 @@ func (sb *Sandbox) restoreOslSandbox() error {
var routes []*types.StaticRoute
// restore osl sandbox
Ifaces := make(map[string][]osl.IfaceOption)
Ifaces := make(map[osl.Iface][]osl.IfaceOption)
for _, ep := range sb.endpoints {
ep.mu.Lock()
joinInfo := ep.joinInfo
@ -790,7 +790,7 @@ func (sb *Sandbox) restoreOslSandbox() error {
if len(i.llAddrs) != 0 {
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
}
Ifaces[i.srcName+i.dstPrefix] = ifaceOptions
Ifaces[osl.Iface{SrcName: i.srcName, DstPrefix: i.dstPrefix}] = ifaceOptions
if joinInfo != nil {
routes = append(routes, joinInfo.StaticRoutes...)
}