From 1ba393f010de7d8ec7247f2c23d02913076779f1 Mon Sep 17 00:00:00 2001 From: rambohe Date: Mon, 27 May 2024 20:04:38 +0800 Subject: [PATCH] improve restmapper for gateway resource (#2054) --- pkg/yurthub/kubernetes/meta/restmapper.go | 28 +++++++++++++++++-- .../kubernetes/meta/restmapper_test.go | 28 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/yurthub/kubernetes/meta/restmapper.go b/pkg/yurthub/kubernetes/meta/restmapper.go index 7c41731015b..291edf42488 100644 --- a/pkg/yurthub/kubernetes/meta/restmapper.go +++ b/pkg/yurthub/kubernetes/meta/restmapper.go @@ -42,6 +42,10 @@ var ( // It is not updatable and is only used for the judgment of scheme resources unsafeSchemeRESTMapper = NewDefaultRESTMapperFromScheme() ErrGVRNotRecognized = errors.New("GroupVersionResource is not recognized") + + specifiedResources = []string{ + "gateway", + } ) // RESTMapperManager is responsible for managing different kind of RESTMapper @@ -141,7 +145,7 @@ func (rm *RESTMapperManager) dynamicKindFor(gvr schema.GroupVersionResource) (sc // Used to delete the mapping relationship between GVR and GVK in dynamicRESTMapper func (rm *RESTMapperManager) deleteKind(gvk schema.GroupVersionKind) error { kindName := strings.TrimSuffix(gvk.Kind, "List") - plural, singular := meta.UnsafeGuessKindToResource(gvk.GroupVersion().WithKind(kindName)) + plural, singular := specifiedKindToResource(gvk.GroupVersion().WithKind(kindName)) rm.Lock() delete(rm.dynamicRESTMapper, plural) delete(rm.dynamicRESTMapper, singular) @@ -193,7 +197,7 @@ func (rm *RESTMapperManager) DeleteKindFor(gvr schema.GroupVersionResource) erro func (rm *RESTMapperManager) UpdateKind(gvk schema.GroupVersionKind) error { kindName := strings.TrimSuffix(gvk.Kind, "List") gvk = gvk.GroupVersion().WithKind(kindName) - plural, singular := meta.UnsafeGuessKindToResource(gvk.GroupVersion().WithKind(kindName)) + plural, singular := specifiedKindToResource(gvk.GroupVersion().WithKind(kindName)) // If it is not a built-in resource and it is not stored in DynamicRESTMapper, add it to DynamicRESTMapper isScheme, t := rm.KindFor(singular) if !isScheme && t.Empty() { @@ -269,3 +273,23 @@ func IsSchemeResource(gvr schema.GroupVersionResource) bool { return false } + +// specifiedKindToResource converts Kind to a resource name. +// Broken. This method only "sort of" works when used outside of this package. It assumes that Kinds and Resources match +// and they aren't guaranteed to do so. +func specifiedKindToResource(kind schema.GroupVersionKind) ( /*plural*/ schema.GroupVersionResource /*singular*/, schema.GroupVersionResource) { + kindName := kind.Kind + if len(kindName) == 0 { + return schema.GroupVersionResource{}, schema.GroupVersionResource{} + } + singularName := strings.ToLower(kindName) + singular := kind.GroupVersion().WithResource(singularName) + + for _, skip := range specifiedResources { + if strings.HasSuffix(singularName, skip) { + return kind.GroupVersion().WithResource(singularName + "s"), singular + } + } + + return meta.UnsafeGuessKindToResource(kind) +} diff --git a/pkg/yurthub/kubernetes/meta/restmapper_test.go b/pkg/yurthub/kubernetes/meta/restmapper_test.go index 84ddccc7131..11f075a1015 100644 --- a/pkg/yurthub/kubernetes/meta/restmapper_test.go +++ b/pkg/yurthub/kubernetes/meta/restmapper_test.go @@ -237,3 +237,31 @@ func dynamicRESTMapperToString(m map[schema.GroupVersionResource]schema.GroupVer } return resultMapper } + +func TestKindToResource(t *testing.T) { + testCases := []struct { + Kind string + Plural, Singular string + }{ + {Kind: "Pod", Plural: "pods", Singular: "pod"}, + + {Kind: "Gateway", Plural: "gateways", Singular: "gateway"}, + + {Kind: "ReplicationController", Plural: "replicationcontrollers", Singular: "replicationcontroller"}, + + // Add "ies" when ending with "y" + {Kind: "ImageRepository", Plural: "imagerepositories", Singular: "imagerepository"}, + // Add "es" when ending with "s" + {Kind: "miss", Plural: "misses", Singular: "miss"}, + // Add "s" otherwise + {Kind: "lowercase", Plural: "lowercases", Singular: "lowercase"}, + } + for i, testCase := range testCases { + version := schema.GroupVersion{} + + plural, singular := specifiedKindToResource(version.WithKind(testCase.Kind)) + if singular != version.WithResource(testCase.Singular) || plural != version.WithResource(testCase.Plural) { + t.Errorf("%d: unexpected plural and singular: %v %v", i, plural, singular) + } + } +}