| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package k8s_kubelet |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/prometheus" |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 16 | ) |
| 17 | |
| 18 | //go:embed "config_schema.json" |
| 19 | var configSchema string |
| 20 | |
| 21 | func init() { |
| 22 | collectorapi.Register("k8s_kubelet", collectorapi.Creator{ |
| 23 | JobConfigSchema: configSchema, |
| 24 | Defaults: collectorapi.Defaults{ |
| 25 | // NETDATA_CHART_PRIO_CGROUPS_CONTAINERS 40000 |
| 26 | Priority: 50000, |
| 27 | }, |
| 28 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 29 | Config: func() any { return &Config{} }, |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | func New() *Collector { |
| 34 | return &Collector{ |
| 35 | Config: Config{ |
| 36 | HTTPConfig: web.HTTPConfig{ |
| 37 | RequestConfig: web.RequestConfig{ |
| 38 | URL: "http://127.0.0.1:10255/metrics", |
| 39 | Headers: make(map[string]string), |
| 40 | BearerTokenFile: "/var/run/secrets/kubernetes.io/serviceaccount/token", |
| 41 | }, |
| 42 | ClientConfig: web.ClientConfig{ |
| 43 | Timeout: confopt.Duration(time.Second), |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | |
| 48 | charts: charts.Copy(), |
| 49 | collectedVMPlugins: make(map[string]bool), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | type Config struct { |
| 54 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 55 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 56 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 57 | web.HTTPConfig `yaml:",inline" json:""` |
| 58 | } |
| 59 | |
| 60 | type Collector struct { |
| 61 | collectorapi.Base |
| 62 | Config `yaml:",inline" json:""` |
| 63 | |
| 64 | charts *Charts |
| 65 | |
| 66 | prom prometheus.Prometheus |
| 67 | |
| 68 | collectedVMPlugins map[string]bool // volume_manager_total_volumes |
| 69 | } |
| 70 | |
| 71 | func (c *Collector) Configuration() any { |
| 72 | return c.Config |
| 73 | } |
| 74 | |
| 75 | func (c *Collector) Init(context.Context) error { |
| 76 | if err := c.validateConfig(); err != nil { |
| 77 | return fmt.Errorf("config validation: %v", err) |
| 78 | } |
| 79 | |
| 80 | prom, err := c.initPrometheusClient() |
| 81 | if err != nil { |
| 82 | return fmt.Errorf("init prometheus client: %v", err) |
| 83 | } |
| 84 | c.prom = prom |
| 85 | |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | func (c *Collector) Check(context.Context) error { |
| 90 | mx, err := c.collect() |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if len(mx) == 0 { |
| 95 | return errors.New("no metrics collected") |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (c *Collector) Charts() *Charts { |
| 101 | return c.charts |
| 102 | } |
| 103 | |
| 104 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 105 | mx, err := c.collect() |
| 106 | |
| 107 | if err != nil { |
| 108 | c.Error(err) |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | return mx |
| 113 | } |
| 114 | |
| 115 | func (c *Collector) Cleanup(context.Context) { |
| 116 | if c.prom != nil && c.prom.HTTPClient() != nil { |
| 117 | c.prom.HTTPClient().CloseIdleConnections() |
| 118 | } |
| 119 | } |