From e3d2ae153b856117f000cfac511e04469f74828b Mon Sep 17 00:00:00 2001 From: "Nelo-T. Wallus" Date: Tue, 30 Jun 2026 14:25:20 +0200 Subject: [PATCH] Use the GroupVersion.Version instead of the resources' preferred version APIResource.Version may be empty: https://github.com/kubernetes/apimachinery/blob/6fa8dff7b19f13e310ef489972c8f394a9c185ad/pkg/apis/meta/v1/types.go#L1235-L1237 Additionally it reports the resources' preferred version, not the version it was listed for per APIResourceList.GroupVersion: https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/types.go I believe this is irrelevant in most cases however we hit this when using api-syncagent against resources provided by an aggregated API server, which does not fill the .Version on the resource. Signed-off-by: Nelo-T. Wallus --- internal/discovery/client.go | 5 ++-- test/e2e/discovery/discovery_test.go | 35 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/discovery/client.go b/internal/discovery/client.go index 4233736f..0ecd675d 100644 --- a/internal/discovery/client.go +++ b/internal/discovery/client.go @@ -106,15 +106,14 @@ func (c *Client) RetrieveCRD(ctx context.Context, gk schema.GroupKind) (*apiexte if subresource == "" { resource = &res } else { - list, ok := subresourcesPerVersion[res.Version] + list, ok := subresourcesPerVersion[gv.Version] if !ok { list = sets.New[string]() } list.Insert(subresource) - subresourcesPerVersion[res.Version] = list + subresourcesPerVersion[gv.Version] = list } - // res.Version is also empty for built-in resources availableVersions.Insert(gv.Version) } } diff --git a/test/e2e/discovery/discovery_test.go b/test/e2e/discovery/discovery_test.go index b5513368..e97652ed 100644 --- a/test/e2e/discovery/discovery_test.go +++ b/test/e2e/discovery/discovery_test.go @@ -35,11 +35,12 @@ import ( func TestDiscoverSingleVersionCRD(t *testing.T) { testcases := []struct { - name string - crdFiles []string - groupKind schema.GroupKind - expectedVersions []string - expectedNames apiextensionsv1.CustomResourceDefinitionNames + name string + crdFiles []string + groupKind schema.GroupKind + expectedVersions []string + expectedNames apiextensionsv1.CustomResourceDefinitionNames + expectedSubresources map[string]apiextensionsv1.CustomResourceSubresources }{ { name: "get non-CRD resource", @@ -89,6 +90,18 @@ func TestDiscoverSingleVersionCRD(t *testing.T) { ListKind: "HorizontalPodAutoscalerList", Categories: []string{"all"}, }, + expectedSubresources: map[string]apiextensionsv1.CustomResourceSubresources{ + "v1": { + Status: &apiextensionsv1.CustomResourceSubresourceStatus{}, + // envtest returns nil here instead of the real default value. + Scale: nil, + }, + "v2": { + Status: &apiextensionsv1.CustomResourceSubresourceStatus{}, + // envtest returns nil here instead of the real default value. + Scale: nil, + }, + }, }, } @@ -130,6 +143,18 @@ func TestDiscoverSingleVersionCRD(t *testing.T) { if diff := cmp.Diff(testcase.expectedVersions, versions); diff != "" { t.Errorf("Did not get expected CRD versions:\n\n%s", diff) } + + if testcase.expectedSubresources != nil { + gotSubresources := map[string]apiextensionsv1.CustomResourceSubresources{} + for _, v := range crd.Spec.Versions { + if v.Subresources != nil { + gotSubresources[v.Name] = *v.Subresources + } + } + if diff := cmp.Diff(testcase.expectedSubresources, gotSubresources); diff != "" { + t.Errorf("Did not get expected CRD subresources:\n\n%s", diff) + } + } }) } }