-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathapiextensions.go
607 lines (492 loc) · 20.6 KB
/
apiextensions.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
Copyright 2022 The KCP 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 server
import (
"context"
"fmt"
"mime"
_ "net/http/pprof"
"strings"
"github.com/kcp-dev/apimachinery/pkg/logicalcluster"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionslisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/kcp"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/endpoints/request"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clusters"
"k8s.io/klog/v2"
apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1"
tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
kcpclientset "github.com/kcp-dev/kcp/pkg/client/clientset/versioned"
apislisters "github.com/kcp-dev/kcp/pkg/client/listers/apis/v1alpha1"
tenancylisters "github.com/kcp-dev/kcp/pkg/client/listers/tenancy/v1alpha1"
kcpfeatures "github.com/kcp-dev/kcp/pkg/features"
"github.com/kcp-dev/kcp/pkg/reconciler/apis/apibinding"
"github.com/kcp-dev/kcp/third_party/conditions/util/conditions"
)
// SystemCRDLogicalCluster is the logical cluster we install system CRDs into for now. These are needed
// to start wildcard informers until a "real" workspace gets them installed.
var SystemCRDLogicalCluster = logicalcluster.New("system:system-crds")
type systemCRDProvider struct {
rootCRDs sets.String
orgCRDs sets.String
universalCRDs sets.String
getClusterWorkspace func(key string) (*tenancyv1alpha1.ClusterWorkspace, error)
getCRD func(key string) (*apiextensionsv1.CustomResourceDefinition, error)
}
// NewSystemCRDProvider returns CRDs for certain cluster workspace types and the root workspace.
// TODO(sttts): This must be replaced by some non-hardcoded mechanism in the (near) future, probably by
// using APIBindings. For now, this is our way to enforce to have no schema drift of these CRDs
// as that would break wildcard informers.
func newSystemCRDProvider(
getClusterWorkspace func(key string) (*tenancyv1alpha1.ClusterWorkspace, error),
getCRD func(key string) (*apiextensionsv1.CustomResourceDefinition, error),
) *systemCRDProvider {
p := &systemCRDProvider{
rootCRDs: sets.NewString(
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "clusterworkspaces.tenancy.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "clusterworkspacetypes.tenancy.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "clusterworkspaceshards.tenancy.kcp.dev"),
// the following is installed to get discovery and OpenAPI right. But it is actually
// served by a native rest storage, projecting the clusterworkspaces.
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "workspaces.tenancy.kcp.dev"),
),
orgCRDs: sets.NewString(
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "clusterworkspaces.tenancy.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "clusterworkspacetypes.tenancy.kcp.dev"),
// the following is installed to get discovery and OpenAPI right. But it is actually
// served by a native rest storage, projecting the clusterworkspaces.
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "workspaces.tenancy.kcp.dev"),
),
universalCRDs: sets.NewString(
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "apiresourceimports.apiresource.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "negotiatedapiresources.apiresource.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "workloadclusters.workload.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "apiexports.apis.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "apibindings.apis.kcp.dev"),
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "apiresourceschemas.apis.kcp.dev"),
),
getClusterWorkspace: getClusterWorkspace,
getCRD: getCRD,
}
if utilfeature.DefaultFeatureGate.Enabled(kcpfeatures.LocationAPI) {
p.rootCRDs.Insert(
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "locations.scheduling.kcp.dev"),
)
p.orgCRDs.Insert(
clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "locations.scheduling.kcp.dev"),
)
// the following is installed to get discovery and OpenAPI right. But it is actually
// served by a native rest storage, projecting the locations into this workspace.
p.universalCRDs.Insert(clusters.ToClusterAwareKey(SystemCRDLogicalCluster, "locations.scheduling.kcp.dev"))
}
return p
}
func (p *systemCRDProvider) List(clusterName logicalcluster.LogicalCluster) ([]*apiextensionsv1.CustomResourceDefinition, error) {
var ret []*apiextensionsv1.CustomResourceDefinition
for _, key := range p.Keys(clusterName).List() {
crd, err := p.getCRD(key)
if err != nil {
klog.Errorf("Failed to get CRD %s for %s: %v", key, clusterName, err)
// we shouldn't see this because getCRD is backed by a quorum-read client on cache-miss
return nil, fmt.Errorf("error getting system CRD %q: %w", key, err)
}
ret = append(ret, crd)
}
return ret, nil
}
func (p *systemCRDProvider) Keys(clusterName logicalcluster.LogicalCluster) sets.String {
switch {
case clusterName == tenancyv1alpha1.RootCluster:
return p.rootCRDs
case clusterName.HasPrefix(tenancyv1alpha1.RootCluster):
parent, ws := clusterName.Split()
workspaceKey := clusters.ToClusterAwareKey(parent, ws)
clusterWorkspace, err := p.getClusterWorkspace(workspaceKey)
if err != nil {
// this shouldn't happen. The getters use quorum-read client on cache-miss.
klog.Errorf("error getting cluster workspace %q: %v", workspaceKey, err)
break
}
switch clusterWorkspace.Spec.Type {
case "Universal":
return p.universalCRDs
case "Organization", "Team":
// TODO(sttts): this cannot be hardcoded. There might be other org-like types.y
return p.orgCRDs
}
}
return sets.NewString()
}
// apiBindingAwareCRDLister is a CRD lister combines APIs coming from APIBindings with CRDs in a workspace.
type apiBindingAwareCRDLister struct {
kcpClusterClient kcpclientset.ClusterInterface
crdLister apiextensionslisters.CustomResourceDefinitionLister
workspaceLister tenancylisters.ClusterWorkspaceLister
apiBindingLister apislisters.APIBindingLister
apiBindingIndexer cache.Indexer
apiExportIndexer cache.Indexer
systemCRDProvider *systemCRDProvider
getAPIResourceSchema func(clusterName logicalcluster.LogicalCluster, name string) (*apisv1alpha1.APIResourceSchema, error)
}
var _ kcp.ClusterAwareCRDLister = &apiBindingAwareCRDLister{}
// List lists all CustomResourceDefinitions that come in via APIBindings as well as all in the current
// logical cluster retrieved from the context.
func (c *apiBindingAwareCRDLister) List(ctx context.Context, selector labels.Selector) ([]*apiextensionsv1.CustomResourceDefinition, error) {
clusterName, err := request.ClusterNameFrom(ctx)
if err != nil {
return nil, err
}
crdName := func(crd *apiextensionsv1.CustomResourceDefinition) string {
return crd.Spec.Names.Plural + "." + crd.Spec.Group
}
selectAll := selector.Empty()
matchesSelector := func(crd *apiextensionsv1.CustomResourceDefinition) bool {
if selectAll {
return true
}
return selector.Matches(labels.Set(crd.Labels))
}
// Seen keeps track of which CRDs have already been found from system and apibindings.
seen := sets.NewString()
kcpSystemCRDs, err := c.systemCRDProvider.List(clusterName)
if err != nil {
return nil, fmt.Errorf("error retrieving kcp system CRDs: %w", err)
}
// Priority 1: add system CRDs. These take priority over CRDs from APIBindings and CRDs from the local workspace.
var ret = kcpSystemCRDs
for i := range kcpSystemCRDs {
seen.Insert(crdName(kcpSystemCRDs[i]))
}
apiBindings, err := c.apiBindingLister.List(labels.Everything())
if err != nil {
return nil, err
}
// TODO(sttts): optimize this looping by using an informer index
for _, apiBinding := range apiBindings {
if logicalcluster.From(apiBinding) != clusterName {
continue
}
if !conditions.IsTrue(apiBinding, apisv1alpha1.InitialBindingCompleted) {
continue
}
for _, boundResource := range apiBinding.Status.BoundResources {
crdKey := clusters.ToClusterAwareKey(apibinding.ShadowWorkspaceName, boundResource.Schema.UID)
crd, err := c.crdLister.Get(crdKey)
if err != nil {
klog.Errorf("Error getting bound CRD %q: %v", crdKey, err)
continue
}
if !matchesSelector(crd) {
continue
}
// system CRDs take priority over APIBindings from the local workspace.
if seen.Has(crdName(crd)) {
// Came from system
klog.Infof("Skipping APIBinding CRD %s|%s because it came in via system CRDs", crd.ClusterName, crd.Name)
continue
}
// Priority 2: Add APIBinding CRDs. These take priority over those from the local workspace.
// Add the APIExport identity hash as an annotation to the CRD so the RESTOptionsGetter can assign
// the correct etcd resource prefix.
crd = shallowCopyCRD(crd)
crd.Annotations[apisv1alpha1.AnnotationAPIIdentityKey] = boundResource.Schema.IdentityHash
ret = append(ret, crd)
seen.Insert(crdName(crd))
}
}
// TODO use scoping lister when available
crds, err := c.crdLister.List(selector)
if err != nil {
return nil, err
}
for i := range crds {
crd := crds[i]
if logicalcluster.From(crd) != clusterName {
continue
}
// system CRDs and local APIBindings take priority over CRDs from the local workspace.
if seen.Has(crdName(crd)) {
klog.Infof("Skipping local CRD %s|%s because it came in via APIBindings or system CRDs", crd.ClusterName, crd.Name)
continue
}
// Priority 3: add local workspace CRDs that weren't already coming from APIBindings or kcp system.
ret = append(ret, crd)
}
return ret, nil
}
func isPartialMetadataRequest(ctx context.Context) bool {
if accept := ctx.Value(acceptHeaderContextKey).(string); len(accept) > 0 {
if _, params, err := mime.ParseMediaType(accept); err == nil {
return params["as"] == "PartialObjectMetadata" || params["as"] == "PartialObjectMetadataList"
}
}
return false
}
func (c *apiBindingAwareCRDLister) Refresh(crd *apiextensionsv1.CustomResourceDefinition) (*apiextensionsv1.CustomResourceDefinition, error) {
crdKey := clusters.ToClusterAwareKey(logicalcluster.From(crd), crd.Name)
updatedCRD, err := c.crdLister.Get(crdKey)
if err != nil {
return nil, err
}
// Start with a shallow copy
refreshed := shallowCopyCRD(updatedCRD)
// If crd has the identity annotation, make sure it's added to refreshed
if identity := crd.Annotations[apisv1alpha1.AnnotationAPIIdentityKey]; identity != "" {
refreshed.Annotations[apisv1alpha1.AnnotationAPIIdentityKey] = identity
}
// If crd was only partial metadata, make sure refreshed is too
if _, partialMetadata := crd.Annotations[annotationKeyPartialMetadata]; partialMetadata {
partialMetadataCRD(refreshed)
}
return refreshed, nil
}
// Get gets a CustomResourceDefinition.
func (c *apiBindingAwareCRDLister) Get(ctx context.Context, name string) (*apiextensionsv1.CustomResourceDefinition, error) {
var (
crd *apiextensionsv1.CustomResourceDefinition
err error
)
clusterName, err := request.ClusterNameFrom(ctx)
if err != nil {
return nil, err
}
// Priority 1: system CRD
crd, err = c.getSystemCRD(clusterName, name)
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
}
if crd == nil {
// Not a system CRD
if identity := IdentityFromContext(ctx); identity != "" {
// Priority 2: identity request
crd, err = c.getForIdentity(name, identity)
} else if isPartialMetadataRequest(ctx) {
// Priority 3: partial metadata
crd, err = c.getForPartialMetadata(name)
} else {
// Priority 4: full data
crd, err = c.getForFullData(clusterName, name)
}
}
if err != nil {
return nil, err
}
if isPartialMetadataRequest(ctx) {
crd = shallowCopyCRD(crd)
partialMetadataCRD(crd)
}
return crd, nil
}
// shallowCopyCRD makes a shallow copy of in, with a deep copy of in.ObjectMeta.Annotations.
func shallowCopyCRD(in *apiextensionsv1.CustomResourceDefinition) *apiextensionsv1.CustomResourceDefinition {
out := *in
out.Annotations = make(map[string]string)
for k, v := range in.Annotations {
out.Annotations[k] = v
}
return &out
}
// partialMetadataCRD modifies CRD and replaces all version schemas with minimal ones suitable for partial object
// metadata.
func partialMetadataCRD(crd *apiextensionsv1.CustomResourceDefinition) {
crd.Annotations[annotationKeyPartialMetadata] = ""
// set minimal schema that prunes everything but ObjectMeta
for _, v := range crd.Spec.Versions {
v.Schema = &apiextensionsv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
Type: "object",
},
}
}
}
func (c *apiBindingAwareCRDLister) getForFullData(clusterName logicalcluster.LogicalCluster, name string) (*apiextensionsv1.CustomResourceDefinition, error) {
if clusterName == logicalcluster.Wildcard {
return c.getWildcard(name)
}
return c.get(clusterName, name)
}
func (c *apiBindingAwareCRDLister) getWildcard(name string) (*apiextensionsv1.CustomResourceDefinition, error) {
// TODO use an index by group+resource
crds, err := c.crdLister.List(labels.Everything())
if err != nil {
return nil, err
}
crd, equivalentSchemas := findCRD(name, crds)
if !equivalentSchemas {
err = apierrors.NewInternalError(fmt.Errorf("error resolving resource: cannot watch across logical clusters for a resource type with several distinct schemas"))
return nil, err
}
if crd == nil {
return nil, errors.NewNotFound(apiextensionsv1.Resource("customresourcedefinitions"), name)
}
return crd, nil
}
// getForIdentity handles finding the right CRD for an incoming wildcard request with identity, such as
// /clusters/*/apis/$group/$version/$resource:$identity.
func (c *apiBindingAwareCRDLister) getForIdentity(name, identity string) (*apiextensionsv1.CustomResourceDefinition, error) {
group, resource := crdNameToGroupResource(name)
indexKey := apibinding.IdentityGroupResourceKeyFunc(identity, group, resource)
apiBindings, err := c.apiBindingIndexer.ByIndex(apibinding.IndexAPIBindingsByIdentityGroupResource, indexKey)
if err != nil {
return nil, err
}
if len(apiBindings) == 0 {
return nil, errors.NewNotFound(apiextensionsv1.Resource("customresourcedefinitions"), name)
}
// TODO(ncdc): if there are multiple bindings that match on identity/group/resource, do we need to consider some
// sort of greatest-common-denominator for the CRD/schema?
apiBinding := apiBindings[0].(*apisv1alpha1.APIBinding)
var boundCRDName string
for _, r := range apiBinding.Status.BoundResources {
if r.Group == group && r.Resource == resource && r.Schema.IdentityHash == identity {
boundCRDName = r.Schema.UID
break
}
}
if boundCRDName == "" {
return nil, errors.NewNotFound(apiextensionsv1.Resource("customresourcedefinitions"), name)
}
crdKey := clusters.ToClusterAwareKey(apibinding.ShadowWorkspaceName, boundCRDName)
crd, err := c.crdLister.Get(crdKey)
if err != nil {
return nil, err
}
// Add the APIExport identity hash as an annotation to the CRD so the RESTOptionsGetter can assign
// the correct etcd resource prefix. Use a shallow copy because deep copy is expensive (but deep copy the annotations).
crd = shallowCopyCRD(crd)
crd.Annotations[apisv1alpha1.AnnotationAPIIdentityKey] = identity
return crd, nil
}
const annotationKeyPartialMetadata = "crd.kcp.dev/partial-metadata"
func (c *apiBindingAwareCRDLister) getForPartialMetadata(name string) (*apiextensionsv1.CustomResourceDefinition, error) {
// TODO add index on CRDs by group+resource
crds, err := c.crdLister.List(labels.Everything())
if err != nil {
return nil, err
}
group, resource := crdNameToGroupResource(name)
crd := findFirstCRDMatchingGroupResource(group, resource, crds)
if crd == nil {
return nil, apierrors.NewNotFound(apiextensionsv1.Resource("customresourcedefinitions"), name)
}
return crd, nil
}
func (c *apiBindingAwareCRDLister) getSystemCRD(clusterName logicalcluster.LogicalCluster, name string) (*apiextensionsv1.CustomResourceDefinition, error) {
if clusterName == logicalcluster.Wildcard {
systemCRDKeyName := clusters.ToClusterAwareKey(SystemCRDLogicalCluster, name)
return c.crdLister.Get(systemCRDKeyName)
}
systemCRDKeys := c.systemCRDProvider.Keys(clusterName)
systemCRDKeyName := clusters.ToClusterAwareKey(SystemCRDLogicalCluster, name)
if !systemCRDKeys.Has(systemCRDKeyName) {
return nil, apierrors.NewNotFound(apiextensionsv1.Resource("customresourcedefinitions"), name)
}
return c.crdLister.Get(systemCRDKeyName)
}
func (c *apiBindingAwareCRDLister) get(clusterName logicalcluster.LogicalCluster, name string) (*apiextensionsv1.CustomResourceDefinition, error) {
var crd *apiextensionsv1.CustomResourceDefinition
// Priority 1: see if it comes from any APIBindings
group, resource := crdNameToGroupResource(name)
// TODO use scoped lister when ready
apiBindings, err := c.apiBindingLister.List(labels.Everything())
if err != nil {
return nil, err
}
for _, apiBinding := range apiBindings {
if logicalcluster.From(apiBinding) != clusterName {
continue
}
if !conditions.IsTrue(apiBinding, apisv1alpha1.InitialBindingCompleted) {
continue
}
for _, boundResource := range apiBinding.Status.BoundResources {
if boundResource.Group == group && boundResource.Resource == resource {
crdKey := clusters.ToClusterAwareKey(apibinding.ShadowWorkspaceName, boundResource.Schema.UID)
crd, err = c.crdLister.Get(crdKey)
if err != nil && apierrors.IsNotFound(err) {
// If we got here, it means there is supposed to be a CRD coming from an APIBinding, but
// the CRD doesn't exist for some reason.
return nil, apierrors.NewServiceUnavailable(fmt.Sprintf("%s is currently unavailable", name))
} else if err != nil {
// something went wrong w/the lister - could only happen if meta.Accessor() fails on an item in the store.
return nil, err
}
// Add the APIExport identity hash as an annotation to the CRD so the RESTOptionsGetter can assign
// the correct etcd resource prefix.
crd = shallowCopyCRD(crd)
crd.Annotations[apisv1alpha1.AnnotationAPIIdentityKey] = boundResource.Schema.IdentityHash
return crd, nil
}
}
}
// Priority 2: see if it exists in the current logical cluster
crdKey := clusters.ToClusterAwareKey(clusterName, name)
crd, err = c.crdLister.Get(crdKey)
if err != nil && !apierrors.IsNotFound(err) {
// something went wrong w/the lister - could only happen if meta.Accessor() fails on an item in the store.
return nil, err
}
if crd != nil {
return crd, nil
}
return nil, apierrors.NewNotFound(schema.GroupResource{Group: apiextensionsv1.SchemeGroupVersion.Group, Resource: "customresourcedefinitions"}, name)
}
// findCRD tries to locate a CRD named crdName in crds. It returns the located CRD, if any, and a bool
// indicating that if there were multiple matches, they all have the same spec (true) or not (false).
func findCRD(name string, crds []*apiextensionsv1.CustomResourceDefinition) (*apiextensionsv1.CustomResourceDefinition, bool) {
var crd *apiextensionsv1.CustomResourceDefinition
group, resource := crdNameToGroupResource(name)
for _, aCRD := range crds {
if aCRD.Spec.Group != group || aCRD.Spec.Names.Plural != resource {
continue
}
if crd == nil {
crd = aCRD
} else {
if !equality.Semantic.DeepEqual(crd.Spec, aCRD.Spec) {
return nil, false
}
}
}
return crd, true
}
func findFirstCRDMatchingGroupResource(group, resource string, crds []*apiextensionsv1.CustomResourceDefinition) *apiextensionsv1.CustomResourceDefinition {
for _, crd := range crds {
if _, bound := crd.Annotations[apisv1alpha1.AnnotationBoundCRDKey]; bound {
continue
}
if crd.Spec.Group == group && crd.Spec.Names.Plural == resource {
return crd
}
}
return nil
}
func crdNameToGroupResource(name string) (group, resource string) {
parts := strings.SplitN(name, ".", 2)
resource = parts[0]
if len(parts) > 1 {
group = parts[1]
}
if group == "core" {
group = ""
}
return group, resource
}