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