Skip to content

Commit

Permalink
Add UTs for session affinity config's
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Seetharaman <suryaseetharaman.9@gmail.com>
(cherry picked from commit de0cd0e)
  • Loading branch information
tssurya committed Feb 1, 2024
1 parent 1759d33 commit 86a0457
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
143 changes: 142 additions & 1 deletion go-controller/pkg/ovn/controller/services/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"testing"

"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/nbdb"
libovsdbtest "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/testing/libovsdb"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/types"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilpointer "k8s.io/utils/pointer"
)

func TestEnsureLBs(t *testing.T) {
func TestEnsureStaleLBs(t *testing.T) {
nbClient, cleanup, err := libovsdbtest.NewNBTestHarness(libovsdbtest.TestSetup{}, nil)
if err != nil {
t.Fatalf("Error creating NB: %v", err)
Expand Down Expand Up @@ -88,3 +90,142 @@ func TestEnsureLBs(t *testing.T) {
t.Fatalf("EnsureLBs did not set UUID of cached LB as is should")
}
}

func TestEnsureLBs(t *testing.T) {
tests := []struct {
desc string
service *v1.Service
LBs []LB
finalLB *nbdb.LoadBalancer
}{
{
desc: "create service with permanent session affinity",
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "testns"},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeClusterIP,
SessionAffinity: v1.ServiceAffinityClientIP,
SessionAffinityConfig: &v1.SessionAffinityConfig{
ClientIP: &v1.ClientIPConfig{
TimeoutSeconds: utilpointer.Int32(86400),
},
},
},
},
LBs: []LB{
{
Name: "Service_foo/testns_TCP_cluster",
ExternalIDs: map[string]string{
types.LoadBalancerKindExternalID: "Service",
types.LoadBalancerOwnerExternalID: fmt.Sprintf("%s/%s", "foo", "testns"),
},
Routers: []string{"gr-node-a"},
Protocol: "TCP",
Rules: []LBRule{
{
Source: Addr{IP: "192.168.1.1", Port: 80},
Targets: []Addr{{IP: "10.0.244.3", Port: 8080}},
},
},
UUID: "test-UUID",
Opts: LBOpts{
Reject: true,
AffinityTimeOut: 86400,
},
},
},
finalLB: &nbdb.LoadBalancer{
UUID: loadBalancerClusterWideTCPServiceName("foo", "testns"),
Name: loadBalancerClusterWideTCPServiceName("foo", "testns"),
Options: servicesOptions(),
Protocol: &nbdb.LoadBalancerProtocolTCP,
Vips: map[string]string{
"192.168.1.1:80": "10.0.244.3:8080",
},
ExternalIDs: serviceExternalIDs(namespacedServiceName("foo", "testns")),
SelectionFields: []string{"ip_src", "ip_dst"}, // permanent session affinity, no learn flows
},
},
{
desc: "create service with default session affinity timeout",
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "testns"},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeClusterIP,
SessionAffinity: v1.ServiceAffinityClientIP,
SessionAffinityConfig: &v1.SessionAffinityConfig{
ClientIP: &v1.ClientIPConfig{
TimeoutSeconds: utilpointer.Int32(10800),
},
},
},
},
LBs: []LB{
{
Name: "Service_foo/testns_TCP_cluster",
ExternalIDs: map[string]string{
types.LoadBalancerKindExternalID: "Service",
types.LoadBalancerOwnerExternalID: fmt.Sprintf("%s/%s", "foo", "testns"),
},
Routers: []string{"gr-node-a"},
Protocol: "TCP",
Rules: []LBRule{
{
Source: Addr{IP: "192.168.1.1", Port: 80},
Targets: []Addr{{IP: "10.0.244.3", Port: 8080}},
},
},
UUID: "test-UUID",
Opts: LBOpts{
Reject: true,
AffinityTimeOut: 10800,
},
},
},
finalLB: &nbdb.LoadBalancer{
UUID: loadBalancerClusterWideTCPServiceName("foo", "testns"),
Name: loadBalancerClusterWideTCPServiceName("foo", "testns"),
Options: servicesOptionsWithAffinityTimeout(), // timeout set in the options
Protocol: &nbdb.LoadBalancerProtocolTCP,
Vips: map[string]string{
"192.168.1.1:80": "10.0.244.3:8080",
},
ExternalIDs: serviceExternalIDs(namespacedServiceName("foo", "testns")),
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
nbClient, cleanup, err := libovsdbtest.NewNBTestHarness(libovsdbtest.TestSetup{
NBData: []libovsdbtest.TestData{
&nbdb.LogicalRouter{
Name: "gr-node-a",
},
},
}, nil)
if err != nil {
t.Fatalf("test: \"%s\" failed to set up test harness: %v", tt.desc, err)
}
t.Cleanup(cleanup.Cleanup)

err = EnsureLBs(nbClient, tt.service, []LB{}, tt.LBs)
if err != nil {
t.Fatalf("Error EnsureLBs: %v", err)
}
matcher := libovsdbtest.HaveDataIgnoringUUIDs([]libovsdbtest.TestData{
tt.finalLB,
&nbdb.LogicalRouter{
Name: "gr-node-a",
LoadBalancer: []string{loadBalancerClusterWideTCPServiceName("foo", "testns")},
},
})
success, err := matcher.Match(nbClient)
if !success {
t.Fatal(fmt.Errorf("test: \"%s\" didn't match expected with actual, err: %v", tt.desc, matcher.FailureMessage(nbClient)))
}
if err != nil {
t.Fatal(fmt.Errorf("test: \"%s\" encountered error: %v", tt.desc, err))
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,12 @@ func servicesOptions() map[string]string {
}
}

func servicesOptionsWithAffinityTimeout() map[string]string {
options := servicesOptions()
options["affinity_timeout"] = "10800"
return options
}

func templateServicesOptions() map[string]string {
// Template LBs need "options:template=true" and "options:address-family" set.
opts := servicesOptions()
Expand Down

0 comments on commit 86a0457

Please sign in to comment.