Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/controllers/resources/endpointslices/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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.


return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 == "" ||

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.

managedBy == "endpointslice-controller.k8s.io" ||
managedBy == "endpointslice-mirroring-controller.k8s.io"
}
325 changes: 325 additions & 0 deletions pkg/controllers/resources/endpointslices/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ func TestSync(t *testing.T) {
Endpoints: updatedEndpointSlice.Endpoints,
}

nilLabelsHostEndpointSlice := syncedEndpointSlice.DeepCopy()
nilLabelsHostEndpointSlice.Labels = nil

repointedVEndpointSliceForSync := updatedEndpointSlice.DeepCopy()
repointedVEndpointSliceForSync.Labels = map[string]string{
translate.K8sServiceNameLabel: "repointed-svc",
}

expectedNilLabelsSyncedEndpointSlice := syncedEndpointSlice.DeepCopy()
expectedNilLabelsSyncedEndpointSlice.Labels = map[string]string{
translate.K8sServiceNameLabel: translate.Default.HostName(nil, "repointed-svc", "test").Name,
}
expectedNilLabelsSyncedEndpointSlice.Endpoints = repointedVEndpointSliceForSync.Endpoints

request := ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: "default",
Expand Down Expand Up @@ -224,6 +238,31 @@ func TestSync(t *testing.T) {
assert.NilError(t, err)
},
},
{
Name: "Forward update with nil physical labels",
InitialVirtualState: []runtime.Object{
repointedVEndpointSliceForSync,
},
InitialPhysicalState: []runtime.Object{
nilLabelsHostEndpointSlice,
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice"): {
expectedNilLabelsSyncedEndpointSlice,
},
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*endpointSliceSyncer).Sync(syncCtx, synccontext.NewSyncEventWithOld(
nilLabelsHostEndpointSlice,
nilLabelsHostEndpointSlice,
baseEndpointSlice,
repointedVEndpointSliceForSync,
))
assert.NilError(t, err)
assert.Equal(t, nilLabelsHostEndpointSlice.Labels[translate.K8sServiceNameLabel], translate.Default.HostName(nil, "repointed-svc", "test").Name)
},
},
{
Name: "Don't sync default/kubernetes endpointSlice",
Sync: func(ctx *synccontext.RegisterContext) {
Expand All @@ -234,3 +273,289 @@ func TestSync(t *testing.T) {
},
})
}

func TestCustomManagedEndpointSlice(t *testing.T) {
vService := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "example-db-secondary",
Namespace: "example-prod",
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "mariadb",
},
},
}
vEndpointSlice := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "example-db-secondary",
Namespace: "example-prod",
Labels: map[string]string{
discoveryv1.LabelManagedBy: "mariadb-operator.k8s.mariadb.com",
translate.K8sServiceNameLabel: "example-db-secondary",
},
},
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"10.15.104.41"},
TargetRef: &corev1.ObjectReference{
Kind: "Pod",
Name: "example-db-1",
Namespace: "example-prod",
},
},
},
}
expectedHostSvcName := translate.Default.HostName(nil, vService.Name, vService.Namespace).Name
expectedHostEndpointSliceName := translate.Default.HostName(nil, vEndpointSlice.Name, vEndpointSlice.Namespace).Name
expectedPodName := translate.Default.HostName(nil, "example-db-1", "example-prod").Name

expectedEndpointSlice := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "999",
Name: expectedHostEndpointSliceName,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vEndpointSlice.Name,
translate.NamespaceAnnotation: vEndpointSlice.Namespace,
translate.KindAnnotation: discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice").String(),
translate.HostNamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.HostNameAnnotation: expectedHostEndpointSliceName,
},
Labels: map[string]string{
discoveryv1.LabelManagedBy: "mariadb-operator.k8s.mariadb.com",
translate.K8sServiceNameLabel: expectedHostSvcName,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vEndpointSlice.Namespace,
},
},
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"10.15.104.41"},
TargetRef: &corev1.ObjectReference{
Kind: "Pod",
Name: expectedPodName,
Namespace: "test",
},
},
},
}

updatedVEndpointSlice := vEndpointSlice.DeepCopy()
updatedVEndpointSlice.ResourceVersion = "2"
updatedVEndpointSlice.Labels["extra-label"] = "true"
updatedVEndpointSlice.Endpoints = append(updatedVEndpointSlice.Endpoints, discoveryv1.Endpoint{
Addresses: []string{"10.15.107.9"},
TargetRef: &corev1.ObjectReference{
Kind: "Pod",
Name: "example-db-0",
Namespace: "example-prod",
},
})

expectedUpdatedEndpointSlice := expectedEndpointSlice.DeepCopy()
expectedUpdatedEndpointSlice.Labels["extra-label"] = "true"
expectedUpdatedEndpointSlice.Endpoints = append(expectedUpdatedEndpointSlice.Endpoints, discoveryv1.Endpoint{
Addresses: []string{"10.15.107.9"},
TargetRef: &corev1.ObjectReference{
Kind: "Pod",
Name: translate.Default.HostName(nil, "example-db-0", "example-prod").Name,
Namespace: "test",
},
})

vServicePrimary := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "example-db-primary",
Namespace: "example-prod",
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "mariadb",
},
},
}

repointedVEndpointSlice := vEndpointSlice.DeepCopy()
repointedVEndpointSlice.ResourceVersion = "2"
repointedVEndpointSlice.Labels[translate.K8sServiceNameLabel] = vServicePrimary.Name

expectedRepointedEndpointSlice := expectedEndpointSlice.DeepCopy()
expectedRepointedEndpointSlice.Labels[translate.K8sServiceNameLabel] = translate.Default.HostName(nil, vServicePrimary.Name, vServicePrimary.Namespace).Name

k8sEndpointSlice := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "k8s-slice",
Namespace: "example-prod",
Labels: map[string]string{
discoveryv1.LabelManagedBy: "endpointslice-controller.k8s.io",
translate.K8sServiceNameLabel: "example-db-secondary",
},
},
AddressType: discoveryv1.AddressTypeIPv4,
}
expectedHostK8sEndpointSliceName := translate.Default.HostName(nil, k8sEndpointSlice.Name, k8sEndpointSlice.Namespace).Name
hostK8sEndpointSlice := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: expectedHostK8sEndpointSliceName,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: k8sEndpointSlice.Name,
},
},
}

syncertesting.RunTests(t, []*syncertesting.SyncTest{
{
Name: "Custom-managed endpointSlice with service selector is synced to host with translated service name",
InitialVirtualState: []runtime.Object{
vService,
vEndpointSlice,
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice"): {
expectedEndpointSlice,
},
},
Sync: func(ctx *synccontext.RegisterContext) {
_, fakeSyncer := newFakeSyncer(t, ctx)
syncController, err := syncer.NewSyncController(ctx, fakeSyncer)
assert.NilError(t, err)

_, err = syncController.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: vEndpointSlice.Namespace,
Name: vEndpointSlice.Name,
}})
assert.NilError(t, err)
},
},
{
Name: "Custom-managed endpointSlice update preserves translated kubernetes.io/service-name",
InitialVirtualState: []runtime.Object{
vService,
updatedVEndpointSlice,
},
InitialPhysicalState: []runtime.Object{
expectedEndpointSlice,
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice"): {
expectedUpdatedEndpointSlice,
},
},
Sync: func(ctx *synccontext.RegisterContext) {
_, fakeSyncer := newFakeSyncer(t, ctx)
syncController, err := syncer.NewSyncController(ctx, fakeSyncer)
assert.NilError(t, err)

_, err = syncController.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: updatedVEndpointSlice.Namespace,
Name: updatedVEndpointSlice.Name,
}})
assert.NilError(t, err)
},
},
{
Name: "Custom-managed endpointSlice repointed to another service updates host kubernetes.io/service-name",
InitialVirtualState: []runtime.Object{
vService,
vServicePrimary,
repointedVEndpointSlice,
},
InitialPhysicalState: []runtime.Object{
expectedEndpointSlice,
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice"): {
expectedRepointedEndpointSlice,
},
},
Sync: func(ctx *synccontext.RegisterContext) {
_, fakeSyncer := newFakeSyncer(t, ctx)
syncController, err := syncer.NewSyncController(ctx, fakeSyncer)
assert.NilError(t, err)

_, err = syncController.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: repointedVEndpointSlice.Namespace,
Name: repointedVEndpointSlice.Name,
}})
assert.NilError(t, err)
},
},
{
Name: "K8s-managed endpointSlice is deleted when service has selector and host slice was vcluster-managed",
InitialVirtualState: []runtime.Object{
vService,
k8sEndpointSlice,
},
InitialPhysicalState: []runtime.Object{
hostK8sEndpointSlice,
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{},
Sync: func(ctx *synccontext.RegisterContext) {
_, fakeSyncer := newFakeSyncer(t, ctx)
syncController, err := syncer.NewSyncController(ctx, fakeSyncer)
assert.NilError(t, err)

_, err = syncController.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: k8sEndpointSlice.Namespace,
Name: k8sEndpointSlice.Name,
}})
assert.NilError(t, err)
},
},
})
}

func TestIsManagedByK8s(t *testing.T) {
tests := []struct {
name string
labels map[string]string
expected bool
}{
{
name: "nil labels",
labels: nil,
expected: true,
},
{
name: "missing managed-by label",
labels: map[string]string{"foo": "bar"},
expected: true,
},
{
name: "empty managed-by label",
labels: map[string]string{discoveryv1.LabelManagedBy: ""},
expected: true,
},
{
name: "endpointslice-controller.k8s.io",
labels: map[string]string{discoveryv1.LabelManagedBy: "endpointslice-controller.k8s.io"},
expected: true,
},
{
name: "endpointslice-mirroring-controller.k8s.io",
labels: map[string]string{discoveryv1.LabelManagedBy: "endpointslice-mirroring-controller.k8s.io"},
expected: true,
},
{
name: "mariadb-operator.k8s.mariadb.com",
labels: map[string]string{discoveryv1.LabelManagedBy: "mariadb-operator.k8s.mariadb.com"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
eps := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: tt.labels,
},
}
assert.Equal(t, isManagedByK8s(eps), tt.expected)
})
}
}
Loading