-
Notifications
You must be signed in to change notification settings - Fork 90
/
clientset.go
63 lines (52 loc) · 1.49 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
package k8sutil
import (
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
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) {
if kubernetesConfigFlags != nil {
cfg, err := kubernetesConfigFlags.ToRESTConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
}
return cfg, nil
}
cfg, err := config.GetConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get config")
}
return cfg, nil
}
func GetK8sVersion() (string, error) {
clientset, err := GetClientset()
if err != nil {
return "", errors.Wrap(err, "failed to create kubernetes clientset")
}
k8sVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return "", errors.Wrap(err, "failed to get kubernetes server version")
}
return k8sVersion.GitVersion, nil
}