-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: panic in util.GVRFromType
for structured types
#2553
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8859a42
Fix a panic in GVRFromType for structured types
L3n41c 4a96a78
Log an error instead of panicking
L3n41c 61134a0
Return an error instead of only logging
L3n41c c3b1afe
Handle the cases where GVRFromType returns neither a GVR nor an error
L3n41c 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,7 +21,7 @@ import ( | |
"runtime" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/discovery" | ||
"k8s.io/client-go/dynamic" | ||
|
@@ -97,8 +97,11 @@ func CreateCustomResourceClients(apiserver string, kubeconfig string, factories | |
if err != nil { | ||
return nil, err | ||
} | ||
gvrString := GVRFromType(f.Name(), f.ExpectedType()).String() | ||
customResourceClients[gvrString] = customResourceClient | ||
gvr, err := GVRFromType(f.Name(), f.ExpectedType()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
customResourceClients[gvr.String()] = customResourceClient | ||
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. Here too? 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. Also fixed in c3b1afe |
||
} | ||
return customResourceClients, nil | ||
} | ||
|
@@ -137,12 +140,16 @@ func CreateDynamicClient(apiserver string, kubeconfig string) (*dynamic.DynamicC | |
} | ||
|
||
// GVRFromType returns the GroupVersionResource for a given type. | ||
func GVRFromType(resourceName string, expectedType interface{}) *schema.GroupVersionResource { | ||
func GVRFromType(resourceName string, expectedType interface{}) (*schema.GroupVersionResource, error) { | ||
if _, ok := expectedType.(*testUnstructuredMock.Foo); ok { | ||
// testUnstructuredMock.Foo is a mock type for testing | ||
return nil | ||
return nil, nil | ||
} | ||
apiVersion := expectedType.(*unstructured.Unstructured).Object["apiVersion"].(string) | ||
t, err := meta.TypeAccessor(expectedType) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get type accessor for %T: %w", expectedType, err) | ||
} | ||
apiVersion := t.GetAPIVersion() | ||
g, v, found := strings.Cut(apiVersion, "/") | ||
if !found { | ||
g = "core" | ||
|
@@ -153,7 +160,7 @@ func GVRFromType(resourceName string, expectedType interface{}) *schema.GroupVer | |
Group: g, | ||
Version: v, | ||
Resource: r, | ||
} | ||
}, nil | ||
} | ||
|
||
// GatherAndCount gathers all metrics from the provided Gatherer and counts | ||
|
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.
We probably need a similar check as
61134a0
(#2553) here.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 hesitated.
The thing is that the
if
block that checks ifgvrString
isnil
and adapts if it is that I added ininternal/discovery/discovery.go:224
has been copied frominternal/store/builder.go:205
andinternal/store/builder.go:564
because, in those three cases, ifutil.GVRFromType(…)
returns an error, we only log an error and continue executing the function.But, in this case and and in
pkg/util/utils.go
below, ifutil.GVRFromType(…)
returns an error, the function forwards the error and exits early so that this line isn’t executed.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.
@rexagod I’ve just fixed this function to make it gracefully handle the cases where
utils.GVRFromType(…)
returns neither an error nor a valid GVR: c3b1afe