/
clusters.go
61 lines (49 loc) · 2.01 KB
/
clusters.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 (
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
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) getClusters(
systemServices map[tree.Path]*xdsapi.Service) (clusters []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)
}
}()
clusters = make([]envoycache.Resource, 0)
for path, service := range systemServices {
servicePath, err := s.Path()
if err != nil {
return nil, err
}
isLocalService := servicePath == path
for componentName, component := range service.Components {
for port := range component.Ports {
clusterName := xdsutil.GetClusterNameForComponentPort(
s.ServiceCluster(), path, componentName, port)
clusters = append(clusters, xdsmsgs.NewEdsCluster(
clusterName,
xdsconstants.ClusterConnectTimeout,
xdsconstants.ClusterLBPolicyRoundRobin))
if isLocalService {
clusterName = xdsutil.GetLocalClusterNameForComponentPort(
s.ServiceCluster(), path, componentName, port)
clusters = append(clusters, xdsmsgs.NewStaticCluster(
clusterName,
xdsconstants.ClusterConnectTimeout,
xdsconstants.ClusterLBPolicyRoundRobin,
[]*envoycore.Address{xdsmsgs.NewTcpSocketAddress(xdsconstants.Localhost, port)}))
}
}
}
}
return clusters, err
}