From 26c0292851628a23b4bd981bf1caad188f5dd2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9na=C3=AFc=20Huard?= Date: Fri, 8 Nov 2024 15:43:21 +0100 Subject: [PATCH] Fix a panic in GVRFromType for structured types --- pkg/util/utils.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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"