|
| 1 | +/* |
| 2 | +Copyright 2026 The KCP Authors. |
| 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 sync |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "go.uber.org/zap" |
| 23 | + |
| 24 | + "github.com/kcp-dev/api-syncagent/internal/sync/templating" |
| 25 | + |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/client-go/util/workqueue" |
| 29 | + ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 32 | + mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile" |
| 33 | +) |
| 34 | + |
| 35 | +// byOwnerEventHandler enqueues the primary object by inspecting the OwnerReferences |
| 36 | +// of the changed related object and finding one with the configured Kind. |
| 37 | +type byOwnerEventHandler struct { |
| 38 | + clusterName string |
| 39 | + ownerKind string |
| 40 | +} |
| 41 | + |
| 42 | +func (h *byOwnerEventHandler) Create(_ context.Context, evt event.TypedCreateEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 43 | + h.enqueue(evt.Object, q) |
| 44 | +} |
| 45 | + |
| 46 | +func (h *byOwnerEventHandler) Update(_ context.Context, evt event.TypedUpdateEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 47 | + h.enqueue(evt.ObjectNew, q) |
| 48 | +} |
| 49 | + |
| 50 | +func (h *byOwnerEventHandler) Delete(_ context.Context, evt event.TypedDeleteEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 51 | + h.enqueue(evt.Object, q) |
| 52 | +} |
| 53 | + |
| 54 | +func (h *byOwnerEventHandler) Generic(_ context.Context, evt event.TypedGenericEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 55 | + h.enqueue(evt.Object, q) |
| 56 | +} |
| 57 | + |
| 58 | +func (h *byOwnerEventHandler) enqueue(obj *unstructured.Unstructured, q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 59 | + for _, ref := range obj.GetOwnerReferences() { |
| 60 | + if ref.Kind == h.ownerKind { |
| 61 | + q.Add(mcreconcile.Request{ |
| 62 | + ClusterName: h.clusterName, |
| 63 | + Request: reconcile.Request{ |
| 64 | + NamespacedName: types.NamespacedName{ |
| 65 | + Namespace: obj.GetNamespace(), |
| 66 | + Name: ref.Name, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) |
| 70 | + return |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// byLabelEventHandler enqueues primary objects by evaluating label templates against |
| 76 | +// the changed related object and listing primaries matching the resulting label selector. |
| 77 | +type byLabelEventHandler struct { |
| 78 | + clusterName string |
| 79 | + client ctrlruntimeclient.Client |
| 80 | + primaryDummy *unstructured.Unstructured |
| 81 | + labelTemplates map[string]string |
| 82 | + log *zap.SugaredLogger |
| 83 | +} |
| 84 | + |
| 85 | +func (h *byLabelEventHandler) Create(ctx context.Context, evt event.TypedCreateEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 86 | + h.enqueue(ctx, evt.Object, q) |
| 87 | +} |
| 88 | + |
| 89 | +func (h *byLabelEventHandler) Update(ctx context.Context, evt event.TypedUpdateEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 90 | + h.enqueue(ctx, evt.ObjectNew, q) |
| 91 | +} |
| 92 | + |
| 93 | +func (h *byLabelEventHandler) Delete(ctx context.Context, evt event.TypedDeleteEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 94 | + h.enqueue(ctx, evt.Object, q) |
| 95 | +} |
| 96 | + |
| 97 | +func (h *byLabelEventHandler) Generic(ctx context.Context, evt event.TypedGenericEvent[*unstructured.Unstructured], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 98 | + h.enqueue(ctx, evt.Object, q) |
| 99 | +} |
| 100 | + |
| 101 | +func (h *byLabelEventHandler) enqueue(ctx context.Context, obj *unstructured.Unstructured, q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) { |
| 102 | + // Build the template context using the changed related object. |
| 103 | + data := map[string]any{ |
| 104 | + "watchObject": map[string]any{ |
| 105 | + "name": obj.GetName(), |
| 106 | + "namespace": obj.GetNamespace(), |
| 107 | + "labels": obj.GetLabels(), |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + // Evaluate each label template to build the selector. |
| 112 | + matchingLabels := ctrlruntimeclient.MatchingLabels{} |
| 113 | + for key, tpl := range h.labelTemplates { |
| 114 | + value, err := templating.Render(tpl, data) |
| 115 | + if err != nil { |
| 116 | + h.log.Warnw("Failed to evaluate byLabel template", "key", key, "template", tpl, "error", err) |
| 117 | + return |
| 118 | + } |
| 119 | + matchingLabels[key] = value |
| 120 | + } |
| 121 | + |
| 122 | + // List primary objects matching the derived label selector. |
| 123 | + primaryList := &unstructured.UnstructuredList{} |
| 124 | + primaryList.SetAPIVersion(h.primaryDummy.GetAPIVersion()) |
| 125 | + primaryList.SetKind(h.primaryDummy.GetKind() + "List") |
| 126 | + |
| 127 | + if err := h.client.List(ctx, primaryList, matchingLabels); err != nil { |
| 128 | + h.log.Warnw("Failed to list primary objects for byLabel watch", "selector", matchingLabels, "error", err) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + for i := range primaryList.Items { |
| 133 | + primary := &primaryList.Items[i] |
| 134 | + q.Add(mcreconcile.Request{ |
| 135 | + ClusterName: h.clusterName, |
| 136 | + Request: reconcile.Request{ |
| 137 | + NamespacedName: types.NamespacedName{ |
| 138 | + Namespace: primary.GetNamespace(), |
| 139 | + Name: primary.GetName(), |
| 140 | + }, |
| 141 | + }, |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments