Skip to content

Commit f31933b

Browse files
openshift-merge-bot[bot]mumesan
authored andcommitted
Merge pull request openstack-k8s-operators#572 from mumesan/remove_configmapfn_OSPRH-10696
Remove configMapFn and refactor findObjectsForSrc <JIRA: OSPRH-10696>
1 parent 0dabd1f commit f31933b

File tree

2 files changed

+66
-84
lines changed

2 files changed

+66
-84
lines changed

controllers/ironicapi_controller.go

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -224,44 +224,6 @@ func (r *IronicAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
224224

225225
// SetupWithManager sets up the controller with the Manager.
226226
func (r *IronicAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
227-
// watch for configmap where the CM owner label AND the CR.Spec.ManagingCrName label matches
228-
configMapFn := func(ctx context.Context, o client.Object) []reconcile.Request {
229-
Log := r.GetLogger(ctx)
230-
231-
result := []reconcile.Request{}
232-
233-
// get all API CRs
234-
apis := &ironicv1.IronicAPIList{}
235-
listOpts := []client.ListOption{
236-
client.InNamespace(o.GetNamespace()),
237-
}
238-
if err := r.Client.List(ctx, apis, listOpts...); err != nil {
239-
Log.Error(err, "Unable to retrieve API CRs %v")
240-
return nil
241-
}
242-
243-
label := o.GetLabels()
244-
// TODO: Just trying to verify that the CM is owned by this CR's managing CR
245-
if l, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(ironic.ServiceName))]; ok {
246-
for _, cr := range apis.Items {
247-
// return reconcil event for the CR where the CM owner label AND the parentIronicName matches
248-
if l == ironicv1.GetOwningIronicName(&cr) {
249-
// return namespace and Name of CR
250-
name := client.ObjectKey{
251-
Namespace: o.GetNamespace(),
252-
Name: cr.Name,
253-
}
254-
Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
255-
result = append(result, reconcile.Request{NamespacedName: name})
256-
}
257-
}
258-
}
259-
if len(result) > 0 {
260-
return result
261-
}
262-
return nil
263-
}
264-
265227
// index passwordSecretField
266228
if err := mgr.GetFieldIndexer().IndexField(ctx, &ironicv1.IronicAPI{}, passwordSecretField, func(rawObj client.Object) []string {
267229
// Extract the secret name from the spec, if one is provided
@@ -333,16 +295,15 @@ func (r *IronicAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
333295
Owns(&rbacv1.Role{}).
334296
Owns(&rbacv1.RoleBinding{}).
335297
// watch the config CMs we don't own
336-
Watches(&corev1.ConfigMap{},
337-
handler.EnqueueRequestsFromMapFunc(configMapFn)).
338298
Watches(
339299
&corev1.Secret{},
340300
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
341301
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
342302
).
343303
Watches(&topologyv1.Topology{},
344304
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
345-
builder.WithPredicates(predicate.GenerationChangedPredicate{})).
305+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
306+
).
346307
Complete(r)
347308
}
348309

@@ -351,8 +312,36 @@ func (r *IronicAPIReconciler) findObjectsForSrc(ctx context.Context, src client.
351312

352313
l := log.FromContext(ctx).WithName("Controllers").WithName("IronicAPI")
353314

315+
crList := &ironicv1.IronicAPIList{}
316+
listOpts := []client.ListOption{client.InNamespace(src.GetNamespace())}
317+
318+
if err := r.List(ctx, crList, listOpts...); err != nil {
319+
l.Error(err, "Unable to retrieve Conductor CRs %v")
320+
} else {
321+
label := src.GetLabels()
322+
// TODO: Just trying to verify that the Secret is owned by this CR's managing CR
323+
if lbl, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(ironic.ServiceName))]; ok {
324+
for _, item := range crList.Items {
325+
// return reconcile event for the CR where the Secret owner label AND the parentIronicName matches
326+
if lbl == ironicv1.GetOwningIronicName(&item) {
327+
// return Namespace and Name of CR
328+
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
329+
330+
requests = append(requests,
331+
reconcile.Request{
332+
NamespacedName: types.NamespacedName{
333+
Name: item.GetName(),
334+
Namespace: item.GetNamespace(),
335+
},
336+
},
337+
)
338+
339+
}
340+
}
341+
}
342+
}
343+
354344
for _, field := range ironicAPIWatchFields {
355-
crList := &ironicv1.IronicAPIList{}
356345
listOps := &client.ListOptions{
357346
FieldSelector: fields.OneTermEqualSelector(field, src.GetName()),
358347
Namespace: src.GetNamespace(),

controllers/ironicconductor_controller.go

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -207,43 +207,6 @@ func (r *IronicConductorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
207207

208208
// SetupWithManager sets up the controller with the Manager.
209209
func (r *IronicConductorReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
210-
// watch for configmap where the CM owner label AND the CR.Spec.ManagingCrName label matches
211-
configMapFn := func(ctx context.Context, o client.Object) []reconcile.Request {
212-
result := []reconcile.Request{}
213-
Log := r.GetLogger(ctx)
214-
215-
// get all API CRs
216-
apis := &ironicv1.IronicConductorList{}
217-
listOpts := []client.ListOption{
218-
client.InNamespace(o.GetNamespace()),
219-
}
220-
if err := r.Client.List(ctx, apis, listOpts...); err != nil {
221-
Log.Error(err, "Unable to retrieve API CRs %v")
222-
return nil
223-
}
224-
225-
label := o.GetLabels()
226-
// TODO: Just trying to verify that the CM is owned by this CR's managing CR
227-
if l, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(ironic.ServiceName))]; ok {
228-
for _, cr := range apis.Items {
229-
// return reconcil event for the CR where the CM owner label AND the parentIronicName matches
230-
if l == ironicv1.GetOwningIronicName(&cr) {
231-
// return namespace and Name of CR
232-
name := client.ObjectKey{
233-
Namespace: o.GetNamespace(),
234-
Name: cr.Name,
235-
}
236-
Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
237-
result = append(result, reconcile.Request{NamespacedName: name})
238-
}
239-
}
240-
}
241-
if len(result) > 0 {
242-
return result
243-
}
244-
return nil
245-
}
246-
247210
// index passwordSecretField
248211
if err := mgr.GetFieldIndexer().IndexField(ctx, &ironicv1.IronicConductor{}, passwordSecretField, func(rawObj client.Object) []string {
249212
// Extract the secret name from the spec, if one is provided
@@ -291,16 +254,16 @@ func (r *IronicConductorReconciler) SetupWithManager(ctx context.Context, mgr ct
291254
Owns(&rbacv1.Role{}).
292255
Owns(&rbacv1.RoleBinding{}).
293256
// watch the config CMs we don't own
294-
Watches(&corev1.ConfigMap{},
295-
handler.EnqueueRequestsFromMapFunc(configMapFn)).
296257
Watches(
297258
&corev1.Secret{},
298259
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
299260
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
300261
).
301-
Watches(&topologyv1.Topology{},
262+
Watches(
263+
&topologyv1.Topology{},
302264
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
303-
builder.WithPredicates(predicate.GenerationChangedPredicate{})).
265+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
266+
).
304267
Complete(r)
305268
}
306269

@@ -309,8 +272,38 @@ func (r *IronicConductorReconciler) findObjectsForSrc(ctx context.Context, src c
309272

310273
l := log.FromContext(ctx).WithName("Controllers").WithName("IronicConductor")
311274

275+
crList := &ironicv1.IronicConductorList{}
276+
namespace := src.GetNamespace()
277+
listOpts := []client.ListOption{client.InNamespace(namespace)}
278+
279+
if err := r.List(ctx, crList, listOpts...); err != nil {
280+
l.Error(err, "Unable to retrieve Conductor CRs %v")
281+
} else {
282+
label := src.GetLabels()
283+
// TODO: Just trying to verify that the Secret is owned by this CR's managing CR
284+
if lbl, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(ironic.ServiceName))]; ok {
285+
for _, item := range crList.Items {
286+
// return reconcil event for the CR where the Secret owner label AND the parentIronicName matches
287+
if lbl == ironicv1.GetOwningIronicName(&item) {
288+
// return Namespace and Name of CR
289+
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
290+
291+
requests = append(
292+
requests,
293+
reconcile.Request{
294+
NamespacedName: k8s_types.NamespacedName{
295+
Name: item.GetName(),
296+
Namespace: item.GetNamespace(),
297+
},
298+
},
299+
)
300+
301+
}
302+
}
303+
}
304+
}
305+
312306
for _, field := range ironicConductorWatchFields {
313-
crList := &ironicv1.IronicConductorList{}
314307
listOps := &client.ListOptions{
315308
FieldSelector: fields.OneTermEqualSelector(field, src.GetName()),
316309
Namespace: src.GetNamespace(),

0 commit comments

Comments
 (0)