@@ -58,7 +58,6 @@ import (
58
58
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
59
59
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
60
60
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
61
- "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/objects"
62
61
)
63
62
64
63
const (
@@ -171,24 +170,33 @@ func patchServer(ctx context.Context, patchHelper *patch.Helper, openStackServer
171
170
}
172
171
173
172
func (r * OpenStackServerReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , _ controller.Options ) error {
173
+ const imageRefPath = "spec.image.imageRef.name"
174
+
174
175
log := ctrl .LoggerFrom (ctx )
175
176
177
+ // Index servers by referenced image
178
+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & infrav1alpha1.OpenStackServer {}, imageRefPath , func (obj client.Object ) []string {
179
+ server , ok := obj .(* infrav1alpha1.OpenStackServer )
180
+ if ! ok {
181
+ return nil
182
+ }
183
+ if server .Spec .Image .ImageRef == nil {
184
+ return nil
185
+ }
186
+ return []string {server .Spec .Image .ImageRef .Name }
187
+ }); err != nil {
188
+ return fmt .Errorf ("adding servers by image index: %w" , err )
189
+ }
190
+
176
191
return ctrl .NewControllerManagedBy (mgr ).
177
192
For (& infrav1alpha1.OpenStackServer {}).
178
193
Watches (& orcv1alpha1.Image {},
179
194
handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , obj client.Object ) []reconcile.Request {
180
195
log = log .WithValues ("watch" , "Image" )
181
196
182
197
k8sClient := mgr .GetClient ()
183
- label , err := objToDependencyLabel (obj , k8sClient .Scheme ())
184
- if err != nil {
185
- log .Error (err , "objToDependencyLabel" )
186
- }
187
-
188
- labels := map [string ]string {label : "" }
189
198
serverList := & infrav1alpha1.OpenStackServerList {}
190
- err = k8sClient .List (ctx , serverList , client .MatchingLabels (labels ), client .InNamespace (obj .GetNamespace ()))
191
- if err != nil {
199
+ if err := k8sClient .List (ctx , serverList , client .InNamespace (obj .GetNamespace ()), client.MatchingFields {imageRefPath : obj .GetName ()}); err != nil {
192
200
log .Error (err , "listing OpenStackServers" )
193
201
return nil
194
202
}
@@ -267,27 +275,6 @@ func (r *OpenStackServerReconciler) reconcileDelete(scope *scope.WithLogger, ope
267
275
return nil
268
276
}
269
277
270
- func objToDependencyLabel (obj client.Object , scheme * runtime.Scheme ) (string , error ) {
271
- gvk , err := objects .GetGVK (obj , scheme )
272
- if err != nil {
273
- return "" , err
274
- }
275
-
276
- if gvk .Group != orcv1alpha1 .SchemeGroupVersion .Group {
277
- return "" , fmt .Errorf ("dependency object has unexpected group %s" , gvk .Group )
278
- }
279
-
280
- var prefixType string
281
- switch gvk .Kind {
282
- case "Image" :
283
- prefixType = "image"
284
- default :
285
- return "" , fmt .Errorf ("dependency object has unexpected kind %s" , gvk .Kind )
286
- }
287
-
288
- return "orc-dependency-" + prefixType + "/" + obj .GetName (), nil
289
- }
290
-
291
278
func IsServerTerminalError (server * infrav1alpha1.OpenStackServer ) bool {
292
279
if server .Status .InstanceState != nil && * server .Status .InstanceState == infrav1 .InstanceStateError {
293
280
return true
@@ -310,31 +297,7 @@ func (r *OpenStackServerReconciler) reconcileNormal(ctx context.Context, scope *
310
297
openStackServer .SetLabels (labels )
311
298
}
312
299
313
- changed , dependencies , resolveDone , err := compute .ResolveServerSpec (ctx , scope , r .Client , openStackServer )
314
-
315
- errs := make ([]error , 0 , len (dependencies )+ 1 )
316
- errs = append (errs , err )
317
-
318
- // Add a dependency label for every object we depend on
319
- // Set changed if we update any labels
320
- // Collate all errors and return in a single transaction
321
- for i := range dependencies {
322
- errs = append (errs , func () error {
323
- dependency := dependencies [i ]
324
- label , err := objToDependencyLabel (dependency , r .Client .Scheme ())
325
- if err != nil {
326
- return err
327
- }
328
-
329
- if _ , ok := labels [label ]; ! ok {
330
- labels [label ] = ""
331
- changed = true
332
- }
333
- return nil
334
- }())
335
- }
336
- err = errors .Join (errs ... )
337
-
300
+ changed , resolveDone , err := compute .ResolveServerSpec (ctx , scope , r .Client , openStackServer )
338
301
if err != nil || ! resolveDone {
339
302
return ctrl.Result {}, err
340
303
}
0 commit comments