@@ -242,6 +242,29 @@ func (c *clusterCache) GetAPIGroups() []metav1.APIGroup {
242
242
return c .apiGroups
243
243
}
244
244
245
+ func (c * clusterCache ) AppendAPIGroups (apiGroup metav1.APIGroup ) {
246
+ exists := false
247
+ for i := range c .apiGroups {
248
+ if c .apiGroups [i ].Name == apiGroup .Name {
249
+ exists = true
250
+ break
251
+ }
252
+ }
253
+ if ! exists {
254
+ c .apiGroups = append (c .apiGroups , apiGroup )
255
+ }
256
+ }
257
+
258
+ func (c * clusterCache ) DeleteAPIGroup (apiGroup metav1.APIGroup ) {
259
+ for i := range c .apiGroups {
260
+ if c .apiGroups [i ].Name == apiGroup .Name {
261
+ c .apiGroups [i ] = c .apiGroups [len (c .apiGroups )- 1 ]
262
+ c .apiGroups = c .apiGroups [:len (c .apiGroups )- 1 ]
263
+ break
264
+ }
265
+ }
266
+ }
267
+
245
268
func (c * clusterCache ) replaceResourceCache (gk schema.GroupKind , resources []* Resource , ns string ) {
246
269
objByKey := make (map [kube.ResourceKey ]* Resource )
247
270
for i := range resources {
@@ -504,15 +527,38 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
504
527
505
528
c .processEvent (event .Type , obj )
506
529
if kube .IsCRD (obj ) {
530
+ var apiGroup metav1.APIGroup
531
+ name , nameOK , nameErr := unstructured .NestedString (obj .Object , "metadata" , "name" )
532
+ group , groupOk , groupErr := unstructured .NestedString (obj .Object , "spec" , "group" )
533
+ version , versionOK , versionErr := unstructured .NestedString (obj .Object , "spec" , "version" )
534
+ if nameOK && nameErr == nil {
535
+ apiGroup .Name = name
536
+ var groupVersions []metav1.GroupVersionForDiscovery
537
+ if groupOk && groupErr == nil && versionOK && versionErr == nil {
538
+ groupVersion := metav1.GroupVersionForDiscovery {
539
+ GroupVersion : group + "/" + version ,
540
+ Version : version ,
541
+ }
542
+ groupVersions = append (groupVersions , groupVersion )
543
+ }
544
+ apiGroup .Versions = groupVersions
545
+ }
546
+
507
547
if event .Type == watch .Deleted {
508
- group , groupOk , groupErr := unstructured .NestedString (obj .Object , "spec" , "group" )
509
548
kind , kindOk , kindErr := unstructured .NestedString (obj .Object , "spec" , "names" , "kind" )
510
-
511
549
if groupOk && groupErr == nil && kindOk && kindErr == nil {
512
550
gk := schema.GroupKind {Group : group , Kind : kind }
513
551
c .stopWatching (gk , ns )
514
552
}
553
+ // remove CRD's groupkind from c.apigroups
554
+ if nameOK && nameErr == nil {
555
+ c .DeleteAPIGroup (apiGroup )
556
+ }
515
557
} else {
558
+ // add new CRD's groupkind to c.apigroups
559
+ if event .Type == watch .Added && nameOK && nameErr == nil {
560
+ c .AppendAPIGroups (apiGroup )
561
+ }
516
562
err = runSynced (& c .lock , func () error {
517
563
return c .startMissingWatches ()
518
564
})
0 commit comments