This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/
client.go
68 lines (53 loc) · 1.76 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
package kube
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure" // auth for AKS clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/exec" // auth for OIDC
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // auth for GKE clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // auth for OIDC
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type KubeClient struct {
Client *clientset.Clientset
Config *restclient.Config
}
func NewClient(context string) (*KubeClient, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here
configOverrides := &clientcmd.ConfigOverrides{
CurrentContext: context,
}
// if you want to change override values or bind them to flags, there are methods to help you
var config *restclient.Config
var err error
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err = kubeConfig.ClientConfig()
if err != nil {
return nil, err
}
client, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
return &KubeClient{
Client: client,
Config: config,
}, nil
}
func (kubeClient *KubeClient) ListPods(namespace string) ([]v1.Pod, error) {
objs, err := kubeClient.Client.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
return objs.Items, nil
}
func (kubeClient *KubeClient) GetClusterUID() (string, error) {
ns, err := kubeClient.Client.CoreV1().Namespaces().Get("kube-system", metav1.GetOptions{})
if err != nil {
return "", err
}
return string(ns.UID), nil
}