Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

agent: Simplify tearing down the network #225

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 5 additions & 29 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -492,10 +483,6 @@ func setupDNS(dns []string) error {
return nil
}

func removeDNS(dns []string) error {
return nil
}

////////////
// Global //
////////////
Expand All @@ -508,23 +495,12 @@ 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",
iface, err)
}
}

if err := removeDNS(s.network.dns); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not remove network DNS: %v", err)
}

return nil
}