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,28 +158,43 @@ 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)
switch res.Action {
case "create":
// range over the flat slice of nodes
for _, node := range res.Service.Nodes {
gateway := node.Address
if node.Port > 0 {
gateway = fmt.Sprintf("%s:%d", node.Address, node.Port)
}
route := Route{ route := Route{
Destination: res.Service.Name, Destination: res.Service.Name,
Gateway: gateway,
Router: r.opts.Address, Router: r.opts.Address,
Network: r.opts.Network, Network: r.opts.Network,
Metric: DefaultLocalMetric, Metric: DefaultLocalMetric,
} }
switch res.Action {
case "create":
// only return error if the route is not duplicate, but something else has failed
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute { if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("failed adding route for service %v: %s", res.Service.Name, err) return fmt.Errorf("error adding route for service %s: %s", res.Service.Name, err)
}
} }
case "delete": case "delete":
for _, node := range res.Service.Nodes {
route := Route{
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 // 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 { 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) return fmt.Errorf("failed adding route for service %v: %s", res.Service.Name, err)
} }
} }
} }
}
return watchErr return watchErr
} }