Skip to content

Commit f0fce1c

Browse files
authored
outbound: Simplify Endpoint type (#935)
The outbound proxy's `Endpoint` target-type is currently coupled with the `Logical` type and it contains two `SocketAddr` values that are undifferentiated. This change updates the `Endpoint` type to be decoupled from the `Logical` type and to hold a single `addr` value.
1 parent 1ae78aa commit f0fce1c

File tree

3 files changed

+56
-55
lines changed

3 files changed

+56
-55
lines changed

linkerd/app/outbound/src/http/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Param<normalize_uri::DefaultAuthority> for Logical {
8787

8888
impl Param<client::Settings> for Endpoint {
8989
fn param(&self) -> client::Settings {
90-
match self.logical.protocol {
90+
match self.protocol {
9191
Version::H2 => client::Settings::H2,
9292
Version::Http1 => match self.metadata.protocol_hint() {
9393
ProtocolHint::Unknown => client::Settings::Http1,
@@ -99,7 +99,7 @@ impl Param<client::Settings> for Endpoint {
9999

100100
impl Param<Option<SessionProtocol>> for Endpoint {
101101
fn param(&self) -> Option<SessionProtocol> {
102-
match self.logical.protocol {
102+
match self.protocol {
103103
Version::H2 => Some(SessionProtocol::Http2),
104104
Version::Http1 => match self.metadata.protocol_hint() {
105105
ProtocolHint::Http2 => Some(SessionProtocol::Http2),
@@ -133,7 +133,7 @@ impl tap::Inspect for Endpoint {
133133
}
134134

135135
fn dst_addr<B>(&self, _: &Request<B>) -> Option<SocketAddr> {
136-
Some(self.addr)
136+
Some(self.addr.into())
137137
}
138138

139139
fn dst_labels<B>(&self, _: &Request<B>) -> Option<&IndexMap<String, String>> {

linkerd/app/outbound/src/target.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub struct Concrete<P> {
3535

3636
#[derive(Clone, Debug)]
3737
pub struct Endpoint<P> {
38-
pub addr: SocketAddr,
39-
pub target_addr: SocketAddr,
38+
pub addr: Remote<ServerAddr>,
4039
pub tls: tls::ConditionalClientTls,
4140
pub metadata: Metadata,
42-
pub logical: Logical<P>,
41+
pub logical_addr: Addr,
42+
pub protocol: P,
4343
}
4444

4545
// === impl Accept ===
@@ -188,28 +188,25 @@ impl<P> Param<ConcreteAddr> for Concrete<P> {
188188

189189
impl<P> Endpoint<P> {
190190
pub fn from_logical(reason: tls::NoClientTls) -> impl (Fn(Logical<P>) -> Self) + Clone {
191-
move |logical| {
192-
let target_addr = logical.orig_dst;
193-
match logical
194-
.profile
195-
.as_ref()
196-
.and_then(|p| p.borrow().endpoint.clone())
197-
{
198-
None => Self {
199-
addr: logical.param(),
200-
metadata: Metadata::default(),
201-
tls: Conditional::None(reason),
202-
logical,
203-
target_addr,
204-
},
205-
Some((addr, metadata)) => Self {
206-
addr,
207-
tls: EndpointFromMetadata::client_tls(&metadata),
208-
metadata,
209-
logical,
210-
target_addr,
211-
},
212-
}
191+
move |logical| match logical
192+
.profile
193+
.as_ref()
194+
.and_then(|p| p.borrow().endpoint.clone())
195+
{
196+
None => Self {
197+
addr: Remote(ServerAddr(logical.orig_dst)),
198+
metadata: Metadata::default(),
199+
tls: Conditional::None(reason),
200+
logical_addr: logical.addr(),
201+
protocol: logical.protocol,
202+
},
203+
Some((addr, metadata)) => Self {
204+
addr: Remote(ServerAddr(addr)),
205+
tls: EndpointFromMetadata::client_tls(&metadata),
206+
metadata,
207+
logical_addr: logical.addr(),
208+
protocol: logical.protocol,
209+
},
213210
}
214211
}
215212

@@ -226,7 +223,7 @@ impl<P> Endpoint<P> {
226223

227224
impl<P> Param<Remote<ServerAddr>> for Endpoint<P> {
228225
fn param(&self) -> Remote<ServerAddr> {
229-
Remote(ServerAddr(self.addr))
226+
self.addr
230227
}
231228
}
232229

@@ -245,10 +242,10 @@ impl<P> Param<transport::labels::Key> for Endpoint<P> {
245242
impl<P> Param<metrics::OutboundEndpointLabels> for Endpoint<P> {
246243
fn param(&self) -> metrics::OutboundEndpointLabels {
247244
metrics::OutboundEndpointLabels {
248-
authority: Some(self.logical.addr().to_http_authority()),
245+
authority: Some(self.logical_addr.to_http_authority()),
249246
labels: metrics::prefix_labels("dst", self.metadata.labels().iter()),
250247
server_id: self.tls.clone(),
251-
target_addr: self.target_addr,
248+
target_addr: self.addr.into(),
252249
}
253250
}
254251
}
@@ -263,8 +260,8 @@ impl<P: std::hash::Hash> std::hash::Hash for Endpoint<P> {
263260
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
264261
self.addr.hash(state);
265262
self.tls.hash(state);
266-
self.logical.orig_dst.hash(state);
267-
self.logical.protocol.hash(state);
263+
self.logical_addr.hash(state);
264+
self.protocol.hash(state);
268265
}
269266
}
270267

@@ -297,7 +294,7 @@ impl EndpointFromMetadata {
297294
}
298295
}
299296

300-
impl<P: Clone + std::fmt::Debug> MapEndpoint<Concrete<P>, Metadata> for EndpointFromMetadata {
297+
impl<P: Copy + std::fmt::Debug> MapEndpoint<Concrete<P>, Metadata> for EndpointFromMetadata {
301298
type Out = Endpoint<P>;
302299

303300
fn map_endpoint(
@@ -307,12 +304,13 @@ impl<P: Clone + std::fmt::Debug> MapEndpoint<Concrete<P>, Metadata> for Endpoint
307304
metadata: Metadata,
308305
) -> Self::Out {
309306
tracing::trace!(%addr, ?metadata, ?concrete, "Resolved endpoint");
307+
let tls = Self::client_tls(&metadata);
310308
Endpoint {
311-
addr,
312-
tls: Self::client_tls(&metadata),
309+
addr: Remote(ServerAddr(addr)),
310+
tls,
313311
metadata,
314-
logical: concrete.logical.clone(),
315-
target_addr: concrete.logical.orig_dst,
312+
logical_addr: concrete.logical.addr(),
313+
protocol: concrete.logical.protocol,
316314
}
317315
}
318316
}

linkerd/app/outbound/src/tcp/opaque_transport.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use linkerd_app_core::{
33
dns, io,
44
svc::{self, Param},
55
tls,
6+
transport::{Remote, ServerAddr},
67
transport_header::{SessionProtocol, TransportHeader, PROTOCOL},
78
Error,
89
};
@@ -56,14 +57,15 @@ where
5657
fn call(&mut self, mut ep: Endpoint<P>) -> Self::Future {
5758
// Configure the target port from the endpoint. In opaque cases, this is
5859
// the application's actual port to be encoded in the header.
59-
let mut target_port = ep.addr.port();
60+
let Remote(ServerAddr(addr)) = ep.addr;
61+
let mut target_port = addr.port();
6062

6163
// If this endpoint should use opaque transport, then we update the
6264
// endpoint so the connection actually targets the target proxy's
6365
// inbound port.
6466
if let Some(opaque_port) = ep.metadata.opaque_transport_port() {
6567
debug!(target_port, opaque_port, "Using opaque transport");
66-
ep.addr = (ep.addr.ip(), opaque_port).into();
68+
ep.addr = Remote(ServerAddr((addr.ip(), opaque_port).into()));
6769
}
6870

6971
// If an authority override is present, we're communicating with a
@@ -112,30 +114,27 @@ where
112114
#[cfg(test)]
113115
mod test {
114116
use super::*;
115-
use crate::target::{Endpoint, Logical};
117+
use crate::target::Endpoint;
116118
use futures::future;
117119
use linkerd_app_core::{
118120
io::{self, AsyncWriteExt},
119121
proxy::api_resolve::{Metadata, ProtocolHint},
120122
tls,
123+
transport::{Remote, ServerAddr},
121124
transport_header::TransportHeader,
122-
Conditional,
125+
Addr, Conditional,
123126
};
124127
use pin_project::pin_project;
125128
use std::task::Context;
126129
use tower::util::{service_fn, ServiceExt};
127130

128131
fn ep(metadata: Metadata) -> Endpoint<()> {
129132
Endpoint {
130-
addr: ([127, 0, 0, 2], 4321).into(),
131-
target_addr: ([127, 0, 0, 2], 4321).into(),
133+
addr: Remote(ServerAddr(([127, 0, 0, 2], 4321).into())),
132134
tls: Conditional::None(tls::NoClientTls::NotProvidedByServiceDiscovery),
133135
metadata,
134-
logical: Logical {
135-
orig_dst: ([127, 0, 0, 2], 4321).into(),
136-
profile: None,
137-
protocol: (),
138-
},
136+
logical_addr: Addr::Socket(([127, 0, 0, 2], 4321).into()),
137+
protocol: (),
139138
}
140139
}
141140

@@ -146,7 +145,8 @@ mod test {
146145

147146
let svc = OpaqueTransport {
148147
inner: service_fn(|ep: Endpoint<()>| {
149-
assert_eq!(ep.addr.port(), 4321);
148+
let Remote(ServerAddr(sa)) = ep.addr;
149+
assert_eq!(sa.port(), 4321);
150150
future::ready(Ok::<_, io::Error>(Io {
151151
io: tokio_test::io::Builder::new().write(b"hello").build(),
152152
alpn: None,
@@ -167,9 +167,10 @@ mod test {
167167

168168
let svc = OpaqueTransport {
169169
inner: service_fn(|ep: Endpoint<()>| {
170-
assert_eq!(ep.addr.port(), 4143);
170+
let Remote(ServerAddr(sa)) = ep.addr;
171+
assert_eq!(sa.port(), 4143);
171172
let hdr = TransportHeader {
172-
port: ep.logical.orig_dst.port(),
173+
port: 4321,
173174
name: None,
174175
protocol: None,
175176
};
@@ -202,7 +203,8 @@ mod test {
202203

203204
let svc = OpaqueTransport {
204205
inner: service_fn(|ep: Endpoint<()>| {
205-
assert_eq!(ep.addr.port(), 4143);
206+
let Remote(ServerAddr(sa)) = ep.addr;
207+
assert_eq!(sa.port(), 4143);
206208
let hdr = TransportHeader {
207209
port: 5555,
208210
name: Some(dns::Name::from_str("foo.bar.example.com").unwrap()),
@@ -237,9 +239,10 @@ mod test {
237239

238240
let svc = OpaqueTransport {
239241
inner: service_fn(|ep: Endpoint<()>| {
240-
assert_eq!(ep.addr.port(), 4143);
242+
let Remote(ServerAddr(sa)) = ep.addr;
243+
assert_eq!(sa.port(), 4143);
241244
let hdr = TransportHeader {
242-
port: ep.logical.orig_dst.port(),
245+
port: 4321,
243246
name: None,
244247
protocol: None,
245248
};

0 commit comments

Comments
 (0)