github.com/clly/consul@v1.4.5/agent/xds/endpoints.go (about) 1 package xds 2 3 import ( 4 "errors" 5 6 envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2" 7 envoyendpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint" 8 "github.com/gogo/protobuf/proto" 9 10 "github.com/hashicorp/consul/agent/proxycfg" 11 "github.com/hashicorp/consul/agent/structs" 12 ) 13 14 // endpointsFromSnapshot returns the xDS API representation of the "endpoints" 15 // (upstream instances) in the snapshot. 16 func endpointsFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot, token string) ([]proto.Message, error) { 17 if cfgSnap == nil { 18 return nil, errors.New("nil config given") 19 } 20 resources := make([]proto.Message, 0, len(cfgSnap.UpstreamEndpoints)) 21 for id, endpoints := range cfgSnap.UpstreamEndpoints { 22 if len(endpoints) < 1 { 23 continue 24 } 25 la := makeLoadAssignment(id, endpoints) 26 resources = append(resources, la) 27 } 28 return resources, nil 29 } 30 31 func makeEndpoint(clusterName, host string, port int) envoyendpoint.LbEndpoint { 32 return envoyendpoint.LbEndpoint{ 33 Endpoint: &envoyendpoint.Endpoint{ 34 Address: makeAddressPtr(host, port), 35 }, 36 } 37 } 38 39 func makeLoadAssignment(clusterName string, endpoints structs.CheckServiceNodes) *envoy.ClusterLoadAssignment { 40 es := make([]envoyendpoint.LbEndpoint, 0, len(endpoints)) 41 for _, ep := range endpoints { 42 addr := ep.Service.Address 43 if addr == "" { 44 addr = ep.Node.Address 45 } 46 es = append(es, envoyendpoint.LbEndpoint{ 47 Endpoint: &envoyendpoint.Endpoint{ 48 Address: makeAddressPtr(addr, ep.Service.Port), 49 }, 50 }) 51 } 52 return &envoy.ClusterLoadAssignment{ 53 ClusterName: clusterName, 54 Endpoints: []envoyendpoint.LocalityLbEndpoints{{ 55 LbEndpoints: es, 56 }}, 57 } 58 }