/
root.go
66 lines (56 loc) · 1.49 KB
/
root.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
package option
import (
"github.com/pkg/errors"
k8sclient "github.com/symcn/mesh-operator/pkg/k8s/client"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog"
)
// RootOption ...
type RootOption struct {
Kubeconfig string
ConfigContext string
Namespace string
DefaultNamespace string
DevelopmentMode bool
QPS float32
Burst int
}
// DefaultRootOption ...
func DefaultRootOption() *RootOption {
return &RootOption{
Namespace: corev1.NamespaceAll,
DevelopmentMode: true,
QPS: 120,
Burst: 120,
}
}
// GetK8sConfig ...
func (r *RootOption) GetK8sConfig() (*rest.Config, error) {
config, err := k8sclient.GetConfigWithContext(r.Kubeconfig, r.ConfigContext)
if err != nil {
return nil, errors.Wrap(err, "could not get k8s config")
}
return k8sclient.SetDefaultQPS(config, r.QPS, r.Burst), nil
}
// GetKubeInterface ...
func (r *RootOption) GetKubeInterface() (kubernetes.Interface, error) {
cfg, err := r.GetK8sConfig()
if err != nil {
return nil, errors.Wrap(err, "could not get k8s config")
}
kubeCli, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("failed to get kubernetes Clientset: %v", err)
}
return kubeCli, nil
}
// GetKubeInterfaceOrDie ...
func (r *RootOption) GetKubeInterfaceOrDie() kubernetes.Interface {
kubeCli, err := r.GetKubeInterface()
if err != nil {
klog.Fatalf("unable to get kube interface err: %v", err)
}
return kubeCli
}