Skip to content

Commit 33ebc2d

Browse files
authored
Merge pull request #6523 from XiShanYongYe-Chang/fix-6522
Fixed the issue that reporting repeat EndpointSlice resources leads to duplicate backend IPs
2 parents 997a0a1 + d2f22e8 commit 33ebc2d

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed

pkg/controllers/mcs/endpointslice_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func (c *EndpointSliceController) collectEndpointSliceFromWork(ctx context.Conte
153153
desiredEndpointSlice.Labels = util.DedupeAndMergeLabels(desiredEndpointSlice.Labels, map[string]string{
154154
workv1alpha2.WorkPermanentIDLabel: work.Labels[workv1alpha2.WorkPermanentIDLabel],
155155
discoveryv1.LabelServiceName: names.GenerateDerivedServiceName(work.Labels[util.ServiceNameLabel]),
156+
discoveryv1.LabelManagedBy: util.EndpointSliceControllerLabelValue,
156157
})
157158
desiredEndpointSlice.Annotations = util.DedupeAndMergeAnnotations(desiredEndpointSlice.Annotations, map[string]string{
158159
workv1alpha2.WorkNamespaceAnnotation: work.Namespace,

pkg/controllers/mcs/service_export_controller.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,11 @@ func (c *ServiceExportController) handleEndpointSliceEvent(ctx context.Context,
394394
return err
395395
}
396396

397+
// Exclude EndpointSlice resources that are managed by Karmada system to avoid duplicate reporting.
398+
if helper.IsEndpointSliceManagedByKarmada(endpointSliceObj.GetLabels()) {
399+
return nil
400+
}
401+
397402
if err = c.reportEndpointSliceWithEndpointSliceCreateOrUpdate(ctx, endpointSliceKey.Cluster, endpointSliceObj); err != nil {
398403
klog.ErrorS(err, "Failed to handle endpointSlice event", "namespace", endpointSliceKey.Namespace, "name", endpointSliceKey.Name)
399404
return err
@@ -440,7 +445,13 @@ func (c *ServiceExportController) reportEndpointSliceWithServiceExportCreate(ctx
440445
}
441446

442447
for index := range endpointSliceObjects {
443-
if err = reportEndpointSlice(ctx, c.Client, endpointSliceObjects[index].(*unstructured.Unstructured), serviceExportKey.Cluster); err != nil {
448+
endpointSlice := endpointSliceObjects[index].(*unstructured.Unstructured)
449+
// Exclude EndpointSlice resources that are managed by Karmada system to avoid duplicate reporting.
450+
if helper.IsEndpointSliceManagedByKarmada(endpointSlice.GetLabels()) {
451+
continue
452+
}
453+
454+
if err = reportEndpointSlice(ctx, c.Client, endpointSlice, serviceExportKey.Cluster); err != nil {
444455
errs = append(errs, err)
445456
}
446457
}

pkg/controllers/multiclusterservice/endpointslice_collect_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ func (c *EndpointSliceCollectController) handleEndpointSliceEvent(ctx context.Co
299299
return err
300300
}
301301

302-
if util.GetLabelValue(endpointSliceObj.GetLabels(), discoveryv1.LabelManagedBy) == util.EndpointSliceDispatchControllerLabelValue {
302+
// Exclude EndpointSlice resources that are managed by Karmada system to avoid duplicate reporting.
303+
if helper.IsEndpointSliceManagedByKarmada(endpointSliceObj.GetLabels()) {
303304
return nil
304305
}
305306

@@ -358,7 +359,8 @@ func (c *EndpointSliceCollectController) collectTargetEndpointSlice(ctx context.
358359
klog.ErrorS(err, "Failed to convert object to EndpointSlice")
359360
return err
360361
}
361-
if util.GetLabelValue(eps.GetLabels(), discoveryv1.LabelManagedBy) == util.EndpointSliceDispatchControllerLabelValue {
362+
// Exclude EndpointSlice resources that are managed by Karmada system to avoid duplicate reporting.
363+
if helper.IsEndpointSliceManagedByKarmada(eps.GetLabels()) {
362364
continue
363365
}
364366
epsUnstructured, err := helper.ToUnstructured(eps)

pkg/util/constants.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ const (
5656
// managed by karmada controllers.
5757
KarmadaSystemLabel = "karmada.io/system"
5858

59-
// EndpointSliceDispatchControllerLabelValue indicates the endpointSlice are controlled by Karmada
60-
EndpointSliceDispatchControllerLabelValue = "endpointslice-dispatch-controller.karmada.io"
61-
6259
// RetainReplicasLabel is a reserved label to indicate whether the replicas should be retained. e.g:
6360
// resourcetemplate.karmada.io/retain-replicas: true // with value `true` indicates retain
6461
// resourcetemplate.karmada.io/retain-replicas: false // with value `false` and others, indicates not retain
@@ -71,6 +68,7 @@ const (
7168
EndpointSliceWorkManagedByLabel = "endpointslice.karmada.io/managed-by"
7269
)
7370

71+
// Define label values used by Karmada system.
7472
const (
7573
// ManagedByKarmadaLabelValue indicates that these are workloads in member cluster synchronized by karmada controllers.
7674
ManagedByKarmadaLabelValue = "true"
@@ -80,6 +78,12 @@ const (
8078

8179
// RetainReplicasValue is an optional value of RetainReplicasLabel, indicating retain
8280
RetainReplicasValue = "true"
81+
82+
// EndpointSliceDispatchControllerLabelValue indicates the endpointSlice is controlled by Karmada endpointslice-dispatch-controller
83+
EndpointSliceDispatchControllerLabelValue = "endpointslice-dispatch-controller.karmada.io"
84+
85+
// EndpointSliceControllerLabelValue indicates the endpointSlice is controlled by Karmada endpointslice-controller
86+
EndpointSliceControllerLabelValue = "endpointslice-controller.karmada.io"
8387
)
8488

8589
// Define annotations used by karmada system.

pkg/util/helper/mcs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ func GetConsumerClusters(client client.Client, mcs *networkingv1alpha1.MultiClus
141141
}
142142
return allClusters, nil
143143
}
144+
145+
// IsEndpointSliceManagedByKarmada checks if the EndpointSlice is managed by Karmada.
146+
func IsEndpointSliceManagedByKarmada(epsLabels map[string]string) bool {
147+
switch util.GetLabelValue(epsLabels, discoveryv1.LabelManagedBy) {
148+
case util.EndpointSliceDispatchControllerLabelValue, util.EndpointSliceControllerLabelValue:
149+
return true
150+
}
151+
return false
152+
}

pkg/util/helper/mcs_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3030

31+
"github.com/karmada-io/karmada/pkg/util"
3132
"github.com/karmada-io/karmada/pkg/util/gclient"
3233
)
3334

@@ -223,3 +224,50 @@ func TestDeleteEndpointSlice(t *testing.T) {
223224
})
224225
}
225226
}
227+
228+
func TestIsEndpointSliceManagedByKarmada(t *testing.T) {
229+
tests := []struct {
230+
name string
231+
labels map[string]string
232+
want bool
233+
}{
234+
{
235+
name: "managed by endpointslice-dispatch-controller",
236+
labels: map[string]string{
237+
discoveryv1.LabelManagedBy: util.EndpointSliceDispatchControllerLabelValue,
238+
},
239+
want: true,
240+
},
241+
{
242+
name: "managed by endpointslice-controller",
243+
labels: map[string]string{
244+
discoveryv1.LabelManagedBy: util.EndpointSliceControllerLabelValue,
245+
},
246+
want: true,
247+
},
248+
{
249+
name: "not managed by karmada",
250+
labels: map[string]string{
251+
discoveryv1.LabelManagedBy: "not-karmada",
252+
},
253+
want: false,
254+
},
255+
{
256+
name: "nil labels",
257+
labels: nil,
258+
want: false,
259+
},
260+
{
261+
name: "empty labels",
262+
labels: map[string]string{},
263+
want: false,
264+
},
265+
}
266+
for _, tt := range tests {
267+
t.Run(tt.name, func(t *testing.T) {
268+
if got := IsEndpointSliceManagedByKarmada(tt.labels); got != tt.want {
269+
t.Errorf("IsEndpointSliceManagedByKarmada() = %v, want %v", got, tt.want)
270+
}
271+
})
272+
}
273+
}

0 commit comments

Comments
 (0)