-
Notifications
You must be signed in to change notification settings - Fork 38.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(apimachinery): deprecate meta.UnsafeGuessKindToResource #119924
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ import ( | |
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/gengo/namer" | ||
"k8s.io/gengo/types" | ||
) | ||
|
||
// Implements RESTScope interface | ||
|
@@ -62,6 +64,8 @@ type DefaultRESTMapper struct { | |
kindToScope map[schema.GroupVersionKind]RESTScope | ||
singularToPlural map[schema.GroupVersionResource]schema.GroupVersionResource | ||
pluralToSingular map[schema.GroupVersionResource]schema.GroupVersionResource | ||
|
||
namer namer.Namer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may give fmt error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ACK. What's the fmt tool to check this and how can I trigger it? |
||
} | ||
|
||
func (m *DefaultRESTMapper) String() string { | ||
|
@@ -93,11 +97,15 @@ func NewDefaultRESTMapper(defaultGroupVersions []schema.GroupVersion) *DefaultRE | |
defaultGroupVersions: defaultGroupVersions, | ||
singularToPlural: singularToPlural, | ||
pluralToSingular: pluralToSingular, | ||
namer: namer.NewAllLowercasePluralNamer(nil), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really see how embedding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pending response on the alternative for |
||
} | ||
} | ||
|
||
func (m *DefaultRESTMapper) Add(kind schema.GroupVersionKind, scope RESTScope) { | ||
plural, singular := UnsafeGuessKindToResource(kind) | ||
t := types.Ref(kind.GroupVersion().String(), kind.Kind) | ||
plural := kind.GroupVersion().WithResource(m.namer.Name(t)) | ||
singular := kind.GroupVersion().WithResource(strings.ToLower(kind.Kind)) | ||
|
||
m.AddSpecific(kind, plural, singular, scope) | ||
} | ||
|
||
|
@@ -123,6 +131,9 @@ var unpluralizedSuffixes = []string{ | |
// UnsafeGuessKindToResource 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. | ||
// | ||
// Deprecated: This is and may yield unwanted and unexpected results. | ||
// Use k8s.io/gengo/namer's NewAllLowercasePluralNamer() instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment about not depending on gengo at runtime |
||
func UnsafeGuessKindToResource(kind schema.GroupVersionKind) ( /*plural*/ schema.GroupVersionResource /*singular*/, schema.GroupVersionResource) { | ||
kindName := kind.Kind | ||
if len(kindName) == 0 { | ||
|
@@ -244,7 +255,6 @@ func (m *DefaultRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]s | |
ret = append(ret, plural) | ||
} | ||
} | ||
|
||
} | ||
|
||
case hasVersion: | ||
|
@@ -329,7 +339,6 @@ func (m *DefaultRESTMapper) KindsFor(input schema.GroupVersionResource) ([]schem | |
ret = append(ret, currKind) | ||
} | ||
} | ||
|
||
} | ||
|
||
case hasVersion: | ||
|
@@ -495,7 +504,7 @@ func (m *DefaultRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string | |
} | ||
|
||
for _, gvk := range potentialGVK { | ||
//Ensure we have a REST mapping | ||
// Ensure we have a REST mapping | ||
res, ok := m.kindToPluralResource[gvk] | ||
if !ok { | ||
continue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,12 +249,10 @@ func TestRESTMapperKindsFor(t *testing.T) { | |
continue | ||
} | ||
} | ||
|
||
} else { | ||
if testCase.ExpectedKinds[0] != singleKind { | ||
t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKinds[0], singleKind) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
@@ -426,12 +424,10 @@ func TestRESTMapperResourcesFor(t *testing.T) { | |
continue | ||
} | ||
} | ||
|
||
} else { | ||
if testCase.ExpectedResources[0] != singleResource { | ||
t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResources[0], singleResource) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
@@ -443,23 +439,30 @@ func TestKindToResource(t *testing.T) { | |
Plural, Singular string | ||
}{ | ||
{Kind: "Pod", Plural: "pods", Singular: "pod"}, | ||
|
||
{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"}, | ||
// Take into account Endpoints. | ||
{Kind: "Endpoints", Plural: "endpoints", Singular: "endpoints"}, | ||
// TODO: this fails because UnsafeGuessKindToResource produces gatewaies as a plural form of gateway. | ||
// {Kind: "Gateway", Plural: "gateways", Singular: "gateway"}, | ||
} | ||
for i, testCase := range testCases { | ||
version := schema.GroupVersion{} | ||
|
||
plural, singular := UnsafeGuessKindToResource(version.WithKind(testCase.Kind)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert the changes to this test... this is exercising UnsafeGuessKindToResource which we don't want to lose coverage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, reverted this but I retained the change using the subtests and adding the |
||
if singular != version.WithResource(testCase.Singular) || plural != version.WithResource(testCase.Plural) { | ||
t.Errorf("%d: unexpected plural and singular: %v %v", i, plural, singular) | ||
} | ||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.Kind, func(t *testing.T) { | ||
version := schema.GroupVersion{} | ||
|
||
plural, singular := UnsafeGuessKindToResource(version.WithKind(testCase.Kind)) | ||
|
||
if singular != version.WithResource(testCase.Singular) || plural != version.WithResource(testCase.Plural) { | ||
t.Errorf("unexpected plural '%v' and singular: '%v'", plural.Resource, singular.Resource) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gengo is for code-generation prior to build, not something we want to rely on at runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. What suggestion do you have on this one?
namer
ability to provide plural names was suggested in #110053 (comment) as a replacement forUnsafeGuessKindToResource
. What other options do we have?