From d411a1deff85ab8456356211f136301af43cad18 Mon Sep 17 00:00:00 2001 From: LochanRn Date: Tue, 10 Aug 2021 23:39:19 +0530 Subject: [PATCH] fix managed clusters custom vnet delete issue updated the comment message --- .../virtualnetworks/virtualnetworks.go | 10 +++- .../virtualnetworks/virtualnetworks_test.go | 47 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/azure/services/virtualnetworks/virtualnetworks.go b/azure/services/virtualnetworks/virtualnetworks.go index af5a4560a29..55762a5acad 100644 --- a/azure/services/virtualnetworks/virtualnetworks.go +++ b/azure/services/virtualnetworks/virtualnetworks.go @@ -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 diff --git a/azure/services/virtualnetworks/virtualnetworks_test.go b/azure/services/virtualnetworks/virtualnetworks_test.go index 2da2bc4ff5e..a8dc273bbbe 100644 --- a/azure/services/virtualnetworks/virtualnetworks_test.go +++ b/azure/services/virtualnetworks/virtualnetworks_test.go @@ -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") }, }, @@ -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")) }, }, { @@ -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) }, }, { @@ -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")) },