github.com/netdata/go.d.plugin@v0.58.1/modules/k8s_kubelet/kubelet.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package k8s_kubelet 4 5 import ( 6 _ "embed" 7 "os" 8 "time" 9 10 "github.com/netdata/go.d.plugin/pkg/prometheus" 11 "github.com/netdata/go.d.plugin/pkg/web" 12 13 "github.com/netdata/go.d.plugin/agent/module" 14 ) 15 16 //go:embed "config_schema.json" 17 var configSchema string 18 19 func init() { 20 module.Register("k8s_kubelet", module.Creator{ 21 JobConfigSchema: configSchema, 22 Defaults: module.Defaults{ 23 // NETDATA_CHART_PRIO_CGROUPS_CONTAINERS 40000 24 Priority: 50000, 25 }, 26 Create: func() module.Module { return New() }, 27 }) 28 } 29 30 // New creates Kubelet with default values. 31 func New() *Kubelet { 32 config := Config{ 33 HTTP: web.HTTP{ 34 Request: web.Request{ 35 URL: "http://127.0.0.1:10255/metrics", 36 Headers: make(map[string]string), 37 }, 38 Client: web.Client{ 39 Timeout: web.Duration{Duration: time.Second}, 40 }, 41 }, 42 TokenPath: "/var/run/secrets/kubernetes.io/serviceaccount/token", 43 } 44 45 return &Kubelet{ 46 Config: config, 47 charts: charts.Copy(), 48 collectedVMPlugins: make(map[string]bool), 49 } 50 } 51 52 type ( 53 Config struct { 54 web.HTTP `yaml:",inline"` 55 TokenPath string `yaml:"token_path"` 56 } 57 58 Kubelet struct { 59 module.Base 60 Config `yaml:",inline"` 61 62 prom prometheus.Prometheus 63 charts *Charts 64 // volume_manager_total_volumes 65 collectedVMPlugins map[string]bool 66 } 67 ) 68 69 // Cleanup makes cleanup. 70 func (Kubelet) Cleanup() {} 71 72 // Init makes initialization. 73 func (k *Kubelet) Init() bool { 74 b, err := os.ReadFile(k.TokenPath) 75 if err != nil { 76 k.Warningf("error on reading service account token from '%s': %v", k.TokenPath, err) 77 } else { 78 k.Request.Headers["Authorization"] = "Bearer " + string(b) 79 } 80 81 client, err := web.NewHTTPClient(k.Client) 82 if err != nil { 83 k.Errorf("error on creating http client: %v", err) 84 return false 85 } 86 87 k.prom = prometheus.New(client, k.Request) 88 return true 89 } 90 91 // Check makes check. 92 func (k *Kubelet) Check() bool { 93 return len(k.Collect()) > 0 94 } 95 96 // Charts creates Charts. 97 func (k Kubelet) Charts() *Charts { 98 return k.charts 99 } 100 101 // Collect collects mx. 102 func (k *Kubelet) Collect() map[string]int64 { 103 mx, err := k.collect() 104 105 if err != nil { 106 k.Error(err) 107 return nil 108 } 109 110 return mx 111 }