2023-08-17 18:43:21 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package xdsv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
|
|
|
"github.com/hashicorp/consul/agent/xds/response"
|
2023-08-17 19:55:54 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
2023-08-17 18:43:21 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1/pbproxystate"
|
2023-08-17 19:55:54 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-08-17 18:43:21 +00:00
|
|
|
)
|
|
|
|
|
2023-08-17 19:55:54 +00:00
|
|
|
func makeEnvoyLbEndpoint(endpoint *pbproxystate.Endpoint) *envoy_endpoint_v3.LbEndpoint {
|
|
|
|
hs := int32(endpoint.GetHealthStatus().Number())
|
|
|
|
return &envoy_endpoint_v3.LbEndpoint{
|
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: makeEnvoyEndpoint(endpoint),
|
|
|
|
},
|
|
|
|
HealthStatus: envoy_core_v3.HealthStatus(hs),
|
|
|
|
LoadBalancingWeight: endpoint.GetLoadBalancingWeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeEnvoyEndpoint(endpoint *pbproxystate.Endpoint) *envoy_endpoint_v3.Endpoint {
|
2023-08-17 18:43:21 +00:00
|
|
|
var address *envoy_core_v3.Address
|
|
|
|
if endpoint.GetUnixSocket() != nil {
|
|
|
|
address = response.MakePipeAddress(endpoint.GetUnixSocket().GetPath(), 0)
|
|
|
|
} else {
|
|
|
|
address = response.MakeAddress(endpoint.GetHostPort().GetHost(), int(endpoint.GetHostPort().Port))
|
|
|
|
}
|
2023-08-17 19:55:54 +00:00
|
|
|
|
|
|
|
return &envoy_endpoint_v3.Endpoint{
|
|
|
|
Address: address,
|
2023-08-17 18:43:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeEnvoyClusterLoadAssignment(clusterName string, endpoints []*pbproxystate.Endpoint) *envoy_endpoint_v3.ClusterLoadAssignment {
|
2023-08-17 19:55:54 +00:00
|
|
|
localityLbEndpoints := &envoy_endpoint_v3.LocalityLbEndpoints{}
|
2023-08-17 18:43:21 +00:00
|
|
|
for _, endpoint := range endpoints {
|
2023-08-17 19:55:54 +00:00
|
|
|
localityLbEndpoints.LbEndpoints = append(localityLbEndpoints.LbEndpoints, makeEnvoyLbEndpoint(endpoint))
|
2023-08-17 18:43:21 +00:00
|
|
|
}
|
|
|
|
return &envoy_endpoint_v3.ClusterLoadAssignment{
|
|
|
|
ClusterName: clusterName,
|
2023-08-17 19:55:54 +00:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{localityLbEndpoints},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *ProxyResources) makeXDSEndpoints() ([]proto.Message, error) {
|
|
|
|
endpoints := make([]proto.Message, 0)
|
|
|
|
|
|
|
|
for clusterName, eps := range pr.proxyState.GetEndpoints() {
|
|
|
|
if clusterName != xdscommon.LocalAppClusterName {
|
|
|
|
protoEndpoint := makeEnvoyClusterLoadAssignment(clusterName, eps.Endpoints)
|
|
|
|
endpoints = append(endpoints, protoEndpoint)
|
|
|
|
}
|
2023-08-17 18:43:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 19:55:54 +00:00
|
|
|
return endpoints, nil
|
2023-08-17 18:43:21 +00:00
|
|
|
}
|