@@ -40,6 +40,8 @@ import (
40
40
"sigs.k8s.io/apiserver-network-proxy/proto/header"
41
41
)
42
42
43
+ const xfrChannelSize = 10
44
+
43
45
type key int
44
46
45
47
type ProxyClientConnection struct {
@@ -136,6 +138,9 @@ type ProxyServer struct {
136
138
serverID string // unique ID of this server
137
139
serverCount int // Number of proxy server instances, should be 1 unless it is a HA server.
138
140
141
+ // Allows a special debug flag which warns if we write to a full transfer channel
142
+ warnOnChannelLimit bool
143
+
139
144
// agent authentication
140
145
AgentAuthenticationOptions * AgentTokenAuthenticationOptions
141
146
@@ -321,7 +326,7 @@ func (s *ProxyServer) getFrontendsForBackendConn(agentID string, backend Backend
321
326
}
322
327
323
328
// NewProxyServer creates a new ProxyServer instance
324
- func NewProxyServer (serverID string , proxyStrategies []ProxyStrategy , serverCount int , agentAuthenticationOptions * AgentTokenAuthenticationOptions ) * ProxyServer {
329
+ func NewProxyServer (serverID string , proxyStrategies []ProxyStrategy , serverCount int , agentAuthenticationOptions * AgentTokenAuthenticationOptions , warnOnChannelLimit bool ) * ProxyServer {
325
330
var bms []BackendManager
326
331
for _ , ps := range proxyStrategies {
327
332
switch ps {
@@ -343,9 +348,10 @@ func NewProxyServer(serverID string, proxyStrategies []ProxyStrategy, serverCoun
343
348
serverCount : serverCount ,
344
349
BackendManagers : bms ,
345
350
AgentAuthenticationOptions : agentAuthenticationOptions ,
346
- // use the first backendmanager as the Readiness Manager
347
- Readiness : bms [0 ],
348
- proxyStrategies : proxyStrategies ,
351
+ // use the first backend-manager as the Readiness Manager
352
+ Readiness : bms [0 ],
353
+ proxyStrategies : proxyStrategies ,
354
+ warnOnChannelLimit : warnOnChannelLimit ,
349
355
}
350
356
}
351
357
@@ -361,12 +367,13 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
361
367
userAgent := md .Get (header .UserAgent )
362
368
klog .V (2 ).InfoS ("proxy request from client" , "userAgent" , userAgent )
363
369
364
- recvCh := make (chan * client.Packet , 10 )
370
+ recvCh := make (chan * client.Packet , xfrChannelSize )
365
371
stopCh := make (chan error )
366
372
367
373
go s .serveRecvFrontend (stream , recvCh )
368
374
369
375
defer func () {
376
+ klog .V (2 ).InfoS ("Receive channel on Proxy is stopping" , "userAgent" , userAgent , "serverID" , s .serverID )
370
377
close (recvCh )
371
378
}()
372
379
@@ -375,6 +382,7 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
375
382
for {
376
383
in , err := stream .Recv ()
377
384
if err == io .EOF {
385
+ klog .V (2 ).InfoS ("Stream closed on Proxy" , "userAgent" , userAgent , "serverID" , s .serverID )
378
386
close (stopCh )
379
387
return
380
388
}
@@ -384,6 +392,9 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
384
392
return
385
393
}
386
394
395
+ if s .warnOnChannelLimit && len (recvCh ) >= xfrChannelSize {
396
+ klog .V (2 ).InfoS ("Receive channel on Proxy is full" , "userAgent" , userAgent , "serverID" , s .serverID )
397
+ }
387
398
recvCh <- in
388
399
}
389
400
}()
@@ -410,13 +421,15 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
410
421
// a new connection to the address.
411
422
backend , err = s .getBackend (pkt .GetDialRequest ().Address )
412
423
if err != nil {
413
- klog .ErrorS (err , "Failed to get a backend" )
424
+ klog .ErrorS (err , "Failed to get a backend" , "serverID" , s . serverID )
414
425
415
426
resp := & client.Packet {
416
427
Type : client .PacketType_DIAL_RSP ,
417
428
Payload : & client.Packet_DialResponse {DialResponse : & client.DialResponse {Error : err .Error ()}},
418
429
}
419
- stream .Send (resp )
430
+ if err := stream .Send (resp ); err != nil {
431
+ klog .V (5 ).Infoln ("Failed to send DIAL_RSP for no backend" , "error" , err , "serverID" , s .serverID )
432
+ }
420
433
// The Dial is failing; no reason to keep this goroutine.
421
434
return
422
435
}
@@ -430,29 +443,30 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
430
443
backend : backend ,
431
444
})
432
445
if err := backend .Send (pkt ); err != nil {
433
- klog .ErrorS (err , "DIAL_REQ to Backend failed" )
446
+ klog .ErrorS (err , "DIAL_REQ to Backend failed" , "serverID" , s . serverID )
434
447
}
435
448
klog .V (5 ).Infoln ("DIAL_REQ sent to backend" ) // got this. but backend didn't receive anything.
436
449
437
450
case client .PacketType_CLOSE_REQ :
438
451
connID := pkt .GetCloseRequest ().ConnectID
439
452
klog .V (5 ).InfoS ("Received CLOSE_REQ" , "connectionID" , connID )
440
453
if backend == nil {
441
- klog .V (2 ).InfoS ("Backend has not been initialized for requested connection. Client should send a Dial Request first" , "connectionID" , connID )
454
+ klog .V (2 ).InfoS ("Backend has not been initialized for requested connection. Client should send a Dial Request first" ,
455
+ "serverID" , s .serverID , "connectionID" , connID )
442
456
continue
443
457
}
444
458
if err := backend .Send (pkt ); err != nil {
445
459
// TODO: retry with other backends connecting to this agent.
446
- klog .ErrorS (err , "CLOSE_REQ to Backend failed" )
460
+ klog .ErrorS (err , "CLOSE_REQ to Backend failed" , "serverID" , s . serverID , "connectionID" , connID )
447
461
}
448
- klog .V (5 ).Infoln ("CLOSE_REQ sent to backend" )
462
+ klog .V (5 ).Infoln ("CLOSE_REQ sent to backend" , "serverID" , s . serverID , "connectionID" , connID )
449
463
450
464
case client .PacketType_DIAL_CLS :
451
465
random := pkt .GetCloseDial ().Random
452
- klog .V (5 ).InfoS ("Received DIAL_CLOSE" , "random" , random )
466
+ klog .V (5 ).InfoS ("Received DIAL_CLOSE" , "serverID" , s . serverID , " random" , random )
453
467
// Currently not worrying about backend as we do not have an established connection,
454
468
s .PendingDial .Remove (random )
455
- klog .V (5 ).Infoln ("Removing pending dial request" , "random" , random )
469
+ klog .V (5 ).Infoln ("Removing pending dial request" , "serverID" , s . serverID , " random" , random )
456
470
457
471
case client .PacketType_DATA :
458
472
connID := pkt .GetData ().ConnectID
@@ -470,17 +484,18 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
470
484
}
471
485
if err := backend .Send (pkt ); err != nil {
472
486
// TODO: retry with other backends connecting to this agent.
473
- klog .ErrorS (err , "DATA to Backend failed" )
487
+ klog .ErrorS (err , "DATA to Backend failed" , "serverID" , s . serverID , "connectionID" , connID )
474
488
continue
475
489
}
476
490
klog .V (5 ).Infoln ("DATA sent to Backend" )
477
491
478
492
default :
479
- klog .V (5 ).InfoS ("Ignore packet coming from frontend" , "type" , pkt .Type )
493
+ klog .V (5 ).InfoS ("Ignore packet coming from frontend" ,
494
+ "type" , pkt .Type , "serverID" , s .serverID , "connectionID" , firstConnID )
480
495
}
481
496
}
482
497
483
- klog .V (5 ).InfoS ("Close streaming" , "connectionID" , firstConnID )
498
+ klog .V (5 ).InfoS ("Close streaming" , "serverID" , s . serverID , " connectionID" , firstConnID )
484
499
485
500
pkt := & client.Packet {
486
501
Type : client .PacketType_CLOSE_REQ ,
@@ -496,7 +511,7 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
496
511
return
497
512
}
498
513
if err := backend .Send (pkt ); err != nil {
499
- klog .ErrorS (err , "CLOSE_REQ to Backend failed" )
514
+ klog .ErrorS (err , "CLOSE_REQ to Backend failed" , "serverID" , s . serverID )
500
515
}
501
516
}
502
517
@@ -617,24 +632,26 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
617
632
618
633
if s .AgentAuthenticationOptions .Enabled {
619
634
if err := s .authenticateAgentViaToken (stream .Context ()); err != nil {
620
- klog .ErrorS (err , "Client authentication failed" )
635
+ klog .ErrorS (err , "Client authentication failed" , "agentID" , agentID )
621
636
return err
622
637
}
623
638
}
624
639
625
640
h := metadata .Pairs (header .ServerID , s .serverID , header .ServerCount , strconv .Itoa (s .serverCount ))
626
641
if err := stream .SendHeader (h ); err != nil {
642
+ klog .ErrorS (err , "Failed to send server count back to agent" , "agentID" , agentID )
627
643
return err
628
644
}
629
645
630
646
backend := s .addBackend (agentID , stream )
631
647
defer s .removeBackend (agentID , stream )
632
648
633
- recvCh := make (chan * client.Packet , 10 )
649
+ recvCh := make (chan * client.Packet , xfrChannelSize )
634
650
635
651
go s .serveRecvBackend (backend , stream , agentID , recvCh )
636
652
637
653
defer func () {
654
+ klog .V (2 ).InfoS ("Receive channel on Connect is stopping" , "agentID" , agentID , "serverID" , s .serverID )
638
655
close (recvCh )
639
656
}()
640
657
@@ -643,6 +660,7 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
643
660
for {
644
661
in , err := stream .Recv ()
645
662
if err == io .EOF {
663
+ klog .V (2 ).InfoS ("Stream closed on Connect" , "agentID" , agentID , "serverID" , s .serverID )
646
664
close (stopCh )
647
665
return
648
666
}
@@ -652,6 +670,9 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
652
670
return
653
671
}
654
672
673
+ if s .warnOnChannelLimit && len (recvCh ) >= xfrChannelSize {
674
+ klog .V (2 ).InfoS ("Receive channel on Connect is full" , "agentID" , agentID , "serverID" , s .serverID )
675
+ }
655
676
recvCh <- in
656
677
}
657
678
}()
@@ -666,7 +687,8 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
666
687
// TODO(#126): Frontends in PendingDial state that have not been added to the
667
688
// list of frontends should also be closed.
668
689
frontends , _ := s .getFrontendsForBackendConn (agentID , backend )
669
- klog .V (3 ).InfoS ("Close frontends connected to agent" , "count" , len (frontends ), "agentID" , agentID )
690
+ klog .V (3 ).InfoS ("Close frontends connected to agent" ,
691
+ "serverID" , s .serverID , "count" , len (frontends ), "agentID" , agentID )
670
692
671
693
for _ , frontend := range frontends {
672
694
s .removeFrontend (agentID , frontend .connectID )
@@ -678,7 +700,7 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
678
700
}
679
701
pkt .GetCloseResponse ().ConnectID = frontend .connectID
680
702
if err := frontend .send (pkt ); err != nil {
681
- klog .ErrorS (err , "CLOSE_RSP to frontend failed" )
703
+ klog .ErrorS (err , "CLOSE_RSP to frontend failed" , "serverID" , s . serverID , "agentID" , agentID )
682
704
}
683
705
}
684
706
}()
@@ -690,17 +712,18 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
690
712
klog .V (5 ).InfoS ("Received DIAL_RSP" , "random" , resp .Random , "agentID" , agentID , "connectionID" , resp .ConnectID )
691
713
692
714
if frontend , ok := s .PendingDial .Get (resp .Random ); ! ok {
693
- klog .V (5 ).Infoln ("DIAL_RSP not recognized; dropped" )
715
+ klog .V (2 ).Infoln ("DIAL_RSP not recognized; dropped" , "random" , resp . Random , "agentID" , agentID , "connectionID" , resp . ConnectID )
694
716
} else {
695
717
dialErr := false
696
718
if resp .Error != "" {
697
- klog .ErrorS (errors .New (resp .Error ), "DIAL_RSP contains failure" )
719
+ klog .ErrorS (errors .New (resp .Error ), "DIAL_RSP contains failure" , "random" , resp . Random , "agentID" , agentID , "connectionID" , resp . ConnectID )
698
720
dialErr = true
699
721
}
700
722
err := frontend .send (pkt )
701
723
s .PendingDial .Remove (resp .Random )
702
724
if err != nil {
703
- klog .ErrorS (err , "DIAL_RSP send to frontend stream failure" )
725
+ klog .ErrorS (err , "DIAL_RSP send to frontend stream failure" ,
726
+ "random" , resp .Random , "serverID" , s .serverID , "agentID" , agentID , "connectionID" , resp .ConnectID )
704
727
dialErr = true
705
728
}
706
729
// Avoid adding the frontend if there was an error dialing the destination
@@ -719,35 +742,35 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
719
742
klog .V (5 ).InfoS ("Received data from agent" , "bytes" , len (resp .Data ), "agentID" , agentID , "connectionID" , resp .ConnectID )
720
743
frontend , err := s .getFrontend (agentID , resp .ConnectID )
721
744
if err != nil {
722
- klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
745
+ klog .ErrorS (err , "could not get frontend client" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
723
746
break
724
747
}
725
748
if err := frontend .send (pkt ); err != nil {
726
- klog .ErrorS (err , "send to client stream failure" )
749
+ klog .ErrorS (err , "send to client stream failure" , "serverID" , s . serverID , "agentID" , agentID , "connectionID" , resp . ConnectID )
727
750
} else {
728
751
klog .V (5 ).InfoS ("DATA sent to frontend" )
729
752
}
730
753
731
754
case client .PacketType_CLOSE_RSP :
732
755
resp := pkt .GetCloseResponse ()
733
- klog .V (5 ).InfoS ("Received CLOSE_RSP" , "connectionID" , resp .ConnectID )
756
+ klog .V (5 ).InfoS ("Received CLOSE_RSP" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
734
757
frontend , err := s .getFrontend (agentID , resp .ConnectID )
735
758
if err != nil {
736
- klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
759
+ klog .ErrorS (err , "could not get frontend client" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
737
760
break
738
761
}
739
762
if err := frontend .send (pkt ); err != nil {
740
763
// Normal when frontend closes it.
741
- klog .ErrorS (err , "CLOSE_RSP send to client stream error" , "connectionID" , resp .ConnectID )
764
+ klog .ErrorS (err , "CLOSE_RSP send to client stream error" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
742
765
} else {
743
766
klog .V (5 ).Infoln ("CLOSE_RSP sent to frontend" , "connectionID" , resp .ConnectID )
744
767
}
745
768
s .removeFrontend (agentID , resp .ConnectID )
746
769
klog .V (5 ).InfoS ("Close streaming" , "agentID" , agentID , "connectionID" , resp .ConnectID )
747
770
748
771
default :
749
- klog .V (2 ).InfoS ("Unrecognized packet" , "packet" , pkt )
772
+ klog .V (2 ).InfoS ("Unrecognized packet" , "packet" , pkt , "serverID" , s . serverID , "agentID" , agentID )
750
773
}
751
774
}
752
- klog .V (5 ).InfoS ("Close backend of agent" , "backend" , stream , "agentID" , agentID )
775
+ klog .V (5 ).InfoS ("Close backend of agent" , "backend" , stream , "serverID" , s . serverID , " agentID" , agentID )
753
776
}
0 commit comments