@@ -83,6 +83,8 @@ type ProxyRunOptions struct {
83
83
agentPort uint
84
84
// Port we listen for admin connections on.
85
85
adminPort uint
86
+ // Port we listen for health connections on.
87
+ healthPort uint
86
88
87
89
// ID of this proxy server.
88
90
serverID string
@@ -111,6 +113,7 @@ func (o *ProxyRunOptions) Flags() *pflag.FlagSet {
111
113
flags .UintVar (& o .serverPort , "server-port" , o .serverPort , "Port we listen for server connections on. Set to 0 for UDS." )
112
114
flags .UintVar (& o .agentPort , "agent-port" , o .agentPort , "Port we listen for agent connections on." )
113
115
flags .UintVar (& o .adminPort , "admin-port" , o .adminPort , "Port we listen for admin connections on." )
116
+ flags .UintVar (& o .healthPort , "health-port" , o .healthPort , "Port we listen for health connections on." )
114
117
flags .StringVar (& o .serverID , "server-id" , o .serverID , "The unique ID of this server." )
115
118
flags .UintVar (& o .serverCount , "server-count" , o .serverCount , "The number of proxy server instances, should be 1 unless it is an HA server." )
116
119
flags .StringVar (& o .agentNamespace , "agent-namespace" , o .agentNamespace , "Expected agent's namespace during agent authentication (used with agent-service-account, authentication-audience, kubeconfig)." )
@@ -132,6 +135,7 @@ func (o *ProxyRunOptions) Print() {
132
135
klog .Warningf ("Server port set to %d.\n " , o .serverPort )
133
136
klog .Warningf ("Agent port set to %d.\n " , o .agentPort )
134
137
klog .Warningf ("Admin port set to %d.\n " , o .adminPort )
138
+ klog .Warningf ("Health port set to %d.\n " , o .healthPort )
135
139
klog .Warningf ("ServerID set to %s.\n " , o .serverID )
136
140
klog .Warningf ("ServerCount set to %d.\n " , o .serverCount )
137
141
klog .Warningf ("AgentNamespace set to %q.\n " , o .agentNamespace )
@@ -209,6 +213,10 @@ func (o *ProxyRunOptions) Validate() error {
209
213
if o .adminPort > 49151 {
210
214
return fmt .Errorf ("please do not try to use ephemeral port %d for the admin port" , o .adminPort )
211
215
}
216
+ if o .healthPort > 49151 {
217
+ return fmt .Errorf ("please do not try to use ephemeral port %d for the health port" , o .healthPort )
218
+ }
219
+
212
220
if o .serverPort < 1024 {
213
221
if o .udsName == "" {
214
222
return fmt .Errorf ("please do not try to use reserved port %d for the server port" , o .serverPort )
@@ -220,6 +228,9 @@ func (o *ProxyRunOptions) Validate() error {
220
228
if o .adminPort < 1024 {
221
229
return fmt .Errorf ("please do not try to use reserved port %d for the admin port" , o .adminPort )
222
230
}
231
+ if o .healthPort < 1024 {
232
+ return fmt .Errorf ("please do not try to use reserved port %d for the health port" , o .healthPort )
233
+ }
223
234
224
235
// validate agent authentication params
225
236
// all 4 parametes must be empty or must have value (except kubeconfigPath that might be empty)
@@ -256,6 +267,7 @@ func newProxyRunOptions() *ProxyRunOptions {
256
267
serverPort : 8090 ,
257
268
agentPort : 8091 ,
258
269
adminPort : 8092 ,
270
+ healthPort : 8093 ,
259
271
serverID : uuid .New ().String (),
260
272
serverCount : 1 ,
261
273
agentNamespace : "" ,
@@ -331,6 +343,12 @@ func (p *Proxy) run(o *ProxyRunOptions) error {
331
343
return fmt .Errorf ("failed to run the admin server: %v" , err )
332
344
}
333
345
346
+ klog .Info ("Starting health server for healthchecks." )
347
+ err = p .runHealthServer (o , server )
348
+ if err != nil {
349
+ return fmt .Errorf ("failed to run the health server: %v" , err )
350
+ }
351
+
334
352
stopCh := SetupSignalHandler ()
335
353
<- stopCh
336
354
klog .Info ("Shutting down server." )
@@ -496,28 +514,48 @@ func (p *Proxy) runAgentServer(o *ProxyRunOptions, server *server.ProxyServer) e
496
514
}
497
515
498
516
func (p * Proxy ) runAdminServer (o * ProxyRunOptions , server * server.ProxyServer ) error {
517
+ metricsHandler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
518
+ prometheus .Handler ().ServeHTTP (w , r )
519
+ })
520
+
521
+ muxHandler := http .NewServeMux ()
522
+ muxHandler .HandleFunc ("/metrics" , metricsHandler )
523
+ adminServer := & http.Server {
524
+ Addr : fmt .Sprintf ("127.0.0.1:%d" , o .adminPort ),
525
+ Handler : muxHandler ,
526
+ MaxHeaderBytes : 1 << 20 ,
527
+ }
528
+
529
+ go func () {
530
+ err := adminServer .ListenAndServe ()
531
+ if err != nil {
532
+ klog .Warningf ("admin server received %v.\n " , err )
533
+ }
534
+ klog .Warningf ("Admin server stopped listening\n " )
535
+ }()
536
+
537
+ return nil
538
+ }
539
+
540
+ func (p * Proxy ) runHealthServer (o * ProxyRunOptions , server * server.ProxyServer ) error {
499
541
livenessHandler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
500
542
fmt .Fprintf (w , "ok" )
501
543
})
502
544
readinessHandler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
503
545
fmt .Fprintf (w , "ok" )
504
546
})
505
- metricsHandler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
506
- prometheus .Handler ().ServeHTTP (w , r )
507
- })
508
547
509
548
muxHandler := http .NewServeMux ()
510
549
muxHandler .HandleFunc ("/healthz" , livenessHandler )
511
550
muxHandler .HandleFunc ("/ready" , readinessHandler )
512
- muxHandler .HandleFunc ("/metrics" , metricsHandler )
513
- adminServer := & http.Server {
514
- Addr : fmt .Sprintf ("127.0.0.1:%d" , o .adminPort ),
551
+ healthServer := & http.Server {
552
+ Addr : fmt .Sprintf (":%d" , o .healthPort ),
515
553
Handler : muxHandler ,
516
554
MaxHeaderBytes : 1 << 20 ,
517
555
}
518
556
519
557
go func () {
520
- err := adminServer .ListenAndServe ()
558
+ err := healthServer .ListenAndServe ()
521
559
if err != nil {
522
560
klog .Warningf ("health server received %v.\n " , err )
523
561
}
0 commit comments