Read and remove routes based on registry event deltas

This commit is contained in:
Milos Gajdos 2019-07-06 01:31:59 +01:00
parent b68f0e237f
commit 30d05e34a9
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

View File

@ -158,25 +158,40 @@ func (r *router) watchServices(w registry.Watcher) error {
break break
} }
log.Logf("r.watchServices() new service event: %s", res.Service.Name) log.Logf("r.watchServices() new service event: Action: %s Service: %v", res.Action, res.Service)
route := Route{
Destination: res.Service.Name,
Router: r.opts.Address,
Network: r.opts.Network,
Metric: DefaultLocalMetric,
}
switch res.Action { switch res.Action {
case "create": case "create":
// only return error if the route is not duplicate, but something else has failed // range over the flat slice of nodes
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute { for _, node := range res.Service.Nodes {
return fmt.Errorf("failed adding route for service %v: %s", res.Service.Name, err) gateway := node.Address
if node.Port > 0 {
gateway = fmt.Sprintf("%s:%d", node.Address, node.Port)
}
route := Route{
Destination: res.Service.Name,
Gateway: gateway,
Router: r.opts.Address,
Network: r.opts.Network,
Metric: DefaultLocalMetric,
}
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("error adding route for service %s: %s", res.Service.Name, err)
}
} }
case "delete": case "delete":
// only return error if the route is not in the table, but something else has failed for _, node := range res.Service.Nodes {
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound { route := Route{
return fmt.Errorf("failed adding route for service %v: %s", res.Service.Name, err) Destination: res.Service.Name,
Gateway: node.Address,
Router: r.opts.Address,
Network: r.opts.Network,
Metric: DefaultLocalMetric,
}
// only return error if the route is not in the table, but something else has failed
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound {
return fmt.Errorf("failed adding route for service %v: %s", res.Service.Name, err)
}
} }
} }
} }