-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
51 lines (45 loc) · 1.45 KB
/
kube.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
package util
import (
"fmt"
"strings"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/cert"
"github.com/openvswitch/ovn-kubernetes/go-controller/pkg/config"
)
// NewClientset creates a Kubernetes clientset from either a kubeconfig,
// TLS properties, or an apiserver URL
func NewClientset(conf *config.KubernetesConfig) (*kubernetes.Clientset, error) {
var kconfig *rest.Config
var err error
if conf.Kubeconfig != "" {
// uses the current context in kubeconfig
kconfig, err = clientcmd.BuildConfigFromFlags("", conf.Kubeconfig)
} else if strings.HasPrefix(conf.APIServer, "https") {
if conf.APIServer == "" || conf.Token == "" {
return nil, fmt.Errorf("TLS-secured apiservers require token and CA certificate")
}
kconfig = &rest.Config{
Host: conf.APIServer,
BearerToken: conf.Token,
}
if conf.CACert != "" {
if _, err := cert.NewPool(conf.CACert); err != nil {
return nil, err
}
kconfig.TLSClientConfig = rest.TLSClientConfig{CAFile: conf.CACert}
}
} else if strings.HasPrefix(conf.APIServer, "http") {
kconfig, err = clientcmd.BuildConfigFromFlags(conf.APIServer, "")
} else {
// Assume we are running from a container managed by kubernetes
// and read the apiserver address and tokens from the
// container's environment.
kconfig, err = rest.InClusterConfig()
}
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(kconfig)
}