Skip to content

Commit

Permalink
test/framework: allow to include arbitrary types when dumping resources
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vince@prigna.com>
  • Loading branch information
vincepri committed Oct 18, 2024
1 parent b03a2d2 commit 2e4b858
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions test/framework/alltypes_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 2e4b858

Please sign in to comment.