From e66b6399a31a2821031dc89e8203c868ad1d3000 Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Thu, 17 Feb 2022 15:54:43 +0100 Subject: [PATCH] refactor: spec.loadBalancerSourceRanges from vcluster --- .../resources/services/syncer_test.go | 35 ++++++++++--------- .../resources/services/translate.go | 11 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pkg/controllers/resources/services/syncer_test.go b/pkg/controllers/resources/services/syncer_test.go index cfcef7f96..4f4f4eced 100644 --- a/pkg/controllers/resources/services/syncer_test.go +++ b/pkg/controllers/resources/services/syncer_test.go @@ -64,6 +64,7 @@ func TestSync(t *testing.T) { ExternalName: "external", ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyTypeLocal, SessionAffinity: corev1.ServiceAffinityClientIP, + LoadBalancerSourceRanges: []string{"backwardRange"}, SessionAffinityConfig: &corev1.SessionAffinityConfig{ ClientIP: &corev1.ClientIPConfig{}, }, @@ -94,17 +95,15 @@ func TestSync(t *testing.T) { Spec: updateForwardSpec, } updateBackwardSpec := corev1.ServiceSpec{ - ExternalName: "backwardExternal", - ExternalIPs: []string{"123:221:123:221"}, - LoadBalancerIP: "123:213:123:213", - LoadBalancerSourceRanges: []string{"backwardRange"}, + ExternalName: "backwardExternal", + ExternalIPs: []string{"123:221:123:221"}, + LoadBalancerIP: "123:213:123:213", } updateBackwardRecreateSpec := corev1.ServiceSpec{ - ClusterIP: "123:123:123:123", - ExternalName: updateBackwardSpec.ExternalName, - ExternalIPs: updateBackwardSpec.ExternalIPs, - LoadBalancerIP: updateBackwardSpec.LoadBalancerIP, - LoadBalancerSourceRanges: updateBackwardSpec.LoadBalancerSourceRanges, + ClusterIP: "123:123:123:123", + ExternalName: updateBackwardSpec.ExternalName, + ExternalIPs: updateBackwardSpec.ExternalIPs, + LoadBalancerIP: updateBackwardSpec.LoadBalancerIP, } updateBackwardSpecService := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -113,7 +112,11 @@ func TestSync(t *testing.T) { Labels: pObjectMeta.Labels, Annotations: pObjectMeta.Annotations, }, - Spec: updateBackwardSpec, + Spec: corev1.ServiceSpec{ + ExternalName: "backwardExternal", + ExternalIPs: []string{"123:221:123:221"}, + LoadBalancerIP: "123:213:123:213", + }, } updatedBackwardSpecService := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -121,9 +124,8 @@ func TestSync(t *testing.T) { Namespace: vObjectMeta.Namespace, }, Spec: corev1.ServiceSpec{ - ExternalIPs: []string{"123:221:123:221"}, - LoadBalancerIP: "123:213:123:213", - LoadBalancerSourceRanges: []string{"backwardRange"}, + ExternalIPs: []string{"123:221:123:221"}, + LoadBalancerIP: "123:213:123:213", }, } updateBackwardSpecRecreateService := &corev1.Service{ @@ -142,10 +144,9 @@ func TestSync(t *testing.T) { ClusterName: vObjectMeta.ClusterName, }, Spec: corev1.ServiceSpec{ - ClusterIP: "123:123:123:123", - ExternalIPs: []string{"123:221:123:221"}, - LoadBalancerIP: "123:213:123:213", - LoadBalancerSourceRanges: []string{"backwardRange"}, + ClusterIP: "123:123:123:123", + ExternalIPs: []string{"123:221:123:221"}, + LoadBalancerIP: "123:213:123:213", }, } updateBackwardStatus := corev1.ServiceStatus{ diff --git a/pkg/controllers/resources/services/translate.go b/pkg/controllers/resources/services/translate.go index 140cbe7ac..4974d2190 100644 --- a/pkg/controllers/resources/services/translate.go +++ b/pkg/controllers/resources/services/translate.go @@ -50,11 +50,6 @@ func (s *serviceSyncer) translateUpdateBackwards(pObj, vObj *corev1.Service) *co updated.Spec.LoadBalancerIP = pObj.Spec.LoadBalancerIP } - if !equality.Semantic.DeepEqual(vObj.Spec.LoadBalancerSourceRanges, pObj.Spec.LoadBalancerSourceRanges) { - updated = newIfNil(updated, vObj) - updated.Spec.LoadBalancerSourceRanges = pObj.Spec.LoadBalancerSourceRanges - } - // check if we need to sync node ports from host to virtual if portsEqual(pObj, vObj) && !equality.Semantic.DeepEqual(vObj.Spec.Ports, pObj.Spec.Ports) { updated = newIfNil(updated, vObj) @@ -124,6 +119,12 @@ func (s *serviceSyncer) translateUpdate(pObj, vObj *corev1.Service) *corev1.Serv updated.Spec.SessionAffinityConfig = vObj.Spec.SessionAffinityConfig } + // load balancer source ranges + if !equality.Semantic.DeepEqual(vObj.Spec.LoadBalancerSourceRanges, pObj.Spec.LoadBalancerSourceRanges) { + updated = newIfNil(updated, pObj) + updated.Spec.LoadBalancerSourceRanges = vObj.Spec.LoadBalancerSourceRanges + } + // healthCheckNodePort if vObj.Spec.HealthCheckNodePort != pObj.Spec.HealthCheckNodePort { updated = newIfNil(updated, pObj)