Repository navigation
fix(endpointslices): preserve custom-managed EndpointSlices for Services with selectors #4267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,7 @@ func (s *endpointSliceSyncer) Sync(ctx *synccontext.SyncContext, event *synccont | |
|
|
||
| // bi-directional sync of annotations and labels | ||
| event.Virtual.Annotations, event.Host.Annotations = translate.AnnotationsBidirectionalUpdate(event, s.excludedAnnotations...) | ||
| event.Virtual.Labels, event.Host.Labels = translate.LabelsBidirectionalUpdate(event) | ||
| event.Virtual.Labels, event.Host.Labels = translate.LabelsBidirectionalUpdate(event, translate.K8sServiceNameLabel) | ||
|
|
||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
@@ -166,7 +166,7 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c | |
| } | ||
|
|
||
| return true, err | ||
| } else if svc.Spec.Selector != nil { | ||
| } else if svc.Spec.Selector != nil && isManagedByK8s(eps) { | ||
| // check if it was a managed endpointSlice object before and delete it | ||
| endpointSlice := &discoveryv1.EndpointSlice{} | ||
| err = ctx.HostClient.Get(ctx, s.VirtualToHost(ctx, req.NamespacedName, nil), endpointSlice) | ||
|
|
@@ -209,3 +209,16 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c | |
| } | ||
|
|
||
| func (s *endpointSliceSyncer) ReconcileEnd() {} | ||
|
|
||
| // isManagedByK8s checks whether an EndpointSlice is managed by a Kubernetes controller. | ||
| // Treating an absent or empty endpointslice.kubernetes.io/managed-by label as Kubernetes-managed | ||
| // is intentional: it preserves pre-PR behavior for selector-backed Services and avoids syncing | ||
| // unlabeled slices that could duplicate endpoints managed by the host Kubernetes EndpointSlice | ||
| // controller, so custom controllers managing slices for selector Services are expected to set | ||
| // their own managed-by value. | ||
| func isManagedByK8s(endpointSlice *discoveryv1.EndpointSlice) bool { | ||
| managedBy := endpointSlice.GetLabels()[discoveryv1.LabelManagedBy] | ||
| return managedBy == "" || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] An absent or empty managed-by counts as Kubernetes-managed here. The kube controller only touches slices carrying its own managed-by value, so an operator that forgets the label still gets its slice dropped for a selector Service. That is the pre-PR behaviour, so keeping it is defensible; a word in a comment that this is deliberate would help the next reader. |
||
| managedBy == "endpointslice-controller.k8s.io" || | ||
| managedBy == "endpointslice-mirroring-controller.k8s.io" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] With the exclusion in place nothing refreshes the host
kubernetes.io/service-namelabel after creation:translateUpdateonly copies the endpoints. A virtual slice whose label is re-pointed to another Service would keep the old host target. One line intranslateUpdatesetting the label throughtranslateSvcNamewould close that, but it is an edge case and fine as a follow-up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for testing this so thoroughly. I picked up both notes in 84ac2fb. translateUpdate now refreshes the service-name label from the virtual slice, so repointing it to another Service updates the host target (there's a test for that). I also added a comment on isManagedByK8s saying the empty managed-by case is kept on purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking both up so quickly. Checked 84ac2fb: the repointed-service case fails without the refresh and passes with it,
go test -race, vet, gofmt and lint are clean on the package, and merge-tree against main is still clean. Nothing further from my side.