@@ -89,6 +89,7 @@ type Server struct {
89
89
auth AuthInterface
90
90
host HostInterface
91
91
restfulCont containerInterface
92
+ metricsBuckets map [string ]bool
92
93
resourceAnalyzer stats.ResourceAnalyzer
93
94
redirectContainerStreaming bool
94
95
}
@@ -225,6 +226,7 @@ func NewServer(
225
226
resourceAnalyzer : resourceAnalyzer ,
226
227
auth : auth ,
227
228
restfulCont : & filteringContainer {Container : restful .NewContainer ()},
229
+ metricsBuckets : make (map [string ]bool ),
228
230
redirectContainerStreaming : redirectContainerStreaming ,
229
231
}
230
232
if auth != nil {
@@ -280,14 +282,32 @@ func (s *Server) InstallAuthFilter() {
280
282
})
281
283
}
282
284
285
+ // addMetricsBucketMatcher adds a regexp matcher and the relevant bucket to use when
286
+ // it matches. Please be aware this is not thread safe and should not be used dynamically
287
+ func (s * Server ) addMetricsBucketMatcher (bucket string ) {
288
+ s .metricsBuckets [bucket ] = true
289
+ }
290
+
291
+ // getMetricBucket find the appropriate metrics reporting bucket for the given path
292
+ func (s * Server ) getMetricBucket (path string ) string {
293
+ root := getURLRootPath (path )
294
+ if s .metricsBuckets [root ] == true {
295
+ return root
296
+ }
297
+ return "Invalid path"
298
+ }
299
+
283
300
// InstallDefaultHandlers registers the default set of supported HTTP request
284
301
// patterns with the restful Container.
285
302
func (s * Server ) InstallDefaultHandlers (enableCAdvisorJSONEndpoints bool ) {
303
+ s .addMetricsBucketMatcher ("healthz" )
286
304
healthz .InstallHandler (s .restfulCont ,
287
305
healthz .PingHealthz ,
288
306
healthz .LogHealthz ,
289
307
healthz .NamedCheck ("syncloop" , s .syncLoopHealthCheck ),
290
308
)
309
+
310
+ s .addMetricsBucketMatcher ("pods" )
291
311
ws := new (restful.WebService )
292
312
ws .
293
313
Path ("/pods" ).
@@ -297,7 +317,14 @@ func (s *Server) InstallDefaultHandlers(enableCAdvisorJSONEndpoints bool) {
297
317
Operation ("getPods" ))
298
318
s .restfulCont .Add (ws )
299
319
320
+ s .addMetricsBucketMatcher ("stats" )
300
321
s .restfulCont .Add (stats .CreateHandlers (statsPath , s .host , s .resourceAnalyzer , enableCAdvisorJSONEndpoints ))
322
+
323
+ s .addMetricsBucketMatcher ("metrics" )
324
+ s .addMetricsBucketMatcher ("metrics/cadvisor" )
325
+ s .addMetricsBucketMatcher ("metrics/probes" )
326
+ s .addMetricsBucketMatcher ("metrics/resource/v1alpha1" )
327
+ s .addMetricsBucketMatcher ("metrics/resource" )
301
328
//lint:ignore SA1019 https://github.com/kubernetes/enhancements/issues/1206
302
329
s .restfulCont .Handle (metricsPath , legacyregistry .Handler ())
303
330
@@ -321,12 +348,14 @@ func (s *Server) InstallDefaultHandlers(enableCAdvisorJSONEndpoints bool) {
321
348
)
322
349
323
350
// deprecated endpoint which will be removed in release 1.20.0+.
351
+ s .addMetricsBucketMatcher ("metrics/resource/v1alpha1" )
324
352
v1alpha1ResourceRegistry := compbasemetrics .NewKubeRegistry ()
325
353
v1alpha1ResourceRegistry .CustomMustRegister (stats .NewPrometheusResourceMetricCollector (s .resourceAnalyzer , v1alpha1 .Config ()))
326
354
s .restfulCont .Handle (path .Join (resourceMetricsPath , v1alpha1 .Version ),
327
355
compbasemetrics .HandlerFor (v1alpha1ResourceRegistry , compbasemetrics.HandlerOpts {ErrorHandling : compbasemetrics .ContinueOnError }),
328
356
)
329
357
358
+ s .addMetricsBucketMatcher ("metrics/resource" )
330
359
resourceRegistry := compbasemetrics .NewKubeRegistry ()
331
360
resourceRegistry .CustomMustRegister (collectors .NewResourceMetricsCollector (s .resourceAnalyzer ))
332
361
s .restfulCont .Handle (resourceMetricsPath ,
@@ -335,13 +364,15 @@ func (s *Server) InstallDefaultHandlers(enableCAdvisorJSONEndpoints bool) {
335
364
336
365
// prober metrics are exposed under a different endpoint
337
366
367
+ s .addMetricsBucketMatcher ("metrics/probes" )
338
368
p := compbasemetrics .NewKubeRegistry ()
339
369
_ = compbasemetrics .RegisterProcessStartTime (p .Register )
340
370
p .MustRegister (prober .ProberResults )
341
371
s .restfulCont .Handle (proberMetricsPath ,
342
372
compbasemetrics .HandlerFor (p , compbasemetrics.HandlerOpts {ErrorHandling : compbasemetrics .ContinueOnError }),
343
373
)
344
374
375
+ s .addMetricsBucketMatcher ("spec" )
345
376
if enableCAdvisorJSONEndpoints {
346
377
ws := new (restful.WebService )
347
378
ws .
@@ -361,6 +392,7 @@ const pprofBasePath = "/debug/pprof/"
361
392
func (s * Server ) InstallDebuggingHandlers (criHandler http.Handler ) {
362
393
klog .Infof ("Adding debug handlers to kubelet server." )
363
394
395
+ s .addMetricsBucketMatcher ("run" )
364
396
ws := new (restful.WebService )
365
397
ws .
366
398
Path ("/run" )
@@ -372,6 +404,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
372
404
Operation ("getRun" ))
373
405
s .restfulCont .Add (ws )
374
406
407
+ s .addMetricsBucketMatcher ("exec" )
375
408
ws = new (restful.WebService )
376
409
ws .
377
410
Path ("/exec" )
@@ -389,6 +422,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
389
422
Operation ("getExec" ))
390
423
s .restfulCont .Add (ws )
391
424
425
+ s .addMetricsBucketMatcher ("attach" )
392
426
ws = new (restful.WebService )
393
427
ws .
394
428
Path ("/attach" )
@@ -406,6 +440,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
406
440
Operation ("getAttach" ))
407
441
s .restfulCont .Add (ws )
408
442
443
+ s .addMetricsBucketMatcher ("portForward" )
409
444
ws = new (restful.WebService )
410
445
ws .
411
446
Path ("/portForward" )
@@ -423,6 +458,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
423
458
Operation ("getPortForward" ))
424
459
s .restfulCont .Add (ws )
425
460
461
+ s .addMetricsBucketMatcher ("logs" )
426
462
ws = new (restful.WebService )
427
463
ws .
428
464
Path (logsPath )
@@ -435,6 +471,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
435
471
Param (ws .PathParameter ("logpath" , "path to the log" ).DataType ("string" )))
436
472
s .restfulCont .Add (ws )
437
473
474
+ s .addMetricsBucketMatcher ("containerLogs" )
438
475
ws = new (restful.WebService )
439
476
ws .
440
477
Path ("/containerLogs" )
@@ -443,8 +480,10 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
443
480
Operation ("getContainerLogs" ))
444
481
s .restfulCont .Add (ws )
445
482
483
+ s .addMetricsBucketMatcher ("configz" )
446
484
configz .InstallHandler (s .restfulCont )
447
485
486
+ s .addMetricsBucketMatcher ("debug" )
448
487
handlePprofEndpoint := func (req * restful.Request , resp * restful.Response ) {
449
488
name := strings .TrimPrefix (req .Request .URL .Path , pprofBasePath )
450
489
switch name {
@@ -460,7 +499,6 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
460
499
pprof .Index (resp , req .Request )
461
500
}
462
501
}
463
-
464
502
// Setup pprof handlers.
465
503
ws = new (restful.WebService ).Path (pprofBasePath )
466
504
ws .Route (ws .GET ("/{subpath:*}" ).To (func (req * restful.Request , resp * restful.Response ) {
@@ -473,6 +511,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
473
511
s .restfulCont .Handle ("/debug/flags/v" , routes .StringFlagPutHandler (logs .GlogSetter ))
474
512
475
513
// The /runningpods endpoint is used for testing only.
514
+ s .addMetricsBucketMatcher ("runningpods" )
476
515
ws = new (restful.WebService )
477
516
ws .
478
517
Path ("/runningpods/" ).
@@ -482,6 +521,7 @@ func (s *Server) InstallDebuggingHandlers(criHandler http.Handler) {
482
521
Operation ("getRunningPods" ))
483
522
s .restfulCont .Add (ws )
484
523
524
+ s .addMetricsBucketMatcher ("cri" )
485
525
if criHandler != nil {
486
526
s .restfulCont .Handle ("/cri/" , criHandler )
487
527
}
@@ -493,6 +533,14 @@ func (s *Server) InstallDebuggingDisabledHandlers() {
493
533
http .Error (w , "Debug endpoints are disabled." , http .StatusMethodNotAllowed )
494
534
})
495
535
536
+ s .addMetricsBucketMatcher ("run" )
537
+ s .addMetricsBucketMatcher ("exec" )
538
+ s .addMetricsBucketMatcher ("attach" )
539
+ s .addMetricsBucketMatcher ("portForward" )
540
+ s .addMetricsBucketMatcher ("containerLogs" )
541
+ s .addMetricsBucketMatcher ("runningpods" )
542
+ s .addMetricsBucketMatcher ("pprof" )
543
+ s .addMetricsBucketMatcher ("logs" )
496
544
paths := []string {
497
545
"/run/" , "/exec/" , "/attach/" , "/portForward/" , "/containerLogs/" ,
498
546
"/runningpods/" , pprofBasePath , logsPath }
@@ -809,10 +857,10 @@ func (s *Server) getPortForward(request *restful.Request, response *restful.Resp
809
857
proxyStream (response .ResponseWriter , request .Request , url )
810
858
}
811
859
812
- // trimURLPath trims a URL path.
860
+ // getURLRootPath trims a URL path.
813
861
// For paths in the format of "/metrics/xxx", "metrics/xxx" is returned;
814
862
// For all other paths, the first part of the path is returned.
815
- func trimURLPath (path string ) string {
863
+ func getURLRootPath (path string ) string {
816
864
parts := strings .SplitN (strings .TrimPrefix (path , "/" ), "/" , 3 )
817
865
if len (parts ) == 0 {
818
866
return path
@@ -860,7 +908,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
860
908
serverType = "readwrite"
861
909
}
862
910
863
- method , path := req .Method , trimURLPath (req .URL .Path )
911
+ method , path := req .Method , s . getMetricBucket (req .URL .Path )
864
912
865
913
longRunning := strconv .FormatBool (isLongRunningRequest (path ))
866
914
0 commit comments