|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package common //nolint:revive // common is the established package name for multi-group shared code |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "github.com/openstack-k8s-operators/lib-common/modules/common" |
| 25 | + "github.com/openstack-k8s-operators/lib-common/modules/common/util" |
| 26 | + "k8s.io/apimachinery/pkg/api/meta" |
| 27 | + "k8s.io/apimachinery/pkg/fields" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 32 | +) |
| 33 | + |
| 34 | +// FindObjectsForSrcByField returns reconcile requests for CRs in src's namespace |
| 35 | +// that reference src via one of the indexed watchFields. |
| 36 | +func FindObjectsForSrcByField( |
| 37 | + ctx context.Context, |
| 38 | + log logr.Logger, |
| 39 | + reader client.Reader, |
| 40 | + src client.Object, |
| 41 | + watchFields []string, |
| 42 | + newList func() client.ObjectList, |
| 43 | +) []reconcile.Request { |
| 44 | + var requests []reconcile.Request |
| 45 | + |
| 46 | + for _, field := range watchFields { |
| 47 | + crList := newList() |
| 48 | + listOps := &client.ListOptions{ |
| 49 | + FieldSelector: fields.OneTermEqualSelector(field, src.GetName()), |
| 50 | + Namespace: src.GetNamespace(), |
| 51 | + } |
| 52 | + err := reader.List(ctx, crList, listOps) |
| 53 | + if err != nil { |
| 54 | + log.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GetObjectKind().GroupVersionKind().Kind, field, src.GetNamespace())) |
| 55 | + return requests |
| 56 | + } |
| 57 | + |
| 58 | + items, err := meta.ExtractList(crList) |
| 59 | + if err != nil { |
| 60 | + log.Error(err, fmt.Sprintf("extracting items from %s", crList.GetObjectKind().GroupVersionKind().Kind)) |
| 61 | + return requests |
| 62 | + } |
| 63 | + requests = appendRequestsForObjects(log, src, requests, items) |
| 64 | + } |
| 65 | + |
| 66 | + return requests |
| 67 | +} |
| 68 | + |
| 69 | +// FindObjectsForSrcInNamespace returns reconcile requests for all CRs in src's namespace. |
| 70 | +func FindObjectsForSrcInNamespace( |
| 71 | + ctx context.Context, |
| 72 | + log logr.Logger, |
| 73 | + reader client.Reader, |
| 74 | + src client.Object, |
| 75 | + newList func() client.ObjectList, |
| 76 | +) []reconcile.Request { |
| 77 | + crList := newList() |
| 78 | + listOps := &client.ListOptions{ |
| 79 | + Namespace: src.GetNamespace(), |
| 80 | + } |
| 81 | + err := reader.List(ctx, crList, listOps) |
| 82 | + if err != nil { |
| 83 | + log.Error(err, fmt.Sprintf("listing %s for namespace: %s", crList.GetObjectKind().GroupVersionKind().Kind, src.GetNamespace())) |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + items, err := meta.ExtractList(crList) |
| 88 | + if err != nil { |
| 89 | + log.Error(err, fmt.Sprintf("extracting items from %s", crList.GetObjectKind().GroupVersionKind().Kind)) |
| 90 | + return nil |
| 91 | + } |
| 92 | + return appendRequestsForObjects(log, src, nil, items) |
| 93 | +} |
| 94 | + |
| 95 | +// FindObjectsWithAppSelectorLabelInNamespace returns reconcile requests for all CRs |
| 96 | +// in src's namespace when src carries an AppSelector label matching allowedServices. |
| 97 | +func FindObjectsWithAppSelectorLabelInNamespace( |
| 98 | + ctx context.Context, |
| 99 | + log logr.Logger, |
| 100 | + reader client.Reader, |
| 101 | + src client.Object, |
| 102 | + allowedServices []string, |
| 103 | + newList func() client.ObjectList, |
| 104 | +) []reconcile.Request { |
| 105 | + if svc, ok := src.GetLabels()[common.AppSelector]; ok && util.StringInSlice(svc, allowedServices) { |
| 106 | + return FindObjectsForSrcInNamespace(ctx, log, reader, src, newList) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func appendRequestsForObjects( |
| 113 | + log logr.Logger, |
| 114 | + src client.Object, |
| 115 | + requests []reconcile.Request, |
| 116 | + items []runtime.Object, |
| 117 | +) []reconcile.Request { |
| 118 | + for _, item := range items { |
| 119 | + accessor, err := meta.Accessor(item) |
| 120 | + if err != nil { |
| 121 | + log.Error(err, "extracting object metadata") |
| 122 | + continue |
| 123 | + } |
| 124 | + log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), accessor.GetName(), accessor.GetNamespace())) |
| 125 | + |
| 126 | + requests = append(requests, |
| 127 | + reconcile.Request{ |
| 128 | + NamespacedName: types.NamespacedName{ |
| 129 | + Name: accessor.GetName(), |
| 130 | + Namespace: accessor.GetNamespace(), |
| 131 | + }, |
| 132 | + }, |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + return requests |
| 137 | +} |
0 commit comments