Skip to content

Commit

Permalink
Merge pull request #1598 from spectrocloud/fix-managedclusters-custom…
Browse files Browse the repository at this point in the history
…-vnet-delete

fix managed clusters custom VNet delete issue
  • Loading branch information
k8s-ci-robot authored Aug 13, 2021
2 parents b9f62a3 + d411a1d commit 4f0e0fc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
10 changes: 8 additions & 2 deletions azure/services/virtualnetworks/virtualnetworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ func (s *Service) Delete(ctx context.Context) error {
defer span.End()

vnetSpec := s.Scope.VNetSpec()
if !s.Scope.Vnet().IsManaged(s.Scope.ClusterName()) {
existingVnet, err := s.getExisting(ctx, vnetSpec)
if azure.ResourceNotFound(err) {
// vnet does not exist, there is nothing to delete
return nil
}

if !existingVnet.IsManaged(s.Scope.ClusterName()) {
s.Scope.V(4).Info("Skipping VNet deletion in custom vnet mode")
return nil
}

s.Scope.V(2).Info("deleting VNet", "VNet", vnetSpec.Name)
err := s.Client.Delete(ctx, vnetSpec.ResourceGroup, vnetSpec.Name)
err = s.Client.Delete(ctx, vnetSpec.ResourceGroup, vnetSpec.Name)
if err != nil {
if azure.ResourceGroupNotFound(err) || azure.ResourceNotFound(err) {
return nil
Expand Down
47 changes: 45 additions & 2 deletions azure/services/virtualnetworks/virtualnetworks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ func TestDeleteVnet(t *testing.T) {
Name: "vnet-exists",
CIDRs: []string{"10.0.0.0/16"},
})
m.Get(gomockinternal.AContext(), "my-rg", "vnet-exists").
Return(network.VirtualNetwork{
ID: to.StringPtr("azure/fake/id"),
Name: to.StringPtr("vnet-exists"),
VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{
AddressSpace: &network.AddressSpace{
AddressPrefixes: to.StringSlicePtr([]string{"10.0.0.0/8"}),
},
},
Tags: map[string]*string{
"Name": to.StringPtr("vnet-exists"),
"sigs.k8s.io_cluster-api-provider-azure_cluster_fake-cluster": to.StringPtr("owned"),
"sigs.k8s.io_cluster-api-provider-azure_role": to.StringPtr("common"),
},
}, nil)
m.Delete(gomockinternal.AContext(), "my-rg", "vnet-exists")
},
},
Expand All @@ -315,8 +330,8 @@ func TestDeleteVnet(t *testing.T) {
Name: "vnet-exists",
CIDRs: []string{"10.0.0.0/16"},
})
m.Delete(gomockinternal.AContext(), "my-rg", "vnet-exists").
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found"))
m.Get(gomockinternal.AContext(), "my-rg", "vnet-exists").
Return(network.VirtualNetwork{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found"))
},
},
{
Expand All @@ -333,6 +348,19 @@ func TestDeleteVnet(t *testing.T) {
Name: "my-vnet",
CIDRs: []string{"10.0.0.0/16"},
})
m.Get(gomockinternal.AContext(), "my-rg", "my-vnet").
Return(network.VirtualNetwork{
ID: to.StringPtr("azure/custom-vnet/id"),
Name: to.StringPtr("my-vnet"),
VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{
AddressSpace: &network.AddressSpace{
AddressPrefixes: to.StringSlicePtr([]string{"10.0.0.0/16"}),
},
},
Tags: map[string]*string{
"Name": to.StringPtr("my-vnet"),
},
}, nil)
},
},
{
Expand All @@ -353,6 +381,21 @@ func TestDeleteVnet(t *testing.T) {
Name: "vnet-exists",
CIDRs: []string{"10.0.0.0/16"},
})
m.Get(gomockinternal.AContext(), "my-rg", "vnet-exists").
Return(network.VirtualNetwork{
ID: to.StringPtr("azure/fake/id"),
Name: to.StringPtr("vnet-exists"),
VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{
AddressSpace: &network.AddressSpace{
AddressPrefixes: to.StringSlicePtr([]string{"10.0.0.0/8"}),
},
},
Tags: map[string]*string{
"Name": to.StringPtr("vnet-exists"),
"sigs.k8s.io_cluster-api-provider-azure_cluster_fake-cluster": to.StringPtr("owned"),
"sigs.k8s.io_cluster-api-provider-azure_role": to.StringPtr("common"),
},
}, nil)
m.Delete(gomockinternal.AContext(), "my-rg", "vnet-exists").
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Honk Server"))
},
Expand Down

0 comments on commit 4f0e0fc

Please sign in to comment.