Repository: submariner-io/lighthouse
Pull request #451 (2021-02-04 01:40:58)

Add service port unit tests
Also changed the code to return a nil array instead of an empty `ServicePort` when no port exists.




Patch:
diff --git a/pkg/agent/controller/agent.go b/pkg/agent/controller/agent.go
index a82eefc..3000965 100644
--- a/pkg/agent/controller/agent.go
+++ b/pkg/agent/controller/agent.go
@@ -425,11 +425,16 @@ func (a *Controller) newServiceImport(svcExport *mcsv1a1.ServiceExport) *mcsv1a1
 
 func (a *Controller) getIPsAndPortsForService(service *corev1.Service, siType mcsv1a1.ServiceImportType) (
 	[]string, []mcsv1a1.ServicePort, error) {
-	mcsPort := mcsv1a1.ServicePort{}
+	var mcsPorts []mcsv1a1.ServicePort
+
 	if len(service.Spec.Ports) > 0 {
-		mcsPort.Name = service.Spec.Ports[0].Name
-		mcsPort.Protocol = service.Spec.Ports[0].Protocol
-		mcsPort.Port = service.Spec.Ports[0].Port
+		mcsPorts = []mcsv1a1.ServicePort{
+			{
+				Name:     service.Spec.Ports[0].Name,
+				Protocol: service.Spec.Ports[0].Protocol,
+				Port:     service.Spec.Ports[0].Port,
+			},
+		}
 	}
 
 	if siType == mcsv1a1.ClusterSetIP {
@@ -438,7 +443,7 @@ func (a *Controller) getIPsAndPortsForService(service *corev1.Service, siType mc
 			mcsIp = service.Spec.ClusterIP
 		}
 
-		return []string{mcsIp}, []mcsv1a1.ServicePort{mcsPort}, nil
+		return []string{mcsIp}, mcsPorts, nil
 	}
 
 	endpoint, err := a.kubeClientSet.CoreV1().Endpoints(service.Namespace).Get(service.Name, metav1.GetOptions{})
@@ -448,10 +453,10 @@ func (a *Controller) getIPsAndPortsForService(service *corev1.Service, siType mc
 			return nil, nil, errors.WithMessage(err, "Error retrieving the Endpoints for the Service")
 		}
 
-		return make([]string, 0), make([]mcsv1a1.ServicePort, 0), nil
+		return make([]string, 0), nil, nil
 	}
 
-	return getIPsFromEndpoint(endpoint), []mcsv1a1.ServicePort{mcsPort}, nil
+	return getIPsFromEndpoint(endpoint), mcsPorts, nil
 }
 
 func (a *Controller) getObjectNameWithClusterId(name, namespace string) string {


Test patch:
diff --git a/pkg/agent/controller/agent_test.go b/pkg/agent/controller/agent_test.go
index 86cac03..771795f 100644
--- a/pkg/agent/controller/agent_test.go
+++ b/pkg/agent/controller/agent_test.go
@@ -173,6 +173,24 @@ var _ = Describe("ServiceImport syncing", func() {
 			t.awaitNoServiceImport(t.brokerServiceImportClient)
 		})
 	})
+
+	When("a Service has port information", func() {
+		BeforeEach(func() {
+			t.service.Spec.Ports = []corev1.ServicePort{
+				{
+					Name:     "eth0",
+					Protocol: corev1.ProtocolTCP,
+					Port:     123,
+				},
+			}
+		})
+
+		It("should set the appropriate port information in the ServiceImport", func() {
+			t.createService()
+			t.createServiceExport()
+			t.awaitServiceExported(t.service.Spec.ClusterIP, 0)
+		})
+	})
 })
 
 var _ = Describe("Globalnet enabled", func() {
@@ -515,6 +533,14 @@ func awaitServiceImport(client dynamic.ResourceInterface, service *corev1.Servic
 		Expect(serviceImport.Spec.IPs).To(Equal([]string{serviceIP}))
 	}
 
+	Expect(serviceImport.Spec.Ports).To(HaveLen(len(service.Spec.Ports)))
+
+	for i := range service.Spec.Ports {
+		Expect(serviceImport.Spec.Ports[i].Name).To(Equal(service.Spec.Ports[i].Name))
+		Expect(serviceImport.Spec.Ports[i].Protocol).To(Equal(service.Spec.Ports[i].Protocol))
+		Expect(serviceImport.Spec.Ports[i].Port).To(Equal(service.Spec.Ports[i].Port))
+	}
+
 	labels := serviceImport.GetObjectMeta().GetLabels()
 	Expect(labels[lhconstants.LabelSourceNamespace]).To(Equal(service.GetNamespace()))
 	Expect(labels[lhconstants.LabelSourceName]).To(Equal(service.GetName()))