Ensuring EndpointSlices are recreated after Service recreation
This fixes a bug that occurred when a Service was rapidly recreated. This relied on an unfortunate series of events: 1. When the Service is deleted, the EndpointSlice controller removes it from the EndpointSliceTracker along with any associated EndpointSlices. 2. When the Service is recreated, the EndpointSlice controller sees that there are still appropriate EndpointSlices for the Service and does nothing. (They have not yet been garbage collected). 3. When the EndpointSlice is deleted, the EndpointSlice controller checks with the EndpointSliceTracker to see if it thinks we should have this EndpointSlice. This check was intended to ensure we wouldn't requeue a Service every time we delete an EndpointSlice for it. This adds a check in reconciler to ensure that EndpointSlices it is working with are owned by a Service with a matching UID. If not, it will mark those EndpointSlices for deletion (assuming they're about to be garbage collected anyway) and create new EndpointSlices.
This commit is contained in:
@@ -29,6 +29,8 @@ import (
|
||||
discovery "k8s.io/api/discovery/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/informers"
|
||||
@@ -126,7 +128,7 @@ func TestSyncServiceWithSelector(t *testing.T) {
|
||||
ns := metav1.NamespaceDefault
|
||||
serviceName := "testing-1"
|
||||
client, esController := newController([]string{"node-1"}, time.Duration(0))
|
||||
standardSyncService(t, esController, ns, serviceName, "true")
|
||||
standardSyncService(t, esController, ns, serviceName)
|
||||
expectActions(t, client.Actions(), 1, "create", "endpointslices")
|
||||
|
||||
sliceList, err := client.DiscoveryV1beta1().EndpointSlices(ns).List(context.TODO(), metav1.ListOptions{})
|
||||
@@ -192,7 +194,7 @@ func TestSyncServicePodSelection(t *testing.T) {
|
||||
pod2.Labels["foo"] = "boo"
|
||||
esController.podStore.Add(pod2)
|
||||
|
||||
standardSyncService(t, esController, ns, "testing-1", "true")
|
||||
standardSyncService(t, esController, ns, "testing-1")
|
||||
expectActions(t, client.Actions(), 1, "create", "endpointslices")
|
||||
|
||||
// an endpoint slice should be created, it should only reference pod1 (not pod2)
|
||||
@@ -211,12 +213,17 @@ func TestSyncServiceEndpointSliceLabelSelection(t *testing.T) {
|
||||
client, esController := newController([]string{"node-1"}, time.Duration(0))
|
||||
ns := metav1.NamespaceDefault
|
||||
serviceName := "testing-1"
|
||||
service := createService(t, esController, ns, serviceName)
|
||||
|
||||
gvk := schema.GroupVersionKind{Version: "v1", Kind: "Service"}
|
||||
ownerRef := metav1.NewControllerRef(service, gvk)
|
||||
|
||||
// 5 slices, 3 with matching labels for our service
|
||||
endpointSlices := []*discovery.EndpointSlice{{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "matching-1",
|
||||
Namespace: ns,
|
||||
Name: "matching-1",
|
||||
Namespace: ns,
|
||||
OwnerReferences: []metav1.OwnerReference{*ownerRef},
|
||||
Labels: map[string]string{
|
||||
discovery.LabelServiceName: serviceName,
|
||||
discovery.LabelManagedBy: controllerName,
|
||||
@@ -225,8 +232,9 @@ func TestSyncServiceEndpointSliceLabelSelection(t *testing.T) {
|
||||
AddressType: discovery.AddressTypeIPv4,
|
||||
}, {
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "matching-2",
|
||||
Namespace: ns,
|
||||
Name: "matching-2",
|
||||
Namespace: ns,
|
||||
OwnerReferences: []metav1.OwnerReference{*ownerRef},
|
||||
Labels: map[string]string{
|
||||
discovery.LabelServiceName: serviceName,
|
||||
discovery.LabelManagedBy: controllerName,
|
||||
@@ -278,9 +286,9 @@ func TestSyncServiceEndpointSliceLabelSelection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// +1 for extra action involved in Service creation before syncService call.
|
||||
numActionsBefore := len(client.Actions()) + 1
|
||||
standardSyncService(t, esController, ns, serviceName, "false")
|
||||
numActionsBefore := len(client.Actions())
|
||||
err := esController.syncService(fmt.Sprintf("%s/%s", ns, serviceName))
|
||||
assert.Nil(t, err, "Expected no error syncing service")
|
||||
|
||||
if len(client.Actions()) != numActionsBefore+2 {
|
||||
t.Errorf("Expected 2 more actions, got %d", len(client.Actions())-numActionsBefore)
|
||||
@@ -784,21 +792,22 @@ func addPods(t *testing.T, esController *endpointSliceController, namespace stri
|
||||
}
|
||||
}
|
||||
|
||||
func standardSyncService(t *testing.T, esController *endpointSliceController, namespace, serviceName, managedBySetup string) {
|
||||
func standardSyncService(t *testing.T, esController *endpointSliceController, namespace, serviceName string) {
|
||||
t.Helper()
|
||||
createService(t, esController, namespace, serviceName, managedBySetup)
|
||||
createService(t, esController, namespace, serviceName)
|
||||
|
||||
err := esController.syncService(fmt.Sprintf("%s/%s", namespace, serviceName))
|
||||
assert.Nil(t, err, "Expected no error syncing service")
|
||||
}
|
||||
|
||||
func createService(t *testing.T, esController *endpointSliceController, namespace, serviceName, managedBySetup string) *v1.Service {
|
||||
func createService(t *testing.T, esController *endpointSliceController, namespace, serviceName string) *v1.Service {
|
||||
t.Helper()
|
||||
service := &v1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: serviceName,
|
||||
Namespace: namespace,
|
||||
CreationTimestamp: metav1.NewTime(time.Now()),
|
||||
UID: types.UID(namespace + "-" + serviceName),
|
||||
},
|
||||
Spec: v1.ServiceSpec{
|
||||
Ports: []v1.ServicePort{{TargetPort: intstr.FromInt(80)}},
|
||||
|
Reference in New Issue
Block a user