Skip to content

Commit 87565e3

Browse files
authored
Merge pull request #18185 from hakman/azure-delete-cluster
azure: Fix deletion ordering issues
2 parents 83f07f6 + 8423f26 commit 87565e3

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

pkg/resources/azure/azure.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,16 @@ func (g *resourceGetter) toSubnetResource(subnet *network.Subnet, vnetID string)
231231
blocks = append(blocks, toKey(typeVirtualNetwork, vnetID))
232232
blocks = append(blocks, toKey(typeResourceGroup, g.resourceGroupID()))
233233

234-
if subnet.Properties != nil && subnet.Properties.NatGateway != nil && subnet.Properties.NatGateway.ID != nil {
235-
blocks = append(blocks, toKey(typeNatGateway, *subnet.Properties.NatGateway.ID))
234+
if subnet.Properties != nil {
235+
if subnet.Properties.NatGateway != nil && subnet.Properties.NatGateway.ID != nil {
236+
blocks = append(blocks, toKey(typeNatGateway, *subnet.Properties.NatGateway.ID))
237+
}
238+
if subnet.Properties.RouteTable != nil && subnet.Properties.RouteTable.ID != nil {
239+
blocks = append(blocks, toKey(typeRouteTable, *subnet.Properties.RouteTable.ID))
240+
}
241+
if subnet.Properties.NetworkSecurityGroup != nil && subnet.Properties.NetworkSecurityGroup.ID != nil {
242+
blocks = append(blocks, toKey(typeNetworkSecurityGroup, *subnet.Properties.NetworkSecurityGroup.ID))
243+
}
236244
}
237245

238246
vnet, err := arm.ParseResourceID(vnetID)

pkg/resources/azure/errors.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2026 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package azure
18+
19+
import (
20+
"errors"
21+
22+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
23+
)
24+
25+
// IsDependencyViolation checks if the error is a transient dependency/conflict
26+
// error that should be retried quietly during resource deletion.
27+
func IsDependencyViolation(err error) bool {
28+
var azErr *azcore.ResponseError
29+
if !errors.As(err, &azErr) {
30+
return false
31+
}
32+
switch azErr.ErrorCode {
33+
// Conflict is returned when deleting multiple VMSS VMs concurrently;
34+
// Azure only allows one mutating operation per VMSS at a time.
35+
case "Conflict":
36+
return true
37+
case "InUseRouteTableCannotBeDeleted", "InUseNetworkSecurityGroupCannotBeDeleted",
38+
"InUseSubnetCannotBeDeleted", "NatGatewayInUseBySubnet":
39+
return true
40+
default:
41+
return false
42+
}
43+
}

pkg/resources/ops/delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/klog/v2"
2525
"k8s.io/kops/pkg/resources"
2626
awsresources "k8s.io/kops/pkg/resources/aws"
27+
azureresources "k8s.io/kops/pkg/resources/azure"
2728
gceresources "k8s.io/kops/pkg/resources/gce"
2829
"k8s.io/kops/upup/pkg/fi"
2930
)
@@ -129,7 +130,7 @@ func DeleteResources(cloud fi.Cloud, resourceMap map[string]*resources.Resource,
129130
}
130131
if err != nil {
131132
mutex.Lock()
132-
if awsresources.IsDependencyViolation(err) || gceresources.IsDependencyViolation(err) {
133+
if awsresources.IsDependencyViolation(err) || gceresources.IsDependencyViolation(err) || azureresources.IsDependencyViolation(err) {
133134
fmt.Printf("%s\tstill has dependencies, will retry\n", human)
134135
klog.V(4).Infof("resource %q generated a dependency error: %v", human, err)
135136
} else {

0 commit comments

Comments
 (0)