Skip to content

Commit

Permalink
Parametrize bind address of local Golem server (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieletorelli authored Dec 27, 2024
1 parent aae56dc commit 457a64a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
6 changes: 6 additions & 0 deletions golem/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use std::path::PathBuf;
pub enum SingleExecutableCommand {
#[clap(name = "start", about = "Start a golem server for local development")]
Start {
/// Address to serve the main API on
#[clap(long, default_value = "0.0.0.0")]
router_addr: String,

/// Port to serve the main API on
#[clap(long, default_value_t = 9881)]
router_port: u16,
Expand All @@ -46,6 +50,7 @@ impl<Ctx> CliCommand<Ctx> for SingleExecutableCommand {
async fn run(self, _ctx: Ctx) -> Result<GolemResult, GolemError> {
match self {
SingleExecutableCommand::Start {
router_addr: router_host,
router_port,
custom_request_port,
data_dir,
Expand All @@ -63,6 +68,7 @@ impl<Ctx> CliCommand<Ctx> for SingleExecutableCommand {
};

match launch_golem_services(&LaunchArgs {
router_host,
router_port,
custom_request_port,
data_dir,
Expand Down
10 changes: 6 additions & 4 deletions golem/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use tracing::Instrument;
use crate::proxy;

pub struct LaunchArgs {
pub router_host: String,
pub router_port: u16,
pub custom_request_port: u16,
pub data_dir: PathBuf,
Expand Down Expand Up @@ -110,6 +111,7 @@ pub async fn launch_golem_services(args: &LaunchArgs) -> Result<(), anyhow::Erro

// Don't drop the channel, it will cause the proxy to fail
let _proxy_command_channel = proxy::start_proxy(
&args.router_host,
args.router_port,
healthcheck_port,
&all_run_details,
Expand Down Expand Up @@ -188,14 +190,14 @@ fn worker_executor_config(
blob_storage: blob_storage_config(args),
component_service: golem_worker_executor_base::services::golem_config::ComponentServiceConfig::Grpc(
ComponentServiceGrpcConfig {
host: "127.0.0.1".to_string(),
host: args.router_host.clone(),
port: component_service_run_details.grpc_port,
..ComponentServiceGrpcConfig::default()
}
),
compiled_component_service: CompiledComponentServiceConfig::Disabled(golem_worker_executor_base::services::golem_config::CompiledComponentServiceDisabledConfig { }),
shard_manager_service: ShardManagerServiceConfig::Grpc(ShardManagerServiceGrpcConfig {
host: "127.0.0.1".to_string(),
host: args.router_host.clone(),
port: shard_manager_run_details.grpc_port,
..ShardManagerServiceGrpcConfig::default()
}),
Expand Down Expand Up @@ -236,12 +238,12 @@ fn worker_service_config(
),
blob_storage: blob_storage_config(args),
component_service: golem_worker_service_base::app_config::ComponentServiceConfig {
host: "127.0.0.1".to_string(),
host: args.router_host.clone(),
port: component_service_run_details.grpc_port,
..golem_worker_service_base::app_config::ComponentServiceConfig::default()
},
routing_table: RoutingTableConfig {
host: "127.0.0.1".to_string(),
host: args.router_host.clone(),
port: shard_manager_run_details.grpc_port,
..RoutingTableConfig::default()
},
Expand Down
27 changes: 22 additions & 5 deletions golem/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,33 @@ use sozu_command_lib::{
RequestHttpFrontend, RulePosition, SocketAddress, WorkerRequest,
},
};
use std::net::Ipv4Addr;
use tokio::task::JoinSet;
use tracing::info;

use crate::AllRunDetails;

pub fn start_proxy(
listener_addr: &str,
listener_port: u16,
healthcheck_port: u16,
all_run_details: &AllRunDetails,
join_set: &mut JoinSet<Result<(), anyhow::Error>>,
) -> Result<Channel<WorkerRequest, WorkerResponse>, anyhow::Error> {
info!("Starting proxy");

let listener_address = SocketAddress::new_v4(127, 0, 0, 1, listener_port);

let http_listener = ListenerBuilder::new_http(listener_address).to_http(None)?;
let ipv4_addr: Ipv4Addr = listener_addr.parse().context(format!(
"Failed at parsing the listener host address {}",
listener_addr
))?;
let listener_socket_addr = SocketAddress::new_v4(
ipv4_addr.octets()[0],
ipv4_addr.octets()[1],
ipv4_addr.octets()[2],
ipv4_addr.octets()[3],
listener_port,
);
let http_listener = ListenerBuilder::new_http(listener_socket_addr).to_http(None)?;

let (mut command_channel, proxy_channel) =
Channel::generate(1000, 10000).with_context(|| "should create a channel")?;
Expand Down Expand Up @@ -86,7 +97,13 @@ pub fn start_proxy(
content: RequestType::AddBackend(AddBackend {
cluster_id: name.to_string(),
backend_id: name.to_string(),
address: SocketAddress::new_v4(127, 0, 0, 1, port),
address: SocketAddress::new_v4(
ipv4_addr.octets()[0],
ipv4_addr.octets()[1],
ipv4_addr.octets()[2],
ipv4_addr.octets()[3],
port,
),
..Default::default()
})
.into(),
Expand All @@ -110,7 +127,7 @@ pub fn start_proxy(
id: format!("add-golem-frontend-${route_counter}"),
content: RequestType::AddHttpFrontend(RequestHttpFrontend {
cluster_id: Some(cluster_id.to_string()),
address: SocketAddress::new_v4(127, 0, 0, 1, listener_port),
address: listener_socket_addr,
hostname: "*".to_string(),
path,
position: RulePosition::Post.into(),
Expand Down

0 comments on commit 457a64a

Please sign in to comment.