Skip to content

Commit

Permalink
Merge pull request #18 from snapp-incubator/chore--change-default-loa…
Browse files Browse the repository at this point in the history
…dbalancing

feat: change default load balancing algorithm to roundrobin
  • Loading branch information
m-yosefpor authored Dec 18, 2023
2 parents e50b88d + 251b0cc commit b22938e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
5 changes: 2 additions & 3 deletions internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ var _ = Describe("Testing Route to HTTPProxy Controller", func() {
It("should create HTTPProxy object with custom load balancer algorithm (only tests the algorithm)", func() {
objRoute := getSampleRoute()
objRoute.Annotations = map[string]string{
consts.AnnotBalance: "roundrobin",
consts.AnnotDisableCookies: "true",
consts.AnnotBalance: "cookie",
}
Expect(k8sClient.Create(context.Background(), objRoute)).To(Succeed())

Expand All @@ -358,7 +357,7 @@ var _ = Describe("Testing Route to HTTPProxy Controller", func() {
httpProxyList := contourv1.HTTPProxyList{}
g.Expect(k8sClient.List(context.Background(), &httpProxyList, client.InNamespace(DefaultNamespace))).To(Succeed())
g.Expect(len(httpProxyList.Items)).To(Equal(1))
g.Expect(httpProxyList.Items[0].Spec.Routes[0].LoadBalancerPolicy.Strategy).To(Equal(consts.StrategyRoundRobin))
g.Expect(httpProxyList.Items[0].Spec.Routes[0].LoadBalancerPolicy.Strategy).To(Equal(consts.StrategyCookie))
}).Should(Succeed())

cleanUpRoute(objRoute)
Expand Down
3 changes: 1 addition & 2 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
AnnotBalance = haproxyAnnotationPrefix + "balance"
AnnotTimeout = haproxyAnnotationPrefix + "timeout"
AnnotIPWhitelist = haproxyAnnotationPrefix + "ip_whitelist"
AnnotDisableCookies = haproxyAnnotationPrefix + "disable_cookies"

AnnotationKeyPrefix = "snappcloud.io/"
AnnotationKeyReconciliationPaused = AnnotationKeyPrefix + "paused"
Expand All @@ -37,7 +36,7 @@ const (
StrategyRoundRobin = "RoundRobin"
StrategyWeightedLeastRequest = "WeightedLeastRequest"
StrategyRequestHash = "RequestHash"
StrategyDefault = StrategyWeightedLeastRequest
StrategyDefault = StrategyRoundRobin

TLSSecretGenerateName = "managed-tls-secret-"

Expand Down
37 changes: 17 additions & 20 deletions pkg/utils/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,24 @@ func GetIPWhitelist(route *routev1.Route) []contourv1.IPFilterPolicy {

func GetLoadBalancerPolicy(route *routev1.Route) (*contourv1.LoadBalancerPolicy, error) {
lbPolicy := contourv1.LoadBalancerPolicy{}
disableCookies := route.Annotations[consts.AnnotDisableCookies]
if disableCookies != "true" && disableCookies != "TRUE" {
lbPolicy.Strategy = consts.StrategyRoundRobin
policy, ok := route.Annotations[consts.AnnotBalance]
if !ok {
lbPolicy.Strategy = consts.StrategyDefault
} else {
policy, ok := route.Annotations[consts.AnnotBalance]
if !ok {
lbPolicy.Strategy = consts.StrategyDefault
} else {
switch policy {
case "roundrobin":
lbPolicy.Strategy = consts.StrategyRoundRobin
case "leastconn":
lbPolicy.Strategy = consts.StrategyWeightedLeastRequest
case "source":
lbPolicy.Strategy = consts.StrategyRequestHash
lbPolicy.RequestHashPolicies = []contourv1.RequestHashPolicy{{HashSourceIP: true}}
case "random":
lbPolicy.Strategy = consts.StrategyRandom
default:
return nil, fmt.Errorf("invalid loadbalancer policy specified on route")
}
switch policy {
case "roundrobin":
lbPolicy.Strategy = consts.StrategyRoundRobin
case "leastconn":
lbPolicy.Strategy = consts.StrategyWeightedLeastRequest
case "source":
lbPolicy.Strategy = consts.StrategyRequestHash
lbPolicy.RequestHashPolicies = []contourv1.RequestHashPolicy{{HashSourceIP: true}}
case "random":
lbPolicy.Strategy = consts.StrategyRandom
case "cookie":
lbPolicy.Strategy = consts.StrategyCookie
default:
return nil, fmt.Errorf("invalid loadbalancer policy specified on route")
}
}

Expand Down

0 comments on commit b22938e

Please sign in to comment.