-
Notifications
You must be signed in to change notification settings - Fork 148
/
reflector.go
424 lines (378 loc) · 13.8 KB
/
reflector.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2023 The PipeCD Authors.
//
// 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"
"go.uber.org/zap"
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/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"github.com/pipe-cd/pipecd/pkg/app/piped/livestatestore/kubernetes/kubernetesmetrics"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/kubernetes"
"github.com/pipe-cd/pipecd/pkg/config"
)
var (
// This is the default whitelist of resources that should be watched.
// User can add/remove other resources to be watched in piped config at cloud provider part.
groupWhitelist = map[string]struct{}{
"": {},
"apps": {},
"extensions": {},
"batch": {},
"storage.k8s.io": {},
"autoscaling": {},
"networking.k8s.io": {},
"apiextensions.k8s.io": {},
"rbac.authorization.k8s.io": {},
"policy": {},
"apiregistration.k8s.io": {},
"authorization.k8s.io": {},
}
versionWhitelist = map[string]struct{}{
"v1": {},
"v1beta1": {},
"v1beta2": {},
}
kindWhitelist = map[string]struct{}{
"Service": {},
"Endpoints": {},
"Deployment": {},
"DaemonSet": {},
"StatefulSet": {},
"ReplicationController": {},
"ReplicaSet": {},
"Pod": {},
"Job": {},
"CronJob": {},
"ConfigMap": {},
"Secret": {},
"Ingress": {},
"NetworkPolicy": {},
"StorageClass": {},
"PersistentVolume": {},
"PersistentVolumeClaim": {},
"HorizontalPodAutoscaler": {},
"ServiceAccount": {},
"Role": {},
"RoleBinding": {},
"ClusterRole": {},
"ClusterRoleBinding": {},
"CustomResourceDefinition": {},
"PodDisruptionBudget": {},
"PodSecurityPolicy": {},
"APIService": {},
"LocalSubjectAccessReview": {},
"SelfSubjectAccessReview": {},
"SelfSubjectRulesReview": {},
"SubjectAccessReview": {},
"ResourceQuota": {},
"PodTemplate": {},
"IngressClass": {},
"Namespace": {},
}
ignoreResourceKeys = map[string]struct{}{
"v1:Service:default:kubernetes": {},
"v1:Service:kube-system:heapster": {},
"v1:Service:kube-system:metrics-server": {},
"v1:Service:kube-system:kube-dns": {},
"v1:Service:kube-system:kubernetes-dashboard": {},
"v1:Service:kube-system:default-http-backend": {},
"apps/v1:Deployment:kube-system:kube-dns": {},
"apps/v1:Deployment:kube-system:kube-dns-autoscaler": {},
"apps/v1:Deployment:kube-system:fluentd-gcp-scaler": {},
"apps/v1:Deployment:kube-system:kubernetes-dashboard": {},
"apps/v1:Deployment:kube-system:l7-default-backend": {},
"apps/v1:Deployment:kube-system:heapster-gke": {},
"apps/v1:Deployment:kube-system:stackdriver-metadata-agent-cluster-level": {},
"extensions/v1beta1:Deployment:kube-system:kube-dns": {},
"extensions/v1beta1:Deployment:kube-system:kube-dns-autoscaler": {},
"extensions/v1beta1:Deployment:kube-system:fluentd-gcp-scaler": {},
"extensions/v1beta1:Deployment:kube-system:kubernetes-dashboard": {},
"extensions/v1beta1:Deployment:kube-system:l7-default-backend": {},
"extensions/v1beta1:Deployment:kube-system:heapster-gke": {},
"extensions/v1beta1:Deployment:kube-system:stackdriver-metadata-agent-cluster-level": {},
"v1:Endpoints:kube-system:kube-controller-manager": {},
"v1:Endpoints:kube-system:kube-scheduler": {},
"v1:Endpoints:kube-system:vpa-recommender": {},
"v1:Endpoints:kube-system:gcp-controller-manager": {},
"v1:Endpoints:kube-system:managed-certificate-controller": {},
"v1:Endpoints:kube-system:cluster-autoscaler": {},
"v1:ConfigMap:kube-system:cluster-kubestore": {},
"v1:ConfigMap:kube-system:ingress-gce-lock": {},
"v1:ConfigMap:kube-system:gke-common-webhook-lock": {},
"v1:ConfigMap:kube-system:cluster-autoscaler-status": {},
"rbac.authorization.k8s.io/v1:ClusterRole::system:managed-certificate-controller": {},
"rbac.authorization.k8s.io/v1:ClusterRoleBinding::system:managed-certificate-controller": {},
}
)
// reflector watches the live state of application with the cluster
// and triggers the specified callbacks.
type reflector struct {
config *config.PlatformProviderKubernetesConfig
kubeConfig *restclient.Config
pipedConfig *config.PipedSpec
onAdd func(obj *unstructured.Unstructured)
onUpdate func(oldObj, obj *unstructured.Unstructured)
onDelete func(obj *unstructured.Unstructured)
watchingResourceKinds []provider.APIVersionKind
stopCh chan struct{}
logger *zap.Logger
}
func (r *reflector) start(ctx context.Context) error {
matcher := newResourceMatcher(r.config.AppStateInformer)
// Use discovery to discover APIs supported by the Kubernetes API server.
// This should be run periodically with a low rate because the APIs are not added frequently.
// https://godoc.org/k8s.io/client-go/discovery
discoveryClient, err := discovery.NewDiscoveryClientForConfig(r.kubeConfig)
if err != nil {
return fmt.Errorf("failed to create discovery client: %v", err)
}
groupResources, err := discoveryClient.ServerPreferredResources()
if err != nil {
return fmt.Errorf("failed to fetch preferred resources: %v", err)
}
r.logger.Info(fmt.Sprintf("successfully preferred resources that contains for %d groups", len(groupResources)))
// Filter above APIResources.
var (
targetResources = make([]schema.GroupVersionResource, 0)
namespacedTargetResources = make([]schema.GroupVersionResource, 0)
)
for _, gr := range groupResources {
for _, resource := range gr.APIResources {
gvk := schema.FromAPIVersionAndKind(gr.GroupVersion, resource.Kind)
if !matcher.Match(gvk) {
r.logger.Info(fmt.Sprintf("skip watching %v because of not matching the configured list", gvk))
continue
}
if !isSupportedList(resource) || !isSupportedWatch(resource) {
r.logger.Info(fmt.Sprintf("skip watching %v because of not supporting watch or list verb", gvk))
continue
}
gv := gvk.GroupVersion()
r.watchingResourceKinds = append(r.watchingResourceKinds, provider.APIVersionKind{
APIVersion: gv.String(),
Kind: gvk.Kind,
})
target := gv.WithResource(resource.Name)
if resource.Namespaced {
namespacedTargetResources = append(namespacedTargetResources, target)
} else {
targetResources = append(targetResources, target)
}
}
}
r.logger.Info("filtered target resources",
zap.Any("targetResources", targetResources),
zap.Any("namespacedTargetResources", namespacedTargetResources),
)
// Use dynamic to perform generic operations on arbitrary Kubernets API objects.
// https://godoc.org/k8s.io/client-go/dynamic
dynamicClient, err := dynamic.NewForConfig(r.kubeConfig)
if err != nil {
return fmt.Errorf("failed to create dynamic client: %v", err)
}
stopCh := make(chan struct{})
startInformer := func(namespace string, resources []schema.GroupVersionResource) {
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicClient, 30*time.Minute, namespace, nil)
for _, tr := range resources {
di := factory.ForResource(tr).Informer()
di.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: r.onObjectAdd,
UpdateFunc: r.onObjectUpdate,
DeleteFunc: r.onObjectDelete,
})
go di.Run(r.stopCh)
if cache.WaitForCacheSync(stopCh, di.HasSynced) {
r.logger.Info(fmt.Sprintf("informer cache for %v has been synced", tr))
} else {
// TODO: Handle the case informer cache has not been synced correctly.
r.logger.Info(fmt.Sprintf("informer cache for %v has not been synced correctly", tr))
}
}
}
ns := r.config.AppStateInformer.Namespace
if ns == "" {
ns = metav1.NamespaceAll
}
r.logger.Info(fmt.Sprintf("start running %d namespaced-resource informers", len(namespacedTargetResources)))
startInformer(ns, namespacedTargetResources)
if ns == metav1.NamespaceAll {
r.logger.Info(fmt.Sprintf("start running %d non-namespaced-resource informers", len(targetResources)))
startInformer(metav1.NamespaceAll, targetResources)
}
r.logger.Info("all informer caches have been synced")
return nil
}
func (r *reflector) onObjectAdd(obj interface{}) {
u := obj.(*unstructured.Unstructured)
key := provider.MakeResourceKey(u)
// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventAdd,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventAdd,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
r.logger.Debug(fmt.Sprintf("received add event for %s", key.String()))
r.onAdd(u)
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventAdd,
kubernetesmetrics.LabelEventHandled,
)
}
func (r *reflector) onObjectUpdate(oldObj, obj interface{}) {
u := obj.(*unstructured.Unstructured)
oldU := oldObj.(*unstructured.Unstructured)
// Ignore all predefined ones.
key := provider.MakeResourceKey(u)
if _, ok := ignoreResourceKeys[key.String()]; ok {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventUpdate,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventUpdate,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
r.logger.Debug(fmt.Sprintf("received update event for %s", key.String()))
r.onUpdate(oldU, u)
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventUpdate,
kubernetesmetrics.LabelEventHandled,
)
}
func (r *reflector) onObjectDelete(obj interface{}) {
u := obj.(*unstructured.Unstructured)
key := provider.MakeResourceKey(u)
// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventDelete,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventDelete,
kubernetesmetrics.LabelEventNotYetHandled,
)
return
}
r.logger.Debug(fmt.Sprintf("received delete event for %s", key.String()))
r.onDelete(u)
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventDelete,
kubernetesmetrics.LabelEventHandled,
)
}
func isSupportedWatch(r metav1.APIResource) bool {
for _, v := range r.Verbs {
if v == "watch" {
return true
}
}
return false
}
func isSupportedList(r metav1.APIResource) bool {
for _, v := range r.Verbs {
if v == "list" {
return true
}
}
return false
}
type resourceMatcher struct {
includes map[string]struct{}
excludes map[string]struct{}
}
func newResourceMatcher(cfg config.KubernetesAppStateInformer) *resourceMatcher {
r := &resourceMatcher{
includes: make(map[string]struct{}, len(cfg.IncludeResources)),
excludes: make(map[string]struct{}, len(cfg.ExcludeResources)),
}
for _, m := range cfg.IncludeResources {
if m.Kind == "" {
r.includes[m.APIVersion] = struct{}{}
} else {
r.includes[m.APIVersion+":"+m.Kind] = struct{}{}
}
}
for _, m := range cfg.ExcludeResources {
if m.Kind == "" {
r.excludes[m.APIVersion] = struct{}{}
} else {
r.excludes[m.APIVersion+":"+m.Kind] = struct{}{}
}
}
return r
}
func (m *resourceMatcher) Match(gvk schema.GroupVersionKind) bool {
var (
gv = gvk.GroupVersion()
apiVersion = gv.String()
key = apiVersion + ":" + gvk.Kind
)
// Any resource matches the specified ExcludeResources will be ignored.
if _, ok := m.excludes[apiVersion]; ok {
return false
}
if _, ok := m.excludes[key]; ok {
return false
}
// Any resources matches the specified IncludeResources will be included.
if _, ok := m.includes[apiVersion]; ok {
return true
}
if _, ok := m.includes[key]; ok {
return true
}
// Check the predefined list.
if _, ok := kindWhitelist[gvk.Kind]; !ok {
return false
}
if _, ok := groupWhitelist[gv.Group]; !ok {
return false
}
if _, ok := versionWhitelist[gv.Version]; !ok {
return false
}
return true
}