Skip to content

Commit 3a24c32

Browse files
authored
Merge pull request kubernetes#87980 from tnqn/endpointslicetracker
EndpointSliceTracker should track updated resource version
2 parents 65879f9 + c2d3e54 commit 3a24c32

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

pkg/controller/endpointslice/reconciler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,26 @@ func (r *reconciler) finalize(
206206

207207
for _, endpointSlice := range slicesToCreate {
208208
addTriggerTimeAnnotation(endpointSlice, triggerTime)
209-
_, err := r.client.DiscoveryV1beta1().EndpointSlices(service.Namespace).Create(context.TODO(), endpointSlice, metav1.CreateOptions{})
209+
createdSlice, err := r.client.DiscoveryV1beta1().EndpointSlices(service.Namespace).Create(context.TODO(), endpointSlice, metav1.CreateOptions{})
210210
if err != nil {
211211
// If the namespace is terminating, creates will continue to fail. Simply drop the item.
212212
if errors.HasStatusCause(err, corev1.NamespaceTerminatingCause) {
213213
return nil
214214
}
215215
errs = append(errs, fmt.Errorf("Error creating EndpointSlice for Service %s/%s: %v", service.Namespace, service.Name, err))
216216
} else {
217-
r.endpointSliceTracker.Update(endpointSlice)
217+
r.endpointSliceTracker.Update(createdSlice)
218218
metrics.EndpointSliceChanges.WithLabelValues("create").Inc()
219219
}
220220
}
221221

222222
for _, endpointSlice := range slicesToUpdate {
223223
addTriggerTimeAnnotation(endpointSlice, triggerTime)
224-
_, err := r.client.DiscoveryV1beta1().EndpointSlices(service.Namespace).Update(context.TODO(), endpointSlice, metav1.UpdateOptions{})
224+
updatedSlice, err := r.client.DiscoveryV1beta1().EndpointSlices(service.Namespace).Update(context.TODO(), endpointSlice, metav1.UpdateOptions{})
225225
if err != nil {
226226
errs = append(errs, fmt.Errorf("Error updating %s EndpointSlice for Service %s/%s: %v", endpointSlice.Name, service.Namespace, service.Name, err))
227227
} else {
228-
r.endpointSliceTracker.Update(endpointSlice)
228+
r.endpointSliceTracker.Update(updatedSlice)
229229
metrics.EndpointSliceChanges.WithLabelValues("update").Inc()
230230
}
231231
}

pkg/controller/endpointslice/reconciler_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func TestReconcileEmpty(t *testing.T) {
6262
assert.Equal(t, svc.Name, slices[0].Labels[discovery.LabelServiceName])
6363
assert.EqualValues(t, []discovery.EndpointPort{}, slices[0].Ports)
6464
assert.EqualValues(t, []discovery.Endpoint{}, slices[0].Endpoints)
65+
expectTrackedResourceVersion(t, r.endpointSliceTracker, &slices[0], "100")
6566
expectMetrics(t, expectedMetrics{desiredSlices: 1, actualSlices: 1, desiredEndpoints: 0, addedPerSync: 0, removedPerSync: 0, numCreated: 1, numUpdated: 0, numDeleted: 0})
6667
}
6768

@@ -190,6 +191,8 @@ func TestReconcile1Pod(t *testing.T) {
190191
t.Errorf("Expected endpoint: %+v, got: %+v", testCase.expectedEndpoint, endpoint)
191192
}
192193

194+
expectTrackedResourceVersion(t, r.endpointSliceTracker, &slice, "100")
195+
193196
expectMetrics(t, expectedMetrics{desiredSlices: 1, actualSlices: 1, desiredEndpoints: 1, addedPerSync: 1, removedPerSync: 0, numCreated: 1, numUpdated: 0, numDeleted: 0})
194197
})
195198
}
@@ -221,6 +224,7 @@ func TestReconcile1EndpointSlice(t *testing.T) {
221224
assert.Equal(t, svc.Name, slices[0].Labels[discovery.LabelServiceName])
222225
assert.EqualValues(t, []discovery.EndpointPort{}, slices[0].Ports)
223226
assert.EqualValues(t, []discovery.Endpoint{}, slices[0].Endpoints)
227+
expectTrackedResourceVersion(t, r.endpointSliceTracker, &slices[0], "200")
224228
expectMetrics(t, expectedMetrics{desiredSlices: 1, actualSlices: 1, desiredEndpoints: 0, addedPerSync: 0, removedPerSync: 0, numCreated: 0, numUpdated: 1, numDeleted: 0})
225229
}
226230

@@ -821,6 +825,17 @@ func expectActions(t *testing.T, actions []k8stesting.Action, num int, verb, res
821825
}
822826
}
823827

828+
func expectTrackedResourceVersion(t *testing.T, tracker *endpointSliceTracker, slice *discovery.EndpointSlice, expectedRV string) {
829+
rrv := tracker.relatedResourceVersions(slice)
830+
rv, tracked := rrv[slice.Name]
831+
if !tracked {
832+
t.Fatalf("Expected EndpointSlice %s to be tracked", slice.Name)
833+
}
834+
if rv != expectedRV {
835+
t.Errorf("Expected ResourceVersion of %s to be %s, got %s", slice.Name, expectedRV, rv)
836+
}
837+
}
838+
824839
func portsAndAddressTypeEqual(slice1, slice2 discovery.EndpointSlice) bool {
825840
return apiequality.Semantic.DeepEqual(slice1.Ports, slice2.Ports) && apiequality.Semantic.DeepEqual(slice1.AddressType, slice2.AddressType)
826841
}

pkg/controller/endpointslice/utils_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,15 @@ func newClientset() *fake.Clientset {
401401
endpointSlice.ObjectMeta.Name = fmt.Sprintf("%s-%s", endpointSlice.ObjectMeta.GenerateName, rand.String(8))
402402
endpointSlice.ObjectMeta.GenerateName = ""
403403
}
404+
endpointSlice.ObjectMeta.ResourceVersion = "100"
404405

405406
return false, endpointSlice, nil
406407
}))
408+
client.PrependReactor("update", "endpointslices", k8stesting.ReactionFunc(func(action k8stesting.Action) (bool, runtime.Object, error) {
409+
endpointSlice := action.(k8stesting.CreateAction).GetObject().(*discovery.EndpointSlice)
410+
endpointSlice.ObjectMeta.ResourceVersion = "200"
411+
return false, endpointSlice, nil
412+
}))
407413

408414
return client
409415
}

0 commit comments

Comments
 (0)