Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: remove shared security rule when no svc with shared-nsg annotation exists #3391

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/provider/azure_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,7 @@ func (az *Cloud) reconcileSecurityRules(sg network.SecurityGroup, service *v1.Se
}
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:]...)
MartinForReal marked this conversation as resolved.
Show resolved Hide resolved
continue
}
existingPrefixes := *sharedRule.DestinationAddressPrefixes
Expand Down Expand Up @@ -2664,10 +2665,14 @@ func (az *Cloud) reconcileSecurityRules(sg network.SecurityGroup, service *v1.Se
}
if foundRule && allowsConsolidation(expectedRule) {
index, _ := findConsolidationCandidate(updatedRules, expectedRule)
updatedRules[index] = consolidate(updatedRules[index], expectedRule)
if updatedRules[index].DestinationAddressPrefixes != nil {
updatedRules[index] = consolidate(updatedRules[index], expectedRule)
} else {
updatedRules = append(updatedRules[:index], updatedRules[index+1:]...)
feiskyer marked this conversation as resolved.
Show resolved Hide resolved
}
dirtySg = true
}
if !foundRule {
if !foundRule && wantLb {
klog.V(10).Infof("reconcile(%s)(%t): sg rule(%s) - adding", serviceName, wantLb, *expectedRule.Name)

nextAvailablePriority, err := getNextAvailablePriority(updatedRules)
Expand Down
32 changes: 32 additions & 0 deletions pkg/provider/azure_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3637,6 +3637,38 @@ func TestReconcileSecurityGroup(t *testing.T) {
},
},
},
{
desc: "reconcileSecurityGroup shall delete shared sgs 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"}),
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{},
},
},
},
{
desc: "reconcileSecurityGroup shall create sgs with floating IP disabled",
service: getTestService("test1", v1.ProtocolTCP, map[string]string{consts.ServiceAnnotationDisableLoadBalancerFloatingIP: "true"}, false, 80),
Expand Down
26 changes: 14 additions & 12 deletions tests/e2e/network/network_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var _ = Describe("Network security group", Label(utils.TestSuiteLabelNSG), func(
Expect(err).To(BeNil(), "Fail to automatically delete the rule")
})

It("should support service annotation `service.beta.kubernetes.io/azure-shared-securityrule`", func() {
It("should support service annotation `service.beta.kubernetes.io/azure-shared-securityrule`", func(ctx SpecContext) {
By("Exposing two services with shared security rule")
annotation := map[string]string{
consts.ServiceAnnotationSharedSecurityRule: "true",
Expand Down Expand Up @@ -169,21 +169,23 @@ var _ = Describe("Network security group", Label(utils.TestSuiteLabelNSG), func(
By("Validate automatically adjust or delete the rule, when service is deleted")
Expect(utils.DeleteService(cs, ns.Name, serviceName)).NotTo(HaveOccurred())
ipList = ips2
Expect(validateSharedSecurityRuleExists(nsgs, ipList, port)).To(BeTrue(), "Security rule should be modified to only contain service %s", serviceName2)

Eventually(func() (bool, error) {
nsgs, err = tc.GetClusterSecurityGroups()
if err != nil {
return false, err
}
return validateSharedSecurityRuleExists(nsgs, ips2, port) && !validateSharedSecurityRuleExists(nsgs, ips1, port), nil
}).WithContext(ctx).Should(BeTrue(), "Security rule should be modified to only contain service %s", serviceName2)

Expect(utils.DeleteService(cs, ns.Name, serviceName2)).NotTo(HaveOccurred())
isDeleted := false
for i := 1; i <= 30; i++ {
Eventually(func() (bool, error) {
nsgs, err := tc.GetClusterSecurityGroups()
Expect(err).NotTo(HaveOccurred())
if !validateSharedSecurityRuleExists(nsgs, ipList, port) {
utils.Logf("Target rule successfully deleted")
isDeleted = true
break
if err != nil {
return false, err
}
time.Sleep(20 * time.Second)
}
Expect(isDeleted).To(BeTrue(), "Fail to automatically delete the shared security rule")
return validateSharedSecurityRuleExists(nsgs, ipList, port), nil
}).WithContext(ctx).Should(BeFalse(), "Fail to automatically delete the shared security rule")
})

It("can set source IP prefixes automatically according to corresponding service tag", func() {
Expand Down