Skip to content

Commit 2f9f0e7

Browse files
committed
Allow setting of source CIDR for LB rule
* Annotation added to allow setting of Source CIDR for Load Balancer rule
1 parent a315d9e commit 2f9f0e7

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

cloudstack_loadbalancer.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package cloudstack
2222
import (
2323
"context"
2424
"fmt"
25+
"net"
2526
"strconv"
2627
"strings"
2728

@@ -41,9 +42,9 @@ const (
4142
// service to enable the proxy protocol on a CloudStack load balancer.
4243
// Note that this protocol only applies to TCP service ports and
4344
// CloudStack >= 4.6 is required for it to work.
44-
ServiceAnnotationLoadBalancerProxyProtocol = "service.beta.kubernetes.io/cloudstack-load-balancer-proxy-protocol"
45-
45+
ServiceAnnotationLoadBalancerProxyProtocol = "service.beta.kubernetes.io/cloudstack-load-balancer-proxy-protocol"
4646
ServiceAnnotationLoadBalancerLoadbalancerHostname = "service.beta.kubernetes.io/cloudstack-load-balancer-hostname"
47+
ServiceAnnotationLoadBalancerSourceCidrs = "service.beta.kubernetes.io/cloudstack-load-balancer-source-cidrs"
4748
)
4849

4950
type loadBalancer struct {
@@ -162,7 +163,7 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
162163
}
163164
} else {
164165
klog.V(4).Infof("Creating load balancer rule: %v", lbRuleName)
165-
lbRule, err = lb.createLoadBalancerRule(lbRuleName, port, protocol)
166+
lbRule, err = lb.createLoadBalancerRule(lbRuleName, port, protocol, service)
166167
if err != nil {
167168
return nil, err
168169
}
@@ -596,7 +597,7 @@ func (lb *loadBalancer) updateLoadBalancerRule(lbRuleName string, protocol LoadB
596597
}
597598

598599
// createLoadBalancerRule creates a new load balancer rule and returns it's ID.
599-
func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port corev1.ServicePort, protocol LoadBalancerProtocol) (*cloudstack.LoadBalancerRule, error) {
600+
func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port corev1.ServicePort, protocol LoadBalancerProtocol, service *corev1.Service) (*cloudstack.LoadBalancerRule, error) {
600601
p := lb.LoadBalancer.NewCreateLoadBalancerRuleParams(
601602
lb.algorithm,
602603
lbRuleName,
@@ -606,12 +607,30 @@ func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port corev1.Se
606607

607608
p.SetNetworkid(lb.networkID)
608609
p.SetPublicipid(lb.ipAddrID)
609-
610610
p.SetProtocol(protocol.CSProtocol())
611611

612612
// Do not open the firewall implicitly, we always create explicit firewall rules
613613
p.SetOpenfirewall(false)
614614

615+
// Read the source CIDR annotation
616+
sourceCIDRs, ok := service.Annotations[ServiceAnnotationLoadBalancerSourceCidrs]
617+
var cidrList []string
618+
if ok && sourceCIDRs != "" {
619+
cidrList = strings.Split(sourceCIDRs, ",")
620+
for i, cidr := range cidrList {
621+
cidr = strings.TrimSpace(cidr)
622+
if _, _, err := net.ParseCIDR(cidr); err != nil {
623+
return nil, fmt.Errorf("invalid CIDR in annotation %s: %s", ServiceAnnotationLoadBalancerSourceCidrs, cidr)
624+
}
625+
cidrList[i] = cidr
626+
}
627+
} else {
628+
cidrList = []string{defaultAllowedCIDR}
629+
}
630+
631+
// Set the CIDR list in the parameters
632+
p.SetCidrlist(cidrList)
633+
615634
// Create a new load balancer rule.
616635
r, err := lb.LoadBalancer.CreateLoadBalancerRule(p)
617636
if err != nil {

0 commit comments

Comments
 (0)