Skip to content

Commit 03d87b4

Browse files
committed
Merge remote-tracking branch 'ds/gateway-wavekv' into gateway-wavekv
2 parents 3e44d36 + 7190b24 commit 03d87b4

15 files changed

Lines changed: 116 additions & 479 deletions

gateway/src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@ async fn maybe_gen_certs(config: &Config, tls_config: &TlsConfig) -> Result<()>
7373
return Ok(());
7474
}
7575

76+
// Build alt_names: include rpc_domain and hostname from my_url
77+
let mut alt_names = vec![config.rpc_domain.clone()];
78+
if let Ok(url) = reqwest::Url::parse(&config.sync.my_url) {
79+
if let Some(host) = url.host_str() {
80+
if host != config.rpc_domain {
81+
alt_names.push(host.to_string());
82+
}
83+
}
84+
}
7685
if !config.debug.insecure_skip_attestation {
7786
info!("Using dstack guest agent for certificate generation");
7887
let agent_client = dstack_agent().context("Failed to create dstack client")?;
88+
7989
let response = agent_client
8090
.get_tls_key(GetTlsKeyArgs {
8191
subject: "dstack-gateway".to_string(),
82-
alt_names: vec![config.rpc_domain.clone()],
92+
alt_names,
8393
usage_ra_tls: true,
8494
usage_server_auth: true,
8595
usage_client_auth: true,
@@ -114,7 +124,7 @@ async fn maybe_gen_certs(config: &Config, tls_config: &TlsConfig) -> Result<()>
114124
let cert = ra_tls::cert::CertRequest::builder()
115125
.key(&key)
116126
.subject("dstack-gateway")
117-
.alt_names(std::slice::from_ref(&config.rpc_domain))
127+
.alt_names(&alt_names)
118128
.usage_server_auth(true)
119129
.build()
120130
.self_signed()

gateway/src/main_service.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub struct ProxyInner {
7272
kv_store: Arc<KvStore>,
7373
/// WaveKV sync service for network synchronization
7474
pub(crate) wavekv_sync: Option<Arc<WaveKvSyncService>>,
75+
/// HTTPS client config for mTLS (used for bootnode peer discovery)
76+
https_config: Option<HttpsClientConfig>,
7577
}
7678

7779
#[derive(Debug, Serialize, Deserialize, Default)]
@@ -189,7 +191,7 @@ impl ProxyInner {
189191

190192
// Create WaveKV sync service (only if sync is enabled)
191193
let wavekv_sync = if config.sync.enabled {
192-
match WaveKvSyncService::new(&kv_store, &config.sync, https_config) {
194+
match WaveKvSyncService::new(&kv_store, &config.sync, https_config.clone()) {
193195
Ok(sync_service) => Some(Arc::new(sync_service)),
194196
Err(err) => {
195197
error!("Failed to create WaveKV sync service: {err}");
@@ -266,6 +268,7 @@ impl ProxyInner {
266268
certbot,
267269
kv_store,
268270
wavekv_sync,
271+
https_config: Some(https_config),
269272
})
270273
}
271274

@@ -289,6 +292,7 @@ impl Proxy {
289292
start_certbot_task(self.clone()).await;
290293
start_cert_store_watch_task(self.clone());
291294
start_zt_domain_watch_task(self.clone());
295+
start_bootnode_discovery_task(self.clone());
292296
Ok(())
293297
}
294298

@@ -583,6 +587,48 @@ fn start_zt_domain_watch_task(proxy: Proxy) {
583587
info!("ZT-Domain watch task started");
584588
}
585589

590+
/// Periodically retry bootnode peer discovery if no peers are available
591+
fn start_bootnode_discovery_task(proxy: Proxy) {
592+
if !proxy.config.sync.enabled || proxy.config.sync.bootnode.is_empty() {
593+
return;
594+
}
595+
596+
let bootnode = proxy.config.sync.bootnode.clone();
597+
let node_id = proxy.config.sync.node_id;
598+
let kv_store = proxy.kv_store.clone();
599+
let https_config = match &proxy.https_config {
600+
Some(config) => config.clone(),
601+
None => return,
602+
};
603+
604+
tokio::spawn(async move {
605+
let mut interval = tokio::time::interval(Duration::from_secs(10));
606+
loop {
607+
interval.tick().await;
608+
// Check if we already have peers
609+
let n_peers = kv_store
610+
.load_all_node_statuses()
611+
.keys()
612+
.filter(|&id| *id != node_id)
613+
.count();
614+
if n_peers > 0 {
615+
info!("bootnode peer discovery finished, {n_peers} peers found");
616+
break;
617+
}
618+
// Try to fetch peers from bootnode
619+
debug!("retrying bootnode peer discovery...");
620+
if let Err(err) =
621+
fetch_peers_from_bootnode(&bootnode, &kv_store, node_id, &https_config).await
622+
{
623+
debug!("bootnode discovery retry failed: {err}");
624+
} else {
625+
info!("bootnode peer discovery succeeded");
626+
}
627+
}
628+
});
629+
info!("Bootnode discovery task started (will retry every 10s if no peers)");
630+
}
631+
586632
async fn start_wavekv_sync_task(proxy: Proxy, wavekv_sync: Arc<WaveKvSyncService>) {
587633
if !proxy.config.sync.enabled {
588634
info!("WaveKV sync is disabled");

gateway/src/proxy/tls_passthough.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async fn resolve_app_address(prefix: &str, sni: &str, compat: bool) -> Result<Ap
5656
};
5757
return AppAddress::parse(data).context("failed to parse app address");
5858
}
59-
anyhow::bail!("failed to resolve app address");
59+
anyhow::bail!("failed to resolve legacy app address");
6060
} else {
6161
let lookup = resolver
6262
.txt_lookup(txt_domain)

gateway/test-run/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/run/
22
.env
3+
/e2e/dstack-gateway

gateway/test-run/e2e/certs/gateway-ca.cert

Lines changed: 0 additions & 31 deletions
This file was deleted.

gateway/test-run/e2e/certs/gateway-ca.key

Lines changed: 0 additions & 52 deletions
This file was deleted.

gateway/test-run/e2e/certs/gateway-rpc.cert

Lines changed: 0 additions & 27 deletions
This file was deleted.

gateway/test-run/e2e/certs/gateway-rpc.key

Lines changed: 0 additions & 28 deletions
This file was deleted.

gateway/test-run/e2e/configs/gateway-1.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ address = "0.0.0.0"
44
port = 9012
55

66
[tls]
7-
key = "/etc/gateway/certs/gateway-rpc.key"
8-
certs = "/etc/gateway/certs/gateway-rpc.cert"
7+
key = "/var/lib/gateway/certs/gateway-rpc.key"
8+
certs = "/var/lib/gateway/certs/gateway-rpc.cert"
99

1010
[tls.mutual]
11-
ca_certs = "/etc/gateway/certs/gateway-ca.cert"
11+
ca_certs = "/var/lib/gateway/certs/gateway-ca.cert"
1212
mandatory = false
1313

1414
[core]
1515
kms_url = ""
16-
rpc_domain = "gateway.test.local"
16+
rpc_domain = "gateway-1"
1717

1818
[core.admin]
1919
enabled = true
@@ -22,7 +22,7 @@ address = "0.0.0.0"
2222

2323
[core.debug]
2424
insecure_enable_debug_rpc = true
25-
insecure_skip_attestation = true
25+
insecure_skip_attestation = false
2626
port = 9015
2727
address = "0.0.0.0"
2828

gateway/test-run/e2e/configs/gateway-2.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ address = "0.0.0.0"
44
port = 9012
55

66
[tls]
7-
key = "/etc/gateway/certs/gateway-rpc.key"
8-
certs = "/etc/gateway/certs/gateway-rpc.cert"
7+
key = "/var/lib/gateway/certs/gateway-rpc.key"
8+
certs = "/var/lib/gateway/certs/gateway-rpc.cert"
99

1010
[tls.mutual]
11-
ca_certs = "/etc/gateway/certs/gateway-ca.cert"
11+
ca_certs = "/var/lib/gateway/certs/gateway-ca.cert"
1212
mandatory = false
1313

1414
[core]
1515
kms_url = ""
16-
rpc_domain = "gateway.test.local"
16+
rpc_domain = "gateway-2"
1717

1818
[core.admin]
1919
enabled = true
@@ -22,7 +22,7 @@ address = "0.0.0.0"
2222

2323
[core.debug]
2424
insecure_enable_debug_rpc = true
25-
insecure_skip_attestation = true
25+
insecure_skip_attestation = false
2626
port = 9015
2727
address = "0.0.0.0"
2828

0 commit comments

Comments
 (0)