module Endpoint
Builds Envoy endpoint resources.
Definitions
def build(cluster_name, endpoints)
Build an EDS cluster load assignment from normalized endpoint state.
Signature
-
parameter
cluster_nameString The cluster name.
-
parameter
endpointsArray(Hash) The endpoints, each containing
:addressesand:healthy.-
returns
Envoy::Config::Endpoint::V3::ClusterLoadAssignment The generated endpoint resource.
Implementation
def build(cluster_name, endpoints)
Envoy::Config::Endpoint::V3::ClusterLoadAssignment.new(
cluster_name: cluster_name.to_s,
endpoints: [
Envoy::Config::Endpoint::V3::LocalityLbEndpoints.new(
lb_endpoints: endpoints.map{|endpoint| load_balancer_endpoint(endpoint)}
)
]
)
end
def load_balancer_endpoint(endpoint)
- private
Build an Envoy load-balancer endpoint from normalized endpoint state.
Signature
-
parameter
endpointHash The endpoint containing
:addressesand:healthy.-
returns
Envoy::Config::Endpoint::V3::LbEndpoint The generated load-balancer endpoint.
-
raises
KeyError If required endpoint state is missing.
-
raises
ArgumentError If the endpoint has no addresses.
- private
Implementation
def load_balancer_endpoint(endpoint)
addresses, healthy = endpoint.fetch_values(:addresses, :healthy)
raise ArgumentError, "An endpoint requires at least one address!" if addresses.empty?
address, *additional_addresses = addresses
Envoy::Config::Endpoint::V3::LbEndpoint.new(
endpoint: Envoy::Config::Endpoint::V3::Endpoint.new(
address: build_address(address),
hostname: endpoint[:hostname],
additional_addresses: additional_addresses.map do |additional_address|
Envoy::Config::Endpoint::V3::Endpoint::AdditionalAddress.new(
address: build_address(additional_address)
)
end
),
health_status: health_status_value(healthy)
)
end
def build_address(address)
- private
Build an Envoy address from a normalized IP or Unix address.
Signature
-
parameter
addressHash An IP
:addressand:port, or a Unix:path.-
returns
Envoy::Config::Core::V3::Address The generated Envoy address.
-
raises
KeyError If required IP address state is missing.
- private
Implementation
def build_address(address)
if path = address[:path]
Envoy::Config::Core::V3::Address.new(
pipe: Envoy::Config::Core::V3::Pipe.new(path: path)
)
else
Envoy::Config::Core::V3::Address.new(
socket_address: Envoy::Config::Core::V3::SocketAddress.new(
protocol: Envoy::Config::Core::V3::SocketAddress::Protocol::TCP,
address: address.fetch(:address),
port_value: address.fetch(:port)
)
)
end
end
def health_status_value(healthy)
- private
Convert an endpoint health status to its Envoy enum value.
Signature
-
parameter
healthyBoolean | Symbol | String The normalized health status.
-
returns
Integer The Envoy health-status enum value.
- private
Implementation
def health_status_value(healthy)
case healthy
when :healthy, :HEALTHY, "healthy", "HEALTHY", true
Envoy::Config::Core::V3::HealthStatus::HEALTHY
when :unhealthy, :UNHEALTHY, "unhealthy", "UNHEALTHY", false
Envoy::Config::Core::V3::HealthStatus::UNHEALTHY
when :degraded, :DEGRADED, "degraded", "DEGRADED"
Envoy::Config::Core::V3::HealthStatus::DEGRADED
else
Envoy::Config::Core::V3::HealthStatus::UNKNOWN
end
end