@@ -22,6 +22,7 @@ package cloudstack
22
22
import (
23
23
"context"
24
24
"fmt"
25
+ "net"
25
26
"strconv"
26
27
"strings"
27
28
@@ -41,9 +42,9 @@ const (
41
42
// service to enable the proxy protocol on a CloudStack load balancer.
42
43
// Note that this protocol only applies to TCP service ports and
43
44
// 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"
46
46
ServiceAnnotationLoadBalancerLoadbalancerHostname = "service.beta.kubernetes.io/cloudstack-load-balancer-hostname"
47
+ ServiceAnnotationLoadBalancerSourceCidrs = "service.beta.kubernetes.io/cloudstack-load-balancer-source-cidrs"
47
48
)
48
49
49
50
type loadBalancer struct {
@@ -162,7 +163,7 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
162
163
}
163
164
} else {
164
165
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 )
166
167
if err != nil {
167
168
return nil , err
168
169
}
@@ -596,7 +597,7 @@ func (lb *loadBalancer) updateLoadBalancerRule(lbRuleName string, protocol LoadB
596
597
}
597
598
598
599
// 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 ) {
600
601
p := lb .LoadBalancer .NewCreateLoadBalancerRuleParams (
601
602
lb .algorithm ,
602
603
lbRuleName ,
@@ -606,12 +607,30 @@ func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port corev1.Se
606
607
607
608
p .SetNetworkid (lb .networkID )
608
609
p .SetPublicipid (lb .ipAddrID )
609
-
610
610
p .SetProtocol (protocol .CSProtocol ())
611
611
612
612
// Do not open the firewall implicitly, we always create explicit firewall rules
613
613
p .SetOpenfirewall (false )
614
614
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
+
615
634
// Create a new load balancer rule.
616
635
r , err := lb .LoadBalancer .CreateLoadBalancerRule (p )
617
636
if err != nil {
0 commit comments