From a33c07db93865999a997388c0807121e9e1d7941 Mon Sep 17 00:00:00 2001 From: yahaa <1477765176@qq.com> Date: Mon, 15 Aug 2022 19:59:53 +0800 Subject: [PATCH] fix: prometheus input plugin run outside k8s cluster error --- docs/LICENSE_OF_DEPENDENCIES.md | 3 +- go.mod | 3 +- go.sum | 3 +- plugins/inputs/prometheus/kubernetes.go | 38 +++++++++++-------------- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/docs/LICENSE_OF_DEPENDENCIES.md b/docs/LICENSE_OF_DEPENDENCIES.md index a9c23efe02027..6f35fc9d180f8 100644 --- a/docs/LICENSE_OF_DEPENDENCIES.md +++ b/docs/LICENSE_OF_DEPENDENCIES.md @@ -101,7 +101,6 @@ following works: - github.com/emicklei/go-restful [MIT License](https://github.com/emicklei/go-restful/blob/v3/LICENSE) - github.com/fatih/color [MIT License](https://github.com/fatih/color/blob/master/LICENSE.md) - github.com/form3tech-oss/jwt-go [MIT License](https://github.com/form3tech-oss/jwt-go/blob/master/LICENSE) -- github.com/ghodss/yaml [MIT License](https://github.com/ghodss/yaml/blob/master/LICENSE) - github.com/go-asn1-ber/asn1-ber [MIT License](https://github.com/go-asn1-ber/asn1-ber/blob/v1.3/LICENSE) - github.com/go-ldap/ldap [MIT License](https://github.com/go-ldap/ldap/blob/v3.4.1/LICENSE) - github.com/go-logfmt/logfmt [MIT License](https://github.com/go-logfmt/logfmt/blob/master/LICENSE) @@ -155,6 +154,7 @@ following works: - github.com/hashicorp/go-uuid [Mozilla Public License 2.0](https://github.com/hashicorp/go-uuid/blob/master/LICENSE) - github.com/hashicorp/golang-lru [Mozilla Public License 2.0](https://github.com/hashicorp/golang-lru/blob/master/LICENSE) - github.com/hashicorp/serf [Mozilla Public License 2.0](https://github.com/hashicorp/serf/blob/master/LICENSE) +- github.com/imdario/mergo [BSD 3-Clause "New" or "Revised" License](https://github.com/imdario/mergo/blob/master/LICENSE) - github.com/influxdata/go-syslog [MIT License](https://github.com/influxdata/go-syslog/blob/develop/LICENSE) - github.com/influxdata/influxdb-observability/common [MIT License](https://github.com/influxdata/influxdb-observability/blob/main/LICENSE) - github.com/influxdata/influxdb-observability/influx2otel [MIT License](https://github.com/influxdata/influxdb-observability/blob/main/LICENSE) @@ -261,6 +261,7 @@ following works: - github.com/sirupsen/logrus [MIT License](https://github.com/sirupsen/logrus/blob/master/LICENSE) - github.com/sleepinggenius2/gosmi [MIT License](https://github.com/sleepinggenius2/gosmi/blob/master/LICENSE) - github.com/snowflakedb/gosnowflake [Apache License 2.0](https://github.com/snowflakedb/gosnowflake/blob/master/LICENSE) +- github.com/spf13/pflag [BSD 3-Clause "New" or "Revised" License](https://github.com/spf13/pflag/blob/master/LICENSE) - github.com/stretchr/objx [MIT License](https://github.com/stretchr/objx/blob/master/LICENSE) - github.com/stretchr/testify [MIT License](https://github.com/stretchr/testify/blob/master/LICENSE) - github.com/testcontainers/testcontainers-go [MIT License](https://github.com/testcontainers/testcontainers-go/blob/main/LICENSE) diff --git a/go.mod b/go.mod index 1021959de531d..59b2ae6dfa46d 100644 --- a/go.mod +++ b/go.mod @@ -61,7 +61,6 @@ require ( github.com/eclipse/paho.golang v0.10.0 github.com/eclipse/paho.mqtt.golang v1.4.1 github.com/fatih/color v1.13.0 - github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/go-ldap/ldap/v3 v3.4.4 github.com/go-logfmt/logfmt v0.5.1 github.com/go-ping/ping v0.0.0-20210201095549-52eed920f98c @@ -182,6 +181,8 @@ require ( ) require ( + github.com/imdario/mergo v0.3.12 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect gopkg.in/macaroon-bakery.v3 v3.0.0 // indirect ) diff --git a/go.sum b/go.sum index a41bd2b513cac..a48be2c9ce1d3 100644 --- a/go.sum +++ b/go.sum @@ -825,8 +825,6 @@ github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebK github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew= -github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= @@ -1348,6 +1346,7 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/flux v0.65.1/go.mod h1:J754/zds0vvpfwuq7Gc2wRdVwEodfpCFM7mYlOw2LqY= diff --git a/plugins/inputs/prometheus/kubernetes.go b/plugins/inputs/prometheus/kubernetes.go index 492789138f06e..84341f4cf171e 100644 --- a/plugins/inputs/prometheus/kubernetes.go +++ b/plugins/inputs/prometheus/kubernetes.go @@ -8,12 +8,10 @@ import ( "net" "net/http" "net/url" - "os" "os/user" "path/filepath" "time" - "github.com/ghodss/yaml" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -23,6 +21,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/clientcmd" ) type podMetadata struct { @@ -39,27 +38,21 @@ type podResponse struct { const cAdvisorPodListDefaultInterval = 60 -// loadClient parses a kubeconfig from a file and returns a Kubernetes -// client. It does not support extensions or client auth providers. -func loadClient(kubeconfigPath string) (*kubernetes.Clientset, error) { - data, err := os.ReadFile(kubeconfigPath) - if err != nil { - return nil, fmt.Errorf("failed reading '%s': %v", kubeconfigPath, err) +// loadConfig parses a kubeconfig from a file and returns a Kubernetes rest.Config +func loadConfig(kubeconfigPath string) (*rest.Config, error) { + if kubeconfigPath == "" { + return rest.InClusterConfig() } - // Unmarshal YAML into a Kubernetes config object. - var config rest.Config - if err := yaml.Unmarshal(data, &config); err != nil { - return nil, err - } - return kubernetes.NewForConfig(&config) + return clientcmd.BuildConfigFromFlags("", kubeconfigPath) } func (p *Prometheus) startK8s(ctx context.Context) error { - config, err := rest.InClusterConfig() + config, err := loadConfig(p.KubeConfig) if err != nil { - return fmt.Errorf("failed to get InClusterConfig - %v", err) + return fmt.Errorf("failed to get rest.Config from %v - %v", p.KubeConfig, err) } + client, err := kubernetes.NewForConfig(config) if err != nil { u, err := user.Current() @@ -67,13 +60,16 @@ func (p *Prometheus) startK8s(ctx context.Context) error { return fmt.Errorf("failed to get current user - %v", err) } - configLocation := filepath.Join(u.HomeDir, ".kube/config") - if p.KubeConfig != "" { - configLocation = p.KubeConfig + kubeconfig := filepath.Join(u.HomeDir, ".kube/config") + + config, err = loadConfig(kubeconfig) + if err != nil { + return fmt.Errorf("failed to get rest.Config from %v - %v", kubeconfig, err) } - client, err = loadClient(configLocation) + + client, err = kubernetes.NewForConfig(config) if err != nil { - return err + return fmt.Errorf("failed to get kubernetes client - %v", err) } }