forked from istio/ztunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxyfactory.rs
146 lines (131 loc) · 4.7 KB
/
proxyfactory.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::config;
use crate::identity::SecretManager;
use crate::state::{DemandProxyState, WorkloadInfo};
use std::sync::Arc;
use tracing::error;
use crate::dns;
use crate::drain::DrainWatcher;
use crate::proxy::connection_manager::ConnectionManager;
use crate::proxy::{Error, LocalWorkloadInformation, Metrics};
use crate::proxy::Proxy;
// Proxy factory creates ztunnel proxies using a socket factory.
// this allows us to create our proxies the same way in regular mode and in inpod mode.
pub struct ProxyFactory {
config: Arc<config::Config>,
state: DemandProxyState,
cert_manager: Arc<SecretManager>,
proxy_metrics: Arc<Metrics>,
dns_metrics: Option<Arc<dns::Metrics>>,
drain: DrainWatcher,
}
impl ProxyFactory {
pub fn new(
config: Arc<config::Config>,
state: DemandProxyState,
cert_manager: Arc<SecretManager>,
proxy_metrics: Arc<Metrics>,
dns_metrics: Option<dns::Metrics>,
drain: DrainWatcher,
) -> std::io::Result<Self> {
let dns_metrics = match dns_metrics {
Some(metrics) => Some(Arc::new(metrics)),
None => {
if config.dns_proxy {
error!("dns proxy configured but no dns metrics provided")
}
None
}
};
Ok(ProxyFactory {
config,
state,
cert_manager,
proxy_metrics,
dns_metrics,
drain,
})
}
pub async fn new_proxies_for_dedicated(
&self,
proxy_workload_info: WorkloadInfo,
) -> Result<ProxyResult, Error> {
let factory: Arc<dyn crate::proxy::SocketFactory + Send + Sync> =
if let Some(mark) = self.config.packet_mark {
Arc::new(crate::proxy::MarkSocketFactory(mark))
} else {
Arc::new(crate::proxy::DefaultSocketFactory)
};
self.new_proxies_from_factory(None, proxy_workload_info, factory)
.await
}
pub async fn new_proxies_from_factory(
&self,
proxy_drain: Option<DrainWatcher>,
proxy_workload_info: WorkloadInfo,
socket_factory: Arc<dyn crate::proxy::SocketFactory + Send + Sync>,
) -> Result<ProxyResult, Error> {
let mut result: ProxyResult = Default::default();
let drain = proxy_drain.unwrap_or_else(|| self.drain.clone());
let mut resolver = None;
let local_workload_information = Arc::new(LocalWorkloadInformation::new(
Arc::new(proxy_workload_info),
self.state.clone(),
self.cert_manager.clone(),
));
// Optionally create the DNS proxy.
if self.config.dns_proxy {
let server = dns::Server::new(
self.config.cluster_domain.clone(),
self.config.dns_proxy_addr,
self.state.clone(),
dns::forwarder_for_mode(
self.config.proxy_mode,
self.config.cluster_domain.clone(),
socket_factory.clone(),
)?,
self.dns_metrics.clone().unwrap(),
drain.clone(),
socket_factory.as_ref(),
local_workload_information.as_fetcher(),
)
.await?;
resolver = Some(server.resolver());
result.dns_proxy = Some(server);
}
// Optionally create the HBONE proxy.
if self.config.proxy {
let cm = ConnectionManager::default();
let pi = crate::proxy::ProxyInputs::new(
self.config.clone(),
cm.clone(),
self.state.clone(),
self.proxy_metrics.clone(),
socket_factory.clone(),
resolver,
local_workload_information,
);
result.connection_manager = Some(cm);
result.proxy = Some(Proxy::from_inputs(pi, drain).await?);
}
Ok(result)
}
}
#[derive(Default)]
pub struct ProxyResult {
pub proxy: Option<Proxy>,
pub dns_proxy: Option<dns::Server>,
pub connection_manager: Option<ConnectionManager>,
}