-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cluster_cache.go
429 lines (358 loc) · 13.7 KB
/
cluster_cache.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
425
426
427
428
429
/*
Copyright 2020 The Kubernetes 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 remote
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
)
const (
healthCheckPollInterval = 10 * time.Second
healthCheckRequestTimeout = 5 * time.Second
healthCheckUnhealthyThreshold = 10
initialCacheSyncTimeout = 5 * time.Minute
clusterCacheControllerName = "cluster-cache-tracker"
)
// ClusterCacheTracker manages client caches for workload clusters.
type ClusterCacheTracker struct {
log logr.Logger
clientUncachedObjects []client.Object
client client.Client
scheme *runtime.Scheme
lock sync.RWMutex
clusterAccessors map[client.ObjectKey]*clusterAccessor
clusterLock *keyedMutex
indexes []Index
}
// ClusterCacheTrackerOptions defines options to configure
// a ClusterCacheTracker.
type ClusterCacheTrackerOptions struct {
// Log is the logger used throughout the lifecycle of caches.
// Defaults to a no-op logger if it's not set.
Log *logr.Logger
// ClientUncachedObjects instructs the Client to never cache the following objects,
// it'll instead query the API server directly.
// Defaults to never caching ConfigMap and Secret if not set.
ClientUncachedObjects []client.Object
Indexes []Index
}
func setDefaultOptions(opts *ClusterCacheTrackerOptions) {
if opts.Log == nil {
l := logr.New(log.NullLogSink{})
opts.Log = &l
}
if len(opts.ClientUncachedObjects) == 0 {
opts.ClientUncachedObjects = []client.Object{
&corev1.ConfigMap{},
&corev1.Secret{},
}
}
}
// NewClusterCacheTracker creates a new ClusterCacheTracker.
func NewClusterCacheTracker(manager ctrl.Manager, options ClusterCacheTrackerOptions) (*ClusterCacheTracker, error) {
setDefaultOptions(&options)
return &ClusterCacheTracker{
log: *options.Log,
clientUncachedObjects: options.ClientUncachedObjects,
client: manager.GetClient(),
scheme: manager.GetScheme(),
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
clusterLock: newKeyedMutex(),
indexes: options.Indexes,
}, nil
}
// GetClient returns a cached client for the given cluster.
func (t *ClusterCacheTracker) GetClient(ctx context.Context, cluster client.ObjectKey) (client.Client, error) {
accessor, err := t.getClusterAccessor(ctx, cluster, t.indexes...)
if err != nil {
return nil, err
}
return accessor.client, nil
}
// clusterAccessor represents the combination of a delegating client, cache, and watches for a remote cluster.
type clusterAccessor struct {
cache *stoppableCache
client client.Client
watches sets.String
}
// clusterAccessorExists returns true if a clusterAccessor exists for cluster.
func (t *ClusterCacheTracker) clusterAccessorExists(cluster client.ObjectKey) bool {
t.lock.RLock()
defer t.lock.RUnlock()
_, exists := t.clusterAccessors[cluster]
return exists
}
// getClusterAccessor first tries to return an already-created clusterAccessor for cluster, falling back to creating a new clusterAccessor if needed.
func (t *ClusterCacheTracker) getClusterAccessor(ctx context.Context, cluster client.ObjectKey, indexes ...Index) (*clusterAccessor, error) {
log := ctrl.LoggerFrom(ctx, "cluster", cluster.Name)
loadExistingAccessor := func() *clusterAccessor {
t.lock.RLock()
defer t.lock.RUnlock()
return t.clusterAccessors[cluster]
}
storeAccessor := func(a *clusterAccessor) {
t.lock.Lock()
defer t.lock.Unlock()
t.clusterAccessors[cluster] = a
}
// if the accessor exists, return early
a := loadExistingAccessor()
if a != nil {
return a, nil
}
// No cluster exists, we might need to initialize one.
// Lock on the cluster to ensure only one accessor is initialized for the cluster.
unlockCluster := t.clusterLock.Lock(cluster)
defer unlockCluster()
// While we were waiting on the cluster lock, a different goroutine holding the lock might have initialized the accessor
// for this cluster successfully. If this is the case we return it.
a = loadExistingAccessor()
if a != nil {
return a, nil
}
// We are the one who needs to initialize it.
log.V(4).Info("creating new cluster accessor")
a, err := t.newClusterAccessor(ctx, cluster, indexes...)
if err != nil {
log.V(4).Info("error creating new cluster accessor")
return nil, errors.Wrap(err, "error creating client and cache for remote cluster")
}
log.V(4).Info("storing new cluster accessor")
storeAccessor(a)
return a, nil
}
// newClusterAccessor creates a new clusterAccessor.
func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster client.ObjectKey, indexes ...Index) (*clusterAccessor, error) {
// Get a rest config for the remote cluster
config, err := RESTConfig(ctx, clusterCacheControllerName, t.client, cluster)
if err != nil {
return nil, errors.Wrapf(err, "error fetching REST client config for remote cluster %q", cluster.String())
}
// Create a mapper for it
mapper, err := apiutil.NewDynamicRESTMapper(config)
if err != nil {
return nil, errors.Wrapf(err, "error creating dynamic rest mapper for remote cluster %q", cluster.String())
}
// Create the client for the remote cluster
c, err := client.New(config, client.Options{Scheme: t.scheme, Mapper: mapper})
if err != nil {
return nil, errors.Wrapf(err, "error creating client for remote cluster %q", cluster.String())
}
// Create the cache for the remote cluster
cacheOptions := cache.Options{
Scheme: t.scheme,
Mapper: mapper,
}
remoteCache, err := cache.New(config, cacheOptions)
if err != nil {
return nil, errors.Wrapf(err, "error creating cache for remote cluster %q", cluster.String())
}
cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
// We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache.
cache := &stoppableCache{
Cache: remoteCache,
cancelFunc: cacheCtxCancel,
}
for _, index := range indexes {
if err := cache.IndexField(ctx, index.Object, index.Field, index.ExtractValue); err != nil {
return nil, fmt.Errorf("failed to index field %s: %w", index.Field, err)
}
}
// Start the cache!!!
go cache.Start(cacheCtx) //nolint:errcheck
// Wait until the cache is initially synced
cacheSyncCtx, cacheSyncCtxCancel := context.WithTimeout(ctx, initialCacheSyncTimeout)
defer cacheSyncCtxCancel()
if !cache.WaitForCacheSync(cacheSyncCtx) {
cache.Stop()
return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, err)
}
// Start cluster healthcheck!!!
go t.healthCheckCluster(cacheCtx, &healthCheckInput{
cluster: cluster,
cfg: config,
})
delegatingClient, err := client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: cache,
Client: c,
UncachedObjects: t.clientUncachedObjects,
})
if err != nil {
return nil, err
}
return &clusterAccessor{
cache: cache,
client: delegatingClient,
watches: sets.NewString(),
}, nil
}
// deleteAccessor stops a clusterAccessor's cache and removes the clusterAccessor from the tracker.
func (t *ClusterCacheTracker) deleteAccessor(cluster client.ObjectKey) {
t.lock.Lock()
defer t.lock.Unlock()
a, exists := t.clusterAccessors[cluster]
if !exists {
return
}
t.log.V(2).Info("Deleting clusterAccessor", "cluster", cluster.String())
t.log.V(4).Info("Stopping cache", "cluster", cluster.String())
a.cache.Stop()
t.log.V(4).Info("Cache stopped", "cluster", cluster.String())
delete(t.clusterAccessors, cluster)
}
// Watcher is a scoped-down interface from Controller that only knows how to watch.
type Watcher interface {
// Watch watches src for changes, sending events to eventHandler if they pass predicates.
Watch(src source.Source, eventHandler handler.EventHandler, predicates ...predicate.Predicate) error
}
// WatchInput specifies the parameters used to establish a new watch for a remote cluster.
type WatchInput struct {
// Name represents a unique watch request for the specified Cluster.
Name string
// Cluster is the key for the remote cluster.
Cluster client.ObjectKey
// Watcher is the watcher (controller) whose Reconcile() function will be called for events.
Watcher Watcher
// Kind is the type of resource to watch.
Kind client.Object
// EventHandler contains the event handlers to invoke for resource events.
EventHandler handler.EventHandler
// Predicates is used to filter resource events.
Predicates []predicate.Predicate
}
// Watch watches a remote cluster for resource events. If the watch already exists based on input.Name, this is a no-op.
func (t *ClusterCacheTracker) Watch(ctx context.Context, input WatchInput) error {
if input.Name == "" {
return errors.New("input.Name is required")
}
a, err := t.getClusterAccessor(ctx, input.Cluster, t.indexes...)
if err != nil {
return err
}
unlock := t.clusterLock.Lock(input.Cluster)
defer unlock()
if a.watches.Has(input.Name) {
t.log.V(6).Info("Watch already exists", "namespace", input.Cluster.Namespace, "cluster", input.Cluster.Name, "name", input.Name)
return nil
}
// Need to create the watch
if err := input.Watcher.Watch(source.NewKindWithCache(input.Kind, a.cache), input.EventHandler, input.Predicates...); err != nil {
return errors.Wrap(err, "error creating watch")
}
a.watches.Insert(input.Name)
return nil
}
// healthCheckInput provides the input for the healthCheckCluster method.
type healthCheckInput struct {
cluster client.ObjectKey
cfg *rest.Config
interval time.Duration
requestTimeout time.Duration
unhealthyThreshold int
path string
}
// setDefaults sets default values if optional parameters are not set.
func (h *healthCheckInput) setDefaults() {
if h.interval == 0 {
h.interval = healthCheckPollInterval
}
if h.requestTimeout == 0 {
h.requestTimeout = healthCheckRequestTimeout
}
if h.unhealthyThreshold == 0 {
h.unhealthyThreshold = healthCheckUnhealthyThreshold
}
if h.path == "" {
h.path = "/"
}
}
// healthCheckCluster will poll the cluster's API at the path given and, if there are
// `unhealthyThreshold` consecutive failures, will deem the cluster unhealthy.
// Once the cluster is deemed unhealthy, the cluster's cache is stopped and removed.
func (t *ClusterCacheTracker) healthCheckCluster(ctx context.Context, in *healthCheckInput) {
// populate optional params for healthCheckInput
in.setDefaults()
unhealthyCount := 0
// This gets us a client that can make raw http(s) calls to the remote apiserver. We only need to create it once
// and we can reuse it inside the polling loop.
codec := runtime.NoopEncoder{Decoder: scheme.Codecs.UniversalDecoder()}
cfg := rest.CopyConfig(in.cfg)
cfg.NegotiatedSerializer = serializer.NegotiatedSerializerWrapper(runtime.SerializerInfo{Serializer: codec})
restClient, restClientErr := rest.UnversionedRESTClientFor(cfg)
runHealthCheckWithThreshold := func() (bool, error) {
if restClientErr != nil {
return false, restClientErr
}
cluster := &clusterv1.Cluster{}
if err := t.client.Get(ctx, in.cluster, cluster); err != nil {
if apierrors.IsNotFound(err) {
// If the cluster can't be found, we should delete the cache.
return false, err
}
// Otherwise, requeue.
return false, nil
}
if !cluster.Status.InfrastructureReady || !conditions.IsTrue(cluster, clusterv1.ControlPlaneInitializedCondition) {
// If the infrastructure or control plane aren't marked as ready, we should requeue and wait.
return false, nil
}
if !t.clusterAccessorExists(in.cluster) {
// Cache for this cluster has already been cleaned up.
// Nothing to do, so return true.
return true, nil
}
// An error here means there was either an issue connecting or the API returned an error.
// If no error occurs, reset the unhealthy counter.
_, err := restClient.Get().AbsPath(in.path).Timeout(in.requestTimeout).DoRaw(ctx)
if err != nil {
unhealthyCount++
} else {
unhealthyCount = 0
}
if unhealthyCount >= in.unhealthyThreshold {
// Cluster is now considered unhealthy.
return false, err
}
return false, nil
}
err := wait.PollImmediateUntil(in.interval, runHealthCheckWithThreshold, ctx.Done())
// An error returned implies the health check has failed a sufficient number of
// times for the cluster to be considered unhealthy
// NB. we are ignoring ErrWaitTimeout because this error happens when the channel is close, that in this case
// happens when the cache is explicitly stopped.
if err != nil && err != wait.ErrWaitTimeout {
t.log.Error(err, "Error health checking cluster", "cluster", in.cluster.String())
t.deleteAccessor(in.cluster)
}
}