-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathclient.go
101 lines (85 loc) · 2.39 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package k8s
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/caicloud/cyclone/pkg/k8s/clientset"
cyscheme "github.com/caicloud/cyclone/pkg/k8s/clientset/scheme"
cyclonev1alpha1 "github.com/caicloud/cyclone/pkg/k8s/clientset/typed/cyclone/v1alpha1"
)
// Scheme consists of kubernetes and cyclone scheme.
var Scheme = runtime.NewScheme()
func init() {
_ = cyscheme.AddToScheme(Scheme)
_ = scheme.AddToScheme(Scheme)
}
// Interface consists of kubernetes and cyclone interfaces
type Interface interface {
kubernetes.Interface
CycloneV1alpha1() cyclonev1alpha1.CycloneV1alpha1Interface
}
// Clientset contains the cyclone Clientset and kubernetes Clientset
type Clientset struct {
*kubernetes.Clientset
cycloneClient *clientset.Clientset
}
// CycloneV1alpha1 retrieves the CycloneV1alpha1Client
func (c *Clientset) CycloneV1alpha1() cyclonev1alpha1.CycloneV1alpha1Interface {
return c.cycloneClient.CycloneV1alpha1()
}
var _ Interface = (*Clientset)(nil)
// NewForConfig creates a new Clientset for the given config.
func NewForConfig(c *rest.Config) (*Clientset, error) {
cycloneClient, err := clientset.NewForConfig(c)
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(c)
if err != nil {
return nil, err
}
return &Clientset{
Clientset: kubeClient,
cycloneClient: cycloneClient,
}, nil
}
// GetClient creates a client for k8s cluster
func GetClient(kubeConfigPath string) (Interface, error) {
config, err := getKubeConfig(kubeConfigPath)
if err != nil {
return nil, err
}
return NewForConfig(config)
}
// GetRateLimitClient creates a client for k8s cluster with custom defined qps and burst.
func GetRateLimitClient(kubeConfigPath string, qps float32, burst int) (Interface, error) {
config, err := getKubeConfig(kubeConfigPath)
if err != nil {
return nil, err
}
if qps > 0.0 {
config.QPS = qps
}
if burst > 0 {
config.Burst = burst
}
return NewForConfig(config)
}
func getKubeConfig(kubeConfigPath string) (*rest.Config, error) {
var config *rest.Config
var err error
if kubeConfigPath != "" {
config, err = clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
return nil, err
}
} else {
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
return config, nil
}