Simplify reconciler unit test setup

Pass initial state objects to fake.NewSimpleClientSet() rather than
calling Create() by hand.

(This will make it easier to have an initial state that is a mix of
Endpoints and EndpointSlices later on.)
This commit is contained in:
Dan Winship 2022-04-27 09:22:58 -04:00
parent 007ca4f69d
commit b07fe3a974
3 changed files with 146 additions and 202 deletions

View File

@ -26,6 +26,7 @@ import (
apiequality "k8s.io/apimachinery/pkg/api/equality" apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
) )
@ -37,7 +38,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled bool endpointSlicesEnabled bool
expectedError error expectedError error
expectedEndpoints *corev1.Endpoints expectedEndpoints *corev1.Endpoints
endpoints []*corev1.Endpoints initialState []runtime.Object
namespaceParam string namespaceParam string
nameParam string nameParam string
}{ }{
@ -45,7 +46,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false, endpointSlicesEnabled: false,
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints1, expectedEndpoints: endpoints1,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
nameParam: "foo", nameParam: "foo",
}, },
@ -53,7 +54,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: true, endpointSlicesEnabled: true,
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints1, expectedEndpoints: endpoints1,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
nameParam: "foo", nameParam: "foo",
}, },
@ -61,7 +62,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false, endpointSlicesEnabled: false,
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"), expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
expectedEndpoints: nil, expectedEndpoints: nil,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "foo", namespaceParam: "foo",
nameParam: "foo", nameParam: "foo",
}, },
@ -69,7 +70,7 @@ func TestEndpointsAdapterGet(t *testing.T) {
endpointSlicesEnabled: false, endpointSlicesEnabled: false,
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"), expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
expectedEndpoints: nil, expectedEndpoints: nil,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
nameParam: "bar", nameParam: "bar",
}, },
@ -77,19 +78,12 @@ func TestEndpointsAdapterGet(t *testing.T) {
for name, testCase := range testCases { for name, testCase := range testCases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset() client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled { if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1() epAdapter.endpointSliceClient = client.DiscoveryV1()
} }
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Get(testCase.namespaceParam, testCase.nameParam, metav1.GetOptions{}) endpoints, err := epAdapter.Get(testCase.namespaceParam, testCase.nameParam, metav1.GetOptions{})
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) { if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -121,7 +115,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError error expectedError error
expectedEndpoints *corev1.Endpoints expectedEndpoints *corev1.Endpoints
expectedEndpointSlice *discovery.EndpointSlice expectedEndpointSlice *discovery.EndpointSlice
endpoints []*corev1.Endpoints initialState []runtime.Object
endpointSlices []*discovery.EndpointSlice endpointSlices []*discovery.EndpointSlice
namespaceParam string namespaceParam string
endpointsParam *corev1.Endpoints endpointsParam *corev1.Endpoints
@ -131,7 +125,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints1, expectedEndpoints: endpoints1,
expectedEndpointSlice: epSlice1, expectedEndpointSlice: epSlice1,
endpoints: []*corev1.Endpoints{}, initialState: []runtime.Object{},
namespaceParam: endpoints1.Namespace, namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -140,7 +134,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints2, expectedEndpoints: endpoints2,
expectedEndpointSlice: epSlice2, expectedEndpointSlice: epSlice2,
endpoints: []*corev1.Endpoints{}, initialState: []runtime.Object{},
namespaceParam: endpoints2.Namespace, namespaceParam: endpoints2.Namespace,
endpointsParam: endpoints2, endpointsParam: endpoints2,
}, },
@ -149,7 +143,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints3, expectedEndpoints: endpoints3,
expectedEndpointSlice: epSlice3, expectedEndpointSlice: epSlice3,
endpoints: []*corev1.Endpoints{}, initialState: []runtime.Object{},
namespaceParam: endpoints3.Namespace, namespaceParam: endpoints3.Namespace,
endpointsParam: endpoints3, endpointsParam: endpoints3,
}, },
@ -158,7 +152,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints1, expectedEndpoints: endpoints1,
expectedEndpointSlice: nil, expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{}, initialState: []runtime.Object{},
namespaceParam: endpoints1.Namespace, namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -167,7 +161,7 @@ func TestEndpointsAdapterCreate(t *testing.T) {
expectedError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"), expectedError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
expectedEndpoints: nil, expectedEndpoints: nil,
expectedEndpointSlice: nil, expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: endpoints1.Namespace, namespaceParam: endpoints1.Namespace,
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -175,19 +169,12 @@ func TestEndpointsAdapterCreate(t *testing.T) {
for name, testCase := range testCases { for name, testCase := range testCases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset() client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled { if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1() epAdapter.endpointSliceClient = client.DiscoveryV1()
} }
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Create(testCase.namespaceParam, testCase.endpointsParam) endpoints, err := epAdapter.Create(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) { if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -241,7 +228,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError error expectedError error
expectedEndpoints *corev1.Endpoints expectedEndpoints *corev1.Endpoints
expectedEndpointSlice *discovery.EndpointSlice expectedEndpointSlice *discovery.EndpointSlice
endpoints []*corev1.Endpoints initialState []runtime.Object
endpointSlices []*discovery.EndpointSlice endpointSlices []*discovery.EndpointSlice
namespaceParam string namespaceParam string
endpointsParam *corev1.Endpoints endpointsParam *corev1.Endpoints
@ -251,7 +238,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints1, expectedEndpoints: endpoints1,
expectedEndpointSlice: nil, expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -260,7 +247,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints4, expectedEndpoints: endpoints4,
expectedEndpointSlice: epSlice4IPv4, expectedEndpointSlice: epSlice4IPv4,
endpoints: []*corev1.Endpoints{endpoints4}, initialState: []runtime.Object{endpoints4},
endpointSlices: []*discovery.EndpointSlice{epSlice4IP}, endpointSlices: []*discovery.EndpointSlice{epSlice4IP},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints4, endpointsParam: endpoints4,
@ -270,7 +257,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedEndpoints: endpoints2, expectedEndpoints: endpoints2,
expectedEndpointSlice: epSlice2, expectedEndpointSlice: epSlice2,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints2, endpointsParam: endpoints2,
}, },
@ -279,7 +266,7 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"), expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
expectedEndpoints: nil, expectedEndpoints: nil,
expectedEndpointSlice: nil, expectedEndpointSlice: nil,
endpoints: []*corev1.Endpoints{endpoints1}, initialState: []runtime.Object{endpoints1},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints3, endpointsParam: endpoints3,
}, },
@ -287,19 +274,12 @@ func TestEndpointsAdapterUpdate(t *testing.T) {
for name, testCase := range testCases { for name, testCase := range testCases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset() client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled { if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1() epAdapter.endpointSliceClient = client.DiscoveryV1()
} }
for _, endpoints := range testCase.endpoints {
_, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating Endpoints: %v", err)
}
}
endpoints, err := epAdapter.Update(testCase.namespaceParam, testCase.endpointsParam) endpoints, err := epAdapter.Update(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) { if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
@ -389,7 +369,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled bool endpointSlicesEnabled bool
expectedError error expectedError error
expectedEndpointSlice *discovery.EndpointSlice expectedEndpointSlice *discovery.EndpointSlice
endpointSlices []*discovery.EndpointSlice initialState []runtime.Object
namespaceParam string namespaceParam string
endpointsParam *corev1.Endpoints endpointsParam *corev1.Endpoints
}{ }{
@ -397,7 +377,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true, endpointSlicesEnabled: true,
expectedError: nil, expectedError: nil,
expectedEndpointSlice: epSlice1, expectedEndpointSlice: epSlice1,
endpointSlices: []*discovery.EndpointSlice{epSlice1}, initialState: []runtime.Object{epSlice1},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -405,7 +385,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true, endpointSlicesEnabled: true,
expectedError: nil, expectedError: nil,
expectedEndpointSlice: epSlice2, expectedEndpointSlice: epSlice2,
endpointSlices: []*discovery.EndpointSlice{epSlice1}, initialState: []runtime.Object{epSlice1},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints2, endpointsParam: endpoints2,
}, },
@ -413,7 +393,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: true, endpointSlicesEnabled: true,
expectedError: nil, expectedError: nil,
expectedEndpointSlice: epSlice1, expectedEndpointSlice: epSlice1,
endpointSlices: []*discovery.EndpointSlice{}, initialState: []runtime.Object{},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -421,7 +401,7 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
endpointSlicesEnabled: false, endpointSlicesEnabled: false,
expectedError: nil, expectedError: nil,
expectedEndpointSlice: nil, expectedEndpointSlice: nil,
endpointSlices: []*discovery.EndpointSlice{}, initialState: []runtime.Object{},
namespaceParam: "testing", namespaceParam: "testing",
endpointsParam: endpoints1, endpointsParam: endpoints1,
}, },
@ -429,19 +409,12 @@ func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
for name, testCase := range testCases { for name, testCase := range testCases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
client := fake.NewSimpleClientset() client := fake.NewSimpleClientset(testCase.initialState...)
epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
if testCase.endpointSlicesEnabled { if testCase.endpointSlicesEnabled {
epAdapter.endpointSliceClient = client.DiscoveryV1() epAdapter.endpointSliceClient = client.DiscoveryV1()
} }
for _, endpointSlice := range testCase.endpointSlices {
_, err := client.DiscoveryV1().EndpointSlices(endpointSlice.Namespace).Create(context.TODO(), endpointSlice, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating EndpointSlice: %v", err)
}
}
err := epAdapter.EnsureEndpointSliceFromEndpoints(testCase.namespaceParam, testCase.endpointsParam) err := epAdapter.EnsureEndpointSliceFromEndpoints(testCase.namespaceParam, testCase.endpointsParam)
if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) { if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err) t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)

View File

@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1" discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing" core "k8s.io/client-go/testing"
netutils "k8s.io/utils/net" netutils "k8s.io/utils/net"
@ -45,7 +46,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip string ip string
endpointPorts []corev1.EndpointPort endpointPorts []corev1.EndpointPort
additionalMasters int additionalMasters int
endpoints *corev1.EndpointsList initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected expectCreate *corev1.Endpoints // nil means none expected
}{ }{
@ -54,7 +55,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil, initialState: nil,
expectCreate: &corev1.Endpoints{ expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
@ -68,14 +69,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
}, },
{ {
@ -83,14 +84,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -106,8 +107,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3, additionalMasters: 3,
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -119,7 +120,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -140,8 +141,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.4", ip: "4.3.2.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3, additionalMasters: 3,
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -153,7 +154,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -174,8 +175,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2", ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3, additionalMasters: 3,
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -184,7 +185,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: nil, expectUpdate: nil,
}, },
@ -194,8 +195,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip: "4.3.2.2", ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
additionalMasters: 3, additionalMasters: 3,
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -203,7 +204,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -221,14 +222,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("bar", true), ObjectMeta: om("bar", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectCreate: &corev1.Endpoints{ expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -243,14 +244,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -265,14 +266,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -287,14 +288,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -309,14 +310,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -335,8 +336,8 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"}, {Name: "baz", Port: 1010, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
@ -346,7 +347,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "baz", Port: 1010, Protocol: "TCP"}, {Name: "baz", Port: 1010, Protocol: "TCP"},
}, },
}}, }},
}}, },
}, },
}, },
{ {
@ -357,14 +358,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -382,7 +383,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "boo", serviceName: "boo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}}, endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
endpoints: nil, initialState: nil,
expectCreate: &corev1.Endpoints{ expectCreate: &corev1.Endpoints{
ObjectMeta: om("boo", true), ObjectMeta: om("boo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
@ -394,10 +395,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
for _, test := range reconcileTests { for _, test := range reconcileTests {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset(test.initialState...)
if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints)
}
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil) epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter) reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true) err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
@ -449,7 +447,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
ip string ip string
endpointPorts []corev1.EndpointPort endpointPorts []corev1.EndpointPort
additionalMasters int additionalMasters int
endpoints *corev1.EndpointsList initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected expectUpdate *corev1.Endpoints // nil means none expected
expectCreate *corev1.Endpoints // nil means none expected expectCreate *corev1.Endpoints // nil means none expected
}{ }{
@ -461,14 +459,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: nil, expectUpdate: nil,
}, },
@ -480,14 +478,14 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -502,7 +500,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil, initialState: nil,
expectCreate: &corev1.Endpoints{ expectCreate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
@ -514,10 +512,7 @@ func TestMasterCountEndpointReconciler(t *testing.T) {
} }
for _, test := range nonReconcileTests { for _, test := range nonReconcileTests {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset(test.initialState...)
if test.endpoints != nil {
fakeClient = fake.NewSimpleClientset(test.endpoints)
}
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil) epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter) reconciler := NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false) err := reconciler.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
@ -576,10 +571,7 @@ func TestEmptySubsets(t *testing.T) {
Subsets: nil, Subsets: nil,
}}, }},
} }
fakeClient := fake.NewSimpleClientset() fakeClient := fake.NewSimpleClientset(endpoints)
if endpoints != nil {
fakeClient = fake.NewSimpleClientset(endpoints)
}
epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil) epAdapter := NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := NewMasterCountEndpointReconciler(1, epAdapter) reconciler := NewMasterCountEndpointReconciler(1, epAdapter)
endpointPorts := []corev1.EndpointPort{ endpointPorts := []corev1.EndpointPort{

View File

@ -29,6 +29,7 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1" discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
netutils "k8s.io/utils/net" netutils "k8s.io/utils/net"
) )
@ -97,7 +98,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip string ip string
endpointPorts []corev1.EndpointPort endpointPorts []corev1.EndpointPort
endpointKeys []string endpointKeys []string
endpoints *corev1.EndpointsList initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected expectUpdate *corev1.Endpoints // nil means none expected
}{ }{
{ {
@ -105,7 +106,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil, initialState: nil,
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
@ -119,14 +120,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
}, },
{ {
@ -135,14 +136,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4"}, endpointKeys: []string{"1.2.3.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
}, },
{ {
@ -150,14 +151,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -173,8 +174,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -186,7 +187,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -207,8 +208,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "4.3.2.4", ip: "4.3.2.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpointKeys: []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -220,7 +221,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -241,8 +242,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip: "4.3.2.2", ip: "4.3.2.2",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"4.3.2.1"}, endpointKeys: []string{"4.3.2.1"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -250,7 +251,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -268,14 +269,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("bar", true), ObjectMeta: om("bar", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -290,14 +291,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -312,14 +313,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -334,14 +335,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -356,14 +357,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -378,14 +379,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", false), ObjectMeta: om("foo", false),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -404,8 +405,8 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
{Name: "baz", Port: 1010, Protocol: "TCP"}, {Name: "baz", Port: 1010, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
@ -415,7 +416,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "baz", Port: 1010, Protocol: "TCP"}, {Name: "baz", Port: 1010, Protocol: "TCP"},
}, },
}}, }},
}}, },
}, },
}, },
{ {
@ -426,14 +427,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -451,15 +452,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases() fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys) fakeLeases.SetKeys(test.endpointKeys)
clientset := fake.NewSimpleClientset() clientset := fake.NewSimpleClientset(test.initialState...)
if test.endpoints != nil {
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
}
}
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
@ -488,7 +481,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
ip string ip string
endpointPorts []corev1.EndpointPort endpointPorts []corev1.EndpointPort
endpointKeys []string endpointKeys []string
endpoints *corev1.EndpointsList initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected expectUpdate *corev1.Endpoints // nil means none expected
}{ }{
{ {
@ -499,14 +492,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: nil, expectUpdate: nil,
}, },
@ -518,14 +511,14 @@ func TestLeaseEndpointReconciler(t *testing.T) {
{Name: "foo", Port: 8080, Protocol: "TCP"}, {Name: "foo", Port: 8080, Protocol: "TCP"},
{Name: "bar", Port: 1000, Protocol: "TCP"}, {Name: "bar", Port: 1000, Protocol: "TCP"},
}, },
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -540,7 +533,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
serviceName: "foo", serviceName: "foo",
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpoints: nil, initialState: nil,
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
@ -554,15 +547,7 @@ func TestLeaseEndpointReconciler(t *testing.T) {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases() fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys) fakeLeases.SetKeys(test.endpointKeys)
clientset := fake.NewSimpleClientset() clientset := fake.NewSimpleClientset(test.initialState...)
if test.endpoints != nil {
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
}
}
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false) err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
@ -602,7 +587,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip string ip string
endpointPorts []corev1.EndpointPort endpointPorts []corev1.EndpointPort
endpointKeys []string endpointKeys []string
endpoints *corev1.EndpointsList initialState []runtime.Object
expectUpdate *corev1.Endpoints // nil means none expected expectUpdate *corev1.Endpoints // nil means none expected
}{ }{
{ {
@ -611,8 +596,8 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "1.2.3.4", ip: "1.2.3.4",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -623,7 +608,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
expectUpdate: &corev1.Endpoints{ expectUpdate: &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
@ -643,8 +628,8 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "5.6.7.8", ip: "5.6.7.8",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: []corev1.EndpointSubset{{ Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{ Addresses: []corev1.EndpointAddress{
@ -655,7 +640,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
}, },
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
}}, }},
}}, },
}, },
}, },
{ {
@ -664,11 +649,11 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
ip: "5.6.7.8", ip: "5.6.7.8",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{ initialState: []runtime.Object{
Items: []corev1.Endpoints{{ &corev1.Endpoints{
ObjectMeta: om("foo", true), ObjectMeta: om("foo", true),
Subsets: nil, Subsets: nil,
}}, },
}, },
}, },
} }
@ -676,13 +661,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
fakeLeases := newFakeLeases() fakeLeases := newFakeLeases()
fakeLeases.SetKeys(test.endpointKeys) fakeLeases.SetKeys(test.endpointKeys)
clientset := fake.NewSimpleClientset() clientset := fake.NewSimpleClientset(test.initialState...)
for _, ep := range test.endpoints.Items {
if _, err := clientset.CoreV1().Endpoints(ep.Namespace).Create(context.TODO(), &ep, metav1.CreateOptions{}); err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
}
epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()} epAdapter := EndpointsAdapter{endpointClient: clientset.CoreV1()}
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases) r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts) err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts)