Skip to content

Commit ae20118

Browse files
committed
feat(dataplane): set gateway name
Set the gateway name from the cmd line --name argument. If no --name is provided, set the gateway name from the hostname provided by the OS. Fail to start if the gatway name cannot be set. With this, the gateway name defaults to the hostname but can always be overriden with --name. Signed-off-by: Fredi Raspall <[email protected]>
1 parent 4c031fa commit ae20118

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dataplane/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ctrlc = { workspace = true, features = ["termination"] }
1616
dpdk = { workspace = true }
1717
dyn-iter = { workspace = true }
1818
futures = { workspace = true }
19+
gwname = { workspace = true }
1920
hyper = { workspace = true }
2021
hyper-util = { workspace = true }
2122
id = { workspace = true }
@@ -26,7 +27,7 @@ mgmt = { workspace = true }
2627
mio = { workspace = true, features = ["os-ext", "net"] }
2728
nat = { workspace = true }
2829
net = { workspace = true, features = ["test_buffer"] }
29-
nix = { workspace = true }
30+
nix = { workspace = true, features = ["hostname"] }
3031
netdev = { workspace = true }
3132
once_cell = { workspace = true }
3233
ordermap = { workspace = true, features = ["std"] }

dataplane/src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use args::{CmdArgs, Parser};
1616
use drivers::kernel::DriverKernel;
1717
use mgmt::{ConfigProcessorParams, MgmtParams, start_mgmt};
1818

19+
use nix::unistd::gethostname;
1920
use pyroscope::PyroscopeAgent;
2021
use pyroscope_pprofrs::{PprofConfig, pprof_backend};
2122

23+
use gwname::{get_gw_name, set_gw_name};
2224
use routing::RouterParamsBuilder;
2325
use tracectl::{custom_target, get_trace_ctl, trace_target};
2426

@@ -29,8 +31,26 @@ custom_target!("tonic", LevelFilter::ERROR, &[]);
2931
custom_target!("h2", LevelFilter::ERROR, &[]);
3032
custom_target!("Pyroscope", LevelFilter::INFO, &[]);
3133

34+
fn init_name(args: &CmdArgs) -> Result<(), String> {
35+
if let Some(name) = args.get_name() {
36+
set_gw_name(name)?;
37+
} else {
38+
let hostname =
39+
gethostname().map_err(|errno| format!("Failed to get hostname: {}", errno.desc()))?;
40+
let name = hostname
41+
.to_str()
42+
.ok_or_else(|| format!("Failed to convert hostname {}", hostname.display()))?;
43+
set_gw_name(name)?;
44+
}
45+
Ok(())
46+
}
3247
fn init_logging() {
3348
let tctl = get_trace_ctl();
49+
info!(
50+
" ━━━━━━ Dataplane for '{}' started ━━━━━━",
51+
get_gw_name().unwrap_or_else(|| unreachable!())
52+
);
53+
3454
tctl.set_default_level(LevelFilter::DEBUG)
3555
.expect("Setting default loglevel failed");
3656
}
@@ -65,9 +85,15 @@ fn process_tracing_cmds(args: &CmdArgs) {
6585
}
6686
}
6787

88+
#[allow(clippy::too_many_lines)]
6889
fn main() {
69-
init_logging();
7090
let args = CmdArgs::parse();
91+
if let Err(e) = init_name(&args) {
92+
eprintln!("Failed to set gateway name: {e}");
93+
std::process::exit(1);
94+
}
95+
init_logging();
96+
7197
let agent_running = args.pyroscope_url().and_then(|url| {
7298
match PyroscopeAgent::builder(url.as_str(), "hedgehog-dataplane")
7399
.backend(pprof_backend(

0 commit comments

Comments
 (0)