-
Notifications
You must be signed in to change notification settings - Fork 18
/
resource_watcher.go
227 lines (181 loc) · 7.11 KB
/
resource_watcher.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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 watcher provides an interface that allows clients to watch for
// change events on Kubernetes resources.
package watcher
import (
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/syncer"
"github.com/submariner-io/admiral/pkg/util"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
type Interface interface {
Start(stopCh <-chan struct{}) error
ListResources(ofType runtime.Object, bySelector labels.Selector) []runtime.Object
}
// EventHandler can handle notifications of events that happen to a resource. The bool return value from each event
// notification method indicates whether or not the resource should be re-queued to be retried later, typically in case
// of an error.
type EventHandler interface {
OnCreate(obj runtime.Object, numRequeues int) bool
OnUpdate(obj runtime.Object, numRequeues int) bool
OnDelete(obj runtime.Object, numRequeues int) bool
}
// EventHandlerFuncs is an adaptor to let you easily specify as many or as few of the notification functions as you want
// while still implementing EventHandler.
type EventHandlerFuncs struct {
OnCreateFunc func(obj runtime.Object, numRequeues int) bool
OnUpdateFunc func(obj runtime.Object, numRequeues int) bool
OnDeleteFunc func(obj runtime.Object, numRequeues int) bool
}
type ResourceConfig struct {
// Name of this watcher used for logging.
Name string
// ResourceType the types of the resources to watch.
ResourceType runtime.Object
// Handler that is notified of create, update, and delete events.
Handler EventHandler
// ResourcesEquivalent function to compare two resources for equivalence. This is invoked on an update notification
// to compare the old and new resources. If true is returned, the update is ignored, otherwise the update is processed.
// By default all updates are processed.
ResourcesEquivalent syncer.ResourceEquivalenceFunc
// ShouldProcess function invoked to determine if a resource should be processed.
ShouldProcess syncer.ShouldProcessFunc
// SourceNamespace the namespace of the resources to watch.
SourceNamespace string
// SourceLabelSelector optional selector to restrict the resources to watch by their labels.
SourceLabelSelector string
// SourceFieldSelector optional selector to restrict the resources to watch by their fields.
SourceFieldSelector string
}
type Config struct {
// RestConfig the REST config used to access the resources to watch.
RestConfig *rest.Config
// RestMapper used to obtain GroupVersionResources. This is optional and is provided for unit testing in lieu of the
// RestConfig. If not specified, one is created from the RestConfig.
RestMapper meta.RESTMapper
// Client used to access the resources to watch. This is optional and is provided for unit testing in lieu
// of the RestConfig. If not specified, one is created from the RestConfig.
Client dynamic.Interface
// WaitForCacheSync if true, waits for the informer cache to sync on Start. Default is true.
WaitForCacheSync *bool
// Scheme used to convert resource objects. By default the global k8s Scheme is used.
Scheme *runtime.Scheme
// ResyncPeriod if non-zero, the period at which resources will be re-synced regardless if anything changed. Default is 0.
ResyncPeriod time.Duration
// ResourceConfigs the configurations for the resources to watch.
ResourceConfigs []ResourceConfig
}
type resourceWatcher struct {
syncers map[reflect.Type]syncer.Interface
}
func New(config *Config) (Interface, error) {
var err error
if len(config.ResourceConfigs) == 0 {
return nil, fmt.Errorf("no resources to watch")
}
restMapper := config.RestMapper
if restMapper == nil {
if restMapper, err = util.BuildRestMapper(config.RestConfig); err != nil {
return nil, errors.Wrap(err, "error building the REST mapper")
}
}
client := config.Client
if client == nil {
if client, err = dynamic.NewForConfig(config.RestConfig); err != nil {
return nil, errors.Wrap(err, "error creating dynamic client")
}
}
watcher := &resourceWatcher{syncers: make(map[reflect.Type]syncer.Interface)}
for _, rc := range config.ResourceConfigs {
handler := rc.Handler
s, err := syncer.NewResourceSyncer(&syncer.ResourceSyncerConfig{
Name: rc.Name,
SourceClient: client,
SourceNamespace: rc.SourceNamespace,
SourceLabelSelector: rc.SourceLabelSelector,
SourceFieldSelector: rc.SourceFieldSelector,
Direction: syncer.RemoteToLocal,
RestMapper: restMapper,
Federator: federate.NewNoopFederator(),
ResourceType: rc.ResourceType,
Transform: func(obj runtime.Object, numRequeues int, op syncer.Operation) (runtime.Object, bool) {
switch op {
case syncer.Create:
return nil, handler.OnCreate(obj, numRequeues)
case syncer.Update:
return nil, handler.OnUpdate(obj, numRequeues)
case syncer.Delete:
return nil, handler.OnDelete(obj, numRequeues)
}
return nil, false
},
ResourcesEquivalent: rc.ResourcesEquivalent,
ShouldProcess: rc.ShouldProcess,
WaitForCacheSync: config.WaitForCacheSync,
Scheme: config.Scheme,
ResyncPeriod: config.ResyncPeriod,
})
if err != nil {
return nil, errors.Wrapf(err, "error creating resource syncer %q", rc.Name)
}
watcher.syncers[reflect.TypeOf(rc.ResourceType)] = s
}
return watcher, nil
}
func (r *resourceWatcher) Start(stopCh <-chan struct{}) error {
for _, syncer := range r.syncers {
err := syncer.Start(stopCh)
if err != nil {
return err //nolint:wrapcheck // OK to return the error as is.
}
}
return nil
}
func (r *resourceWatcher) ListResources(ofType runtime.Object, bySelector labels.Selector) []runtime.Object {
s, found := r.syncers[reflect.TypeOf(ofType)]
if !found {
panic(fmt.Errorf("no Syncer found for %#v", ofType))
}
if bySelector == nil {
bySelector = labels.Everything()
}
return s.ListResourcesBySelector(bySelector)
}
func (r EventHandlerFuncs) OnCreate(obj runtime.Object, numRequeues int) bool {
if r.OnCreateFunc != nil {
return r.OnCreateFunc(obj, numRequeues)
}
return false
}
func (r EventHandlerFuncs) OnUpdate(obj runtime.Object, numRequeues int) bool {
if r.OnUpdateFunc != nil {
return r.OnUpdateFunc(obj, numRequeues)
}
return false
}
func (r EventHandlerFuncs) OnDelete(obj runtime.Object, numRequeues int) bool {
if r.OnDeleteFunc != nil {
return r.OnDeleteFunc(obj, numRequeues)
}
return false
}