Skip to content

Commit

Permalink
move to util.go
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Sep 26, 2023
1 parent e134ac9 commit b41d1ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
39 changes: 0 additions & 39 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
46 changes: 46 additions & 0 deletions pkg/app/util.go
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 13 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-build-kube-state-metrics

undefined: clientset

Check failure on line 13 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-benchmark-tests

undefined: clientset

Check failure on line 13 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-unit-tests

undefined: clientset

Check failure on line 13 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-validate-docs

undefined: clientset
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)

Check failure on line 29 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-build-kube-state-metrics

undefined: clientset

Check failure on line 29 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-benchmark-tests

undefined: clientset

Check failure on line 29 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-unit-tests

undefined: clientset

Check failure on line 29 in pkg/app/util.go

View workflow job for this annotation

GitHub Actions / ci-validate-docs

undefined: clientset
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
}

0 comments on commit b41d1ff

Please sign in to comment.