/
eventhandlers.go
284 lines (240 loc) · 9.15 KB
/
eventhandlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package serviceexportpropagation
import (
"context"
networkingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/networking/v1alpha1"
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/karmada-io/multicluster-cloud-provider/pkg/controllers/indexes"
"github.com/karmada-io/multicluster-cloud-provider/pkg/util"
)
func newServiceEventHandler(ctx context.Context, client client.Client) handler.EventHandler {
return &serviceEventHandler{
ctx: ctx,
client: client,
}
}
var _ handler.EventHandler = (*serviceEventHandler)(nil)
type serviceEventHandler struct {
ctx context.Context
client client.Client
}
func (h *serviceEventHandler) Create(ctx context.Context, e event.CreateEvent, queue workqueue.RateLimitingInterface) {
mciList := &networkingv1alpha1.MultiClusterIngressList{}
if err := h.client.List(h.ctx, mciList,
client.InNamespace(e.Object.GetNamespace()),
client.MatchingFields{indexes.IndexKeyServiceRefName: e.Object.GetName()}); err != nil {
klog.Errorf("failed to fetch multiclusteringresses")
return
}
if len(mciList.Items) > 0 {
queue.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: e.Object.GetNamespace(),
Name: e.Object.GetName(),
}})
return
}
mcs := &networkingv1alpha1.MultiClusterService{}
if err := h.client.Get(h.ctx, types.NamespacedName{
Namespace: e.Object.GetNamespace(),
Name: e.Object.GetName(),
}, mcs); err != nil {
return
}
queue.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: e.Object.GetNamespace(),
Name: e.Object.GetName(),
}})
}
func (h *serviceEventHandler) Update(ctx context.Context, _ event.UpdateEvent, _ workqueue.RateLimitingInterface) {
// We only need to create ServiceExport based on the service and propagate it to
// member clusters. Therefore, we do not need to pay attention to service update.
}
func (h *serviceEventHandler) Delete(ctx context.Context, _ event.DeleteEvent, queue workqueue.RateLimitingInterface) {
// We will add an ownerReference to the service object on the ServiceExport
// object, so that cleanup will be handled by gc controller.
}
func (h *serviceEventHandler) Generic(ctx context.Context, e event.GenericEvent, queue workqueue.RateLimitingInterface) {
queue.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: e.Object.GetNamespace(),
Name: e.Object.GetName(),
}})
}
func newMultiClusterIngressEventHandler(ctx context.Context, client client.Client, svcEventChan chan<- event.GenericEvent, providerClassName string) handler.EventHandler {
return &multiClusterIngressEventHandler{
ctx: ctx,
client: client,
svcEventChan: svcEventChan,
ingClassName: providerClassName,
}
}
var _ handler.EventHandler = (*multiClusterIngressEventHandler)(nil)
type multiClusterIngressEventHandler struct {
ctx context.Context
client client.Client
svcEventChan chan<- event.GenericEvent
ingClassName string
}
func (h *multiClusterIngressEventHandler) Create(ctx context.Context, e event.CreateEvent, _ workqueue.RateLimitingInterface) {
mci := e.Object.(*networkingv1alpha1.MultiClusterIngress)
if !util.CheckIngressClassMatched(h.ctx, h.client, mci, h.ingClassName) {
return
}
h.enqueueImpactedService(mci)
}
func (h *multiClusterIngressEventHandler) Update(ctx context.Context, e event.UpdateEvent, _ workqueue.RateLimitingInterface) {
mciOld := e.ObjectOld.(*networkingv1alpha1.MultiClusterIngress)
mciNew := e.ObjectNew.(*networkingv1alpha1.MultiClusterIngress)
if !util.CheckIngressClassMatched(h.ctx, h.client, mciNew, h.ingClassName) {
return
}
// We only care about the service update:
oldSvcRefs := indexes.BuildServiceRefIndexes(mciOld)
newSvcRefs := indexes.BuildServiceRefIndexes(mciNew)
if equality.Semantic.DeepEqual(oldSvcRefs, newSvcRefs) {
return
}
var targetRefs []string
for _, svc := range oldSvcRefs {
if !slices.Contains(newSvcRefs, svc) {
targetRefs = append(targetRefs, svc)
}
}
for _, svc := range newSvcRefs {
if !slices.Contains(oldSvcRefs, svc) {
targetRefs = append(targetRefs, svc)
}
}
for _, svc := range targetRefs {
h.svcEventChan <- event.GenericEvent{
Object: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: mciNew.Namespace,
Name: svc,
}}}
}
}
func (h *multiClusterIngressEventHandler) Delete(ctx context.Context, e event.DeleteEvent, _ workqueue.RateLimitingInterface) {
mci := e.Object.(*networkingv1alpha1.MultiClusterIngress)
if !util.CheckIngressClassMatched(h.ctx, h.client, mci, h.ingClassName) {
return
}
h.enqueueImpactedService(mci)
}
func (h *multiClusterIngressEventHandler) Generic(ctx context.Context, _ event.GenericEvent, _ workqueue.RateLimitingInterface) {
}
func (h *multiClusterIngressEventHandler) enqueueImpactedService(mci *networkingv1alpha1.MultiClusterIngress) {
svcRefs := indexes.BuildServiceRefIndexes(mci)
for _, svc := range svcRefs {
h.svcEventChan <- event.GenericEvent{
Object: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: mci.Namespace,
Name: svc,
}}}
}
}
func newMultiClusterServiceEventHandler(
ctx context.Context,
client client.Client,
svcEventChan chan<- event.GenericEvent,
) handler.EventHandler {
return &multiClusterServiceEventHandler{
ctx: ctx,
client: client,
svcEventChan: svcEventChan,
}
}
var _ handler.EventHandler = (*multiClusterServiceEventHandler)(nil)
type multiClusterServiceEventHandler struct {
ctx context.Context
client client.Client
svcEventChan chan<- event.GenericEvent
}
func (h *multiClusterServiceEventHandler) Create(ctx context.Context, e event.CreateEvent, _ workqueue.RateLimitingInterface) {
h.enqueueImpactedService(e.Object.GetNamespace(), e.Object.GetName())
}
func (h *multiClusterServiceEventHandler) Update(ctx context.Context, e event.UpdateEvent, _ workqueue.RateLimitingInterface) {
mcsOld := e.ObjectOld.(*networkingv1alpha1.MultiClusterService)
mcsNew := e.ObjectNew.(*networkingv1alpha1.MultiClusterService)
// Only care about the update events below:
if equality.Semantic.DeepEqual(mcsOld.Annotations, mcsNew.Annotations) &&
equality.Semantic.DeepEqual(mcsOld.Spec.Types, mcsNew.Spec.Types) &&
equality.Semantic.DeepEqual(mcsOld.Spec.Ports, mcsNew.Spec.Ports) {
return
}
h.enqueueImpactedService(mcsNew.Namespace, mcsNew.Name)
}
func (h *multiClusterServiceEventHandler) Delete(ctx context.Context, e event.DeleteEvent, _ workqueue.RateLimitingInterface) {
h.enqueueImpactedService(e.Object.GetNamespace(), e.Object.GetName())
}
func (h *multiClusterServiceEventHandler) Generic(ctx context.Context, _ event.GenericEvent, _ workqueue.RateLimitingInterface) {
}
func (h *multiClusterServiceEventHandler) enqueueImpactedService(namespace, name string) {
h.svcEventChan <- event.GenericEvent{
Object: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
}}}
}
func newResourceBindingEventHandler(svcEventChan chan<- event.GenericEvent) handler.EventHandler {
return &resourceBindingEventHandler{
svcEventChan: svcEventChan,
}
}
var _ handler.EventHandler = (*resourceBindingEventHandler)(nil)
type resourceBindingEventHandler struct {
svcEventChan chan<- event.GenericEvent
}
func (h *resourceBindingEventHandler) Create(ctx context.Context, e event.CreateEvent, _ workqueue.RateLimitingInterface) {
// The distribution feature involves directly creating rb objects,
// so it is necessary to care about the rb creation event.
rb := e.Object.(*workv1alpha1.ResourceBinding)
if rb.Spec.Resource.Kind != "Service" {
return
}
h.svcEventChan <- event.GenericEvent{
Object: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: rb.Spec.Resource.Namespace,
Name: rb.Spec.Resource.Name,
}}}
}
func (h *resourceBindingEventHandler) Update(ctx context.Context, e event.UpdateEvent, _ workqueue.RateLimitingInterface) {
rbOlb := e.ObjectOld.(*workv1alpha1.ResourceBinding)
rbNew := e.ObjectNew.(*workv1alpha1.ResourceBinding)
resource := rbNew.Spec.Resource
if resource.Kind != "Service" {
return
}
scheduleResultOld := getClusterResults(rbOlb.Spec)
scheduleResultNew := getClusterResults(rbNew.Spec)
if equality.Semantic.DeepEqual(scheduleResultOld, scheduleResultNew) {
return
}
h.svcEventChan <- event.GenericEvent{
Object: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: resource.Namespace,
Name: resource.Name,
}}}
}
func (h *resourceBindingEventHandler) Delete(ctx context.Context, _ event.DeleteEvent, _ workqueue.RateLimitingInterface) {
// The deletion event of the resourceBinding will be
// processed by the deletion event of service.
}
func (h *resourceBindingEventHandler) Generic(ctx context.Context, _ event.GenericEvent, _ workqueue.RateLimitingInterface) {
}