Skip to content

Commit

Permalink
fix: service unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Nov 4, 2024
1 parent 09248b8 commit 9c60bae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 74 deletions.
3 changes: 0 additions & 3 deletions pkg/controllers/resources/services/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ func (s *serviceSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}
defer func() {
AlignSpecWithServiceType(event.Virtual)
AlignSpecWithServiceType(event.Host)

if err := patch.Patch(ctx, event.Host, event.Virtual); err != nil {
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/controllers/resources/services/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ func TestSync(t *testing.T) {
},
}
selectorKey := "test"
vServiceNodePortFromExternalBefore := &corev1.Service{
ObjectMeta: vObjectMeta,
Spec: corev1.ServiceSpec{
ExternalName: "test.com",
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
},
},
},
}
vServiceNodePortFromExternal := &corev1.Service{
ObjectMeta: vObjectMeta,
Spec: corev1.ServiceSpec{
Expand Down Expand Up @@ -574,7 +586,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
vObjOld := baseService.DeepCopy()
vObjOld := vServiceNodePortFromExternalBefore.DeepCopy()
vObjNew := vServiceClusterIPFromExternal.DeepCopy()
pObj := pServiceExternal.DeepCopy()

Expand All @@ -596,7 +608,7 @@ func TestSync(t *testing.T) {
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
pObjOld := pServiceExternal.DeepCopy()
pObjNew := pServiceExternal.DeepCopy()
vObjOld := baseService.DeepCopy()
vObjOld := vServiceNodePortFromExternalBefore.DeepCopy()
vObjNew := vServiceNodePortFromExternal.DeepCopy()
_, err := syncer.(*serviceSyncer).Sync(syncCtx, synccontext.NewSyncEventWithOld(pObjOld, pObjNew, vObjOld, vObjNew))
assert.NilError(t, err)
Expand Down
69 changes: 0 additions & 69 deletions pkg/controllers/resources/services/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,72 +50,3 @@ func StripNodePorts(vObj *corev1.Service) {
vObj.Spec.Ports[i].NodePort = 0
}
}

// AlignSpecWithServiceType removes any fields that are invalid for the specific service type
func AlignSpecWithServiceType(svc *corev1.Service) {
if svc == nil || svc.Spec.Type == "" {
return
}

// Default to ClusterIP if type is not specified
if svc.Spec.Type == "" {
svc.Spec.Type = corev1.ServiceTypeClusterIP
}

switch svc.Spec.Type {
case corev1.ServiceTypeClusterIP:
cleanClusterIPFields(svc)
case corev1.ServiceTypeNodePort:
cleanNodePortFields(svc)
case corev1.ServiceTypeLoadBalancer:
cleanLoadBalancerFields(svc)
case corev1.ServiceTypeExternalName:
cleanExternalNameFields(svc)
}
}

func cleanClusterIPFields(svc *corev1.Service) {
// Clear fields not valid for ClusterIP
svc.Spec.ExternalTrafficPolicy = ""
svc.Spec.HealthCheckNodePort = 0
svc.Spec.LoadBalancerIP = ""
svc.Spec.LoadBalancerSourceRanges = nil
svc.Spec.LoadBalancerClass = nil
svc.Spec.ExternalName = ""
svc.Spec.ExternalIPs = nil
svc.Spec.AllocateLoadBalancerNodePorts = nil
}

func cleanNodePortFields(svc *corev1.Service) {
// NodePort can have all ClusterIP fields plus some additional ones
// Clear fields not valid for NodePort
svc.Spec.LoadBalancerIP = ""
svc.Spec.LoadBalancerSourceRanges = nil
svc.Spec.LoadBalancerClass = nil
svc.Spec.ExternalName = ""
}

func cleanLoadBalancerFields(svc *corev1.Service) {
// LoadBalancer can have all NodePort fields plus some additional ones
// Only need to clear ExternalName as it inherits from NodePort
svc.Spec.ExternalName = ""
}

func cleanExternalNameFields(svc *corev1.Service) {
// ExternalName services should only have metadata, type, and externalName
svc.Spec.Ports = nil
svc.Spec.ClusterIP = ""
svc.Spec.ExternalIPs = nil
svc.Spec.LoadBalancerIP = ""
svc.Spec.LoadBalancerSourceRanges = nil
svc.Spec.LoadBalancerClass = nil
svc.Spec.ExternalTrafficPolicy = ""
svc.Spec.HealthCheckNodePort = 0
svc.Spec.PublishNotReadyAddresses = false
svc.Spec.SessionAffinity = ""
svc.Spec.SessionAffinityConfig = nil
svc.Spec.IPFamilies = nil
svc.Spec.IPFamilyPolicy = nil
svc.Spec.AllocateLoadBalancerNodePorts = nil
svc.Spec.InternalTrafficPolicy = nil
}
1 change: 1 addition & 0 deletions pkg/patcher/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package patcher
import (
"encoding/json"
"fmt"

"github.com/loft-sh/vcluster/pkg/util/generics"

jsonpatch "github.com/evanphx/json-patch/v5"
Expand Down

0 comments on commit 9c60bae

Please sign in to comment.