Repository: submariner-io/lighthouse
Pull request #318 (2020-10-01 15:17:45)

Retrieve ServiceImport from syncer cache
Optmization...




Patch:
diff --git a/pkg/agent/controller/agent.go b/pkg/agent/controller/agent.go
index ee80c47..e313046 100644
--- a/pkg/agent/controller/agent.go
+++ b/pkg/agent/controller/agent.go
@@ -366,17 +366,19 @@ func (a *Controller) endpointToRemoteServiceImport(obj runtime.Object, op syncer
 
 	svcExport := obj.(*lighthousev2a1.ServiceExport)
 
-	serviceImport, err := a.getServiceImport(svcExport)
-
+	serviceImport, found, err := a.serviceImportController.getServiceImport(
+		a.getObjectNameWithClusterId(svcExport.Name, svcExport.Namespace), a.namespace)
 	if err != nil {
-		if !apierrors.IsNotFound(err) {
-			klog.Errorf("Error retrieving ServiceImport for Endpoints (%s/%s): %v", ep.Namespace, ep.Name, err)
-		}
+		klog.Errorf("Error retrieving ServiceImport for Endpoints (%s/%s): %v", ep.Namespace, ep.Name, err)
 
 		// Requeue
 		return nil, true
 	}
 
+	if !found {
+		return nil, true
+	}
+
 	if serviceImport.Spec.Type == lighthousev2a1.Headless && a.globalnetEnabled {
 		return nil, false
 	}
@@ -490,17 +492,6 @@ func (a *Controller) newServiceImport(svcExport *lighthousev2a1.ServiceExport) *
 	}
 }
 
-func (a *Controller) getServiceImport(svcExport *lighthousev2a1.ServiceExport) (*lighthousev2a1.ServiceImport, error) {
-	siName := a.getObjectNameWithClusterId(svcExport.Name, svcExport.Namespace)
-	serviceImport, err := a.lighthouseClient.LighthouseV2alpha1().ServiceImports(a.namespace).Get(siName, metav1.GetOptions{})
-
-	if err != nil {
-		return nil, err
-	}
-
-	return serviceImport, nil
-}
-
 func (a *Controller) getIPsForService(service *corev1.Service, siType lighthousev2a1.ServiceImportType) ([]string, error) {
 	if siType == lighthousev2a1.ClusterSetIP {
 		mcsIp := getGlobalIpFromService(service)
diff --git a/pkg/agent/controller/serviceimport.go b/pkg/agent/controller/serviceimport.go
index 0f10230..de77970 100644
--- a/pkg/agent/controller/serviceimport.go
+++ b/pkg/agent/controller/serviceimport.go
@@ -130,3 +130,12 @@ func (c *ServiceImportController) serviceImportToEndpointController(obj runtime.
 
 	return nil, c.serviceImportDeleted(serviceImport, key)
 }
+
+func (c *ServiceImportController) getServiceImport(name, namespace string) (*lighthousev2a1.ServiceImport, bool, error) {
+	obj, found, err := c.serviceImportSyncer.GetResource(name, namespace)
+	if obj != nil {
+		return obj.(*lighthousev2a1.ServiceImport), found, err
+	}
+
+	return nil, found, err
+}


Test patch:
diff --git a/pkg/agent/controller/agent_test.go b/pkg/agent/controller/agent_test.go
index b9f2e89..79d916b 100644
--- a/pkg/agent/controller/agent_test.go
+++ b/pkg/agent/controller/agent_test.go
@@ -549,10 +549,7 @@ func awaitServiceImport(client dynamic.ResourceInterface, service *corev1.Servic
 }
 
 func (c *cluster) awaitServiceImport(service *corev1.Service, sType lighthousev2a1.ServiceImportType, serviceIPs ...string) {
-	serviceImport := awaitServiceImport(c.localServiceImportClient, service, sType, serviceIPs...)
-
-	_, err := c.lighthouseClient.LighthouseV2alpha1().ServiceImports(serviceImport.Namespace).Create(serviceImport)
-	Expect(err).To(Succeed())
+	awaitServiceImport(c.localServiceImportClient, service, sType, serviceIPs...)
 }
 
 func awaitUpdatedServiceImport(client dynamic.ResourceInterface, service *corev1.Service,
@@ -585,10 +582,7 @@ func awaitUpdatedServiceImport(client dynamic.ResourceInterface, service *corev1
 }
 
 func (c *cluster) awaitUpdatedServiceImport(service *corev1.Service, serviceIPs ...string) {
-	serviceImport := awaitUpdatedServiceImport(c.localServiceImportClient, service, serviceIPs...)
-
-	_, err := c.lighthouseClient.LighthouseV2alpha1().ServiceImports(serviceImport.Namespace).Update(serviceImport)
-	Expect(err).To(Succeed())
+	awaitUpdatedServiceImport(c.localServiceImportClient, service, serviceIPs...)
 }
 
 func awaitEndpointSlice(endpointSliceClient, serviceImportClient dynamic.ResourceInterface,
@@ -876,9 +870,6 @@ func (t *testDriver) awaitServiceUnexported() {
 func (t *testDriver) awaitHeadlessServiceUnexported() {
 	t.awaitServiceUnexported()
 
-	Expect(t.cluster1.lighthouseClient.LighthouseV2alpha1().ServiceImports(t.service.Namespace).Delete(
-		t.service.Name+"-"+t.service.Namespace+"-"+clusterID1, nil)).To(Succeed())
-
 	// In a real k8s env, the EndpointSlice would be deleted via k8s GC as it is owned by the ServiceImport.
 	Expect(t.cluster1.localEndpointSliceClient.Delete(t.endpoints.Name+"-"+clusterID1, nil)).To(Succeed())