...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/ambex/endpoint.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/ambex

     1  package ambex
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	v3core "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/core/v3"
     9  	v3endpoint "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/endpoint/v3"
    10  )
    11  
    12  // The Endpoints struct is how Endpoint data gets communicated to ambex. This is a bit simpler than
    13  // the envoy endpoint data structures, and also provides us a layer of indirection to buffer us from
    14  // changes in envoy configuration, e.g. we can switch from v2 to v3 endpoint data, or add v3
    15  // endpoint data fairly easily with this layer of indirection.
    16  type Endpoints struct {
    17  	Entries map[string][]*Endpoint
    18  }
    19  
    20  func (e *Endpoints) RoutesString() string {
    21  	var routes []string
    22  	for k, eps := range e.Entries {
    23  		var addrs []string
    24  		for _, ep := range eps {
    25  			addr := fmt.Sprintf("%s:%s:%d", ep.Protocol, ep.Ip, ep.Port)
    26  			addrs = append(addrs, addr)
    27  		}
    28  		routes = append(routes, fmt.Sprintf("%s=[%s]", k, strings.Join(addrs, ", ")))
    29  	}
    30  	sort.Strings(routes)
    31  	return strings.Join(routes, "\n")
    32  }
    33  
    34  // ToMap_v3 produces a map with the envoy v3 friendly forms of all the endpoint data.
    35  func (e *Endpoints) ToMap_v3() map[string]*v3endpoint.ClusterLoadAssignment {
    36  	result := map[string]*v3endpoint.ClusterLoadAssignment{}
    37  	for name, eps := range e.Entries {
    38  		var endpoints []*v3endpoint.LbEndpoint
    39  		for _, ep := range eps {
    40  			endpoints = append(endpoints, ep.ToLbEndpoint_v3())
    41  		}
    42  		loadAssignment := &v3endpoint.ClusterLoadAssignment{
    43  			ClusterName: name,
    44  			Endpoints:   []*v3endpoint.LocalityLbEndpoints{{LbEndpoints: endpoints}},
    45  		}
    46  		result[name] = loadAssignment
    47  	}
    48  	return result
    49  }
    50  
    51  // Endpoint contains the subset of fields we bother to expose.
    52  type Endpoint struct {
    53  	ClusterName string
    54  	Ip          string
    55  	Port        uint32
    56  	Protocol    string
    57  }
    58  
    59  // ToLBEndpoint_v3 translates to envoy v3 frinedly form of the Endpoint data.
    60  func (e *Endpoint) ToLbEndpoint_v3() *v3endpoint.LbEndpoint {
    61  	return &v3endpoint.LbEndpoint{
    62  		HostIdentifier: &v3endpoint.LbEndpoint_Endpoint{
    63  			Endpoint: &v3endpoint.Endpoint{
    64  				Address: &v3core.Address{
    65  					Address: &v3core.Address_SocketAddress{
    66  						SocketAddress: &v3core.SocketAddress{
    67  							Protocol: v3core.SocketAddress_Protocol(v3core.SocketAddress_Protocol_value[e.Protocol]),
    68  							Address:  e.Ip,
    69  							PortSpecifier: &v3core.SocketAddress_PortValue{
    70  								PortValue: e.Port,
    71  							},
    72  							Ipv4Compat: true,
    73  						},
    74  					},
    75  				},
    76  			},
    77  		},
    78  	}
    79  }
    80  

View as plain text