From beb993c62ca151f9966ef5ea4cc8fcede2fae857 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Thu, 31 Jul 2025 17:18:25 +0200 Subject: [PATCH] [cozystack-api] fix type for ApplicationList Signed-off-by: Andrei Kvapil (cherry picked from commit f057d92a4d66fd11b3eb35ba16d95e9a9d96ee3d) --- pkg/registry/apps/application/rest.go | 46 +++++++++++++++++---------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/pkg/registry/apps/application/rest.go b/pkg/registry/apps/application/rest.go index 4b80daa3..8cd246eb 100644 --- a/pkg/registry/apps/application/rest.go +++ b/pkg/registry/apps/application/rest.go @@ -254,7 +254,6 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption klog.Errorf("Invalid field selector: %v", err) return nil, fmt.Errorf("invalid field selector: %v", err) } - // Check if selector is for metadata.name if name, exists := fs.RequiresExactMatch("metadata.name"); exists { // Convert Application name to HelmRelease name @@ -304,17 +303,8 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption return nil, err } - // Initialize empty Application list - appList := &appsv1alpha1.ApplicationList{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "apps.cozystack.io/v1alpha1", - Kind: "ApplicationList", - }, - ListMeta: metav1.ListMeta{ - ResourceVersion: hrList.GetResourceVersion(), - }, - Items: []appsv1alpha1.Application{}, - } + // Initialize unstructured items array + items := make([]unstructured.Unstructured, 0) // Iterate over HelmReleases and convert to Applications for _, hr := range hrList.Items { @@ -352,7 +342,6 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption klog.Errorf("Invalid field selector: %v", err) continue } - fieldsSet := fields.Set{ "metadata.name": app.Name, "metadata.namespace": app.Namespace, @@ -362,10 +351,23 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption } } - appList.Items = append(appList.Items, app) + // Convert Application to unstructured + unstructuredApp, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&app) + if err != nil { + klog.Errorf("Error converting Application %s to unstructured: %v", app.Name, err) + continue + } + items = append(items, unstructured.Unstructured{Object: unstructuredApp}) } - klog.V(6).Infof("Successfully listed %d Application resources in namespace %s", len(appList.Items), namespace) + // Explicitly set apiVersion and kind in unstructured object + appList := &unstructured.UnstructuredList{} + appList.SetAPIVersion("apps.cozystack.io/v1alpha1") + appList.SetKind(r.kindName + "List") + appList.SetResourceVersion(hrList.GetResourceVersion()) + appList.Items = items + + klog.V(6).Infof("Successfully listed %d Application resources in namespace %s", len(items), namespace) return appList, nil } @@ -1015,6 +1017,19 @@ func (r *REST) ConvertToTable(ctx context.Context, object runtime.Object, tableO case *appsv1alpha1.Application: table = r.buildTableFromApplication(*obj) table.ListMeta.ResourceVersion = obj.GetResourceVersion() + case *unstructured.UnstructuredList: + apps := make([]appsv1alpha1.Application, 0, len(obj.Items)) + for _, u := range obj.Items { + var a appsv1alpha1.Application + err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &a) + if err != nil { + klog.Errorf("Failed to convert Unstructured to Application: %v", err) + continue + } + apps = append(apps, a) + } + table = r.buildTableFromApplications(apps) + table.ListMeta.ResourceVersion = obj.GetResourceVersion() case *unstructured.Unstructured: var app appsv1alpha1.Application err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &app) @@ -1046,7 +1061,6 @@ func (r *REST) ConvertToTable(ctx context.Context, object runtime.Object, tableO } klog.V(6).Infof("ConvertToTable: returning table with %d rows", len(table.Rows)) - return &table, nil }