/
routes.go
61 lines (52 loc) · 2.15 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package servicenode
import (
"fmt"
envoyroute "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
envoycache "github.com/envoyproxy/go-control-plane/pkg/cache"
"github.com/mlab-lattice/lattice/pkg/definition/tree"
xdsapi "github.com/mlab-lattice/lattice/pkg/backend/kubernetes/servicemesh/envoy/xdsapi/v2"
xdsconstants "github.com/mlab-lattice/lattice/pkg/backend/kubernetes/servicemesh/envoy/xdsapi/v2/constants"
xdsmsgs "github.com/mlab-lattice/lattice/pkg/backend/kubernetes/servicemesh/envoy/xdsapi/v2/service_node/messages"
xdsutil "github.com/mlab-lattice/lattice/pkg/backend/kubernetes/servicemesh/envoy/xdsapi/v2/util"
lerror "github.com/mlab-lattice/lattice/pkg/util/error"
)
func (s *ServiceNode) getRoutes(
systemServices map[tree.Path]*xdsapi.Service) (routes []envoycache.Resource, err error) {
// NOTE: https://github.com/golang/go/wiki/PanicAndRecover#usage-in-a-package
// support nested builder funcs
defer func() {
if _panic := recover(); _panic != nil {
err = lerror.Errorf("%v", _panic)
}
}()
virtualHosts := make([]envoyroute.VirtualHost, 0)
for path, service := range systemServices {
for componentName, component := range service.Components {
for servicePort, listenerPort := range component.Ports {
if listenerPort.Protocol != "HTTP" {
continue
}
domain := fmt.Sprintf("%v.local", path.ToDomain())
domains := []string{fmt.Sprintf("%v:%v", domain, servicePort)}
// Should be able to access an HTTP component on port 80 via either:
// - http://path.to.service:80
// - http://path.to.service
if servicePort == xdsconstants.PortHTTPDefault {
domains = append(domains, domain)
}
virtualHosts = append(
virtualHosts, *xdsmsgs.NewVirtualHost(
string(path), domains, []envoyroute.Route{
*xdsmsgs.NewRouteRoute(
xdsmsgs.NewPrefixRouteMatch("/"),
xdsmsgs.NewClusterRouteActionRouteRoute(
xdsutil.GetClusterNameForComponentPort(
s.ServiceCluster(), path, componentName, servicePort))),
}))
}
}
}
return []envoycache.Resource{
xdsmsgs.NewRouteConfiguration(xdsconstants.RouteNameEgress, virtualHosts),
}, err
}