-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathk8s.go
73 lines (62 loc) · 1.77 KB
/
k8s.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
package lib
import (
"context"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . IK8sClient
type IK8sClient interface {
Pods(namespace string) (*corev1.PodList, error)
CRDs(group, kind, version string) (*unstructured.UnstructuredList, error)
}
type k8sClient struct {
clientset *kubernetes.Clientset
dynamicclient *dynamic.DynamicClient
}
func NewK8sClient(kubeconfig string) (IK8sClient, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return &k8sClient{}, err
}
// Create the Kubernetes clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return k8sClient{}, err
}
// Create the dynamic client
dynamicclient, err := dynamic.NewForConfig(config)
if err != nil {
return k8sClient{}, err
}
return k8sClient{
clientset: clientset,
dynamicclient: dynamicclient,
}, nil
}
func (k k8sClient) Pods(namespace string) (*corev1.PodList, error) {
pods, err := k.clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
return pods, nil
}
func (k k8sClient) CRDs(group, kind, version string) (*unstructured.UnstructuredList, error) {
// Retrieve the CRD list
crdList, err := k.dynamicclient.Resource(
schema.GroupVersionResource{
Group: strings.ToLower(group),
Resource: strings.ToLower(kind),
Version: strings.ToLower(version),
},
).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
return crdList, nil
}