Skip to content

Commit b61af18

Browse files
authored
service-profiles: Eliminate the HasDestination trait (#643)
The `HasDestination` trait isn't particularly useful, as it's basically just `AsRef<Addr>`. This change updates the `GetRoutes` signature to support this; and it updates the inbound target type to store an `Addr` instead of an `Option<NameAddr>` (so `Target` will be suitable for this in an upcoming change).
1 parent 8cde1d4 commit b61af18

File tree

8 files changed

+42
-67
lines changed

8 files changed

+42
-67
lines changed

linkerd/app/gateway/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ mod test {
147147
))
148148
};
149149

150+
let socket_addr = SocketAddr::from(([127, 0, 0, 1], 4143));
150151
let target = inbound::Target {
151-
addr: SocketAddr::from(([127, 0, 0, 1], 4143)),
152-
dst_name: dst_name.map(|n| NameAddr::from_str(n).unwrap()),
152+
socket_addr,
153+
logical: dst_name
154+
.map(|n| NameAddr::from_str(n).unwrap().into())
155+
.unwrap_or_else(|| socket_addr.into()),
153156
http_settings: http::Settings::Http2,
154157
tls_client_id: peer_id,
155158
};

linkerd/app/gateway/src/make.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ where
7272

7373
fn call(&mut self, target: inbound::Target) -> Self::Future {
7474
let inbound::Target {
75-
dst_name,
75+
logical,
7676
http_settings,
7777
tls_client_id,
7878
..
@@ -85,7 +85,7 @@ where
8585
}
8686
};
8787

88-
let orig_dst = match dst_name {
88+
let orig_dst = match logical.into_name_addr() {
8989
Some(n) => n,
9090
None => {
9191
return Box::pin(future::ok(Gateway::NoAuthority));

linkerd/app/inbound/src/endpoint.rs

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use linkerd2_app_core::{
55
proxy::{http, identity, tap},
66
router, stack_tracing,
77
transport::{connect, listen, tls},
8-
Addr, Conditional, NameAddr, CANONICAL_DST_HEADER, DST_OVERRIDE_HEADER,
8+
Addr, Conditional, CANONICAL_DST_HEADER, DST_OVERRIDE_HEADER,
99
};
1010
use std::fmt;
1111
use std::net::SocketAddr;
@@ -14,8 +14,8 @@ use tracing::debug;
1414

1515
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1616
pub struct Target {
17-
pub addr: SocketAddr,
18-
pub dst_name: Option<NameAddr>,
17+
pub logical: Addr,
18+
pub socket_addr: SocketAddr,
1919
pub http_settings: http::Settings,
2020
pub tls_client_id: tls::PeerIdentity,
2121
}
@@ -59,7 +59,7 @@ impl http::settings::HasSettings for HttpEndpoint {
5959
impl From<Target> for HttpEndpoint {
6060
fn from(target: Target) -> Self {
6161
Self {
62-
port: target.addr.port(),
62+
port: target.socket_addr.port(),
6363
settings: target.http_settings,
6464
}
6565
}
@@ -105,27 +105,16 @@ impl tls::HasPeerIdentity for TcpEndpoint {
105105

106106
impl From<Target> for Profile {
107107
fn from(t: Target) -> Self {
108-
Profile(
109-
t.dst_name
110-
.clone()
111-
.map(|d| d.into())
112-
.unwrap_or_else(|| t.addr.clone().into()),
113-
)
108+
Profile(t.logical)
114109
}
115110
}
116111

117-
impl Profile {
118-
pub fn addr(&self) -> &Addr {
112+
impl AsRef<Addr> for Profile {
113+
fn as_ref(&self) -> &Addr {
119114
&self.0
120115
}
121116
}
122117

123-
impl profiles::HasDestination for Profile {
124-
fn destination(&self) -> Addr {
125-
self.0.clone()
126-
}
127-
}
128-
129118
impl profiles::WithRoute for Profile {
130119
type Route = dst::Route;
131120

@@ -138,7 +127,7 @@ impl profiles::WithRoute for Profile {
138127
}
139128
}
140129

141-
// // === impl Target ===
130+
// === impl Target ===
142131

143132
impl http::normalize_uri::ShouldNormalizeUri for Target {
144133
fn should_normalize_uri(&self) -> Option<http::uri::Authority> {
@@ -147,12 +136,7 @@ impl http::normalize_uri::ShouldNormalizeUri for Target {
147136
..
148137
} = self.http_settings
149138
{
150-
return Some(
151-
self.dst_name
152-
.as_ref()
153-
.map(|dst| dst.as_http_authority())
154-
.unwrap_or_else(|| Addr::from(self.addr).to_http_authority()),
155-
);
139+
return Some(self.logical.to_http_authority());
156140
}
157141
None
158142
}
@@ -173,7 +157,7 @@ impl tls::HasPeerIdentity for Target {
173157
impl Into<metric_labels::EndpointLabels> for Target {
174158
fn into(self) -> metric_labels::EndpointLabels {
175159
metric_labels::EndpointLabels {
176-
authority: self.dst_name.map(|d| d.as_http_authority()),
160+
authority: self.logical.name_addr().map(|d| d.as_http_authority()),
177161
direction: metric_labels::Direction::In,
178162
tls_id: self.tls_client_id.map(metric_labels::TlsId::ClientId),
179163
labels: None,
@@ -207,7 +191,7 @@ impl tap::Inspect for Target {
207191
}
208192

209193
fn dst_addr<B>(&self, _: &http::Request<B>) -> Option<SocketAddr> {
210-
Some(self.addr)
194+
Some(self.socket_addr)
211195
}
212196

213197
fn dst_labels<B>(&self, _: &http::Request<B>) -> Option<&IndexMap<String, String>> {
@@ -234,7 +218,7 @@ impl tap::Inspect for Target {
234218

235219
impl fmt::Display for Target {
236220
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237-
self.addr.fmt(f)
221+
self.logical.fmt(f)
238222
}
239223
}
240224

@@ -243,33 +227,33 @@ impl stack_tracing::GetSpan<()> for Target {
243227
use tracing::info_span;
244228

245229
match self.http_settings {
246-
http::Settings::Http2 => match self.dst_name.as_ref() {
230+
http::Settings::Http2 => match self.logical.name_addr() {
247231
None => info_span!(
248232
"http2",
249-
port = %self.addr.port(),
233+
port = %self.socket_addr.port(),
250234
),
251235
Some(name) => info_span!(
252236
"http2",
253237
%name,
254-
port = %self.addr.port(),
238+
port = %self.socket_addr.port(),
255239
),
256240
},
257241
http::Settings::Http1 {
258242
keep_alive,
259243
wants_h1_upgrade,
260244
was_absolute_form,
261-
} => match self.dst_name.as_ref() {
245+
} => match self.logical.name_addr() {
262246
None => info_span!(
263247
"http1",
264-
port = %self.addr.port(),
248+
port = %self.socket_addr.port(),
265249
keep_alive,
266250
wants_h1_upgrade,
267251
was_absolute_form,
268252
),
269253
Some(name) => info_span!(
270254
"http1",
271255
%name,
272-
port = %self.addr.port(),
256+
port = %self.socket_addr.port(),
273257
keep_alive,
274258
wants_h1_upgrade,
275259
was_absolute_form,
@@ -291,7 +275,7 @@ impl<A> router::Recognize<http::Request<A>> for RequestTarget {
291275
type Key = Target;
292276

293277
fn recognize(&self, req: &http::Request<A>) -> Self::Key {
294-
let dst_name = req
278+
let logical = req
295279
.headers()
296280
.get(CANONICAL_DST_HEADER)
297281
.and_then(|dst| {
@@ -312,12 +296,11 @@ impl<A> router::Recognize<http::Request<A>> for RequestTarget {
312296
})
313297
.or_else(|| http_request_authority_addr(req).ok())
314298
.or_else(|| http_request_host_addr(req).ok())
315-
.or_else(|| self.accept.addrs.target_addr_if_not_local().map(Addr::from))
316-
.and_then(|a| a.name_addr().cloned());
299+
.unwrap_or_else(|| self.accept.addrs.target_addr().into());
317300

318301
Target {
319-
dst_name,
320-
addr: self.accept.addrs.target_addr(),
302+
logical,
303+
socket_addr: self.accept.addrs.target_addr(),
321304
tls_client_id: self.accept.peer_identity.clone(),
322305
http_settings: http::Settings::from_request(req),
323306
}
@@ -330,11 +313,6 @@ impl router::Recognize<Target> for ProfileTarget {
330313
type Key = Profile;
331314

332315
fn recognize(&self, t: &Target) -> Self::Key {
333-
Profile(
334-
t.dst_name
335-
.clone()
336-
.map(Into::into)
337-
.unwrap_or_else(|| t.addr.into()),
338-
)
316+
Profile(t.logical.clone())
339317
}
340318
}

linkerd/app/inbound/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl Config {
258258
),
259259
)
260260
.spawn_buffer(buffer_capacity)
261-
.instrument(|p: &Profile| info_span!("profile", addr = %p.addr()))
261+
.instrument(|p: &Profile| info_span!("profile", addr = %p.as_ref()))
262262
.check_make_service::<Profile, Target>()
263263
.push(router::Layer::new(|()| ProfileTarget))
264264
.check_new_service::<(), Target>()

linkerd/app/inbound/src/prevent_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl admit::Admit<Target> for PreventLoop {
2424
type Error = LoopPrevented;
2525

2626
fn admit(&mut self, ep: &Target) -> Result<(), Self::Error> {
27-
tracing::debug!(addr = %ep.addr, self.port);
28-
if ep.addr.port() == self.port {
27+
tracing::debug!(addr = %ep.socket_addr, self.port);
28+
if ep.socket_addr.port() == self.port {
2929
return Err(LoopPrevented { port: self.port });
3030
}
3131

linkerd/app/outbound/src/endpoint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ impl<T> router::Recognize<Target<T>> for ProfilePerTarget {
430430

431431
// === impl Profile ===
432432

433-
impl profiles::HasDestination for Profile {
434-
fn destination(&self) -> Addr {
435-
self.0.clone()
433+
impl AsRef<Addr> for Profile {
434+
fn as_ref(&self) -> &Addr {
435+
&self.0
436436
}
437437
}
438438

linkerd/service-profiles/src/client.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ where
112112
}
113113
}
114114

115-
impl<S, R> tower::Service<Addr> for Client<S, R>
115+
impl<T, S, R> tower::Service<T> for Client<S, R>
116116
where
117+
T: AsRef<Addr>,
117118
S: GrpcService<BoxBody> + Clone + Send + 'static,
118119
S::ResponseBody: Send,
119120
<S::ResponseBody as Body>::Data: Send,
@@ -132,8 +133,8 @@ where
132133
Poll::Ready(Ok(()))
133134
}
134135

135-
fn call(&mut self, dst: Addr) -> Self::Future {
136-
let path = dst.to_string();
136+
fn call(&mut self, dst: T) -> Self::Future {
137+
let path = dst.as_ref().to_string();
137138

138139
let service = {
139140
// In case the ready service holds resources, pass it into the

linkerd/service-profiles/src/http/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ pub trait GetRoutes<T> {
5050

5151
impl<T, S> GetRoutes<T> for S
5252
where
53-
T: HasDestination,
54-
S: tower::Service<Addr, Response = watch::Receiver<Routes>>,
53+
S: tower::Service<T, Response = watch::Receiver<Routes>>,
5554
S::Error: Into<Error>,
5655
{
5756
type Error = S::Error;
@@ -62,7 +61,7 @@ where
6261
}
6362

6463
fn get_routes(&mut self, target: T) -> Self::Future {
65-
tower::Service::call(self, target.destination())
64+
tower::Service::call(self, target)
6665
}
6766
}
6867

@@ -79,12 +78,6 @@ pub trait OverrideDestination {
7978
fn dst_mut(&mut self) -> &mut Addr;
8079
}
8180

82-
/// Implemented by target types that may have a `NameAddr` destination that
83-
/// can be discovered via `GetRoutes`.
84-
pub trait HasDestination {
85-
fn destination(&self) -> Addr;
86-
}
87-
8881
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
8982
pub struct Route {
9083
labels: Labels,

0 commit comments

Comments
 (0)