@@ -59,7 +59,8 @@ func NewProxyCommand(p *Proxy, o *options.ProxyRunOptions) *cobra.Command {
59
59
Use : "proxy" ,
60
60
Long : `A gRPC proxy server, receives requests from the API server and forwards to the agent.` ,
61
61
RunE : func (cmd * cobra.Command , args []string ) error {
62
- return p .run (o )
62
+ stopCh := SetupSignalHandler ()
63
+ return p .Run (o , stopCh )
63
64
},
64
65
}
65
66
@@ -81,11 +82,16 @@ func tlsCipherSuites(cipherNames []string) []uint16 {
81
82
}
82
83
83
84
type Proxy struct {
85
+ agentServer * grpc.Server
86
+ adminServer * http.Server
87
+ healthServer * http.Server
88
+
89
+ server * server.ProxyServer
84
90
}
85
91
86
92
type StopFunc func ()
87
93
88
- func (p * Proxy ) run (o * options.ProxyRunOptions ) error {
94
+ func (p * Proxy ) Run (o * options.ProxyRunOptions , stopCh <- chan struct {} ) error {
89
95
o .Print ()
90
96
if err := o .Validate (); err != nil {
91
97
return fmt .Errorf ("failed to validate server options with %v" , err )
@@ -126,37 +132,40 @@ func (p *Proxy) run(o *options.ProxyRunOptions) error {
126
132
if err != nil {
127
133
return err
128
134
}
129
- server : = server .NewProxyServer (o .ServerID , ps , int (o .ServerCount ), authOpt )
135
+ p . server = server .NewProxyServer (o .ServerID , ps , int (o .ServerCount ), authOpt )
130
136
131
- frontendStop , err := p .runFrontendServer (ctx , o , server )
137
+ frontendStop , err := p .runFrontendServer (ctx , o , p . server )
132
138
if err != nil {
133
139
return fmt .Errorf ("failed to run the frontend server: %v" , err )
134
140
}
141
+ if frontendStop != nil {
142
+ defer frontendStop ()
143
+ }
135
144
136
145
klog .V (1 ).Infoln ("Starting agent server for tunnel connections." )
137
- err = p .runAgentServer (o , server )
146
+ err = p .runAgentServer (o , p . server )
138
147
if err != nil {
139
148
return fmt .Errorf ("failed to run the agent server: %v" , err )
140
149
}
150
+ defer p .agentServer .Stop ()
151
+
141
152
klog .V (1 ).Infoln ("Starting admin server for debug connections." )
142
- err = p .runAdminServer (o , server )
153
+ err = p .runAdminServer (o , p . server )
143
154
if err != nil {
144
155
return fmt .Errorf ("failed to run the admin server: %v" , err )
145
156
}
157
+ defer p .adminServer .Close ()
158
+
146
159
klog .V (1 ).Infoln ("Starting health server for healthchecks." )
147
- err = p .runHealthServer (o , server )
160
+ err = p .runHealthServer (o , p . server )
148
161
if err != nil {
149
162
return fmt .Errorf ("failed to run the health server: %v" , err )
150
163
}
164
+ defer p .healthServer .Close ()
151
165
152
- stopCh := SetupSignalHandler ()
153
166
<- stopCh
154
167
klog .V (1 ).Infoln ("Shutting down server." )
155
168
156
- if frontendStop != nil {
157
- frontendStop ()
158
- }
159
-
160
169
return nil
161
170
}
162
171
@@ -379,6 +388,7 @@ func (p *Proxy) runAgentServer(o *options.ProxyRunOptions, server *server.ProxyS
379
388
"port" , strconv .FormatUint (uint64 (o .AgentPort ), 10 ),
380
389
)
381
390
go runpprof .Do (context .Background (), labels , func (context.Context ) { grpcServer .Serve (lis ) })
391
+ p .agentServer = grpcServer
382
392
383
393
return nil
384
394
}
@@ -396,7 +406,7 @@ func (p *Proxy) runAdminServer(o *options.ProxyRunOptions, server *server.ProxyS
396
406
runtime .SetBlockProfileRate (1 )
397
407
}
398
408
}
399
- adminServer : = & http.Server {
409
+ p . adminServer = & http.Server {
400
410
Addr : net .JoinHostPort (o .AdminBindAddress , strconv .Itoa (o .AdminPort )),
401
411
Handler : muxHandler ,
402
412
MaxHeaderBytes : 1 << 20 ,
@@ -408,7 +418,7 @@ func (p *Proxy) runAdminServer(o *options.ProxyRunOptions, server *server.ProxyS
408
418
"port" , strconv .FormatUint (uint64 (o .AdminPort ), 10 ),
409
419
)
410
420
go runpprof .Do (context .Background (), labels , func (context.Context ) {
411
- err := adminServer .ListenAndServe ()
421
+ err := p . adminServer .ListenAndServe ()
412
422
if err != nil {
413
423
klog .ErrorS (err , "admin server could not listen" )
414
424
}
@@ -438,7 +448,7 @@ func (p *Proxy) runHealthServer(o *options.ProxyRunOptions, server *server.Proxy
438
448
// "/ready" is deprecated but being maintained for backward compatibility
439
449
muxHandler .HandleFunc ("/ready" , readinessHandler )
440
450
muxHandler .HandleFunc ("/readyz" , readinessHandler )
441
- healthServer : = & http.Server {
451
+ p . healthServer = & http.Server {
442
452
Addr : net .JoinHostPort (o .HealthBindAddress , strconv .Itoa (o .HealthPort )),
443
453
Handler : muxHandler ,
444
454
MaxHeaderBytes : 1 << 20 ,
@@ -450,7 +460,7 @@ func (p *Proxy) runHealthServer(o *options.ProxyRunOptions, server *server.Proxy
450
460
"port" , strconv .FormatUint (uint64 (o .HealthPort ), 10 ),
451
461
)
452
462
go runpprof .Do (context .Background (), labels , func (context.Context ) {
453
- err := healthServer .ListenAndServe ()
463
+ err := p . healthServer .ListenAndServe ()
454
464
if err != nil {
455
465
klog .ErrorS (err , "health server could not listen" )
456
466
}
@@ -459,3 +469,8 @@ func (p *Proxy) runHealthServer(o *options.ProxyRunOptions, server *server.Proxy
459
469
460
470
return nil
461
471
}
472
+
473
+ // ProxyServer exposes internal state for testing.
474
+ func (p * Proxy ) ProxyServer () * server.ProxyServer {
475
+ return p .server
476
+ }
0 commit comments