-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgc_controller.go
286 lines (248 loc) · 9.78 KB
/
gc_controller.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
/*
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 storageversiongc
import (
"context"
"fmt"
"time"
apiserverinternalv1alpha1 "k8s.io/api/apiserverinternal/v1alpha1"
coordinationv1 "k8s.io/api/coordination/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/storageversion"
apiserverinternalinformers "k8s.io/client-go/informers/apiserverinternal/v1alpha1"
coordinformers "k8s.io/client-go/informers/coordination/v1"
"k8s.io/client-go/kubernetes"
coordlisters "k8s.io/client-go/listers/coordination/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/kubernetes/pkg/controlplane"
"k8s.io/klog/v2"
)
// Controller watches kube-apiserver leases and storageversions, and delete stale
// storage version entries and objects.
type Controller struct {
kubeclientset kubernetes.Interface
leaseLister coordlisters.LeaseLister
leasesSynced cache.InformerSynced
storageVersionSynced cache.InformerSynced
leaseQueue workqueue.RateLimitingInterface
storageVersionQueue workqueue.RateLimitingInterface
}
// NewStorageVersionGC creates a new Controller.
func NewStorageVersionGC(ctx context.Context, clientset kubernetes.Interface, leaseInformer coordinformers.LeaseInformer, storageVersionInformer apiserverinternalinformers.StorageVersionInformer) *Controller {
c := &Controller{
kubeclientset: clientset,
leaseLister: leaseInformer.Lister(),
leasesSynced: leaseInformer.Informer().HasSynced,
storageVersionSynced: storageVersionInformer.Informer().HasSynced,
leaseQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "storage_version_garbage_collector_leases"),
storageVersionQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "storage_version_garbage_collector_storageversions"),
}
logger := klog.FromContext(ctx)
leaseInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: func(obj interface{}) {
c.onDeleteLease(logger, obj)
},
})
// use the default resync period from the informer
storageVersionInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.onAddStorageVersion(logger, obj)
},
UpdateFunc: func(old, newObj interface{}) {
c.onUpdateStorageVersion(logger, old, newObj)
},
})
return c
}
// Run starts one worker.
func (c *Controller) Run(ctx context.Context) {
logger := klog.FromContext(ctx)
defer utilruntime.HandleCrash()
defer c.leaseQueue.ShutDown()
defer c.storageVersionQueue.ShutDown()
defer logger.Info("Shutting down storage version garbage collector")
logger.Info("Starting storage version garbage collector")
if !cache.WaitForCacheSync(ctx.Done(), c.leasesSynced, c.storageVersionSynced) {
utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
return
}
// Identity lease deletion and storageversion update don't happen too often. Start one
// worker for each of them.
// runLeaseWorker handles legit identity lease deletion, while runStorageVersionWorker
// handles storageversion creation/update with non-existing id. The latter should rarely
// happen. It's okay for the two workers to conflict on update.
go wait.UntilWithContext(ctx, c.runLeaseWorker, time.Second)
go wait.UntilWithContext(ctx, c.runStorageVersionWorker, time.Second)
<-ctx.Done()
}
func (c *Controller) runLeaseWorker(ctx context.Context) {
for c.processNextLease(ctx) {
}
}
func (c *Controller) processNextLease(ctx context.Context) bool {
key, quit := c.leaseQueue.Get()
if quit {
return false
}
defer c.leaseQueue.Done(key)
err := c.processDeletedLease(ctx, key.(string))
if err == nil {
c.leaseQueue.Forget(key)
return true
}
utilruntime.HandleError(fmt.Errorf("lease %v failed with: %v", key, err))
c.leaseQueue.AddRateLimited(key)
return true
}
func (c *Controller) runStorageVersionWorker(ctx context.Context) {
for c.processNextStorageVersion(ctx) {
}
}
func (c *Controller) processNextStorageVersion(ctx context.Context) bool {
key, quit := c.storageVersionQueue.Get()
if quit {
return false
}
defer c.storageVersionQueue.Done(key)
err := c.syncStorageVersion(ctx, key.(string))
if err == nil {
c.storageVersionQueue.Forget(key)
return true
}
utilruntime.HandleError(fmt.Errorf("storage version %v failed with: %v", key, err))
c.storageVersionQueue.AddRateLimited(key)
return true
}
func (c *Controller) processDeletedLease(ctx context.Context, name string) error {
_, err := c.kubeclientset.CoordinationV1().Leases(metav1.NamespaceSystem).Get(ctx, name, metav1.GetOptions{})
// the lease isn't deleted, nothing we need to do here
if err == nil {
return nil
}
if !apierrors.IsNotFound(err) {
return err
}
// the frequency of this call won't be too high because we only trigger on identity lease deletions
storageVersionList, err := c.kubeclientset.InternalV1alpha1().StorageVersions().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
var errors []error
for _, sv := range storageVersionList.Items {
var serverStorageVersions []apiserverinternalv1alpha1.ServerStorageVersion
hasStaleRecord := false
for _, ssv := range sv.Status.StorageVersions {
if ssv.APIServerID == name {
hasStaleRecord = true
continue
}
serverStorageVersions = append(serverStorageVersions, ssv)
}
if !hasStaleRecord {
continue
}
if err := c.updateOrDeleteStorageVersion(ctx, &sv, serverStorageVersions); err != nil {
errors = append(errors, err)
}
}
return utilerrors.NewAggregate(errors)
}
func (c *Controller) syncStorageVersion(ctx context.Context, name string) error {
sv, err := c.kubeclientset.InternalV1alpha1().StorageVersions().Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
// The problematic storage version that was added/updated recently is gone.
// Nothing we need to do here.
return nil
}
if err != nil {
return err
}
hasInvalidID := false
var serverStorageVersions []apiserverinternalv1alpha1.ServerStorageVersion
for _, v := range sv.Status.StorageVersions {
lease, err := c.kubeclientset.CoordinationV1().Leases(metav1.NamespaceSystem).Get(ctx, v.APIServerID, metav1.GetOptions{})
if err != nil || lease == nil || lease.Labels == nil ||
lease.Labels[controlplane.IdentityLeaseComponentLabelKey] != controlplane.KubeAPIServer {
// We cannot find a corresponding identity lease from apiserver as well.
// We need to clean up this storage version.
hasInvalidID = true
continue
}
serverStorageVersions = append(serverStorageVersions, v)
}
if !hasInvalidID {
return nil
}
return c.updateOrDeleteStorageVersion(ctx, sv, serverStorageVersions)
}
func (c *Controller) onAddStorageVersion(logger klog.Logger, obj interface{}) {
castObj := obj.(*apiserverinternalv1alpha1.StorageVersion)
c.enqueueStorageVersion(logger, castObj)
}
func (c *Controller) onUpdateStorageVersion(logger klog.Logger, oldObj, newObj interface{}) {
castNewObj := newObj.(*apiserverinternalv1alpha1.StorageVersion)
c.enqueueStorageVersion(logger, castNewObj)
}
// enqueueStorageVersion enqueues the storage version if it has entry for invalid apiserver
func (c *Controller) enqueueStorageVersion(logger klog.Logger, obj *apiserverinternalv1alpha1.StorageVersion) {
for _, sv := range obj.Status.StorageVersions {
lease, err := c.leaseLister.Leases(metav1.NamespaceSystem).Get(sv.APIServerID)
if err != nil || lease == nil || lease.Labels == nil ||
lease.Labels[controlplane.IdentityLeaseComponentLabelKey] != controlplane.KubeAPIServer {
// we cannot find a corresponding identity lease in cache, enqueue the storageversion
logger.V(4).Info("Observed storage version with invalid apiserver entry", "objName", obj.Name)
c.storageVersionQueue.Add(obj.Name)
return
}
}
}
func (c *Controller) onDeleteLease(logger klog.Logger, obj interface{}) {
castObj, ok := obj.(*coordinationv1.Lease)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %#v", obj))
return
}
castObj, ok = tombstone.Obj.(*coordinationv1.Lease)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a Lease %#v", obj))
return
}
}
if castObj.Namespace == metav1.NamespaceSystem &&
castObj.Labels != nil &&
castObj.Labels[controlplane.IdentityLeaseComponentLabelKey] == controlplane.KubeAPIServer {
logger.V(4).Info("Observed lease deleted", "castObjName", castObj.Name)
c.enqueueLease(castObj)
}
}
func (c *Controller) enqueueLease(obj *coordinationv1.Lease) {
c.leaseQueue.Add(obj.Name)
}
func (c *Controller) updateOrDeleteStorageVersion(ctx context.Context, sv *apiserverinternalv1alpha1.StorageVersion, serverStorageVersions []apiserverinternalv1alpha1.ServerStorageVersion) error {
if len(serverStorageVersions) == 0 {
return c.kubeclientset.InternalV1alpha1().StorageVersions().Delete(
ctx, sv.Name, metav1.DeleteOptions{})
}
sv.Status.StorageVersions = serverStorageVersions
storageversion.SetCommonEncodingVersion(sv)
_, err := c.kubeclientset.InternalV1alpha1().StorageVersions().UpdateStatus(
ctx, sv, metav1.UpdateOptions{})
return err
}