@@ -15,10 +15,11 @@ import (
1515 "github.com/krateoplatformops/unstructured-runtime/pkg/controller"
1616 "github.com/krateoplatformops/unstructured-runtime/pkg/logging"
1717 "github.com/krateoplatformops/unstructured-runtime/pkg/meta"
18+ "github.com/krateoplatformops/unstructured-runtime/pkg/pluralizer"
1819 "github.com/krateoplatformops/unstructured-runtime/pkg/tools/unstructured/condition"
1920 "github.com/lucasepe/httplib"
2021
21- "github.com/krateoplatformops/rest-dynamic-controller/internal /tools"
22+ "github.com/krateoplatformops/unstructured-runtime/pkg /tools"
2223 unstructuredtools "github.com/krateoplatformops/unstructured-runtime/pkg/tools/unstructured"
2324 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -29,7 +30,7 @@ import (
2930
3031var _ controller.ExternalClient = (* handler )(nil )
3132
32- func NewHandler (cfg * rest.Config , log logging.Logger , swg getter.Getter ) controller.ExternalClient {
33+ func NewHandler (cfg * rest.Config , log logging.Logger , swg getter.Getter , pluralizer pluralizer. Pluralizer ) controller.ExternalClient {
3334 dyn , err := dynamic .NewForConfig (cfg )
3435 if err != nil {
3536 log .Debug ("Creating dynamic client" , "error" , err )
@@ -41,6 +42,7 @@ func NewHandler(cfg *rest.Config, log logging.Logger, swg getter.Getter) control
4142 }
4243
4344 return & handler {
45+ pluralizer : pluralizer ,
4446 logger : log ,
4547 dynamicClient : dyn ,
4648 discoveryClient : dis ,
@@ -49,6 +51,7 @@ func NewHandler(cfg *rest.Config, log logging.Logger, swg getter.Getter) control
4951}
5052
5153type handler struct {
54+ pluralizer pluralizer.Pluralizer
5255 logger logging.Logger
5356 dynamicClient dynamic.Interface
5457 discoveryClient * discovery.DiscoveryClient
@@ -74,10 +77,14 @@ func (h *handler) Observe(ctx context.Context, mg *unstructured.Unstructured) (c
7477 log .Debug ("Swagger info is nil" )
7578 return controller.ExternalObservation {}, fmt .Errorf ("swagger info is nil" )
7679 }
77- tools .Update (ctx , mg , tools.UpdateOptions {
78- DiscoveryClient : h . discoveryClient ,
79- DynamicClient : h .dynamicClient ,
80+ mg , err = tools .Update (ctx , mg , tools.UpdateOptions {
81+ Pluralizer : h . pluralizer ,
82+ DynamicClient : h .dynamicClient ,
8083 })
84+ if err != nil {
85+ log .Debug ("Updating CR" , "error" , err )
86+ return controller.ExternalObservation {}, err
87+ }
8188
8289 cli , err := restclient .BuildClient (clientInfo .URL )
8390 if err != nil {
@@ -143,13 +150,16 @@ func (h *handler) Observe(ctx context.Context, mg *unstructured.Unstructured) (c
143150 log .Debug ("Setting condition" , "error" , err )
144151 return controller.ExternalObservation {}, err
145152 }
153+
154+ _ , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
155+ Pluralizer : h .pluralizer ,
156+ DynamicClient : h .dynamicClient ,
157+ })
158+
146159 return controller.ExternalObservation {
147- ResourceExists : true ,
148- ResourceUpToDate : true ,
149- }, tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
150- DiscoveryClient : h .discoveryClient ,
151- DynamicClient : h .dynamicClient ,
152- })
160+ ResourceExists : true ,
161+ ResourceUpToDate : true ,
162+ }, err
153163 }
154164 if err != nil {
155165 log .Debug ("Building API call" , "error" , err )
@@ -178,9 +188,9 @@ func (h *handler) Observe(ctx context.Context, mg *unstructured.Unstructured) (c
178188 return controller.ExternalObservation {}, err
179189 }
180190
181- err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
182- DiscoveryClient : h . discoveryClient ,
183- DynamicClient : h .dynamicClient ,
191+ mg , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
192+ Pluralizer : h . pluralizer ,
193+ DynamicClient : h .dynamicClient ,
184194 })
185195 if err != nil {
186196 log .Debug ("Updating status" , "error" , err )
@@ -208,9 +218,9 @@ func (h *handler) Observe(ctx context.Context, mg *unstructured.Unstructured) (c
208218 log .Debug ("Setting condition" , "error" , err )
209219 return controller.ExternalObservation {}, err
210220 }
211- err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
212- DiscoveryClient : h . discoveryClient ,
213- DynamicClient : h .dynamicClient ,
221+ mg , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
222+ Pluralizer : h . pluralizer ,
223+ DynamicClient : h .dynamicClient ,
214224 })
215225 if err != nil {
216226 log .Debug ("Updating status" , "error" , err )
@@ -281,9 +291,10 @@ func (h *handler) Create(ctx context.Context, mg *unstructured.Unstructured) err
281291 return err
282292 }
283293
284- err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
285- DiscoveryClient : h .discoveryClient ,
286- DynamicClient : h .dynamicClient ,
294+ _ , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
295+
296+ Pluralizer : h .pluralizer ,
297+ DynamicClient : h .dynamicClient ,
287298 })
288299 if err != nil {
289300 log .Debug ("Updating status" , "error" , err )
@@ -356,18 +367,18 @@ func (h *handler) Update(ctx context.Context, mg *unstructured.Unstructured) err
356367 return err
357368 }
358369
359- err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
360- DiscoveryClient : h . discoveryClient ,
361- DynamicClient : h .dynamicClient ,
370+ mg , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
371+ Pluralizer : h . pluralizer ,
372+ DynamicClient : h .dynamicClient ,
362373 })
363374 if err != nil {
364375 log .Debug ("Updating status" , "error" , err )
365376 return err
366377 }
367378
368- err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
369- DiscoveryClient : h . discoveryClient ,
370- DynamicClient : h .dynamicClient ,
379+ mg , err = tools .UpdateStatus (ctx , mg , tools.UpdateOptions {
380+ Pluralizer : h . pluralizer ,
381+ DynamicClient : h .dynamicClient ,
371382 })
372383 if err != nil {
373384 log .Debug ("Updating status" , "error" , err )
@@ -419,7 +430,7 @@ func (h *handler) Delete(ctx context.Context, mg *unstructured.Unstructured) err
419430 apiCall , callInfo , err := APICallBuilder (cli , clientInfo , apiaction .Delete )
420431 if apiCall == nil {
421432 log .Debug ("API call not found" , "action" , apiaction .Delete )
422- return removeFinalizersAndUpdate (ctx , log , h .discoveryClient , h .dynamicClient , mg )
433+ return removeFinalizersAndUpdate (ctx , log , h .pluralizer , h .dynamicClient , mg )
423434 }
424435 if err != nil {
425436 log .Debug ("Building API call" , "error" , err )
@@ -444,5 +455,5 @@ func (h *handler) Delete(ctx context.Context, mg *unstructured.Unstructured) err
444455 return err
445456 }
446457
447- return removeFinalizersAndUpdate (ctx , log , h .discoveryClient , h .dynamicClient , mg )
458+ return removeFinalizersAndUpdate (ctx , log , h .pluralizer , h .dynamicClient , mg )
448459}
0 commit comments