From 0dbc4bc2dbd09d79e205dbf14d5ae888b14afd55 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Fri, 20 Feb 2026 20:54:06 +0600 Subject: [PATCH] Add helper to register new api types Signed-off-by: Tamal Saha --- hub/registry.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/hub/registry.go b/hub/registry.go index 3580e216fb..89376cf9fd 100644 --- a/hub/registry.go +++ b/hub/registry.go @@ -33,6 +33,7 @@ import ( stringz "gomodules.xyz/x/strings" crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" @@ -142,7 +143,7 @@ func (r *Registry) Reset() { } } -func (r *Registry) Register(gvr schema.GroupVersionResource, cfg *rest.Config) error { +func (r *Registry) Rediscover(gvr schema.GroupVersionResource, cfg *rest.Config) error { r.m.RLock() if _, found := r.regGVR[gvr]; found { r.m.RUnlock() @@ -153,6 +154,44 @@ func (r *Registry) Register(gvr schema.GroupVersionResource, cfg *rest.Config) e return r.DiscoverResources(cfg) } +func (r *Registry) RegisterGVK(gvk schema.GroupVersionKind) error { + r.m.RLock() + if _, found := r.regGVK[gvk]; found { + r.m.RUnlock() + return nil + } + r.m.RUnlock() + + latestGVR, ok := resourcedescriptors.LatestGVRs()[gvk.GroupKind()] + if !ok { + return apierrors.NewNotFound(v1alpha1.Resource(v1alpha1.ResourceKindResourceDescriptor), gvk.String()) + } + return r.RegisterGVR(latestGVR.GroupResource().WithVersion(gvk.Version)) +} + +func (r *Registry) RegisterGVR(gvr schema.GroupVersionResource) error { + r.m.RLock() + if _, found := r.regGVR[gvr]; found { + r.m.RUnlock() + return nil + } + r.m.RUnlock() + + filename := resourcedescriptors.GetName(gvr) + rd, err := resourcedescriptors.LoadByGVR(gvr) + if err != nil { + return err + } + + r.m.Lock() + defer r.m.Unlock() + + r.regGVK[rd.Spec.Resource.GroupVersionKind()] = &rd.Spec.Resource + r.regGVR[rd.Spec.Resource.GroupVersionResource()] = &rd.Spec.Resource + r.cache.Set(filename, rd) + return nil +} + func (r *Registry) createRegistry(cfg *rest.Config) (map[schema.GroupResource]schema.GroupVersionResource, map[string]*v1alpha1.ResourceDescriptor, error) { kc, err := kubernetes.NewForConfig(cfg) if err != nil {