@@ -29,7 +29,7 @@ use std::{
29
29
env, fs,
30
30
future:: Future ,
31
31
io:: ErrorKind ,
32
- net:: SocketAddr ,
32
+ net:: { IpAddr , SocketAddr } ,
33
33
pin:: Pin ,
34
34
str:: FromStr ,
35
35
sync:: Arc ,
@@ -132,8 +132,26 @@ pub(super) async fn main() -> anyhow::Result<()> {
132
132
. short ( 'g' )
133
133
. long ( "grpc-endpoint" )
134
134
. value_name ( "ENDPOINT" )
135
+ . conflicts_with_all ( [ "grpc-ip" , "grpc-port" ] )
135
136
. help ( "ip address where this instance runs, and optionally the gRPC port" )
136
- . default_value ( "[::]" )
137
+ . required ( false )
138
+ )
139
+ . arg (
140
+ Arg :: new ( "grpc-ip" )
141
+ . long ( "grpc-ip" )
142
+ . value_parser ( clap:: value_parser!( IpAddr ) )
143
+ . value_name ( "GRPC_IP" )
144
+ . help ( "ip address this instance listens on" )
145
+ . default_value ( "::" )
146
+ . required ( false )
147
+ )
148
+ . arg (
149
+ Arg :: new ( "grpc-port" )
150
+ . long ( "grpc-port" )
151
+ . value_parser ( clap:: value_parser!( u16 ) )
152
+ . value_name ( "GRPC_PORT" )
153
+ . help ( "port this instance listens on" )
154
+ . default_value ( GRPC_PORT . to_string ( ) )
137
155
. required ( false )
138
156
)
139
157
. arg (
@@ -330,10 +348,7 @@ pub(super) async fn main() -> anyhow::Result<()> {
330
348
let registration_enabled = matches. get_flag ( "enable-registration" ) ;
331
349
332
350
// Parse instance and grpc endpoints from the command line arguments and validate.
333
- let grpc_sock_addr = validate_endpoints (
334
- matches. get_one :: < String > ( "grpc-endpoint" ) . unwrap ( ) ,
335
- registration_enabled,
336
- ) ?;
351
+ let grpc_sock_addr = validate_endpoints ( & matches, registration_enabled) ?;
337
352
338
353
// Start the CSI server, node plugin grpc server and registration loop if registration is
339
354
// enabled.
@@ -427,21 +442,32 @@ async fn check_ana_and_label_node(
427
442
428
443
/// Validate that the grpc endpoint is valid.
429
444
fn validate_endpoints (
430
- grpc_endpoint : & str ,
445
+ matches : & clap :: ArgMatches ,
431
446
registration_enabled : bool ,
432
447
) -> anyhow:: Result < SocketAddr > {
433
- let grpc_endpoint = if grpc_endpoint. contains ( ':' ) {
434
- grpc_endpoint. to_string ( )
435
- } else {
436
- format ! ( "{grpc_endpoint}:{GRPC_PORT}" )
448
+ let grpc_endpoint = matches. get_one :: < String > ( "grpc-endpoint" ) ;
449
+ let grpc_ip = matches. get_one :: < IpAddr > ( "grpc-ip" ) ;
450
+ let grpc_port = matches. get_one :: < u16 > ( "grpc-port" ) ;
451
+ let grpc_endpoint_socket_addr = match grpc_endpoint {
452
+ None => SocketAddr :: new (
453
+ * grpc_ip. expect ( "grpc-ip must be provided if grpc-endpoint is missing" ) ,
454
+ * grpc_port. expect ( "grpc-port must be provided if grpc-endpoint is missing" ) ,
455
+ ) ,
456
+ Some ( grpc_endpoint) => {
457
+ let grpc_endpoint = if grpc_endpoint. contains ( ':' ) {
458
+ grpc_endpoint. to_string ( )
459
+ } else {
460
+ format ! ( "{grpc_endpoint}:{GRPC_PORT}" )
461
+ } ;
462
+ SocketAddr :: from_str ( & grpc_endpoint) ?
463
+ }
437
464
} ;
438
- let grpc_endpoint_url = SocketAddr :: from_str ( & grpc_endpoint) ?;
439
465
// Should not allow using an unspecified ip if registration is enabled as grpc endpoint gets
440
466
// sent in registration request.
441
- if registration_enabled && grpc_endpoint_url . ip ( ) . is_unspecified ( ) {
467
+ if registration_enabled && grpc_endpoint_socket_addr . ip ( ) . is_unspecified ( ) {
442
468
return Err ( anyhow:: format_err!(
443
469
"gRPC endpoint: `[::]`/`0.0.0.0` is not allowed if registration is enabled"
444
470
) ) ;
445
471
}
446
- Ok ( grpc_endpoint_url )
472
+ Ok ( grpc_endpoint_socket_addr )
447
473
}
0 commit comments