@@ -28,7 +28,6 @@ import (
2828 helmv2 "github.com/fluxcd/helm-controller/api/v2"
2929 metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
3030 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3231 fields "k8s.io/apimachinery/pkg/fields"
3332 labels "k8s.io/apimachinery/pkg/labels"
3433 "k8s.io/apimachinery/pkg/runtime"
@@ -142,17 +141,9 @@ func (r *REST) GetSingularName() string {
142141// Create handles the creation of a new Application by converting it to a HelmRelease
143142func (r * REST ) Create (ctx context.Context , obj runtime.Object , createValidation rest.ValidateObjectFunc , options * metav1.CreateOptions ) (runtime.Object , error ) {
144143 // Assert the object is of type Application
145- us , ok := obj .(* unstructured. Unstructured )
144+ app , ok := obj .(* appsv1alpha1. Application )
146145 if ! ok {
147- return nil , fmt .Errorf ("expected unstructured.Unstructured object, got %T" , obj )
148- }
149-
150- app := & appsv1alpha1.Application {}
151-
152- if err := runtime .DefaultUnstructuredConverter .FromUnstructured (us .Object , app ); err != nil {
153- errMsg := fmt .Sprintf ("returned unstructured.Unstructured object was not an Application" )
154- klog .Errorf (errMsg )
155- return nil , fmt .Errorf (errMsg )
146+ return nil , fmt .Errorf ("expected *appsv1alpha1.Application object, got %T" , obj )
156147 }
157148
158149 // Convert Application to HelmRelease
@@ -186,15 +177,14 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
186177
187178 klog .V (6 ).Infof ("Successfully created and converted HelmRelease %s to Application" , helmRelease .GetName ())
188179
189- // Convert Application to unstructured format
190- unstructuredApp , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& convertedApp )
191- if err != nil {
192- klog .Errorf ("Failed to convert Application to unstructured for resource %s: %v" , convertedApp .GetName (), err )
193- return nil , fmt .Errorf ("failed to convert Application to unstructured: %v" , err )
180+ // Explicitly set apiVersion and kind for Application
181+ convertedApp .TypeMeta = metav1.TypeMeta {
182+ APIVersion : "apps.cozystack.io/v1alpha1" ,
183+ Kind : r .kindName ,
194184 }
195185
196- klog .V (6 ).Infof ("Successfully retrieved and converted resource %s of type %s to unstructured " , convertedApp .GetName (), r .gvr .Resource )
197- return & unstructured. Unstructured { Object : unstructuredApp } , nil
186+ klog .V (6 ).Infof ("Successfully retrieved and converted resource %s of type %s" , convertedApp .GetName (), r .gvr .Resource )
187+ return & convertedApp , nil
198188}
199189
200190// Get retrieves an Application by converting the corresponding HelmRelease
@@ -244,19 +234,8 @@ func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions)
244234 Kind : r .kindName ,
245235 }
246236
247- // Convert Application to unstructured format
248- unstructuredApp , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& convertedApp )
249- if err != nil {
250- klog .Errorf ("Failed to convert Application to unstructured for resource %s: %v" , name , err )
251- return nil , fmt .Errorf ("failed to convert Application to unstructured: %v" , err )
252- }
253-
254- // Explicitly set apiVersion and kind in unstructured object
255- unstructuredApp ["apiVersion" ] = "apps.cozystack.io/v1alpha1"
256- unstructuredApp ["kind" ] = r .kindName
257-
258- klog .V (6 ).Infof ("Successfully retrieved and converted resource %s of kind %s to unstructured" , name , r .gvr .Resource )
259- return & unstructured.Unstructured {Object : unstructuredApp }, nil
237+ klog .V (6 ).Infof ("Successfully retrieved and converted resource %s of kind %s" , name , r .gvr .Resource )
238+ return & convertedApp , nil
260239}
261240
262241// List retrieves a list of Applications by converting HelmReleases
@@ -339,8 +318,8 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
339318 return nil , err
340319 }
341320
342- // Initialize unstructured items array
343- items := make ([]unstructured. Unstructured , 0 )
321+ // Initialize Application items array
322+ items := make ([]appsv1alpha1. Application , 0 )
344323
345324 // Iterate over HelmReleases and convert to Applications
346325 for i := range hrList .Items {
@@ -354,6 +333,12 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
354333 continue
355334 }
356335
336+ // Explicitly set apiVersion and kind for Application
337+ app .TypeMeta = metav1.TypeMeta {
338+ APIVersion : "apps.cozystack.io/v1alpha1" ,
339+ Kind : r .kindName ,
340+ }
341+
357342 // If resourceName is set, check for match
358343 if resourceName != "" && app .Name != resourceName {
359344 continue
@@ -387,17 +372,11 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
387372 }
388373 }
389374
390- // Convert Application to unstructured
391- unstructuredApp , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& app )
392- if err != nil {
393- klog .Errorf ("Error converting Application %s to unstructured: %v" , app .Name , err )
394- continue
395- }
396- items = append (items , unstructured.Unstructured {Object : unstructuredApp })
375+ items = append (items , app )
397376 }
398377
399- // Explicitly set apiVersion and kind in unstructured object
400- appList := r .NewList ().(* unstructured. UnstructuredList )
378+ // Create ApplicationList with proper kind
379+ appList := r .NewList ().(* appsv1alpha1. ApplicationList )
401380 appList .SetResourceVersion (hrList .GetResourceVersion ())
402381 appList .Items = items
403382
@@ -447,16 +426,9 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
447426 }
448427
449428 // Assert the new object is of type Application
450- us , ok := newObj .(* unstructured. Unstructured )
429+ app , ok := newObj .(* appsv1alpha1. Application )
451430 if ! ok {
452- errMsg := fmt .Sprintf ("expected unstructured.Unstructured object, got %T" , newObj )
453- klog .Errorf (errMsg )
454- return nil , false , fmt .Errorf (errMsg )
455- }
456- app := & appsv1alpha1.Application {}
457-
458- if err := runtime .DefaultUnstructuredConverter .FromUnstructured (us .Object , app ); err != nil {
459- errMsg := fmt .Sprintf ("returned unstructured.Unstructured object was not an Application" )
431+ errMsg := fmt .Sprintf ("expected *appsv1alpha1.Application object, got %T" , newObj )
460432 klog .Errorf (errMsg )
461433 return nil , false , fmt .Errorf (errMsg )
462434 }
@@ -523,18 +495,9 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
523495 Kind : r .kindName ,
524496 }
525497
526- // Convert Application to unstructured format
527- unstructuredApp , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& convertedApp )
528- if err != nil {
529- klog .Errorf ("Failed to convert Application to unstructured for resource %s: %v" , convertedApp .GetName (), err )
530- return nil , false , fmt .Errorf ("failed to convert Application to unstructured: %v" , err )
531- }
532- obj := & unstructured.Unstructured {Object : unstructuredApp }
533- obj .SetGroupVersionKind (r .gvk )
534-
535- klog .V (6 ).Infof ("Returning patched Application object: %+v" , unstructuredApp )
498+ klog .V (6 ).Infof ("Returning updated Application object: %+v" , convertedApp )
536499
537- return obj , false , nil
500+ return & convertedApp , false , nil
538501}
539502
540503// Delete removes an Application by deleting the corresponding HelmRelease
@@ -728,19 +691,16 @@ func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptio
728691 }
729692 }
730693
731- // Convert Application to unstructured
732- unstructuredApp , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& app )
733- if err != nil {
734- klog .Errorf ("Failed to convert Application to unstructured: %v" , err )
735- continue
694+ // Explicitly set apiVersion and kind for Application
695+ app .TypeMeta = metav1.TypeMeta {
696+ APIVersion : "apps.cozystack.io/v1alpha1" ,
697+ Kind : r .kindName ,
736698 }
737- obj := & unstructured.Unstructured {Object : unstructuredApp }
738- obj .SetGroupVersionKind (r .gvk )
739699
740700 // Create watch event with Application object
741701 appEvent := watch.Event {
742702 Type : event .Type ,
743- Object : obj ,
703+ Object : & app ,
744704 }
745705
746706 // Send event to custom watcher
@@ -766,8 +726,8 @@ func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptio
766726
767727// Helper function to get HelmRelease name from object
768728func helmReleaseName (obj runtime.Object ) string {
769- if u , ok := obj .(* unstructured. Unstructured ); ok {
770- return u .GetName ()
729+ if app , ok := obj .(* appsv1alpha1. Application ); ok {
730+ return app .GetName ()
771731 }
772732 return "<unknown>"
773733}
@@ -1059,56 +1019,6 @@ func (r *REST) ConvertToTable(ctx context.Context, object runtime.Object, tableO
10591019 case * appsv1alpha1.Application :
10601020 table = r .buildTableFromApplication (* obj )
10611021 table .ListMeta .ResourceVersion = obj .GetResourceVersion ()
1062- case * unstructured.UnstructuredList :
1063- apps := make ([]appsv1alpha1.Application , 0 , len (obj .Items ))
1064- for _ , u := range obj .Items {
1065- var a appsv1alpha1.Application
1066- err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .Object , & a )
1067- if err != nil {
1068- klog .Errorf ("Failed to convert Unstructured to Application: %v" , err )
1069- continue
1070- }
1071- apps = append (apps , a )
1072- }
1073- table = r .buildTableFromApplications (apps )
1074- table .ListMeta .ResourceVersion = obj .GetResourceVersion ()
1075- case * unstructured.Unstructured :
1076- var apps []appsv1alpha1.Application
1077- for {
1078- var items interface {}
1079- var ok bool
1080- var objects []unstructured.Unstructured
1081- if items , ok = obj .Object ["items" ]; ! ok {
1082- break
1083- }
1084- if objects , ok = items .([]unstructured.Unstructured ); ! ok {
1085- break
1086- }
1087- apps = make ([]appsv1alpha1.Application , 0 , len (objects ))
1088- var a appsv1alpha1.Application
1089- for i := range objects {
1090- err := runtime .DefaultUnstructuredConverter .FromUnstructured (objects [i ].Object , & a )
1091- if err != nil {
1092- klog .Errorf ("Failed to convert Unstructured to Application: %v" , err )
1093- continue
1094- }
1095- apps = append (apps , a )
1096- }
1097- break
1098- }
1099- if apps != nil {
1100- table = r .buildTableFromApplications (apps )
1101- table .ListMeta .ResourceVersion = obj .GetResourceVersion ()
1102- break
1103- }
1104- var app appsv1alpha1.Application
1105- err := runtime .DefaultUnstructuredConverter .FromUnstructured (obj .UnstructuredContent (), & app )
1106- if err != nil {
1107- klog .Errorf ("Failed to convert Unstructured to Application: %v" , err )
1108- return nil , fmt .Errorf ("failed to convert Unstructured to Application: %v" , err )
1109- }
1110- table = r .buildTableFromApplication (app )
1111- table .ListMeta .ResourceVersion = obj .GetResourceVersion ()
11121022 default :
11131023 resource := schema.GroupResource {}
11141024 if info , ok := request .RequestInfoFrom (ctx ); ok {
@@ -1237,15 +1147,21 @@ func (r *REST) Destroy() {
12371147
12381148// New creates a new instance of Application
12391149func (r * REST ) New () runtime.Object {
1240- obj := & unstructured.Unstructured {}
1241- obj .SetGroupVersionKind (r .gvk )
1150+ obj := & appsv1alpha1.Application {}
1151+ obj .TypeMeta = metav1.TypeMeta {
1152+ APIVersion : r .gvk .GroupVersion ().String (),
1153+ Kind : r .kindName ,
1154+ }
12421155 return obj
12431156}
12441157
12451158// NewList returns an empty list of Application objects
12461159func (r * REST ) NewList () runtime.Object {
1247- obj := & unstructured.UnstructuredList {}
1248- obj .SetGroupVersionKind (r .gvk .GroupVersion ().WithKind (r .kindName + "List" ))
1160+ obj := & appsv1alpha1.ApplicationList {}
1161+ obj .TypeMeta = metav1.TypeMeta {
1162+ APIVersion : r .gvk .GroupVersion ().String (),
1163+ Kind : r .kindName + "List" ,
1164+ }
12491165 return obj
12501166}
12511167
0 commit comments