Skip to content

Commit

Permalink
Merge pull request #3798 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…3787-to-release-1.26

[release-1.26] Remove shared nsg rule immediately when no destinations prefixes are left
  • Loading branch information
k8s-ci-robot committed Apr 23, 2023
2 parents 05e0138 + 5d04a16 commit d4f564c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
45 changes: 26 additions & 19 deletions pkg/provider/azure_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2638,28 +2638,35 @@ func (az *Cloud) reconcileSecurityRules(sg network.SecurityGroup, service *v1.Se
klog.V(4).Infof("Didn't find shared rule %s for service %s", sharedRuleName, service.Name)
continue
}
if sharedRule.DestinationAddressPrefixes == nil {
klog.V(4).Infof("Didn't find DestinationAddressPrefixes in shared rule for service %s", service.Name)
updatedRules = append(updatedRules[:sharedIndex], updatedRules[sharedIndex+1:]...)
continue
}
existingPrefixes := *sharedRule.DestinationAddressPrefixes
for _, destinationIPAddress := range destinationIPAddresses {
addressIndex, found := findIndex(existingPrefixes, destinationIPAddress)
if !found {
klog.Warningf("Didn't find destination address %v in shared rule %s for service %s", destinationIPAddress, sharedRuleName, service.Name)
continue
}
if len(existingPrefixes) == 1 {
updatedRules = append(updatedRules[:sharedIndex], updatedRules[sharedIndex+1:]...)
} else {
newDestinations := append(existingPrefixes[:addressIndex], existingPrefixes[addressIndex+1:]...)
sharedRule.DestinationAddressPrefixes = &newDestinations
updatedRules[sharedIndex] = sharedRule
shouldDeleteNSGRule := false
if sharedRule.DestinationAddressPrefixes == nil || len(*sharedRule.DestinationAddressPrefixes) == 0 {
shouldDeleteNSGRule = true
} else {
existingPrefixes := *sharedRule.DestinationAddressPrefixes
for _, destinationIPAddress := range destinationIPAddresses {
addressIndex, found := findIndex(existingPrefixes, destinationIPAddress)
if !found {
klog.Warningf("Didn't find destination address %v in shared rule %s for service %s", destinationIPAddress, sharedRuleName, service.Name)
continue
}
if len(existingPrefixes) == 1 {
shouldDeleteNSGRule = true
break //shared nsg rule has only one entry and entry owned by deleted svc has been found. skip the rest of the entries
} else {
newDestinations := append(existingPrefixes[:addressIndex], existingPrefixes[addressIndex+1:]...)
sharedRule.DestinationAddressPrefixes = &newDestinations
updatedRules[sharedIndex] = sharedRule
}
dirtySg = true
}
dirtySg = true
}

if shouldDeleteNSGRule {
klog.V(4).Infof("shared rule will be deleted because last service %s which refers this rule is deleted.", service.Name)
updatedRules = append(updatedRules[:sharedIndex], updatedRules[sharedIndex+1:]...)
dirtySg = true
continue
}
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/provider/azure_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,52 @@ func TestReconcileSecurityGroup(t *testing.T) {
},
},
},
{
desc: "reconcileSecurityGroup shall delete shared sgs destination for service with azure-shared-securityrule annotations",
service: getTestService("test1", v1.ProtocolTCP, map[string]string{consts.ServiceAnnotationSharedSecurityRule: "true"}, true, 80),
existingSgs: map[string]network.SecurityGroup{"nsg": {
Name: pointer.String("nsg"),
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
SecurityRules: &[]network.SecurityRule{
{
Name: pointer.String("shared-TCP-80-Internet"),
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Protocol: network.SecurityRuleProtocol("Tcp"),
SourcePortRange: pointer.String("*"),
DestinationPortRange: pointer.String("80"),
SourceAddressPrefix: pointer.String("Internet"),
DestinationAddressPrefixes: &([]string{"1.2.3.4", "5.6.7.8"}),
Access: network.SecurityRuleAccess("Allow"),
Priority: pointer.Int32(500),
Direction: network.SecurityRuleDirection("Inbound"),
},
},
},
},
}},
lbIP: pointer.String("1.2.3.4"),
wantLb: false,
expectedSg: &network.SecurityGroup{
Name: pointer.String("nsg"),
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
SecurityRules: &[]network.SecurityRule{
{
Name: pointer.String("shared-TCP-80-Internet"),
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Protocol: network.SecurityRuleProtocol("Tcp"),
SourcePortRange: pointer.String("*"),
DestinationPortRange: pointer.String("80"),
SourceAddressPrefix: pointer.String("Internet"),
DestinationAddressPrefixes: &([]string{"5.6.7.8"}),
Access: network.SecurityRuleAccess("Allow"),
Priority: pointer.Int32(500),
Direction: network.SecurityRuleDirection("Inbound"),
},
},
},
},
},
},
{
desc: "reconcileSecurityGroup shall create sgs with floating IP disabled",
service: getTestService("test1", v1.ProtocolTCP, map[string]string{consts.ServiceAnnotationDisableLoadBalancerFloatingIP: "true"}, false, 80),
Expand Down

0 comments on commit d4f564c

Please sign in to comment.