-
Notifications
You must be signed in to change notification settings - Fork 90
/
clientset.go
136 lines (111 loc) · 3.5 KB
/
clientset.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package k8sutil
import (
"strconv"
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
memory "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
const (
DEFAULT_K8S_CLIENT_QPS = 100
DEFAULT_K8S_CLIENT_BURST = 100
)
var kubernetesConfigFlags *genericclioptions.ConfigFlags
func init() {
kubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
}
func AddFlags(flags *flag.FlagSet) {
kubernetesConfigFlags.AddFlags(flags)
}
func GetClientset() (*kubernetes.Clientset, error) {
cfg, err := GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create kubernetes clientset")
}
return clientset, nil
}
func GetClusterConfig() (*rest.Config, error) {
var cfg *rest.Config
var err error
if kubernetesConfigFlags != nil {
cfg, err = kubernetesConfigFlags.ToRESTConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
}
} else {
cfg, err = config.GetConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get config")
}
}
cfg.QPS = DEFAULT_K8S_CLIENT_QPS
cfg.Burst = DEFAULT_K8S_CLIENT_BURST
return cfg, nil
}
func GetDynamicClient() (dynamic.Interface, error) {
cfg, err := GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}
dynamicClient, err := dynamic.NewForConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create dynamic client")
}
return dynamicClient, nil
}
func GetK8sVersion(clientset kubernetes.Interface) (string, error) {
k8sVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return "", errors.Wrap(err, "failed to get kubernetes server version")
}
return k8sVersion.GitVersion, nil
}
func GetK8sMinorVersion(clientset kubernetes.Interface) (int, error) {
k8sVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return -1, errors.Wrap(err, "failed to get kubernetes server version")
}
k8sMinorVersion, err := strconv.Atoi(k8sVersion.Minor)
if err != nil {
return -1, errors.Wrap(err, "failed to convert k8s minor version to int")
}
return k8sMinorVersion, nil
}
func GetDynamicResourceInterface(gvk *schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) {
config, err := GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}
disc, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "failed to create discovery client")
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(disc))
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, errors.Wrap(err, "failed to get rest mapping")
}
dynamicClientset, err := dynamic.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "failed to get dynamic clientset")
}
var dr dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
dr = dynamicClientset.Resource(mapping.Resource).Namespace(namespace)
} else {
dr = dynamicClientset.Resource(mapping.Resource)
}
return dr, nil
}