Skip to content

Commit fb6bbc9

Browse files
authored
Merge pull request kubernetes#125359 from yangjunmyfm192085/fixendpointslicemirroring
fix endpointslicemirroring controller not create endpointslice when the endpoints are recreate
2 parents 4fa7ce6 + 811bd53 commit fb6bbc9

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

pkg/controller/endpointslicemirroring/reconciler.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ func (r *reconciler) reconcileByPortMapping(
207207
totals = totalChanges(existingSlices[0], desiredSet)
208208
if totals.added == 0 && totals.updated == 0 && totals.removed == 0 &&
209209
apiequality.Semantic.DeepEqual(endpoints.Labels, compareLabels) &&
210-
apiequality.Semantic.DeepEqual(compareAnnotations, existingSlices[0].Annotations) {
210+
apiequality.Semantic.DeepEqual(compareAnnotations, existingSlices[0].Annotations) &&
211+
!needRebuildExistingSlices(endpoints, existingSlices[0]) {
212+
if !r.endpointSliceTracker.Has(existingSlices[0]) {
213+
r.endpointSliceTracker.Update(existingSlices[0]) // Always ensure each EndpointSlice is being tracked.
214+
}
211215
return slices, totals
212216
}
213217
}
@@ -334,3 +338,13 @@ func totalChanges(existingSlice *discovery.EndpointSlice, desiredSet endpointsli
334338
totals.added = desiredSet.Len() - existingMatches
335339
return totals
336340
}
341+
342+
func needRebuildExistingSlices(endpoints *corev1.Endpoints, existingSlice *discovery.EndpointSlice) bool {
343+
for index := range existingSlice.OwnerReferences {
344+
owner := existingSlice.OwnerReferences[index]
345+
if owner.Kind == "Endpoints" && owner.Name == endpoints.Name && owner.UID != endpoints.UID {
346+
return true
347+
}
348+
}
349+
return false
350+
}

pkg/controller/endpointslicemirroring/reconciler_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
discovery "k8s.io/api/discovery/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
2728
"k8s.io/client-go/kubernetes/fake"
2829
"k8s.io/client-go/kubernetes/scheme"
2930
"k8s.io/client-go/tools/record"
@@ -48,6 +49,7 @@ func TestReconcile(t *testing.T) {
4849
subsets []corev1.EndpointSubset
4950
epLabels map[string]string
5051
epAnnotations map[string]string
52+
uid string
5153
endpointsDeletionPending bool
5254
maxEndpointsPerSubset int32
5355
existingEndpointSlices []*discovery.EndpointSlice
@@ -219,9 +221,19 @@ func TestReconcile(t *testing.T) {
219221
Hostname: "pod-1",
220222
}},
221223
}},
224+
uid: "d8f2c1f6-5285-4b3c-b3c1-9b89f9e7ed7a",
222225
existingEndpointSlices: []*discovery.EndpointSlice{{
223226
ObjectMeta: metav1.ObjectMeta{
224-
Name: "test-ep-1",
227+
Name: "test-ep-1",
228+
Namespace: "test",
229+
OwnerReferences: []metav1.OwnerReference{
230+
{
231+
APIVersion: "v1",
232+
Kind: "Endpoints",
233+
Name: "test-ep",
234+
UID: "d8f2c1f6-5285-4b3c-b3c1-9b89f9e7ed7a",
235+
},
236+
},
225237
},
226238
AddressType: discovery.AddressTypeIPv4,
227239
Ports: []discovery.EndpointPort{{
@@ -237,6 +249,47 @@ func TestReconcile(t *testing.T) {
237249
}},
238250
expectedNumSlices: 1,
239251
expectedClientActions: 0,
252+
}, {
253+
testName: "Endpoints with 1 subset, port, and address and existing slice with same fields but different OwnerReferences",
254+
subsets: []corev1.EndpointSubset{{
255+
Ports: []corev1.EndpointPort{{
256+
Name: "http",
257+
Port: 80,
258+
Protocol: corev1.ProtocolTCP,
259+
}},
260+
Addresses: []corev1.EndpointAddress{{
261+
IP: "10.0.0.1",
262+
Hostname: "pod-1",
263+
}},
264+
}},
265+
uid: "d8f2c1f6-5285-4b3c-b3c1-9b89f9e7ed7a",
266+
existingEndpointSlices: []*discovery.EndpointSlice{{
267+
ObjectMeta: metav1.ObjectMeta{
268+
Name: "test-ep-1",
269+
Namespace: "test",
270+
OwnerReferences: []metav1.OwnerReference{
271+
{
272+
APIVersion: "v1",
273+
Kind: "Endpoints",
274+
Name: "test-ep",
275+
UID: "fb91e798-1875-4857-b5eb-e2c878157b4d",
276+
},
277+
},
278+
},
279+
AddressType: discovery.AddressTypeIPv4,
280+
Ports: []discovery.EndpointPort{{
281+
Name: pointer.String("http"),
282+
Port: pointer.Int32(80),
283+
Protocol: &protoTCP,
284+
}},
285+
Endpoints: []discovery.Endpoint{{
286+
Addresses: []string{"10.0.0.1"},
287+
Hostname: pointer.String("pod-1"),
288+
Conditions: discovery.EndpointConditions{Ready: pointer.Bool(true)},
289+
}},
290+
}},
291+
expectedNumSlices: 1,
292+
expectedClientActions: 1,
240293
}, {
241294
testName: "Endpoints with 1 subset, port, and address and existing slice with an additional annotation",
242295
subsets: []corev1.EndpointSubset{{
@@ -1012,7 +1065,7 @@ func TestReconcile(t *testing.T) {
10121065
setupMetrics()
10131066
namespace := "test"
10141067
endpoints := corev1.Endpoints{
1015-
ObjectMeta: metav1.ObjectMeta{Name: "test-ep", Namespace: namespace, Labels: tc.epLabels, Annotations: tc.epAnnotations},
1068+
ObjectMeta: metav1.ObjectMeta{Name: "test-ep", Namespace: namespace, Labels: tc.epLabels, Annotations: tc.epAnnotations, UID: types.UID(tc.uid)},
10161069
Subsets: tc.subsets,
10171070
}
10181071

0 commit comments

Comments
 (0)