diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 837a0fd56..00c001886 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -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" @@ -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"