
EndpointSlice is always enabled now, so make it non-optional in the EndpointsAdapter, make all of the test cases pass an EndpointSlice client, and remove the "EndpointSlices disabled"-specific tests. By changing makeEndpointsArray() to return both an Endpoints and an EndpointsSlice, the "initialObjects" and "expectCreate"/"expectUpdate" fields of (almost) all of the existing unit tests are automatically switched to be EndpointSlice-aware; instead of having an initial state with just Endpoints (or nothing), and testing that just the Endpoints is created/updated correctly, they now have an initial state with both Endpoints and EndpointSlice (or nothing), and test that both objects are created/updated correctly. The handful of existing test cases that used inconsistent Endpoints and EndpointSlice state have been modified to create the objects separately.
400 lines
16 KiB
Go
400 lines
16 KiB
Go
/*
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package reconcilers
|
|
|
|
/*
|
|
Original Source:
|
|
https://github.com/openshift/origin/blob/bb340c5dd5ff72718be86fb194dedc0faed7f4c7/pkg/cmd/server/election/lease_endpoint_reconciler_test.go
|
|
*/
|
|
|
|
import (
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
netutils "k8s.io/utils/net"
|
|
)
|
|
|
|
type fakeLeases struct {
|
|
keys map[string]bool
|
|
}
|
|
|
|
var _ Leases = &fakeLeases{}
|
|
|
|
func newFakeLeases() *fakeLeases {
|
|
return &fakeLeases{make(map[string]bool)}
|
|
}
|
|
|
|
func (f *fakeLeases) ListLeases() ([]string, error) {
|
|
res := make([]string, 0, len(f.keys))
|
|
for ip := range f.keys {
|
|
res = append(res, ip)
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (f *fakeLeases) UpdateLease(ip string) error {
|
|
f.keys[ip] = true
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeLeases) RemoveLease(ip string) error {
|
|
delete(f.keys, ip)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeLeases) SetKeys(keys []string) {
|
|
for _, ip := range keys {
|
|
f.keys[ip] = false
|
|
}
|
|
}
|
|
|
|
func (f *fakeLeases) GetUpdatedKeys() []string {
|
|
res := []string{}
|
|
for ip, updated := range f.keys {
|
|
if updated {
|
|
res = append(res, ip)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (f *fakeLeases) Destroy() {
|
|
}
|
|
|
|
func TestLeaseEndpointReconciler(t *testing.T) {
|
|
reconcileTests := []struct {
|
|
testName string
|
|
serviceName string
|
|
ip string
|
|
endpointPorts []corev1.EndpointPort
|
|
endpointKeys []string
|
|
initialState []runtime.Object
|
|
expectUpdate []runtime.Object
|
|
expectCreate []runtime.Object
|
|
}{
|
|
{
|
|
testName: "no existing endpoints",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: nil,
|
|
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints satisfy",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints satisfy + refresh existing key",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
endpointKeys: []string{"1.2.3.4"},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints satisfy but too many",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints satisfy but too many + extra masters",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
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"},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints satisfy but too many + extra masters + delete first",
|
|
serviceName: "foo",
|
|
ip: "4.3.2.4",
|
|
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"},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints current IP missing",
|
|
serviceName: "foo",
|
|
ip: "4.3.2.2",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
endpointKeys: []string{"4.3.2.1"},
|
|
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.1", "4.3.2.2"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints wrong name",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("bar", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints wrong IP",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints wrong port",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints wrong protocol",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints wrong port name",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "existing endpoints without skip mirror label",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: []runtime.Object{
|
|
// can't use makeEndpointsArray() here because we don't want the
|
|
// skip-mirror label
|
|
&corev1.Endpoints{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: metav1.NamespaceDefault,
|
|
Name: "foo",
|
|
},
|
|
Subsets: []corev1.EndpointSubset{{
|
|
Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
|
|
Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
}},
|
|
},
|
|
makeEndpointSlice("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
expectUpdate: []runtime.Object{
|
|
makeEndpoints("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
// EndpointSlice does not get updated because it was already correct
|
|
},
|
|
},
|
|
{
|
|
testName: "existing endpoints extra service ports satisfy",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
{Name: "baz", Port: 1010, Protocol: "TCP"},
|
|
},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"},
|
|
[]corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
{Name: "baz", Port: 1010, Protocol: "TCP"},
|
|
},
|
|
),
|
|
},
|
|
{
|
|
testName: "existing endpoints extra service ports missing port",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"},
|
|
[]corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
},
|
|
),
|
|
},
|
|
}
|
|
for _, test := range reconcileTests {
|
|
t.Run(test.testName, func(t *testing.T) {
|
|
fakeLeases := newFakeLeases()
|
|
fakeLeases.SetKeys(test.endpointKeys)
|
|
clientset := fake.NewSimpleClientset(test.initialState...)
|
|
|
|
epAdapter := NewEndpointsAdapter(clientset.CoreV1(), clientset.DiscoveryV1())
|
|
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
|
|
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, true)
|
|
if err != nil {
|
|
t.Errorf("unexpected error reconciling: %v", err)
|
|
}
|
|
|
|
err = verifyCreatesAndUpdates(clientset, test.expectCreate, test.expectUpdate)
|
|
if err != nil {
|
|
t.Errorf("unexpected error in side effects: %v", err)
|
|
}
|
|
|
|
if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip {
|
|
t.Errorf("expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", updatedKeys)
|
|
}
|
|
})
|
|
}
|
|
|
|
nonReconcileTests := []struct {
|
|
testName string
|
|
serviceName string
|
|
ip string
|
|
endpointPorts []corev1.EndpointPort
|
|
endpointKeys []string
|
|
initialState []runtime.Object
|
|
expectUpdate []runtime.Object
|
|
expectCreate []runtime.Object
|
|
}{
|
|
{
|
|
testName: "existing endpoints extra service ports missing port no update",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: nil,
|
|
},
|
|
{
|
|
testName: "existing endpoints extra service ports, wrong ports, wrong IP",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{
|
|
{Name: "foo", Port: 8080, Protocol: "TCP"},
|
|
{Name: "bar", Port: 1000, Protocol: "TCP"},
|
|
},
|
|
initialState: makeEndpointsArray("foo", []string{"4.3.2.1"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "no existing endpoints",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
|
|
initialState: nil,
|
|
expectCreate: makeEndpointsArray("foo", []string{"1.2.3.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
}
|
|
for _, test := range nonReconcileTests {
|
|
t.Run(test.testName, func(t *testing.T) {
|
|
fakeLeases := newFakeLeases()
|
|
fakeLeases.SetKeys(test.endpointKeys)
|
|
clientset := fake.NewSimpleClientset(test.initialState...)
|
|
epAdapter := NewEndpointsAdapter(clientset.CoreV1(), clientset.DiscoveryV1())
|
|
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
|
|
err := r.ReconcileEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts, false)
|
|
if err != nil {
|
|
t.Errorf("unexpected error reconciling: %v", err)
|
|
}
|
|
|
|
err = verifyCreatesAndUpdates(clientset, test.expectCreate, test.expectUpdate)
|
|
if err != nil {
|
|
t.Errorf("unexpected error in side effects: %v", err)
|
|
}
|
|
|
|
if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip {
|
|
t.Errorf("expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", updatedKeys)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLeaseRemoveEndpoints(t *testing.T) {
|
|
stopTests := []struct {
|
|
testName string
|
|
serviceName string
|
|
ip string
|
|
endpointPorts []corev1.EndpointPort
|
|
endpointKeys []string
|
|
initialState []runtime.Object
|
|
expectUpdate []runtime.Object
|
|
}{
|
|
{
|
|
testName: "successful stop reconciling",
|
|
serviceName: "foo",
|
|
ip: "1.2.3.4",
|
|
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"},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "stop reconciling with ip not in endpoint ip list",
|
|
serviceName: "foo",
|
|
ip: "5.6.7.8",
|
|
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"},
|
|
initialState: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
{
|
|
testName: "endpoint with no subset",
|
|
serviceName: "foo",
|
|
ip: "5.6.7.8",
|
|
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"},
|
|
initialState: makeEndpointsArray("foo", nil, nil),
|
|
expectUpdate: makeEndpointsArray("foo", []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}),
|
|
},
|
|
}
|
|
for _, test := range stopTests {
|
|
t.Run(test.testName, func(t *testing.T) {
|
|
fakeLeases := newFakeLeases()
|
|
fakeLeases.SetKeys(test.endpointKeys)
|
|
clientset := fake.NewSimpleClientset(test.initialState...)
|
|
epAdapter := NewEndpointsAdapter(clientset.CoreV1(), clientset.DiscoveryV1())
|
|
r := NewLeaseEndpointReconciler(epAdapter, fakeLeases)
|
|
err := r.RemoveEndpoints(test.serviceName, netutils.ParseIPSloppy(test.ip), test.endpointPorts)
|
|
if err != nil {
|
|
t.Errorf("unexpected error reconciling: %v", err)
|
|
}
|
|
|
|
err = verifyCreatesAndUpdates(clientset, nil, test.expectUpdate)
|
|
if err != nil {
|
|
t.Errorf("unexpected error in side effects: %v", err)
|
|
}
|
|
|
|
for _, key := range fakeLeases.GetUpdatedKeys() {
|
|
if key == test.ip {
|
|
t.Errorf("Found ip %s in leases but shouldn't be there", key)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|