@@ -858,7 +858,7 @@ func (lb *loadBalancer) updateNetworkACL(publicPort int, protocol LoadBalancerPr
858
858
return false , fmt .Errorf ("error fetching Network with ID: %v, due to: %s" , networkId , err )
859
859
}
860
860
861
- networkAcl , count , err := lb .NetworkACL .GetNetworkACLListByID (network .Aclid )
861
+ networkAclList , count , err := lb .NetworkACL .GetNetworkACLListByID (network .Aclid )
862
862
if err != nil {
863
863
return false , fmt .Errorf ("error fetching Network ACL List with ID: %v, due to: %s" , network .Aclid , err )
864
864
}
@@ -867,11 +867,35 @@ func (lb *loadBalancer) updateNetworkACL(publicPort int, protocol LoadBalancerPr
867
867
return false , fmt .Errorf ("failed to find network ACL List with id: %v" , network .Aclid )
868
868
}
869
869
870
- if networkAcl .Name == "default_allow" || networkAcl .Name == "default_deny" {
870
+ if networkAclList .Name == "default_allow" || networkAclList .Name == "default_deny" {
871
871
klog .Infof ("Network is using a default network ACL. Cannot add ACL rules to default ACLs" )
872
872
return true , err
873
873
}
874
874
875
+ networkAclParams := lb .NetworkACL .NewListNetworkACLsParams ()
876
+ networkAclParams .SetAclid (network .Aclid )
877
+ networkAclParams .SetNetworkid (networkId )
878
+
879
+ networkAclResponse , err := lb .NetworkACL .ListNetworkACLs (networkAclParams )
880
+
881
+ if err != nil {
882
+ return false , fmt .Errorf ("error fetching Network ACL with ID: %v for network with id: %v, due to: %s" , network .Aclid , networkId , err )
883
+ }
884
+
885
+ // find all network ACL rules that have a matching proto+port
886
+ // a map may or may not be faster, but is a bit easier to understand
887
+ filtered := make (map [* cloudstack.NetworkACL ]bool )
888
+ for _ , netAclRule := range networkAclResponse .NetworkACLs {
889
+ if netAclRule .Protocol == protocol .IPProtocol () && netAclRule .Startport == strconv .Itoa (publicPort ) && netAclRule .Endport == strconv .Itoa (publicPort ) {
890
+ filtered [netAclRule ] = true
891
+ }
892
+ }
893
+
894
+ if len (filtered ) > 0 {
895
+ klog .Infof ("Network ACL rule for port %v and protocol %v already exists. No need to added a duplicate rule" )
896
+ return true , err
897
+ }
898
+
875
899
// create ACL rule
876
900
acl := lb .NetworkACL .NewCreateNetworkACLParams (protocol .CSProtocol ())
877
901
acl .SetAclid (network .Aclid )
0 commit comments