forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
118 lines (102 loc) · 3.46 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
106
107
108
109
110
111
112
113
114
115
116
117
118
package kube_inventory
import (
"context"
"time"
"github.com/ericchiang/k8s"
v1APPS "github.com/ericchiang/k8s/apis/apps/v1"
v1 "github.com/ericchiang/k8s/apis/core/v1"
v1beta1EXT "github.com/ericchiang/k8s/apis/extensions/v1beta1"
"github.com/influxdata/telegraf/internal/tls"
)
type client struct {
namespace string
timeout time.Duration
*k8s.Client
}
func newClient(baseURL, namespace, bearerToken string, timeout time.Duration, tlsConfig tls.ClientConfig) (*client, error) {
c, err := k8s.NewClient(&k8s.Config{
Clusters: []k8s.NamedCluster{{Name: "cluster", Cluster: k8s.Cluster{
Server: baseURL,
InsecureSkipTLSVerify: tlsConfig.InsecureSkipVerify,
CertificateAuthority: tlsConfig.TLSCA,
}}},
Contexts: []k8s.NamedContext{{Name: "context", Context: k8s.Context{
Cluster: "cluster",
AuthInfo: "auth",
Namespace: namespace,
}}},
AuthInfos: []k8s.NamedAuthInfo{{Name: "auth", AuthInfo: k8s.AuthInfo{
Token: bearerToken,
ClientCertificate: tlsConfig.TLSCert,
ClientKey: tlsConfig.TLSKey,
}}},
})
if err != nil {
return nil, err
}
return &client{
Client: c,
timeout: timeout,
namespace: namespace,
}, nil
}
func (c *client) getDaemonSets(ctx context.Context) (*v1APPS.DaemonSetList, error) {
list := new(v1APPS.DaemonSetList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getDeployments(ctx context.Context) (*v1APPS.DeploymentList, error) {
list := &v1APPS.DeploymentList{}
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getEndpoints(ctx context.Context) (*v1.EndpointsList, error) {
list := new(v1.EndpointsList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getIngress(ctx context.Context) (*v1beta1EXT.IngressList, error) {
list := new(v1beta1EXT.IngressList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getNodes(ctx context.Context) (*v1.NodeList, error) {
list := new(v1.NodeList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, "", list)
}
func (c *client) getPersistentVolumes(ctx context.Context) (*v1.PersistentVolumeList, error) {
list := new(v1.PersistentVolumeList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, "", list)
}
func (c *client) getPersistentVolumeClaims(ctx context.Context) (*v1.PersistentVolumeClaimList, error) {
list := new(v1.PersistentVolumeClaimList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getPods(ctx context.Context) (*v1.PodList, error) {
list := new(v1.PodList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getServices(ctx context.Context) (*v1.ServiceList, error) {
list := new(v1.ServiceList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}
func (c *client) getStatefulSets(ctx context.Context) (*v1APPS.StatefulSetList, error) {
list := new(v1APPS.StatefulSetList)
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return list, c.List(ctx, c.namespace, list)
}