This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclient.go
55 lines (46 loc) · 1.54 KB
/
client.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
package kubernetes
import (
"fmt"
"runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func GetClientset(baseName string) kubernetes.Interface {
var config *rest.Config
//config, err := rest.InClusterConfig()
config, err := Config("", "", baseName)
if err != nil {
panic(fmt.Sprintf("Failed to create k8s InClusterConfig: %v", err))
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(fmt.Sprintf("Failed to create client: %v", err))
}
return clientset
}
// config returns a *rest.Config, using either the kubeconfig (if specified)
// or an in-cluster configuration.
// baseName is used to build the user agent to tell the API Server who is calling it.
// set to "ubiquity" if we, the ubiquity hook executor, is calling the APIs.
// Note that we only need the in-cluster way in production env, others are for test perpose only.
func Config(kubeconfig, kubecontext, baseName string) (*rest.Config, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = kubeconfig
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: kubecontext}
newConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, configOverrides)
clientConfig, err := newConfig.ClientConfig()
if err != nil {
return nil, err
}
clientConfig.UserAgent = buildUserAgent(
baseName,
runtime.GOOS,
runtime.GOARCH,
)
return clientConfig, nil
}
func buildUserAgent(command, os, arch string) string {
return fmt.Sprintf(
"%s (%s/%s)", command, os, arch)
}