forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
53 lines (48 loc) · 1.75 KB
/
endpoints.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
package translator
import (
envoyapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoyendpoints "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
"github.com/solo-io/gloo/pkg/api/types/v1"
"github.com/solo-io/gloo/pkg/endpointdiscovery"
)
// Endpoints
func computeClusterEndpoints(upstreams []*v1.Upstream, endpoints endpointdiscovery.EndpointGroups) []*envoyapi.ClusterLoadAssignment {
var clusterEndpointAssignments []*envoyapi.ClusterLoadAssignment
for _, upstream := range upstreams {
// if there is an endpoint group for this upstream,
// it's using eds and we need to create a load assignment for it
if endpointGroup, ok := endpoints[upstream.Name]; ok {
loadAssignment := loadAssignmentForCluster(upstream.Name, endpointGroup)
clusterEndpointAssignments = append(clusterEndpointAssignments, loadAssignment)
}
}
return clusterEndpointAssignments
}
func loadAssignmentForCluster(clusterName string, addresses []endpointdiscovery.Endpoint) *envoyapi.ClusterLoadAssignment {
var endpoints []envoyendpoints.LbEndpoint
for _, addr := range addresses {
lbEndpoint := envoyendpoints.LbEndpoint{
Endpoint: &envoyendpoints.Endpoint{
Address: &envoycore.Address{
Address: &envoycore.Address_SocketAddress{
SocketAddress: &envoycore.SocketAddress{
Protocol: envoycore.TCP,
Address: addr.Address,
PortSpecifier: &envoycore.SocketAddress_PortValue{
PortValue: uint32(addr.Port),
},
},
},
},
},
}
endpoints = append(endpoints, lbEndpoint)
}
return &envoyapi.ClusterLoadAssignment{
ClusterName: clusterName,
Endpoints: []envoyendpoints.LocalityLbEndpoints{{
LbEndpoints: endpoints,
}},
}
}