@@ -21,7 +21,7 @@ import (
2121 "runtime"
2222 "strings"
2323
24- "k8s.io/apimachinery/pkg/apis /meta/v1/unstructured "
24+ "k8s.io/apimachinery/pkg/api /meta"
2525 "k8s.io/apimachinery/pkg/runtime/schema"
2626 "k8s.io/client-go/discovery"
2727 clientset "k8s.io/client-go/kubernetes"
@@ -95,7 +95,16 @@ func CreateCustomResourceClients(apiserver string, kubeconfig string, factories
9595 if err != nil {
9696 return nil , err
9797 }
98- gvrString := GVRFromType (f .Name (), f .ExpectedType ()).String ()
98+ gvr , err := GVRFromType (f .Name (), f .ExpectedType ())
99+ if err != nil {
100+ return nil , err
101+ }
102+ var gvrString string
103+ if gvr != nil {
104+ gvrString = gvr .String ()
105+ } else {
106+ gvrString = f .Name ()
107+ }
99108 customResourceClients [gvrString ] = customResourceClient
100109 }
101110 return customResourceClients , nil
@@ -119,12 +128,16 @@ func CreateDiscoveryClient(apiserver string, kubeconfig string) (*discovery.Disc
119128}
120129
121130// GVRFromType returns the GroupVersionResource for a given type.
122- func GVRFromType (resourceName string , expectedType interface {}) * schema.GroupVersionResource {
131+ func GVRFromType (resourceName string , expectedType interface {}) ( * schema.GroupVersionResource , error ) {
123132 if _ , ok := expectedType .(* testUnstructuredMock.Foo ); ok {
124133 // testUnstructuredMock.Foo is a mock type for testing
125- return nil
134+ return nil , nil
126135 }
127- apiVersion := expectedType .(* unstructured.Unstructured ).Object ["apiVersion" ].(string )
136+ t , err := meta .TypeAccessor (expectedType )
137+ if err != nil {
138+ return nil , fmt .Errorf ("Failed to get type accessor for %T: %w" , expectedType , err )
139+ }
140+ apiVersion := t .GetAPIVersion ()
128141 g , v , found := strings .Cut (apiVersion , "/" )
129142 if ! found {
130143 g = "core"
@@ -135,7 +148,7 @@ func GVRFromType(resourceName string, expectedType interface{}) *schema.GroupVer
135148 Group : g ,
136149 Version : v ,
137150 Resource : r ,
138- }
151+ }, nil
139152}
140153
141154// GatherAndCount gathers all metrics from the provided Gatherer and counts
0 commit comments