slice mirroring controller mirror annotations

Add support to the endpoint slice mirroring controller to mirror
annotations, in addition to labels, but don´t mirror endpoint
triggertime annotation.

Also, fix a bug in the endpointslice mirroring controller, that
wasn't updating the mirrored slice with the new labels, in case
that only the endpoint labels were modified.
This commit is contained in:
Antonio Ojea
2021-01-16 19:30:48 +01:00
committed by Antonio Ojea
parent 7c702138f9
commit c421e22e2c
5 changed files with 462 additions and 25 deletions

View File

@@ -21,6 +21,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,42 +39,153 @@ func TestNewEndpointSlice(t *testing.T) {
ports := []discovery.EndpointPort{{Name: &portName, Protocol: &protocol}}
addrType := discovery.AddressTypeIPv4
gvk := schema.GroupVersionKind{Version: "v1", Kind: "Endpoints"}
endpoints := v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "test",
Labels: map[string]string{"foo": "bar"},
},
Subsets: []v1.EndpointSubset{{
Ports: []v1.EndpointPort{{Port: 80}},
}},
}
gvk := schema.GroupVersionKind{Version: "v1", Kind: "Endpoints"}
ownerRef := metav1.NewControllerRef(&endpoints, gvk)
expectedSlice := discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
testCases := []struct {
name string
tweakEndpoint func(ep *corev1.Endpoints)
expectedSlice discovery.EndpointSlice
}{
{
name: "create slice from endpoints",
tweakEndpoint: func(ep *corev1.Endpoints) {
},
expectedSlice: discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
},
Annotations: map[string]string{},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
Namespace: endpoints.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
},
},
{
name: "create slice from endpoints with annotations",
tweakEndpoint: func(ep *corev1.Endpoints) {
annotations := map[string]string{"foo": "bar"}
ep.Annotations = annotations
},
expectedSlice: discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
},
Annotations: map[string]string{"foo": "bar"},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
Namespace: endpoints.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
},
},
{
name: "create slice from endpoints with labels",
tweakEndpoint: func(ep *corev1.Endpoints) {
labels := map[string]string{"foo": "bar"}
ep.Labels = labels
},
expectedSlice: discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
},
Annotations: map[string]string{},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
Namespace: endpoints.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
},
},
{
name: "create slice from endpoints with labels and annotations",
tweakEndpoint: func(ep *corev1.Endpoints) {
labels := map[string]string{"foo": "bar"}
ep.Labels = labels
annotations := map[string]string{"foo2": "bar2"}
ep.Annotations = annotations
},
expectedSlice: discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
},
Annotations: map[string]string{"foo2": "bar2"},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
Namespace: endpoints.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
},
},
{
name: "create slice from endpoints with labels and annotations triggertime",
tweakEndpoint: func(ep *corev1.Endpoints) {
labels := map[string]string{"foo": "bar"}
ep.Labels = labels
annotations := map[string]string{
"foo2": "bar2",
corev1.EndpointsLastChangeTriggerTime: "date",
}
ep.Annotations = annotations
},
expectedSlice: discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
discovery.LabelServiceName: endpoints.Name,
discovery.LabelManagedBy: controllerName,
},
Annotations: map[string]string{"foo2": "bar2"},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
Namespace: endpoints.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
},
GenerateName: fmt.Sprintf("%s-", endpoints.Name),
OwnerReferences: []metav1.OwnerReference{*ownerRef},
Namespace: endpoints.Namespace,
},
Ports: ports,
AddressType: addrType,
Endpoints: []discovery.Endpoint{},
}
generatedSlice := newEndpointSlice(&endpoints, ports, addrType, "")
assert.EqualValues(t, expectedSlice, *generatedSlice)
if len(endpoints.Labels) > 1 {
t.Errorf("Expected Endpoints labels to not be modified, got %+v", endpoints.Labels)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ep := endpoints.DeepCopy()
tc.tweakEndpoint(ep)
generatedSlice := newEndpointSlice(ep, ports, addrType, "")
assert.EqualValues(t, tc.expectedSlice, *generatedSlice)
if len(endpoints.Labels) > 1 {
t.Errorf("Expected Endpoints labels to not be modified, got %+v", endpoints.Labels)
}
})
}
}