diff --git a/pkg/app/server.go b/pkg/app/server.go index 3588648b07..279c032fed 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -26,13 +26,10 @@ import ( "net/http/pprof" "os" "path/filepath" - "runtime" "strconv" "strings" "time" - "k8s.io/client-go/rest" - "github.com/oklog/run" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" @@ -41,7 +38,6 @@ import ( "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" "gopkg.in/yaml.v3" - clientset "k8s.io/client-go/kubernetes" _ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize common client auth plugins. "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" @@ -457,38 +453,3 @@ func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.Con } return nil, nil } - -func createKubeClient(apiserver string, kubeconfig string) (clientset.Interface, error) { - var config *rest.Config - - var err error - - if config == nil { - config, err = clientcmd.BuildConfigFromFlags(apiserver, kubeconfig) - if err != nil { - return nil, err - } - } - - config.UserAgent = fmt.Sprintf("%s/%s (%s/%s) kubernetes/%s", "kube-state-metrics", version.Version, runtime.GOOS, runtime.GOARCH, version.Revision) - config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" - config.ContentType = "application/vnd.kubernetes.protobuf" - - kubeClient, err := clientset.NewForConfig(config) - if err != nil { - return nil, err - } - - // Informers don't seem to do a good job logging error messages when it - // can't reach the server, making debugging hard. This makes it easier to - // figure out if apiserver is configured incorrectly. - klog.InfoS("Tested communication with server") - v, err := kubeClient.Discovery().ServerVersion() - if err != nil { - return nil, fmt.Errorf("error while trying to communicate with apiserver: %w", err) - } - klog.InfoS("Run with Kubernetes cluster version", "major", v.Major, "minor", v.Minor, "gitVersion", v.GitVersion, "gitTreeState", v.GitTreeState, "gitCommit", v.GitCommit, "platform", v.Platform) - klog.InfoS("Communication with server successful") - - return kubeClient, nil -} diff --git a/pkg/app/util.go b/pkg/app/util.go new file mode 100644 index 0000000000..2c9659ab3e --- /dev/null +++ b/pkg/app/util.go @@ -0,0 +1,46 @@ +package app + +import ( + "fmt" + "runtime" + + "github.com/prometheus/common/version" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" + "k8s.io/klog/v2" +) + +func createKubeClient(apiserver string, kubeconfig string) (clientset.Interface, error) { + var config *rest.Config + + var err error + + if config == nil { + config, err = clientcmd.BuildConfigFromFlags(apiserver, kubeconfig) + if err != nil { + return nil, err + } + } + + config.UserAgent = fmt.Sprintf("%s/%s (%s/%s) kubernetes/%s", "kube-state-metrics", version.Version, runtime.GOOS, runtime.GOARCH, version.Revision) + config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json" + config.ContentType = "application/vnd.kubernetes.protobuf" + + kubeClient, err := clientset.NewForConfig(config) + if err != nil { + return nil, err + } + + // Informers don't seem to do a good job logging error messages when it + // can't reach the server, making debugging hard. This makes it easier to + // figure out if apiserver is configured incorrectly. + klog.InfoS("Tested communication with server") + v, err := kubeClient.Discovery().ServerVersion() + if err != nil { + return nil, fmt.Errorf("error while trying to communicate with apiserver: %w", err) + } + klog.InfoS("Run with Kubernetes cluster version", "major", v.Major, "minor", v.Minor, "gitVersion", v.GitVersion, "gitTreeState", v.GitTreeState, "gitCommit", v.GitCommit, "platform", v.Platform) + klog.InfoS("Communication with server successful") + + return kubeClient, nil +}