Skip to content

Commit

Permalink
feat: add url to actor ports (#1811)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->
Fixes RVT-4398
## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Jan 11, 2025
1 parent 8df19e8 commit 8a80712
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ fn add_http_port(
let (hostname, path) = ds::util::build_ds_hostname_and_path(
proxied_port.server_id,
&proxied_port.port_name,
proxied_port.datacenter_id,
if is_https {
GameGuardProtocol::Https
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/common/convert/src/impls/provision.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rivet_api::models;
use rivet_operation::prelude::*;

use crate::{ApiFrom, ApiInto};

Expand Down
18 changes: 17 additions & 1 deletion packages/common/util/core/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::iter::Iterator;
use std::{
fmt::{self, Display, Formatter},
iter::Iterator,
};

use lazy_static::lazy_static;
use rand::seq::IteratorRandom;
Expand All @@ -10,6 +13,19 @@ lazy_static! {
static ref SPACE_REPLACE: Regex = Regex::new(r#" +"#).unwrap();
}

/// Renders `Some<T>` as `T` and does not render `None`.
pub struct OptDisplay<T: Display>(pub Option<T>);

impl<T: Display> Display for OptDisplay<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(value) = &self.0 {
write!(f, "{}", value)
} else {
Ok(())
}
}
}

/// Formats a user's biography properly. Assumes util::check::biography succeeded before this function
pub fn biography<T: AsRef<str>>(s: T) -> String {
let s = s.as_ref();
Expand Down
5 changes: 0 additions & 5 deletions packages/services/ds/src/ops/server/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,8 @@ pub async fn ds_server_get(ctx: &OperationCtx, input: &Input) -> GlobalResult<Ou
Ok((
gg_port.port_name.clone(),
create_port_gg(
ctx.config(),
is_connectable,
gg_port,
server.datacenter_id,
unwrap!(GameGuardProtocol::from_repr(gg_port.protocol.try_into()?)),
endpoint_type,
&dc.guard_public_hostname,
Expand Down Expand Up @@ -336,10 +334,8 @@ pub async fn ds_server_get(ctx: &OperationCtx, input: &Input) -> GlobalResult<Ou
}

fn create_port_gg(
config: &rivet_config::Config,
is_connectable: bool,
gg_port: &ServerPortGg,
datacenter_id: Uuid,
protocol: GameGuardProtocol,
endpoint_type: EndpointType,
guard_public_hostname: &cluster::types::GuardPublicHostname,
Expand All @@ -348,7 +344,6 @@ fn create_port_gg(
let (hostname, path) = crate::util::build_ds_hostname_and_path(
gg_port.server_id,
&gg_port.port_name,
datacenter_id,
protocol,
endpoint_type,
guard_public_hostname,
Expand Down
102 changes: 73 additions & 29 deletions packages/services/ds/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ pub enum GameGuardProtocol {
Udp = 4,
}

impl fmt::Display for GameGuardProtocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GameGuardProtocol::Http => write!(f, "http"),
GameGuardProtocol::Https => write!(f, "https"),
GameGuardProtocol::Tcp => write!(f, "tcp"),
GameGuardProtocol::TcpTls => write!(f, "tcps"),
GameGuardProtocol::Udp => write!(f, "udp"),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
pub enum PortAuthorization {
None,
Expand Down Expand Up @@ -254,44 +266,75 @@ impl ApiFrom<NetworkMode> for models::ActorNetworkMode {

impl ApiFrom<Port> for models::ActorPort {
fn api_from(value: Port) -> models::ActorPort {
let (protocol, routing) = match &value.routing {
let (protocol, routing, url) = match &value.routing {
Routing::GameGuard {
protocol,
authorization,
} => (
(*protocol).api_into(),
models::ActorPortRouting {
guard: Some(json!({})),
// Temporarily disabled
// guard: Some(Box::new(models::ActorGuardRouting {
// authorization: match authorization {
// PortAuthorization::None => None,
// PortAuthorization::Bearer(token) => {
// Some(Box::new(models::ActorPortAuthorization {
// bearer: Some(token.clone()),
// ..Default::default()
// }))
// }
// PortAuthorization::Query(key, value) => {
// Some(Box::new(models::ActorPortAuthorization {
// query: Some(Box::new(models::ActorPortQueryAuthorization {
// key: key.clone(),
// value: value.clone(),
// })),
// ..Default::default()
// }))
// }
// },
// })),
..Default::default()
},
),
} => {
let url = match (
protocol,
value.public_hostname.as_ref(),
value.public_port,
value.public_path.as_ref(),
) {
(
GameGuardProtocol::Http | GameGuardProtocol::Https,
Some(hostname),
Some(port),
path,
) => Some(format!(
"{protocol}://{hostname}:{port}{}",
util::format::OptDisplay(path)
)),
(
GameGuardProtocol::Http | GameGuardProtocol::Https,
Some(hostname),
None,
path,
) => Some(format!(
"{protocol}://{hostname}{}",
util::format::OptDisplay(path)
)),
_ => None,
};

(
(*protocol).api_into(),
models::ActorPortRouting {
guard: Some(json!({})),
// Temporarily disabled
// guard: Some(Box::new(models::ActorGuardRouting {
// authorization: match authorization {
// PortAuthorization::None => None,
// PortAuthorization::Bearer(token) => {
// Some(Box::new(models::ActorPortAuthorization {
// bearer: Some(token.clone()),
// ..Default::default()
// }))
// }
// PortAuthorization::Query(key, value) => {
// Some(Box::new(models::ActorPortAuthorization {
// query: Some(Box::new(models::ActorPortQueryAuthorization {
// key: key.clone(),
// value: value.clone(),
// })),
// ..Default::default()
// }))
// }
// },
// })),
..Default::default()
},
url,
)
}
Routing::Host { protocol } => (
(*protocol).api_into(),
models::ActorPortRouting {
host: Some(json!({})),
..Default::default()
},
None,
),
};

Expand All @@ -301,6 +344,7 @@ impl ApiFrom<Port> for models::ActorPort {
hostname: value.public_hostname,
port: value.public_port,
path: value.public_path,
url,
routing: Box::new(routing),
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/services/ds/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub fn is_nomad_ds(job_id: &str) -> bool {
pub fn build_ds_hostname_and_path(
server_id: Uuid,
port_name: &str,
datacenter_id: Uuid,
protocol: GameGuardProtocol,
endpoint_type: EndpointType,
guard_public_hostname: &GuardPublicHostname,
Expand Down
2 changes: 0 additions & 2 deletions packages/services/ds/src/workers/undrain_all.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use chirp_worker::prelude::*;
use proto::backend::pkg::*;

Expand Down
14 changes: 11 additions & 3 deletions packages/services/ds/src/workflows/server/pegboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,17 +925,25 @@ async fn wait_for_traefik_poll(
servers_res.servers.iter().map(|s| s.server_id).collect()
};

tracing::debug!(servers = ?remaining_servers, after_create_ts = ?input.create_ts, "waiting for traefik servers");
tracing::debug!(
servers=?remaining_servers,
after_create_ts=?input.create_ts,
"waiting for traefik servers",
);
let res = tokio::time::timeout(TRAEFIK_POLL_TIMEOUT, async {
// Wait for servers to fetch their configs
loop {
let msg = sub.next().await?;

if let Some(server_id) = msg.server_id {
if msg.latest_ds_create_ts >= input.create_ts {
let did_remove = remaining_servers.remove(&server_id);
let _did_remove = remaining_servers.remove(&server_id);

tracing::debug!(server_id = ?msg.server_id, latest_ds_create_ts = ?msg.latest_ds_create_ts, servers = ?remaining_servers, "received poll from traefik server");
tracing::debug!(
server_id=?msg.server_id,
latest_ds_create_ts=?msg.latest_ds_create_ts,
servers=?remaining_servers, "received poll from traefik server",
);

// Break loop once all servers have polled
if remaining_servers.is_empty() {
Expand Down
4 changes: 4 additions & 0 deletions sdks/api/fern/definition/actor/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ types:
hostname: optional<string>
port: optional<integer>
path: optional<string>
url:
docs: |
Fully formed connection URL including protocol, hostname, port, and path, if applicable.
type: optional<string>
routing: PortRouting

PortProtocol:
Expand Down
4 changes: 3 additions & 1 deletion sdks/api/full/go/actor/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions sdks/api/full/openapi/openapi.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions sdks/api/full/openapi_compat/openapi.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdks/api/full/rust/docs/ActorPort.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions sdks/api/full/rust/src/models/actor_port.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion sdks/api/runtime/go/actor/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions sdks/api/runtime/openapi/openapi.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions sdks/api/runtime/openapi_compat/openapi.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdks/api/runtime/rust/docs/ActorPort.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions sdks/api/runtime/rust/src/models/actor_port.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/src/content/docs/api/spec.json

Large diffs are not rendered by default.

Loading

0 comments on commit 8a80712

Please sign in to comment.