Skip to content

Commit

Permalink
Merge pull request #30 from gkarthiks/minified
Browse files Browse the repository at this point in the history
minified version
  • Loading branch information
gkarthiks authored Jul 25, 2021
2 parents 08e10c3 + a6389ee commit d276d58
Showing 1 changed file with 11 additions and 45 deletions.
56 changes: 11 additions & 45 deletions discovery.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package discovery

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -31,62 +29,30 @@ var logEnabled bool
func NewK8s() (*K8s, error) {
client := K8s{}
_, logEnabled = os.LookupEnv("CLIENTSET_LOG")
if _, inCluster := os.LookupEnv("KUBERNETES_SERVICE_HOST"); inCluster == true {
if logEnabled {
log.Info("Program running inside the cluster, picking the in-cluster configuration")
}

config, err := restClient.InClusterConfig()
client.RestConfig = config
if err != nil {
return nil, err
}
client.Clientset, err = coreClient.NewForConfig(config)
config, err := restClient.InClusterConfig()
if err != nil {
kubeConfig :=
cmdClient.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
config, err = cmdClient.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}
client.MetricsClientSet, err = metricsClient.NewForConfig(config)
if err != nil {
panic(err)
if logEnabled {
log.Info("Program running from outside of the cluster")
}
return &client, nil
}

if logEnabled {
log.Info("Program running from outside of the cluster")
}
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := cmdClient.BuildConfigFromFlags("", *kubeconfig)
client.RestConfig = config
if err != nil {
return nil, err
if logEnabled {
log.Info("Program running inside the cluster, picking the in-cluster configuration")
}
}
client.Clientset, err = coreClient.NewForConfig(config)
if err != nil {
return nil, err
}

client.MetricsClientSet, err = metricsClient.NewForConfig(config)
if err != nil {
panic(err)
}
client.RestConfig = config

return &client, nil
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE")
}

// GetVersion returns the version of the kubernetes cluster that is running
func (o *K8s) GetVersion() (string, error) {
version, err := o.Clientset.Discovery().ServerVersion()
Expand Down

0 comments on commit d276d58

Please sign in to comment.