runtime: it should rollback when failed in Sandbox AddInterface

When Sandbox AddInterface() is called, it may fail after endpoint.HotAttach,
we'd better rollback and call save() in the end.

Fixes: #3419

Signed-off-by: yangfeiyu <yangfeiyu20102011@163.com>
This commit is contained in:
yangfeiyu 2022-01-11 15:49:31 +08:00
parent 97e18cf2d0
commit b133a2368a

View File

@ -869,15 +869,28 @@ func (s *Sandbox) AddInterface(ctx context.Context, inf *pbTypes.Interface) (*pb
return nil, err return nil, err
} }
// Update the sandbox storage defer func() {
s.networkNS.Endpoints = append(s.networkNS.Endpoints, endpoint) if err != nil {
if err := s.Save(); err != nil { if errDetach := endpoint.HotDetach(ctx, s.hypervisor, s.networkNS.NetNsCreated, s.networkNS.NetNsPath); errDetach != nil {
return nil, err s.Logger().WithField("endpoint-type", endpoint.Type()).WithError(errDetach).Error("rollback hot attaching endpoint failed")
} }
}
}()
// Add network for vm // Add network for vm
inf.PciPath = endpoint.PciPath().String() inf.PciPath = endpoint.PciPath().String()
return s.agent.updateInterface(ctx, inf) result, err := s.agent.updateInterface(ctx, inf)
if err != nil {
return nil, err
}
// Update the sandbox storage
s.networkNS.Endpoints = append(s.networkNS.Endpoints, endpoint)
if err = s.Save(); err != nil {
return nil, err
}
return result, nil
} }
// RemoveInterface removes a nic of the sandbox. // RemoveInterface removes a nic of the sandbox.