Repository: submariner-io/lighthouse
Pull request #478 (2021-03-01 10:40:09)

Fix issue with Endpointslice delete
Fixes : lighthouse/#475

Signed-off-by: Aswin Surayanarayanan <asuryana@redhat.com>

aswinsuryan: PR on devel https://github.com/submariner-io/lighthouse/pull/480
tpantelis: Let's merge this - I'll follow-up with UT changes.

Patch:
diff --git a/pkg/agent/controller/endpoint.go b/pkg/agent/controller/endpoint.go
index c054ee6..f5615f2 100644
--- a/pkg/agent/controller/endpoint.go
+++ b/pkg/agent/controller/endpoint.go
@@ -7,10 +7,13 @@ import (
 	lhconstants "github.com/submariner-io/lighthouse/pkg/constants"
 	corev1 "k8s.io/api/core/v1"
 	discovery "k8s.io/api/discovery/v1beta1"
+	"k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/api/meta"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/fields"
+	"k8s.io/apimachinery/pkg/labels"
 	"k8s.io/apimachinery/pkg/runtime"
+	"k8s.io/apimachinery/pkg/runtime/schema"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/client-go/dynamic"
 	"k8s.io/klog"
@@ -52,11 +55,30 @@ func startEndpointController(localClient dynamic.Interface, restMapper meta.REST
 		return nil, err
 	}
 
+	controller.localClient = localClient
+
 	return controller, nil
 }
 
 func (e *EndpointController) stop() {
 	close(e.stopCh)
+	e.cleanup()
+}
+
+func (e *EndpointController) cleanup() {
+	resourceClient := e.localClient.Resource(schema.GroupVersionResource{Group: "discovery.k8s.io",
+		Version: "v1beta1", Resource: "endpointslices"}).Namespace(e.serviceImportSourceNameSpace)
+
+	endpointSliceLabels := labels.SelectorFromSet(map[string]string{lhconstants.LabelServiceImportName: e.serviceImportName})
+	listEndpointSliceOptions := metav1.ListOptions{
+		LabelSelector: endpointSliceLabels.String(),
+	}
+
+	err := resourceClient.DeleteCollection(&metav1.DeleteOptions{}, listEndpointSliceOptions)
+
+	if err != nil && !errors.IsNotFound(err) {
+		klog.Errorf("Error deleting the EndpointSlices associated with serviceImport %q: %v", e.serviceImportName, err)
+	}
 }
 
 func (e *EndpointController) endpointsToEndpointSlice(obj runtime.Object, op syncer.Operation) (runtime.Object, bool) {
@@ -87,7 +109,7 @@ func (e *EndpointController) endpointSliceFromEndpoints(endpoints *corev1.Endpoi
 		lhconstants.LabelSourceName:        e.serviceName,
 	}
 	endpointSlice.OwnerReferences = []metav1.OwnerReference{{
-		APIVersion:         "lighthouse.submariner.io.v2alpha1",
+		APIVersion:         "multicluster.x-k8s.io.v1alpha1",
 		Kind:               "ServiceImport",
 		Name:               e.serviceImportName,
 		UID:                e.serviceImportUID,
diff --git a/pkg/agent/controller/types.go b/pkg/agent/controller/types.go
index a66fddb..df15b65 100644
--- a/pkg/agent/controller/types.go
+++ b/pkg/agent/controller/types.go
@@ -56,6 +56,7 @@ type EndpointController struct {
 	serviceName                  string
 	serviceImportSourceNameSpace string
 	stopCh                       chan struct{}
+	localClient                  dynamic.Interface
 }
 
 type LHServiceExportController struct {


Test patch:
diff --git a/pkg/agent/controller/agent_test.go b/pkg/agent/controller/agent_test.go
index 59affa5..d90eb5a 100644
--- a/pkg/agent/controller/agent_test.go
+++ b/pkg/agent/controller/agent_test.go
@@ -583,7 +583,7 @@ func awaitEndpointSlice(endpointSliceClient, serviceImportClient dynamic.Resourc
 		controllerFlag := false
 
 		Expect(endpointSlice.OwnerReferences[0]).To(Equal(metav1.OwnerReference{
-			APIVersion: "lighthouse.submariner.io.v2alpha1",
+			APIVersion: "multicluster.x-k8s.io.v1alpha1",
 			Kind:       "ServiceImport",
 			Name:       siName,
 			UID:        si.GetUID(),