Skip to content

Commit b461771

Browse files
authored
feat(occm): load balancing algorithm via annotation (#2684)
1 parent 88f6478 commit b461771

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

docs/openstack-cloud-controller-manager/expose-applications-using-loadbalancer-type-service.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ Request Body:
152152

153153
Not supported when `lb-provider=ovn` is configured in openstack-cloud-controller-manager.
154154

155+
- `loadbalancer.openstack.org/lb-method`
156+
157+
Load balancing algorithm to use when distributed to members. [OpenStack Pool Creation | lb_algorithm](https://docs.openstack.org/api-ref/load-balancer/v2/#create-pool)
158+
159+
Default value: defined in your OCCM configuration
160+
161+
Possible values: `ROUND_ROBIN`, `LEAST_CONNECTIONS`, `SOURCE_IP`, `SOURCE_IP_PORT`
162+
155163
- `loadbalancer.openstack.org/timeout-client-data`
156164

157165
Frontend client inactivity timeout in milliseconds for the load balancer.

pkg/openstack/events.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ const (
2323
eventLBAZIgnored = "LoadBalancerAvailabilityZonesIgnored"
2424
eventLBFloatingIPSkipped = "LoadBalancerFloatingIPSkipped"
2525
eventLBRename = "LoadBalancerRename"
26+
eventLBLbMethodUnknown = "LoadBalancerLbMethodUnknown"
2627
)

pkg/openstack/loadbalancer.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
ServiceAnnotationLoadBalancerClass = "loadbalancer.openstack.org/class"
6969
ServiceAnnotationLoadBalancerKeepFloatingIP = "loadbalancer.openstack.org/keep-floatingip"
7070
ServiceAnnotationLoadBalancerPortID = "loadbalancer.openstack.org/port-id"
71+
ServiceAnnotationLoadBalancerLbMethod = "loadbalancer.openstack.org/lb-method"
7172
ServiceAnnotationLoadBalancerProxyEnabled = "loadbalancer.openstack.org/proxy-protocol"
7273
ServiceAnnotationLoadBalancerSubnetID = "loadbalancer.openstack.org/subnet-id"
7374
ServiceAnnotationLoadBalancerNetworkID = "loadbalancer.openstack.org/network-id"
@@ -125,6 +126,7 @@ type serviceConfig struct {
125126
lbPublicSubnetSpec *floatingSubnetSpec
126127
nodeSelectors map[string]string
127128
keepClientIP bool
129+
poolLbMethod string
128130
proxyProtocolVersion *v2pools.Protocol
129131
timeoutClientData int
130132
timeoutMemberConnect int
@@ -900,6 +902,27 @@ func (lbaas *LbaasV2) ensureOctaviaPool(lbID string, name string, listener *list
900902
pool = nil
901903
}
902904

905+
// If LBMethod changes, update the Pool with the new value
906+
var poolLbMethod string
907+
if svcConf.poolLbMethod != "" {
908+
poolLbMethod = svcConf.poolLbMethod
909+
} else {
910+
// if LBMethod is not defined, fallback on default OCCM's default method
911+
poolLbMethod = lbaas.opts.LBMethod
912+
}
913+
if pool != nil && pool.LBMethod != poolLbMethod {
914+
klog.InfoS("Updating LoadBalancer LBMethod", "poolID", pool.ID, "listenerID", listener.ID, "lbID", lbID)
915+
err = openstackutil.UpdatePool(lbaas.lb, lbID, pool.ID, v2pools.UpdateOpts{LBMethod: v2pools.LBMethod(poolLbMethod)})
916+
if err != nil {
917+
err = PreserveGopherError(err)
918+
msg := fmt.Sprintf("Error updating LB method for LoadBalancer: %v", err)
919+
klog.Errorf(msg, "poolID", pool.ID, "listenerID", listener.ID, "lbID", lbID)
920+
lbaas.eventRecorder.Eventf(service, corev1.EventTypeWarning, eventLBLbMethodUnknown, msg)
921+
} else {
922+
pool.LBMethod = poolLbMethod
923+
}
924+
}
925+
903926
if pool == nil {
904927
createOpt := lbaas.buildPoolCreateOpt(listener.Protocol, service, svcConf, name)
905928
createOpt.ListenerID = listener.ID
@@ -972,11 +995,18 @@ func (lbaas *LbaasV2) buildPoolCreateOpt(listenerProtocol string, service *corev
972995
persistence = &v2pools.SessionPersistence{Type: "SOURCE_IP"}
973996
}
974997

975-
lbmethod := v2pools.LBMethod(lbaas.opts.LBMethod)
998+
var lbMethod v2pools.LBMethod
999+
if svcConf.poolLbMethod != "" {
1000+
lbMethod = v2pools.LBMethod(svcConf.poolLbMethod)
1001+
} else {
1002+
// if LBMethod is not defined, fallback on default OCCM's default method
1003+
lbMethod = v2pools.LBMethod(lbaas.opts.LBMethod)
1004+
}
1005+
9761006
return v2pools.CreateOpts{
9771007
Name: name,
9781008
Protocol: poolProto,
979-
LBMethod: lbmethod,
1009+
LBMethod: lbMethod,
9801010
Persistence: persistence,
9811011
}
9821012
}
@@ -1509,6 +1539,7 @@ func (lbaas *LbaasV2) checkService(ctx context.Context, service *corev1.Service,
15091539
func (lbaas *LbaasV2) makeSvcConf(serviceName string, service *corev1.Service, svcConf *serviceConfig) error {
15101540
svcConf.connLimit = getIntFromServiceAnnotation(service, ServiceAnnotationLoadBalancerConnLimit, -1)
15111541
svcConf.lbID = getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerID, "")
1542+
svcConf.poolLbMethod = getStringFromServiceAnnotation(service, ServiceAnnotationLoadBalancerLbMethod, "")
15121543
svcConf.supportLBTags = openstackutil.IsOctaviaFeatureSupported(lbaas.lb, openstackutil.OctaviaFeatureTags, lbaas.opts.LBProvider)
15131544

15141545
// Get service node-selector annotations

pkg/openstack/loadbalancer_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,29 @@ func Test_buildPoolCreateOpt(t *testing.T) {
11551155
Persistence: &pools.SessionPersistence{Type: "SOURCE_IP"},
11561156
},
11571157
},
1158+
{
1159+
name: "test for loadbalancing method",
1160+
args: args{
1161+
protocol: "TCP",
1162+
svcConf: &serviceConfig{
1163+
poolLbMethod: "ROUND_ROBIN",
1164+
},
1165+
lbaasV2: &LbaasV2{
1166+
LoadBalancer{
1167+
opts: LoadBalancerOpts{
1168+
LBProvider: "ovn",
1169+
LBMethod: "SOURCE_IP_PORT",
1170+
},
1171+
},
1172+
},
1173+
service: &corev1.Service{},
1174+
},
1175+
want: pools.CreateOpts{
1176+
Name: "test for loadbalancing method",
1177+
Protocol: pools.ProtocolTCP,
1178+
LBMethod: "ROUND_ROBIN",
1179+
},
1180+
},
11581181
}
11591182

11601183
for _, tt := range tests {

0 commit comments

Comments
 (0)