-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛 RESTMapper: don't treat non-existing GroupVersions as errors #2425
Closed
timebertt
wants to merge
2
commits into
kubernetes-sigs:main
from
timebertt:restmapper-no-match-error
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"net/http" | ||
"sync" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
@@ -166,8 +167,10 @@ func (m *mapper) addKnownGroupAndReload(groupName string, versions ...string) er | |
if err != nil { | ||
return err | ||
} | ||
for _, version := range apiGroup.Versions { | ||
versions = append(versions, version.Version) | ||
if apiGroup != nil { | ||
for _, version := range apiGroup.Versions { | ||
versions = append(versions, version.Version) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -254,17 +257,12 @@ func (m *mapper) findAPIGroupByName(groupName string) (*metav1.APIGroup, error) | |
m.mu.Unlock() | ||
|
||
// Looking in the cache again. | ||
{ | ||
m.mu.RLock() | ||
group, ok := m.apiGroups[groupName] | ||
m.mu.RUnlock() | ||
if ok { | ||
return group, nil | ||
} | ||
} | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
|
||
// If there is still nothing, return an error. | ||
return nil, fmt.Errorf("failed to find API group %q", groupName) | ||
// Don't return an error here if the API group is not present. | ||
// The reloaded RESTMapper will take care of returning a NoMatchError. | ||
return m.apiGroups[groupName], nil | ||
Comment on lines
+263
to
+265
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. |
||
} | ||
|
||
// fetchGroupVersionResources fetches the resources for the specified group and its versions. | ||
|
@@ -276,7 +274,7 @@ func (m *mapper) fetchGroupVersionResources(groupName string, versions ...string | |
groupVersion := schema.GroupVersion{Group: groupName, Version: version} | ||
|
||
apiResourceList, err := m.client.ServerResourcesForGroupVersion(groupVersion.String()) | ||
if err != nil { | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
failedGroups[groupVersion] = err | ||
} | ||
if apiResourceList != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this check here? If apiGroup is nil, the range would throw a panic
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.
Exactly, if the queried group name cannot be found,
apiGroup
isnil
and the next line will cause anil pointer dereference
.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.
Alternatively, we could make
mapper.apiGroups
amap[string]metav1.APIGroup
so that we don't have to check for nil pointers.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.
Could we have
findAPIGroupByName
return a well-known not found error, and catch it here instead of returningnil, nil
?