From 27109e4c49d40bae3f62cd83689394da0499c036 Mon Sep 17 00:00:00 2001 From: Mike Beaumont Date: Wed, 9 Oct 2024 20:35:10 +0200 Subject: [PATCH] refactor: improve grpc endpoint validation Signed-off-by: Mike Beaumont --- .../csi-driver/src/bin/node/main_.rs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/control-plane/csi-driver/src/bin/node/main_.rs b/control-plane/csi-driver/src/bin/node/main_.rs index 34b1ea417..7898c8d7f 100644 --- a/control-plane/csi-driver/src/bin/node/main_.rs +++ b/control-plane/csi-driver/src/bin/node/main_.rs @@ -134,7 +134,6 @@ pub(super) async fn main() -> anyhow::Result<()> { .value_name("ENDPOINT") .conflicts_with_all(["grpc-ip", "grpc-port"]) .help("ip address where this instance runs, and optionally the gRPC port") - .default_value("[::]") .required(false) ) .arg( @@ -446,25 +445,22 @@ fn validate_endpoints( matches: &clap::ArgMatches, registration_enabled: bool, ) -> anyhow::Result { - let grpc_endpoint_src = matches.value_source("grpc-endpoint"); + let grpc_endpoint = matches.get_one::("grpc-endpoint"); let grpc_ip = matches.get_one::("grpc-ip"); let grpc_port = matches.get_one::("grpc-port"); - let grpc_endpoint_socket_addr = if grpc_endpoint_src - .unwrap_or(clap::parser::ValueSource::DefaultValue) - == clap::parser::ValueSource::DefaultValue - { - SocketAddr::new( - *grpc_ip.expect("must be provided if grpc-endpoint is missing"), - *grpc_port.expect("must be provided if grpc-port is missing"), - ) - } else { - let grpc_endpoint_arg = matches.get_one::("grpc-endpoint").unwrap(); - let grpc_endpoint = if grpc_endpoint_arg.contains(':') { - grpc_endpoint_arg.to_string() - } else { - format!("{grpc_endpoint_arg}:{GRPC_PORT}") - }; - SocketAddr::from_str(&grpc_endpoint)? + let grpc_endpoint_socket_addr = match grpc_endpoint { + None => SocketAddr::new( + *grpc_ip.expect("grpc-ip must be provided if grpc-endpoint is missing"), + *grpc_port.expect("grpc-port must be provided if grpc-endpoint is missing"), + ), + Some(grpc_endpoint) => { + let grpc_endpoint = if grpc_endpoint.contains(':') { + grpc_endpoint.to_string() + } else { + format!("{grpc_endpoint}:{GRPC_PORT}") + }; + SocketAddr::from_str(&grpc_endpoint)? + } }; // Should not allow using an unspecified ip if registration is enabled as grpc endpoint gets // sent in registration request.