@@ -199,7 +199,12 @@ func (d *logDumper) dumpRegistered(ctx context.Context, node *corev1.Node) error
199
199
if publicIP != "" {
200
200
return d .dumpNode (ctx , node .Name , publicIP , false )
201
201
} else {
202
- return d .dumpNode (ctx , node .Name , privateIP , true )
202
+ useBastion := true
203
+ if ! d .sshClientFactory .HasBastion () {
204
+ klog .Warningf ("no bastion address set, will attempt to connect to node %s directly via private IP %v" , node .Name , privateIP )
205
+ useBastion = false
206
+ }
207
+ return d .dumpNode (ctx , node .Name , privateIP , useBastion )
203
208
}
204
209
}
205
210
@@ -274,7 +279,12 @@ type sshClient interface {
274
279
275
280
// sshClientFactory is an interface abstracting to a node over SSH
276
281
type sshClientFactory interface {
282
+ // Dial creates a new sshClient
277
283
Dial (ctx context.Context , host string , useBastion bool ) (sshClient , error )
284
+
285
+ // HasBastion returns true if the sshClientFactory has a bastion configured.
286
+ // Calling Dial with useBastion=true will return an error if there is no bastion.
287
+ HasBastion () bool
278
288
}
279
289
280
290
// logDumperNode holds state for a particular node we are dumping
@@ -540,21 +550,31 @@ type sshClientFactoryImplementation struct {
540
550
541
551
var _ sshClientFactory = & sshClientFactoryImplementation {}
542
552
553
+ // HasBastion implements sshClientFactory::HasBastion
554
+ func (f * sshClientFactoryImplementation ) HasBastion () bool {
555
+ return f .bastion != ""
556
+ }
557
+
543
558
// Dial implements sshClientFactory::Dial
544
559
func (f * sshClientFactoryImplementation ) Dial (ctx context.Context , host string , useBastion bool ) (sshClient , error ) {
545
- var addr string
560
+ addr := host
546
561
if useBastion {
562
+ if f .bastion == "" {
563
+ return nil , fmt .Errorf ("bastion is not set, but useBastion is true" )
564
+ }
547
565
addr = f .bastion
548
- } else {
549
- addr = host
566
+ }
567
+
568
+ if addr == "" {
569
+ return nil , fmt .Errorf ("host is empty" )
550
570
}
551
571
addr = net .JoinHostPort (addr , "22" )
552
572
d := net.Dialer {
553
573
Timeout : 5 * time .Second ,
554
574
}
555
575
conn , err := d .DialContext (ctx , "tcp" , addr )
556
576
if err != nil {
557
- return nil , err
577
+ return nil , fmt . Errorf ( "error dialing tcp %s: %w" , addr , err )
558
578
}
559
579
560
580
// We have a TCP connection; we will force-close it to support context cancellation
0 commit comments