Skip to content

Commit 3c778a5

Browse files
authored
fix: register new CRD to apigroups (argoproj#247)
Signed-off-by: kshamajain99 <[email protected]>
1 parent 46073c1 commit 3c778a5

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

pkg/cache/cluster.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,29 @@ func (c *clusterCache) GetAPIGroups() []metav1.APIGroup {
242242
return c.apiGroups
243243
}
244244

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+
245268
func (c *clusterCache) replaceResourceCache(gk schema.GroupKind, resources []*Resource, ns string) {
246269
objByKey := make(map[kube.ResourceKey]*Resource)
247270
for i := range resources {
@@ -504,15 +527,38 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
504527

505528
c.processEvent(event.Type, obj)
506529
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+
507547
if event.Type == watch.Deleted {
508-
group, groupOk, groupErr := unstructured.NestedString(obj.Object, "spec", "group")
509548
kind, kindOk, kindErr := unstructured.NestedString(obj.Object, "spec", "names", "kind")
510-
511549
if groupOk && groupErr == nil && kindOk && kindErr == nil {
512550
gk := schema.GroupKind{Group: group, Kind: kind}
513551
c.stopWatching(gk, ns)
514552
}
553+
// remove CRD's groupkind from c.apigroups
554+
if nameOK && nameErr == nil {
555+
c.DeleteAPIGroup(apiGroup)
556+
}
515557
} else {
558+
// add new CRD's groupkind to c.apigroups
559+
if event.Type == watch.Added && nameOK && nameErr == nil {
560+
c.AppendAPIGroups(apiGroup)
561+
}
516562
err = runSynced(&c.lock, func() error {
517563
return c.startMissingWatches()
518564
})

0 commit comments

Comments
 (0)