-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
discovery.go
82 lines (73 loc) · 2.36 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package discovery
import (
"fmt"
"io/ioutil"
"os"
"strings"
log "github.com/sirupsen/logrus"
coreClient "k8s.io/client-go/kubernetes"
restClient "k8s.io/client-go/rest"
cmdClient "k8s.io/client-go/tools/clientcmd"
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned"
)
// K8s struct holds the instance of clientset and metrics clisentset
type K8s struct {
Clientset coreClient.Interface
MetricsClientSet *metricsClient.Clientset
RestConfig *restClient.Config
}
var logEnabled bool
// NewK8s will provide a new k8s client interface
// resolves where it is running whether inside the kubernetes cluster or outside
// While running outside the cluster, tries to make use of the kubeconfig file
// While running inside the cluster resolved via pod environment uses the in-cluster config
func NewK8s() (*K8s, error) {
client := K8s{}
_, logEnabled = os.LookupEnv("CLIENTSET_LOG")
config, err := restClient.InClusterConfig()
if err != nil {
kubeConfig :=
cmdClient.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
config, err = cmdClient.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}
if logEnabled {
log.Info("Program running from outside of the cluster")
}
} else {
if logEnabled {
log.Info("Program running inside the cluster, picking the in-cluster configuration")
}
}
client.Clientset, err = coreClient.NewForConfig(config)
client.MetricsClientSet, err = metricsClient.NewForConfig(config)
client.RestConfig = config
return &client, nil
}
// GetVersion returns the version of the kubernetes cluster that is running
func (o *K8s) GetVersion() (string, error) {
version, err := o.Clientset.Discovery().ServerVersion()
if err != nil {
return "", err
}
if logEnabled {
log.Infof("Version of running k8s %v", version)
}
return fmt.Sprintf("%s", version), nil
}
// GetNamespace will return the current namespace for the running program
// Checks for the user passed ENV variable POD_NAMESPACE if not available
// pulls the namespace from pod, if not returns ""
func (o *K8s) GetNamespace() (string, error) {
if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
return ns, nil
}
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns, nil
}
return "", err
}
return "", nil
}