diff --git a/grpc.go b/grpc.go index 0011cfc36d..0a06a7eaf8 100644 --- a/grpc.go +++ b/grpc.go @@ -853,6 +853,7 @@ func (a *agentGRPC) CreateSandbox(ctx context.Context, req *pb.CreateSandboxRequ a.sandbox.id = req.Hostname a.sandbox.containers = make(map[string]*container) + a.sandbox.network.ifaces = make(map[string]*pb.Interface) a.sandbox.network.dns = req.Dns a.sandbox.running = true diff --git a/network.go b/network.go index 5e4e8ed5f1..cf26370e04 100644 --- a/network.go +++ b/network.go @@ -25,10 +25,10 @@ import ( // related information. type network struct { ifacesLock sync.Mutex - ifaces []*pb.Interface + ifaces map[string]*pb.Interface routesLock sync.Mutex - routes []*pb.Route + routes []pb.Route dns []string } @@ -139,7 +139,7 @@ func (s *sandbox) addInterface(netHandle *netlink.Handle, iface *pb.Interface) ( } // Update sandbox interface list. - s.network.ifaces = append(s.network.ifaces, iface) + s.network.ifaces[iface.Name] = iface return iface, nil } @@ -172,12 +172,7 @@ func (s *sandbox) removeInterface(netHandle *netlink.Handle, iface *pb.Interface } // Update sandbox interface list. - for idx, sIface := range s.network.ifaces { - if sIface.Name == iface.Name { - s.network.ifaces = append(s.network.ifaces[:idx], s.network.ifaces[idx+1:]...) - break - } - } + delete(s.network.ifaces, iface.Name) return nil, nil } @@ -409,10 +404,6 @@ func getCurrentRoutes(netHandle *netlink.Handle) (*pb.Routes, error) { return &routes, nil } -func (s *sandbox) removeRoute(netHandle *netlink.Handle, route *pb.Route) error { - return s.updateRoute(netHandle, route, false) -} - func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *pb.Route, add bool) (err error) { s.network.routesLock.Lock() defer s.network.routesLock.Unlock() @@ -465,7 +456,7 @@ func (s *sandbox) updateRoute(netHandle *netlink.Handle, route *pb.Route, add bo } // Add route to sandbox route list. - s.network.routes = append(s.network.routes, route) + s.network.routes = append(s.network.routes, *route) } else { if err := netHandle.RouteDel(netRoute); err != nil { return grpcStatus.Errorf(codes.Internal, "Could not remove route dest(%s)/gw(%s)/dev(%s): %v", @@ -492,10 +483,6 @@ func setupDNS(dns []string) error { return nil } -func removeDNS(dns []string) error { - return nil -} - //////////// // Global // //////////// @@ -508,13 +495,6 @@ func (s *sandbox) removeNetwork() error { } defer netHandle.Delete() - for _, route := range s.network.routes { - if err := s.removeRoute(netHandle, route); err != nil { - return grpcStatus.Errorf(codes.Internal, "Could not remove network route %v: %v", - route, err) - } - } - for _, iface := range s.network.ifaces { if _, err := s.removeInterface(netHandle, iface); err != nil { return grpcStatus.Errorf(codes.Internal, "Could not remove network interface %v: %v", @@ -522,9 +502,5 @@ func (s *sandbox) removeNetwork() error { } } - if err := removeDNS(s.network.dns); err != nil { - return grpcStatus.Errorf(codes.Internal, "Could not remove network DNS: %v", err) - } - return nil }