Skip to content

Commit

Permalink
Inherit service labels from K8s EndpointSlices
Browse files Browse the repository at this point in the history
...except ones that contain with "kubernetes.io/".

Fixes #754

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Aug 16, 2023
1 parent 6b76b8d commit 4822f28
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/agent/controller/clusterip_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package controller_test

import (
"fmt"
"strconv"

. "github.com/onsi/ginkgo/v2"
"github.com/submariner-io/admiral/pkg/resource"
Expand Down Expand Up @@ -244,6 +245,7 @@ func testClusterIPServiceInOneCluster() {
constants.MCSLabelSourceCluster: t.cluster1.clusterID,
mcsv1a1.LabelServiceName: service.Name,
constants.LabelSourceNamespace: service.Namespace,
constants.LabelIsHeadless: strconv.FormatBool(false),
},
},
AddressType: discovery.AddressTypeIPv4,
Expand Down Expand Up @@ -438,7 +440,7 @@ func testClusterIPServiceWithMultipleEPS() {
t.cluster1.serviceEndpointSlices = append(t.cluster1.serviceEndpointSlices, discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s2", serviceName, clusterID1),
Labels: map[string]string{discovery.LabelServiceName: serviceName},
Labels: t.cluster1.serviceEndpointSlices[0].Labels,
},
AddressType: discovery.AddressTypeIPv4,
Endpoints: []discovery.Endpoint{
Expand Down
31 changes: 27 additions & 4 deletions pkg/agent/controller/controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"reflect"
"sort"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -173,6 +174,10 @@ func newTestDiver() *testDriver {
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: serviceNamespace,
Labels: map[string]string{
"service-label1": "value1",
"service-label2": "value2",
},
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.253.9.1",
Expand All @@ -189,8 +194,11 @@ func newTestDiver() *testDriver {
serviceEndpointSlices: []discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s1", serviceName, clusterID1),
Labels: map[string]string{discovery.LabelServiceName: serviceName},
Name: fmt.Sprintf("%s-%s1", serviceName, clusterID1),
Labels: map[string]string{
discovery.LabelServiceName: serviceName,
"kubernetes.io/cluster-service": "true",
},
},
AddressType: discovery.AddressTypeIPv4,
Endpoints: []discovery.Endpoint{
Expand Down Expand Up @@ -319,6 +327,10 @@ func (t *testDriver) afterEach() {
}

func (c *cluster) init(syncerConfig *broker.SyncerConfig) {
for k, v := range c.service.Labels {
c.serviceEndpointSlices[0].Labels[k] = v
}

c.serviceIP = c.service.Spec.ClusterIP

c.localDynClient = dynamicfake.NewSimpleDynamicClient(syncerConfig.Scheme)
Expand Down Expand Up @@ -713,10 +725,16 @@ func awaitEndpointSlice(client dynamic.ResourceInterface, serviceName string, ex
Fail(fmt.Sprintf("EndpointSlice for %s/%s not found", expected.Namespace, expected.Name))
}

for k, v := range expected.Labels {
Expect(endpointSlice.Labels).To(HaveKeyWithValue(k, v))
actualLabels := map[string]string{}

for k, v := range endpointSlice.Labels {
if !strings.HasPrefix(k, "submariner-io/") {
actualLabels[k] = v
}
}

Expect(actualLabels).To(Equal(expected.Labels))

for k, v := range expected.Annotations {
Expect(endpointSlice.Annotations).To(HaveKeyWithValue(k, v))
}
Expand Down Expand Up @@ -800,6 +818,10 @@ func (t *testDriver) awaitEndpointSlice(c *cluster) {
AddressType: discovery.AddressTypeIPv4,
}

for k, v := range c.service.Labels {
epsTemplate.Labels[k] = v
}

var expected []discovery.EndpointSlice

if isHeadless {
Expand All @@ -811,6 +833,7 @@ func (t *testDriver) awaitEndpointSlice(c *cluster) {
eps.Endpoints = c.headlessEndpointAddresses[i]
eps.Ports = c.serviceEndpointSlices[i].Ports
eps.Name = c.serviceEndpointSlices[i].Name
eps.Labels[constants.LabelSourceName] = c.serviceEndpointSlices[i].Name
expected = append(expected, *eps)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/controller/headless_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var _ = Describe("Headless Service export", func() {
t.cluster1.serviceEndpointSlices = append(t.cluster1.serviceEndpointSlices, discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s2", serviceName, clusterID1),
Labels: map[string]string{discovery.LabelServiceName: serviceName},
Labels: t.cluster1.serviceEndpointSlices[0].Labels,
},
AddressType: discovery.AddressTypeIPv4,
Endpoints: []discovery.Endpoint{
Expand Down
7 changes: 7 additions & 0 deletions pkg/agent/controller/service_endpoint_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/federate"
Expand Down Expand Up @@ -265,6 +266,12 @@ func (c *ServiceEndpointSliceController) newEndpointSliceFrom(serviceEPS *discov
AddressType: serviceEPS.AddressType,
}

for k, v := range serviceEPS.Labels {
if !strings.Contains(k, "kubernetes.io/") {
eps.Labels[k] = v
}
}

if c.isHeadless() {
eps.Labels[constants.LabelSourceName] = serviceEPS.Name
}
Expand Down

0 comments on commit 4822f28

Please sign in to comment.