-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
50 lines (42 loc) · 1.3 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
package k8sreporter
import (
"fmt"
"os"
"k8s.io/apimachinery/pkg/runtime"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type clientSet struct {
corev1client.CoreV1Interface
appsv1client.AppsV1Interface
runtimeclient.Client
}
// New returns a *ClientBuilder with the given kubeconfig.
func newClient(kubeconfig string, crScheme *runtime.Scheme) (*clientSet, error) {
var config *rest.Config
var err error
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
if kubeconfig != "" {
fmt.Println("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
fmt.Println("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
}
if err != nil {
return nil, fmt.Errorf("Failed to init client")
}
clientSet := &clientSet{}
clientSet.CoreV1Interface = corev1client.NewForConfigOrDie(config)
clientSet.AppsV1Interface = appsv1client.NewForConfigOrDie(config)
clientSet.Client, err = runtimeclient.New(config, client.Options{
Scheme: crScheme,
})
return clientSet, nil
}