Skip to content

Commit 4eb8c44

Browse files
committed
fix discovery of built-in resources (oh Kubernetes, why u like this sometimes?)
On-behalf-of: @SAP [email protected]
1 parent 382725f commit 4eb8c44

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

internal/discovery/client.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,19 @@ func (c *Client) RetrieveCRD(ctx context.Context, gk schema.GroupKind) (*apiexte
8080
subresourcesPerVersion := map[string]sets.Set[string]{}
8181

8282
for _, resList := range resourceLists {
83+
// .Group on an APIResource is empty for built-in resources, so we must
84+
// parse and check GroupVersion of the entire list.
85+
gv, err := schema.ParseGroupVersion(resList.GroupVersion)
86+
if err != nil {
87+
return nil, fmt.Errorf("Kubernetes reported invalid API group version %q: %w", resList.GroupVersion, err)
88+
}
89+
90+
if gv.Group != gk.Group {
91+
continue
92+
}
93+
8394
for _, res := range resList.APIResources {
84-
// ignore other groups
85-
if res.Group != gk.Group || res.Kind != gk.Kind {
95+
if res.Kind != gk.Kind {
8696
continue
8797
}
8898

@@ -106,14 +116,18 @@ func (c *Client) RetrieveCRD(ctx context.Context, gk schema.GroupKind) (*apiexte
106116
subresourcesPerVersion[res.Version] = list
107117
}
108118

109-
availableVersions.Insert(res.Version)
119+
// res.Version is also empty for built-in resources
120+
availableVersions.Insert(gv.Version)
110121
}
111122
}
112123

113124
if resource == nil {
114125
return nil, fmt.Errorf("could not find %v in APIs", gk)
115126
}
116127

128+
// fill-in the missing Group for built-in resources
129+
resource.Group = gk.Group
130+
117131
////////////////////////////////////
118132
// If possible, retrieve the GK as its original CRD, which is always preferred
119133
// because it's much more precise than what we can retrieve from the OpenAPI.
@@ -245,7 +259,7 @@ func (c *Client) RetrieveCRD(ctx context.Context, gk schema.GroupKind) (*apiexte
245259

246260
protoSchema := modelsByGKV[gvk]
247261
if protoSchema == nil {
248-
return nil, fmt.Errorf("no models for %v", gk)
262+
return nil, fmt.Errorf("no models for %v", gvk)
249263
}
250264

251265
var schemaProps apiextensionsv1.JSONSchemaProps
@@ -299,9 +313,21 @@ func (c *Client) getPreferredVersion(resource *metav1.APIResource) (string, erro
299313
}
300314

301315
for _, resList := range result {
316+
// .Group on an APIResource is empty for built-in resources, so we must
317+
// parse and check GroupVersion of the entire list.
318+
gv, err := schema.ParseGroupVersion(resList.GroupVersion)
319+
if err != nil {
320+
return "", fmt.Errorf("Kubernetes reported invalid API group version %q: %w", resList.GroupVersion, err)
321+
}
322+
323+
if gv.Group != resource.Group {
324+
continue
325+
}
326+
302327
for _, res := range resList.APIResources {
303-
if res.Name == resource.Name && res.Group == resource.Group {
304-
return res.Version, nil
328+
if res.Name == resource.Name {
329+
// res.Version is empty for built-in resources
330+
return gv.Version, nil
305331
}
306332
}
307333
}

0 commit comments

Comments
 (0)