Skip to content

Commit

Permalink
improve restmapper for gateway resource (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
rambohe-ch authored May 27, 2024
1 parent e42545e commit 1ba393f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
28 changes: 26 additions & 2 deletions pkg/yurthub/kubernetes/meta/restmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
28 changes: 28 additions & 0 deletions pkg/yurthub/kubernetes/meta/restmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 1ba393f

Please sign in to comment.