-
Notifications
You must be signed in to change notification settings - Fork 127
/
naming_controller.go
381 lines (320 loc) · 13.1 KB
/
naming_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
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
/*
Copyright 2017 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 status
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"k8s.io/klog/v2"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
apiextensionshelpers "k8s.io/apiextensions-apiserver/pkg/apihelpers"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
informers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1"
listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
)
// This controller is reserving names. To avoid conflicts, be sure to run only one instance of the worker at a time.
// This could eventually be lifted, but starting simple.
type NamingConditionController struct {
crdClient client.CustomResourceDefinitionsGetter
crdLister listers.CustomResourceDefinitionLister
crdSynced cache.InformerSynced
// crdMutationCache backs our lister and keeps track of committed updates to avoid racy
// write/lookup cycles. It's got 100 slots by default, so it unlikely to overrun
// TODO to revisit this if naming conflicts are found to occur in the wild
crdMutationCache cache.MutationCache
// To allow injection for testing.
syncFn func(key string) error
queue workqueue.RateLimitingInterface
}
func NewNamingConditionController(
crdInformer informers.CustomResourceDefinitionInformer,
crdClient client.CustomResourceDefinitionsGetter,
) *NamingConditionController {
c := &NamingConditionController{
crdClient: crdClient,
crdLister: crdInformer.Lister(),
crdSynced: crdInformer.Informer().HasSynced,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "crd_naming_condition_controller"),
}
informerIndexer := crdInformer.Informer().GetIndexer()
c.crdMutationCache = cache.NewIntegerResourceVersionMutationCache(informerIndexer, informerIndexer, 60*time.Second, false)
crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.addCustomResourceDefinition,
UpdateFunc: c.updateCustomResourceDefinition,
DeleteFunc: c.deleteCustomResourceDefinition,
})
c.syncFn = c.sync
return c
}
func (c *NamingConditionController) getAcceptedNamesForGroup(group string) (allResources sets.String, allKinds sets.String) {
allResources = sets.String{}
allKinds = sets.String{}
list, err := c.crdLister.List(labels.Everything())
if err != nil {
panic(err)
}
for _, curr := range list {
if curr.Spec.Group != group {
continue
}
// for each item here, see if we have a mutation cache entry that is more recent
// this makes sure that if we tight loop on update and run, our mutation cache will show
// us the version of the objects we just updated to.
item := curr
obj, exists, err := c.crdMutationCache.GetByKey(curr.Name)
if exists && err == nil {
item = obj.(*apiextensionsv1.CustomResourceDefinition)
}
allResources.Insert(item.Status.AcceptedNames.Plural)
allResources.Insert(item.Status.AcceptedNames.Singular)
allResources.Insert(item.Status.AcceptedNames.ShortNames...)
allKinds.Insert(item.Status.AcceptedNames.Kind)
allKinds.Insert(item.Status.AcceptedNames.ListKind)
}
return allResources, allKinds
}
func (c *NamingConditionController) calculateNamesAndConditions(in *apiextensionsv1.CustomResourceDefinition) (apiextensionsv1.CustomResourceDefinitionNames, apiextensionsv1.CustomResourceDefinitionCondition, apiextensionsv1.CustomResourceDefinitionCondition) {
// Get the names that have already been claimed
allResources, allKinds := c.getAcceptedNamesForGroup(in.Spec.Group)
namesAcceptedCondition := apiextensionsv1.CustomResourceDefinitionCondition{
Type: apiextensionsv1.NamesAccepted,
Status: apiextensionsv1.ConditionUnknown,
}
requestedNames := in.Spec.Names
acceptedNames := in.Status.AcceptedNames
newNames := in.Status.AcceptedNames
// Check each name for mismatches. If there's a mismatch between spec and status, then try to deconflict.
// Continue on errors so that the status is the best match possible
if err := equalToAcceptedOrFresh(requestedNames.Plural, acceptedNames.Plural, allResources); err != nil {
namesAcceptedCondition.Status = apiextensionsv1.ConditionFalse
namesAcceptedCondition.Reason = "PluralConflict"
namesAcceptedCondition.Message = err.Error()
} else {
newNames.Plural = requestedNames.Plural
}
if err := equalToAcceptedOrFresh(requestedNames.Singular, acceptedNames.Singular, allResources); err != nil {
namesAcceptedCondition.Status = apiextensionsv1.ConditionFalse
namesAcceptedCondition.Reason = "SingularConflict"
namesAcceptedCondition.Message = err.Error()
} else {
newNames.Singular = requestedNames.Singular
}
if !reflect.DeepEqual(requestedNames.ShortNames, acceptedNames.ShortNames) {
errs := []error{}
existingShortNames := sets.NewString(acceptedNames.ShortNames...)
for _, shortName := range requestedNames.ShortNames {
// if the shortname is already ours, then we're fine
if existingShortNames.Has(shortName) {
continue
}
if err := equalToAcceptedOrFresh(shortName, "", allResources); err != nil {
errs = append(errs, err)
}
}
if err := utilerrors.NewAggregate(errs); err != nil {
namesAcceptedCondition.Status = apiextensionsv1.ConditionFalse
namesAcceptedCondition.Reason = "ShortNamesConflict"
namesAcceptedCondition.Message = err.Error()
} else {
newNames.ShortNames = requestedNames.ShortNames
}
}
if err := equalToAcceptedOrFresh(requestedNames.Kind, acceptedNames.Kind, allKinds); err != nil {
namesAcceptedCondition.Status = apiextensionsv1.ConditionFalse
namesAcceptedCondition.Reason = "KindConflict"
namesAcceptedCondition.Message = err.Error()
} else {
newNames.Kind = requestedNames.Kind
}
if err := equalToAcceptedOrFresh(requestedNames.ListKind, acceptedNames.ListKind, allKinds); err != nil {
namesAcceptedCondition.Status = apiextensionsv1.ConditionFalse
namesAcceptedCondition.Reason = "ListKindConflict"
namesAcceptedCondition.Message = err.Error()
} else {
newNames.ListKind = requestedNames.ListKind
}
newNames.Categories = requestedNames.Categories
// if we haven't changed the condition, then our names must be good.
if namesAcceptedCondition.Status == apiextensionsv1.ConditionUnknown {
namesAcceptedCondition.Status = apiextensionsv1.ConditionTrue
namesAcceptedCondition.Reason = "NoConflicts"
namesAcceptedCondition.Message = "no conflicts found"
}
// set EstablishedCondition initially to false, then set it to true in establishing controller.
// The Establishing Controller will see the NamesAccepted condition when it arrives through the shared informer.
// At that time the API endpoint handler will serve the endpoint, avoiding a race
// which we had if we set Established to true here.
establishedCondition := apiextensionsv1.CustomResourceDefinitionCondition{
Type: apiextensionsv1.Established,
Status: apiextensionsv1.ConditionFalse,
Reason: "NotAccepted",
Message: "not all names are accepted",
}
if old := apiextensionshelpers.FindCRDCondition(in, apiextensionsv1.Established); old != nil {
establishedCondition = *old
}
if establishedCondition.Status != apiextensionsv1.ConditionTrue && namesAcceptedCondition.Status == apiextensionsv1.ConditionTrue {
establishedCondition = apiextensionsv1.CustomResourceDefinitionCondition{
Type: apiextensionsv1.Established,
Status: apiextensionsv1.ConditionFalse,
Reason: "Installing",
Message: "the initial names have been accepted",
}
}
return newNames, namesAcceptedCondition, establishedCondition
}
func equalToAcceptedOrFresh(requestedName, acceptedName string, usedNames sets.String) error {
if requestedName == acceptedName {
return nil
}
if !usedNames.Has(requestedName) {
return nil
}
return fmt.Errorf("%q is already in use", requestedName)
}
func (c *NamingConditionController) sync(key string) error {
inCustomResourceDefinition, err := c.crdLister.Get(key)
if apierrors.IsNotFound(err) {
// CRD was deleted and has freed its names.
// Reconsider all other CRDs in the same group.
if err := c.requeueAllOtherGroupCRDs(key); err != nil {
return err
}
return nil
}
if err != nil {
return err
}
// Skip checking names if Spec and Status names are same.
if equality.Semantic.DeepEqual(inCustomResourceDefinition.Spec.Names, inCustomResourceDefinition.Status.AcceptedNames) {
return nil
}
acceptedNames, namingCondition, establishedCondition := c.calculateNamesAndConditions(inCustomResourceDefinition)
// nothing to do if accepted names and NamesAccepted condition didn't change
if reflect.DeepEqual(inCustomResourceDefinition.Status.AcceptedNames, acceptedNames) &&
apiextensionshelpers.IsCRDConditionEquivalent(&namingCondition, apiextensionshelpers.FindCRDCondition(inCustomResourceDefinition, apiextensionsv1.NamesAccepted)) {
return nil
}
crd := inCustomResourceDefinition.DeepCopy()
crd.Status.AcceptedNames = acceptedNames
apiextensionshelpers.SetCRDCondition(crd, namingCondition)
apiextensionshelpers.SetCRDCondition(crd, establishedCondition)
updatedObj, err := c.crdClient.CustomResourceDefinitions().UpdateStatus(context.TODO(), crd, metav1.UpdateOptions{})
if apierrors.IsNotFound(err) || apierrors.IsConflict(err) {
// deleted or changed in the meantime, we'll get called again
return nil
}
if err != nil {
return err
}
// if the update was successful, go ahead and add the entry to the mutation cache
c.crdMutationCache.Mutation(updatedObj)
// we updated our status, so we may be releasing a name. When this happens, we need to rekick everything in our group
// if we fail to rekick, just return as normal. We'll get everything on a resync
if err := c.requeueAllOtherGroupCRDs(key); err != nil {
return err
}
return nil
}
func (c *NamingConditionController) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Info("Starting NamingConditionController")
defer klog.Info("Shutting down NamingConditionController")
if !cache.WaitForCacheSync(stopCh, c.crdSynced) {
return
}
// only start one worker thread since its a slow moving API and the naming conflict resolution bits aren't thread-safe
go wait.Until(c.runWorker, time.Second, stopCh)
<-stopCh
}
func (c *NamingConditionController) runWorker() {
for c.processNextWorkItem() {
}
}
// processNextWorkItem deals with one key off the queue. It returns false when it's time to quit.
func (c *NamingConditionController) processNextWorkItem() bool {
key, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(key)
err := c.syncFn(key.(string))
if err == nil {
c.queue.Forget(key)
return true
}
utilruntime.HandleError(fmt.Errorf("%v failed with: %v", key, err))
c.queue.AddRateLimited(key)
return true
}
func (c *NamingConditionController) enqueue(obj *apiextensionsv1.CustomResourceDefinition) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", obj, err))
return
}
c.queue.Add(key)
}
func (c *NamingConditionController) addCustomResourceDefinition(obj interface{}) {
castObj := obj.(*apiextensionsv1.CustomResourceDefinition)
klog.V(4).Infof("Adding %s", castObj.Name)
c.enqueue(castObj)
}
func (c *NamingConditionController) updateCustomResourceDefinition(obj, _ interface{}) {
castObj := obj.(*apiextensionsv1.CustomResourceDefinition)
klog.V(4).Infof("Updating %s", castObj.Name)
c.enqueue(castObj)
}
func (c *NamingConditionController) deleteCustomResourceDefinition(obj interface{}) {
castObj, ok := obj.(*apiextensionsv1.CustomResourceDefinition)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
klog.Errorf("Couldn't get object from tombstone %#v", obj)
return
}
castObj, ok = tombstone.Obj.(*apiextensionsv1.CustomResourceDefinition)
if !ok {
klog.Errorf("Tombstone contained object that is not expected %#v", obj)
return
}
}
klog.V(4).Infof("Deleting %q", castObj.Name)
c.enqueue(castObj)
}
func (c *NamingConditionController) requeueAllOtherGroupCRDs(name string) error {
pluralGroup := strings.SplitN(name, ".", 2)
list, err := c.crdLister.List(labels.Everything())
if err != nil {
return err
}
for _, curr := range list {
if curr.Spec.Group == pluralGroup[1] && curr.Name != name {
c.queue.Add(curr.Name)
}
}
return nil
}