forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
105 lines (89 loc) · 3.24 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
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
package kube_inventory
import (
"context"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/influxdata/telegraf/plugins/common/tls"
)
type client struct {
namespace string
timeout time.Duration
*kubernetes.Clientset
}
func newClient(baseURL, namespace, bearerToken string, timeout time.Duration, tlsConfig tls.ClientConfig) (*client, error) {
c, err := kubernetes.NewForConfig(&rest.Config{
TLSClientConfig: rest.TLSClientConfig{
ServerName: baseURL,
Insecure: tlsConfig.InsecureSkipVerify,
CAFile: tlsConfig.TLSCA,
CertFile: tlsConfig.TLSCert,
KeyFile: tlsConfig.TLSKey,
},
Host: baseURL,
BearerToken: bearerToken,
ContentConfig: rest.ContentConfig{},
})
if err != nil {
return nil, err
}
return &client{
Clientset: c,
timeout: timeout,
namespace: namespace,
}, nil
}
func (c *client) getDaemonSets(ctx context.Context) (*appsv1.DaemonSetList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.AppsV1().DaemonSets(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getDeployments(ctx context.Context) (*appsv1.DeploymentList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.AppsV1().Deployments(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getEndpoints(ctx context.Context) (*corev1.EndpointsList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().Endpoints(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getIngress(ctx context.Context) (*netv1.IngressList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.NetworkingV1().Ingresses(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getNodes(ctx context.Context) (*corev1.NodeList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
}
func (c *client) getPersistentVolumes(ctx context.Context) (*corev1.PersistentVolumeList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{})
}
func (c *client) getPersistentVolumeClaims(ctx context.Context) (*corev1.PersistentVolumeClaimList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().PersistentVolumeClaims(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getPods(ctx context.Context) (*corev1.PodList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().Pods(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getServices(ctx context.Context) (*corev1.ServiceList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.CoreV1().Services(c.namespace).List(ctx, metav1.ListOptions{})
}
func (c *client) getStatefulSets(ctx context.Context) (*appsv1.StatefulSetList, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.AppsV1().StatefulSets(c.namespace).List(ctx, metav1.ListOptions{})
}