...
1 package gateway
2
3 import (
4 "fmt"
5
6 v2 "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2"
7 v2core "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/core"
8 v2endpoint "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/endpoint"
9 "github.com/datawire/ambassador/v2/pkg/kates"
10 )
11
12
13 func Compile_Endpoints(endpoints *kates.Endpoints) (*CompiledConfig, error) {
14 var clas []*CompiledLoadAssignment
15
16 for _, subset := range endpoints.Subsets {
17 for _, port := range subset.Ports {
18 var lbEndpoints []*v2endpoint.LbEndpoint
19 for _, addr := range subset.Addresses {
20 lbEndpoints = append(lbEndpoints, makeLbEndpoint("TCP", addr.IP, int(port.Port)))
21 }
22 path := fmt.Sprintf("k8s/%s/%s/%d", endpoints.Namespace, endpoints.Name, port.Port)
23 clas = append(clas, &CompiledLoadAssignment{
24 CompiledItem: NewCompiledItem(SourceFromResource(endpoints)),
25 LoadAssignment: &v2.ClusterLoadAssignment{
26 ClusterName: path,
27 Endpoints: []*v2endpoint.LocalityLbEndpoints{{LbEndpoints: lbEndpoints}},
28 },
29 })
30 if len(subset.Ports) == 1 {
31 path := fmt.Sprintf("k8s/%s/%s", endpoints.Namespace, endpoints.Name)
32 clas = append(clas, &CompiledLoadAssignment{
33 CompiledItem: NewCompiledItem(SourceFromResource(endpoints)),
34 LoadAssignment: &v2.ClusterLoadAssignment{
35 ClusterName: path,
36 Endpoints: []*v2endpoint.LocalityLbEndpoints{{LbEndpoints: lbEndpoints}},
37 },
38 })
39 }
40 }
41 }
42
43 return &CompiledConfig{
44 CompiledItem: NewCompiledItem(SourceFromResource(endpoints)),
45 LoadAssignments: clas,
46 }, nil
47 }
48
49
50 func makeLbEndpoint(protocol, ip string, port int) *v2endpoint.LbEndpoint {
51 return &v2endpoint.LbEndpoint{
52 HostIdentifier: &v2endpoint.LbEndpoint_Endpoint{
53 Endpoint: &v2endpoint.Endpoint{
54 Address: &v2core.Address{
55 Address: &v2core.Address_SocketAddress{
56 SocketAddress: &v2core.SocketAddress{
57 Protocol: v2core.SocketAddress_Protocol(v2core.SocketAddress_Protocol_value[protocol]),
58 Address: ip,
59 PortSpecifier: &v2core.SocketAddress_PortValue{PortValue: uint32(port)},
60 Ipv4Compat: true,
61 },
62 },
63 },
64 },
65 },
66 }
67 }
68
View as plain text