From 2e4b858354b93e2778e3c81b7b0bc6c7827c33e6 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Fri, 18 Oct 2024 07:17:49 -0700 Subject: [PATCH] test/framework: allow to include arbitrary types when dumping resources Signed-off-by: Vince Prignano --- test/framework/alltypes_helpers.go | 32 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/test/framework/alltypes_helpers.go b/test/framework/alltypes_helpers.go index 48a45b140367..c3aa3e466cd5 100644 --- a/test/framework/alltypes_helpers.go +++ b/test/framework/alltypes_helpers.go @@ -34,6 +34,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" @@ -45,8 +46,9 @@ import ( // GetCAPIResourcesInput is the input for GetCAPIResources. type GetCAPIResourcesInput struct { - Lister Lister - Namespace string + Lister Lister + Namespace string + IncludeTypes []metav1.TypeMeta } // GetCAPIResources reads all the CAPI resources in a namespace. @@ -57,13 +59,13 @@ func GetCAPIResources(ctx context.Context, input GetCAPIResourcesInput) []*unstr Expect(input.Namespace).NotTo(BeEmpty(), "input.Namespace is required for GetCAPIResources") types := getClusterAPITypes(ctx, input.Lister) + types.Insert(input.IncludeTypes...) objList := []*unstructured.Unstructured{} - for i := range types { - typeMeta := types[i] + for _, typ := range types.UnsortedList() { typeList := new(unstructured.UnstructuredList) - typeList.SetAPIVersion(typeMeta.APIVersion) - typeList.SetKind(typeMeta.Kind) + typeList.SetAPIVersion(typ.APIVersion) + typeList.SetKind(typ.Kind) if err := input.Lister.List(ctx, typeList, client.InNamespace(input.Namespace)); err != nil { if apierrors.IsNotFound(err) { @@ -86,8 +88,8 @@ func GetCAPIResources(ctx context.Context, input GetCAPIResourcesInput) []*unstr // getClusterAPITypes returns the list of TypeMeta to be considered for the move discovery phase. // This list includes all the types belonging to CAPI providers. -func getClusterAPITypes(ctx context.Context, lister Lister) []metav1.TypeMeta { - discoveredTypes := []metav1.TypeMeta{} +func getClusterAPITypes(ctx context.Context, lister Lister) sets.Set[metav1.TypeMeta] { + discoveredTypes := sets.New[metav1.TypeMeta]() crdList := &apiextensionsv1.CustomResourceDefinitionList{} Eventually(func() error { @@ -100,7 +102,7 @@ func getClusterAPITypes(ctx context.Context, lister Lister) []metav1.TypeMeta { continue } - discoveredTypes = append(discoveredTypes, metav1.TypeMeta{ + discoveredTypes.Insert(metav1.TypeMeta{ Kind: crd.Spec.Names.Kind, APIVersion: metav1.GroupVersion{ Group: crd.Spec.Group, @@ -114,9 +116,10 @@ func getClusterAPITypes(ctx context.Context, lister Lister) []metav1.TypeMeta { // DumpAllResourcesInput is the input for DumpAllResources. type DumpAllResourcesInput struct { - Lister Lister - Namespace string - LogPath string + Lister Lister + Namespace string + LogPath string + IncludeTypes []metav1.TypeMeta } // DumpAllResources dumps Cluster API related resources to YAML @@ -127,8 +130,9 @@ func DumpAllResources(ctx context.Context, input DumpAllResourcesInput) { Expect(input.Namespace).NotTo(BeEmpty(), "input.Namespace is required for DumpAllResources") resources := GetCAPIResources(ctx, GetCAPIResourcesInput{ - Lister: input.Lister, - Namespace: input.Namespace, + Lister: input.Lister, + Namespace: input.Namespace, + IncludeTypes: input.IncludeTypes, }) for i := range resources {