Skip to content

fix(endpointslices): preserve custom-managed EndpointSlices for Services with selectors - #4267

Open
TECHANOO wants to merge 2 commits into
loft-sh:mainfrom
TECHANOO:fix/4142-preserve-custom-endpointslices
Open

TECHANOO wants to merge 2 commits into
loft-sh:mainfrom
TECHANOO:fix/4142-preserve-custom-endpointslices

Conversation

@TECHANOO

@TECHANOO TECHANOO commented Nov 11, 2026 •

Copy link
Copy Markdown

What issue type does this pull request address? (keep at least one, remove the others)
/kind bugfix

What does this pull request do? Which issues does it resolve? (use resolves #<issue_number> if possible)
resolves #4142

When a Service has a selector, the EndpointSlice syncer deleted every host EndpointSlice for it, including slices that some other controller manages. Operators like mariadb-operator create their own slices (managed-by mariadb-operator.k8s.mariadb.com) next to a Service with a selector, so those slices got removed on the host and traffic to them broke.

This PR changes two things in syncer.go:

  1. The delete in ReconcileStart now only applies to slices managed by Kubernetes itself (endpointslice-controller.k8s.io or endpointslice-mirroring-controller.k8s.io), or slices with no or an empty managed-by label. Slices owned by anything else are synced like normal.
  2. kubernetes.io/service-name is excluded from the bidirectional label update in Sync. Without that, after a syncer restart (cold cache) the host label got overwritten with the raw virtual Service name, which detaches the slice from the translated host Service.

I left translate.go alone on purpose since #4192 is already working on the name length side of this issue. The two changes don't overlap.

Please provide a short message that should be published in the vcluster release notes
Fixed an issue where vcluster deleted custom-managed EndpointSlices on the host cluster when the matching Service had a selector.

What else do we need to know?
Unit tests: added TestIsManagedByK8s and extended TestCustomManagedEndpointSlice. One subtest covers the cold cache case and fails if the label exclusion is removed. go vet and go test -race ./pkg/controllers/resources/endpointslices/... pass.

I also tested this by hand on a Kind cluster with a vcluster built from this branch:

  • Service without a selector plus a custom slice: slice synced, label translated, DNS and HTTP work from both sides.
  • Normal Service with a selector: one slice per side, no duplicates.
  • Service with a selector plus a slice managed by mariadb-operator.k8s.mariadb.com (the case from the issue): on main the host slice is deleted. With this change it stays after the first sync, after 60s, after a Service resync, after a syncer pod restart and after changing a label on the virtual slice. The host kubernetes.io/service-name stays translated the whole time.

Happy to adjust the approach or trim the tests if you'd prefer something smaller.

E2E Tests

Default Test Execution

The mandatory PR suite runs automatically. Only specify additional test suites below if needed.

Additional test suites

Additional test suite(s) that will be executed before the mandatory PR suite:

none

Note

Medium Risk
Changes EndpointSlice reconcile and label sync on the networking path; misclassification of managed-by could leave duplicate slices or stop syncing K8s-owned slices for selector Services.

Overview
Fixes vcluster deleting host EndpointSlices that third-party controllers own (e.g. mariadb-operator) when the backing Service has a selector. ReconcileStart now only purges previously vcluster-synced slices for Kubernetes-managed slices (empty/missing managed-by, or endpointslice-controller.k8s.io / endpointslice-mirroring-controller.k8s.io).

Label handling is tightened so host slices stay attached to the translated Service: kubernetes.io/service-name is excluded from bidirectional label sync in Sync, and translateUpdate re-applies the translated service-name on the host (including when host labels were nil or the virtual slice is repointed).

Coverage adds isManagedByK8s, sync/reconcile scenarios for custom-managed slices, and the cold-cache nil-labels case.

Reviewed by Cursor Bugbot for commit 84ac2fb. Bugbot is set up for automated code reviews on this repo. Configure here.

…ces with selectors

When a Service specifies spec.selector, vCluster previously skipped or deleted all EndpointSlice objects on the host, assuming they would be generated by the host endpointslice controller. This prevented custom operators (such as mariadb-operator) from syncing their own EndpointSlices to the host.

Add an isManagedByK8s guard to distinguish built-in Kubernetes controllers from custom operators by inspecting the endpointslice.kubernetes.io/managed-by label. Slices managed by custom operators are synced to the host instead of being deleted. Missing labels, nil labels, and empty strings are treated as Kubernetes-managed.

Additionally, exclude kubernetes.io/service-name from bidirectional label updates during Sync so that cold-cache reconciles (such as syncer restarts) preserve the translated host service name rather than overwriting it with the virtual service name.

Fixes loft-sh#4142

Signed-off-by: Prasad PL <ai@anoo.tech>
@TECHANOO
TECHANOO requested a review from a team as a code owner November 11, 2026 13:31

@blacksoxx blacksoxx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at a2e8307 on main c5e9027: both changes are pinned by the new tests (removing the label exclusion fails the cold-cache case, removing the managed-by condition fails the first two), go test -race, vet, gofmt and lint are clean on the package, merge-tree against main is clean.

The exclusion is matched in both directions because kubernetes.io/service-name is not in LabelsToTranslate, and the cold-cache overwrite comes from the seeded VirtualOld carrying no labels, so the fix sits in the right place. No overlap with #4192, which only touches translate.go.

Two small notes inline, neither blocking. The merge call is the maintainers'.

// 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)

Copy link
Copy Markdown
Contributor

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-name label after creation: translateUpdate only copies the endpoints. A virtual slice whose label is re-pointed to another Service would keep the old host target. One line in translateUpdate setting the label through translateSvcName would close that, but it is an edge case and fine as a follow-up.

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown
Contributor

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.


func isManagedByK8s(endpointSlice *discoveryv1.EndpointSlice) bool {
managedBy := endpointSlice.GetLabels()[discoveryv1.LabelManagedBy]
return managedBy == "" ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Because kubernetes.io/service-name is excluded from bidirectional label updates, translateUpdate must refresh the host kubernetes.io/service-name label directly. Read the service name, virtual namespace, and vCluster name strictly from the virtual object and global configuration (never from the host object) to prevent double translation and label drift when an EndpointSlice is repointed to another service.

Ensure nil safety by initializing or cloning the host label map before writing the updated label. If the virtual object lacks a valid service name label, avoid writing a malformed label.

Add a comment explaining that treating an absent or empty endpointslice.kubernetes.io/managed-by label as Kubernetes-managed is deliberate, preserving pre-existing behavior for selector-backed Services and avoiding syncing unlabeled slices that could duplicate host endpoints.

Add unit tests covering custom-managed EndpointSlice repointing to another service and host slices starting with nil labels.

Refs loft-sh#4142

Signed-off-by: Prasad PL <ai@anoo.tech>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EndpointSlice sync does not translate kubernetes.io/service-name for custom-managed EndpointSlices

2 participants