Skip to content

Commit

Permalink
Fix a panic in GVRFromType for structured types
Browse files Browse the repository at this point in the history
  • Loading branch information
L3n41c committed Nov 8, 2024
1 parent 110f03d commit 26c0292
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"runtime"
"strings"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -142,7 +143,14 @@ func GVRFromType(resourceName string, expectedType interface{}) *schema.GroupVer
// testUnstructuredMock.Foo is a mock type for testing
return nil
}
apiVersion := expectedType.(*unstructured.Unstructured).Object["apiVersion"].(string)
var apiVersion string
if u, ok := expectedType.(unstructured.Unstructured); ok {
apiVersion = u.GetAPIVersion()
} else if t, err := meta.TypeAccessor(expectedType); err == nil {
apiVersion = t.GetAPIVersion()
} else {
panic(err)
}
g, v, found := strings.Cut(apiVersion, "/")
if !found {
g = "core"
Expand Down

0 comments on commit 26c0292

Please sign in to comment.