This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
kubernetes.go
282 lines (249 loc) · 8.21 KB
/
kubernetes.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubernetes
import (
"context"
"fmt"
"time"
"github.com/gravitational/monitoring-app/watcher/lib/constants"
"github.com/cenkalti/backoff"
"github.com/gravitational/trace"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
monitoringv1typed "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func init() {
runtime.Must(monitoringv1.AddToScheme(scheme.Scheme))
}
// Client is the Kubernetes API client
type Client struct {
*kubernetes.Clientset
}
// NewClient returns a new Kubernetes API client
func NewClient(kubeconfig string) (*Client, error) {
var config *rest.Config
var err error
if kubeconfig != "" {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
config, err = rest.InClusterConfig()
}
if err != nil {
return nil, trace.Wrap(err)
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, trace.Wrap(err)
}
return &Client{Clientset: client}, nil
}
// NewMonitoringClient returns a new in-cluster Prometheus CRD API client.
func NewMonitoringClient(kubeconfig string) (*monitoring.Clientset, error) {
var config *rest.Config
var err error
if kubeconfig != "" {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
config, err = rest.InClusterConfig()
}
if err != nil {
return nil, trace.Wrap(err)
}
client, err := monitoring.NewForConfig(config)
if err != nil {
return nil, trace.Wrap(err)
}
return client, nil
}
// Prometheuses returns Prometheus CRD client in monitoring namespace.
func Prometheuses(kubeconfig string) (monitoringv1typed.PrometheusInterface, error) {
client, err := NewMonitoringClient(kubeconfig)
if err != nil {
return nil, trace.Wrap(err)
}
return client.MonitoringV1().Prometheuses(constants.MonitoringNamespace), nil
}
// Alertmanagers returns Alertmanager CRD client in monitoring namespace.
func Alertmanagers(kubeconfig string) (monitoringv1typed.AlertmanagerInterface, error) {
client, err := NewMonitoringClient(kubeconfig)
if err != nil {
return nil, trace.Wrap(err)
}
return client.MonitoringV1().Alertmanagers(constants.MonitoringNamespace), nil
}
// WatchConfigMaps watches Kubernetes API for ConfigMaps using specified configs to match
// and send updates
func (c *Client) WatchConfigMaps(ctx context.Context, configs ...ConfigMap) {
for _, config := range configs {
go func(config ConfigMap) {
retry(ctx, func() error {
err := watchConfigMap(ctx, c.CoreV1().ConfigMaps(constants.MonitoringNamespace), config)
return trace.Wrap(err)
})
}(config)
}
}
// WatchSecrets watches Kubernetes API for Secrets using specified configs to match
// and send updates
func (c *Client) WatchSecrets(ctx context.Context, configs ...Secret) {
for _, config := range configs {
go func(config Secret) {
retry(ctx, func() error {
err := watchSecret(ctx, c.CoreV1().Secrets(constants.MonitoringNamespace), config)
return trace.Wrap(err)
})
}(config)
}
}
// Label represents a Kubernetes label which is used
// as a search target for ConfigMaps
type Label struct {
Key string
Value string
}
// MatchLabel matches a resource with the specified label
func MatchLabel(key, value string) (labels.Selector, error) {
req, err := labels.NewRequirement(key, selection.In, []string{value})
if err != nil {
return nil, trace.Wrap(err, "failed to build a requirement from %v=%q", key, value)
}
selector := labels.NewSelector()
selector = selector.Add(*req)
return selector, nil
}
// ConfigMap describes matching and sending updates for ConfigMaps.
// If Match matches a resource, RecvCh channel receives
// the data from the matched resource
type ConfigMap struct {
// Selector specifies the selector for this ConfigMap
Selector labels.Selector
// RecvCh specifies the channel that receives updates on the matched resource
RecvCh chan ConfigMapUpdate
}
// Secret describes matching and sending updates for Secrets.
// If Match matches a resource, RecvCh channel receives
// the data from the matched resource
type Secret struct {
// Selector specifies the selector for this Secret
Selector labels.Selector
// RecvCh specifies the channel that receives updates on the matched resource
RecvCh chan SecretUpdate
}
// ConfigMapUpdate describes a ConfigMap update
type ConfigMapUpdate struct {
// ResourceUpdate describes the common resource update metadata
ResourceUpdate
// Data descrines the update data payload
Data map[string]string
}
// SecretUpdate describes a Secret update
type SecretUpdate struct {
// ResourceUpdate describes the common resource update metadata
ResourceUpdate
// Data descrines the update data payload
Data map[string][]byte
}
// Meta formats the metadata for readability
func (r ResourceUpdate) Meta() string {
return fmt.Sprintf("%v(%v/%v)", r.Kind, r.Namespace, r.Name)
}
// String formats this update for readability
func (r ResourceUpdate) String() string {
return fmt.Sprintf("%v(%v)", r.EventType, r.Meta())
}
// ResourceUpdate describes an update for a resource
type ResourceUpdate struct {
// EventType specifies the type of event
watch.EventType
// TypeMeta references the resource type metadata
metav1.TypeMeta
// ObjectMeta references the resource metadata
metav1.ObjectMeta
}
func watchConfigMap(ctx context.Context, client corev1.ConfigMapInterface, config ConfigMap) error {
watcher, err := client.Watch(ctx, metav1.ListOptions{LabelSelector: config.Selector.String()})
if err != nil {
return trace.Wrap(err)
}
defer watcher.Stop()
log := log.WithFields(log.Fields{"watch": "configmap", "label": config.Selector.String()})
for {
select {
case event, ok := <-watcher.ResultChan():
if !ok {
return trace.Retry(nil, "configmap watcher closed")
}
switch configMap := event.Object.(type) {
case *v1.ConfigMap:
log.Infof("Detected event %v for configmap %v.", event.Type, configMap.Name)
config.RecvCh <- ConfigMapUpdate{
ResourceUpdate{event.Type, configMap.TypeMeta, configMap.ObjectMeta},
configMap.Data,
}
}
case <-ctx.Done():
return nil
}
}
}
func watchSecret(ctx context.Context, client corev1.SecretInterface, config Secret) error {
watcher, err := client.Watch(ctx, metav1.ListOptions{LabelSelector: config.Selector.String()})
if err != nil {
return trace.Wrap(err)
}
defer watcher.Stop()
log := log.WithFields(log.Fields{"watch": "secret", "label": config.Selector.String()})
for {
select {
case event, ok := <-watcher.ResultChan():
if !ok {
return trace.Retry(nil, "secret watcher closed")
}
switch secret := event.Object.(type) {
case *v1.Secret:
log.Infof("Detected event %v for secret %v.", event.Type, secret.Name)
config.RecvCh <- SecretUpdate{
ResourceUpdate{event.Type, secret.TypeMeta, secret.ObjectMeta},
secret.Data,
}
}
case <-ctx.Done():
return nil
}
}
}
func retry(ctx context.Context, fn func() error) (err error) {
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 0
err = backoff.RetryNotify(
func() error {
return trace.Wrap(fn())
},
backoff.WithContext(b, ctx),
func(err error, d time.Duration) {
log.Debugf("retrying: %v (time %v)", trace.DebugReport(err), d)
})
return trace.Wrap(err)
}