Skip to content

Commit 9fb34ed

Browse files
authored
Merge pull request kubernetes#92599 from nilo19/bug/delete-default-lb-source-range
Delete default load balancer source range (0.0.0.0/0) to prevent redundant network security rules.
2 parents 2e2859a + 40d2dd7 commit 9fb34ed

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ const (
102102
serviceTagKey = "service"
103103
// clusterNameKey is the cluster name key applied for public IP tags.
104104
clusterNameKey = "kubernetes-cluster-name"
105+
106+
defaultLoadBalancerSourceRanges = "0.0.0.0/0"
105107
)
106108

107109
// GetLoadBalancer returns whether the specified load balancer and its components exist, and
@@ -1130,6 +1132,7 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
11301132
if lbIP != nil {
11311133
destinationIPAddress = *lbIP
11321134
}
1135+
11331136
if destinationIPAddress == "" {
11341137
destinationIPAddress = "*"
11351138
}
@@ -1139,6 +1142,12 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
11391142
return nil, err
11401143
}
11411144
serviceTags := getServiceTags(service)
1145+
if len(serviceTags) != 0 {
1146+
if _, ok := sourceRanges[defaultLoadBalancerSourceRanges]; ok {
1147+
delete(sourceRanges, defaultLoadBalancerSourceRanges)
1148+
}
1149+
}
1150+
11421151
var sourceAddressPrefixes []string
11431152
if (sourceRanges == nil || servicehelpers.IsAllowAll(sourceRanges)) && len(serviceTags) == 0 {
11441153
if !requiresInternalLoadBalancer(service) {

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,48 @@ func TestReconcileSecurityGroup(t *testing.T) {
18911891
},
18921892
},
18931893
},
1894+
{
1895+
desc: "reconcileSecurityGroup shall not create unwanted security rules if there is service tags",
1896+
service: getTestService("test1", v1.ProtocolTCP, map[string]string{ServiceAnnotationAllowedServiceTag: "tag"}, true, 80),
1897+
wantLb: true,
1898+
lbIP: to.StringPtr("1.1.1.1"),
1899+
existingSgs: map[string]network.SecurityGroup{"nsg": {
1900+
Name: to.StringPtr("nsg"),
1901+
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
1902+
SecurityRules: &[]network.SecurityRule{
1903+
{
1904+
Name: to.StringPtr("atest1-toBeDeleted"),
1905+
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
1906+
SourceAddressPrefix: to.StringPtr("prefix"),
1907+
SourcePortRange: to.StringPtr("range"),
1908+
DestinationAddressPrefix: to.StringPtr("desPrefix"),
1909+
DestinationPortRange: to.StringPtr("desRange"),
1910+
},
1911+
},
1912+
},
1913+
},
1914+
}},
1915+
expectedSg: &network.SecurityGroup{
1916+
Name: to.StringPtr("nsg"),
1917+
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
1918+
SecurityRules: &[]network.SecurityRule{
1919+
{
1920+
Name: to.StringPtr("atest1-TCP-80-tag"),
1921+
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
1922+
Protocol: network.SecurityRuleProtocol("Tcp"),
1923+
SourcePortRange: to.StringPtr("*"),
1924+
DestinationPortRange: to.StringPtr("80"),
1925+
SourceAddressPrefix: to.StringPtr("tag"),
1926+
DestinationAddressPrefix: to.StringPtr("1.1.1.1"),
1927+
Access: network.SecurityRuleAccess("Allow"),
1928+
Priority: to.Int32Ptr(500),
1929+
Direction: network.SecurityRuleDirection("Inbound"),
1930+
},
1931+
},
1932+
},
1933+
},
1934+
},
1935+
},
18941936
}
18951937

18961938
for i, test := range testCases {

0 commit comments

Comments
 (0)