master
go 119 lines 3.46 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package k8s_apiserver
4
5 import (
6 "errors"
7 "os"
8
9 "github.com/netdata/netdata/go/plugins/pkg/prometheus"
10 "github.com/netdata/netdata/go/plugins/pkg/prometheus/selector"
11 "github.com/netdata/netdata/go/plugins/pkg/web"
12 )
13
14 const (
15 defaultTLSCA = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
16 defaultBearerTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
17 )
18
19 func (c *Collector) validateConfig() error {
20 if c.URL == "" {
21 return errors.New("url not set")
22 }
23
24 // Check if using default in-cluster TLS CA that doesn't exist
25 if c.TLSConfig.TLSCA == defaultTLSCA {
26 if _, err := os.Stat(c.TLSConfig.TLSCA); os.IsNotExist(err) {
27 c.Warningf("default tls_ca '%s' not found (not running inside a Kubernetes pod?); "+
28 "set 'tls_ca' to your CA certificate path, or set it to '' if using 'tls_skip_verify: yes'",
29 defaultTLSCA)
30 }
31 }
32
33 // Check if using default bearer token file that doesn't exist
34 if c.BearerTokenFile == defaultBearerTokenFile {
35 if _, err := os.Stat(c.BearerTokenFile); os.IsNotExist(err) {
36 c.Warningf("default bearer_token_file '%s' not found (not running inside a Kubernetes pod?); "+
37 "set 'bearer_token_file' to your token path, or use other authentication methods",
38 defaultBearerTokenFile)
39 }
40 }
41
42 return nil
43 }
44
45 func (c *Collector) initPrometheusClient() (prometheus.Prometheus, error) {
46 httpClient, err := web.NewHTTPClient(c.ClientConfig)
47 if err != nil {
48 return nil, err
49 }
50
51 // If selector parsing failed or selector is nil, log and collect all metrics
52 if srParseErr != nil || sr == nil {
53 if srParseErr != nil {
54 c.Warningf("selector parse error (collecting all metrics): %v", srParseErr)
55 }
56 return prometheus.New(httpClient, c.RequestConfig), nil
57 }
58
59 return prometheus.NewWithSelector(httpClient, c.RequestConfig, sr), nil
60 }
61
62 // Selector to filter only the metrics we need, reducing memory usage
63 // Parse() is called at package init time; if it fails, sr will be nil and all metrics will be collected
64 var sr, srParseErr = selector.Expr{
65 Allow: []string{
66 // Request metrics
67 "apiserver_request_total",
68 "apiserver_request_count",
69 "apiserver_request_duration_seconds",
70 "apiserver_response_sizes",
71 "apiserver_dropped_requests_total",
72 "apiserver_flowcontrol_rejected_requests_total",
73
74 // Inflight metrics
75 "apiserver_current_inflight_requests",
76 "apiserver_longrunning_requests",
77
78 // REST client metrics
79 "rest_client_requests_total",
80 "rest_client_request_duration_seconds",
81
82 // Admission metrics
83 "apiserver_admission_step_admission_duration_seconds",
84 "apiserver_admission_controller_admission_duration_seconds",
85 "apiserver_admission_webhook_admission_duration_seconds",
86
87 // Etcd/storage metrics
88 "apiserver_storage_objects",
89 "etcd_object_counts",
90
91 // Workqueue metrics
92 "workqueue_depth",
93 "workqueue_adds_total",
94 "workqueue_retries_total",
95 "workqueue_queue_duration_seconds",
96 "workqueue_work_duration_seconds",
97
98 // Audit metrics
99 "apiserver_audit_event_total",
100 "apiserver_audit_requests_rejected_total",
101
102 // Auth metrics
103 "authenticated_user_requests",
104 "apiserver_client_certificate_expiration_seconds",
105
106 // Process metrics
107 "go_goroutines",
108 "go_threads",
109 "go_gc_duration_seconds",
110 "go_memstats_heap_alloc_bytes",
111 "go_memstats_heap_inuse_bytes",
112 "go_memstats_stack_inuse_bytes",
113 "process_cpu_seconds_total",
114 "process_resident_memory_bytes",
115 "process_virtual_memory_bytes",
116 "process_open_fds",
117 "process_max_fds",
118 },
119 }.Parse()