Skip to content

Commit

Permalink
Merge pull request #92646 from nilo19/bug/cherry-pick-92599-1.18
Browse files Browse the repository at this point in the history
Cherry pick of #92599: Delete default load balancer source range (0.0.0.0/0) to prevent redundant network security rules.
  • Loading branch information
k8s-ci-robot committed Jul 9, 2020
2 parents 299f14e + 5bff360 commit 6d83f11
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Expand Up @@ -102,6 +102,8 @@ const (
serviceTagKey = "service"
// clusterNameKey is the cluster name key applied for public IP tags.
clusterNameKey = "kubernetes-cluster-name"

defaultLoadBalancerSourceRanges = "0.0.0.0/0"
)

// GetLoadBalancer returns whether the specified load balancer and its components exist, and
Expand Down Expand Up @@ -1132,6 +1134,7 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
if lbIP != nil {
destinationIPAddress = *lbIP
}

if destinationIPAddress == "" {
destinationIPAddress = "*"
}
Expand All @@ -1141,6 +1144,12 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
return nil, err
}
serviceTags := getServiceTags(service)
if len(serviceTags) != 0 {
if _, ok := sourceRanges[defaultLoadBalancerSourceRanges]; ok {
delete(sourceRanges, defaultLoadBalancerSourceRanges)
}
}

var sourceAddressPrefixes []string
if (sourceRanges == nil || servicehelpers.IsAllowAll(sourceRanges)) && len(serviceTags) == 0 {
if !requiresInternalLoadBalancer(service) {
Expand Down
Expand Up @@ -1860,6 +1860,48 @@ func TestReconcileSecurityGroup(t *testing.T) {
},
},
},
{
desc: "reconcileSecurityGroup shall not create unwanted security rules if there is service tags",
service: getTestService("test1", v1.ProtocolTCP, map[string]string{ServiceAnnotationAllowedServiceTag: "tag"}, true, 80),
wantLb: true,
lbIP: to.StringPtr("1.1.1.1"),
existingSgs: map[string]network.SecurityGroup{"nsg": {
Name: to.StringPtr("nsg"),
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
SecurityRules: &[]network.SecurityRule{
{
Name: to.StringPtr("atest1-toBeDeleted"),
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
SourceAddressPrefix: to.StringPtr("prefix"),
SourcePortRange: to.StringPtr("range"),
DestinationAddressPrefix: to.StringPtr("desPrefix"),
DestinationPortRange: to.StringPtr("desRange"),
},
},
},
},
}},
expectedSg: &network.SecurityGroup{
Name: to.StringPtr("nsg"),
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
SecurityRules: &[]network.SecurityRule{
{
Name: to.StringPtr("atest1-TCP-80-tag"),
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Protocol: network.SecurityRuleProtocol("Tcp"),
SourcePortRange: to.StringPtr("*"),
DestinationPortRange: to.StringPtr("80"),
SourceAddressPrefix: to.StringPtr("tag"),
DestinationAddressPrefix: to.StringPtr("1.1.1.1"),
Access: network.SecurityRuleAccess("Allow"),
Priority: to.Int32Ptr(500),
Direction: network.SecurityRuleDirection("Inbound"),
},
},
},
},
},
},
}

for i, test := range testCases {
Expand Down

0 comments on commit 6d83f11

Please sign in to comment.