From 56a13cd9bf1a82af52841035dae1df0ace21a471 Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Fri, 9 Apr 2021 14:12:00 +0800 Subject: [PATCH] Ensure service deleted when the Azure resource group has been deleted --- .../azure/azure_backoff.go | 6 ++ .../azure/azure_backoff_test.go | 55 +++++++++++++++---- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go index 584d66ac02a14..fbb3783cf794c 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go @@ -275,6 +275,9 @@ func (az *Cloud) ListLB(service *v1.Service) ([]network.LoadBalancer, error) { rgName := az.getLoadBalancerResourceGroup() allLBs, rerr := az.LoadBalancerClient.List(ctx, rgName) if rerr != nil { + if rerr.IsNotFound() { + return nil, nil + } az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", rerr.Error().Error()) klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", rgName, rerr) return nil, rerr.Error() @@ -290,6 +293,9 @@ func (az *Cloud) ListPIP(service *v1.Service, pipResourceGroup string) ([]networ allPIPs, rerr := az.PublicIPAddressesClient.List(ctx, pipResourceGroup) if rerr != nil { + if rerr.IsNotFound() { + return nil, nil + } az.Event(service, v1.EventTypeWarning, "ListPublicIPs", rerr.Error().Error()) klog.Errorf("PublicIPAddressesClient.List(%v) failure with err=%v", pipResourceGroup, rerr) return nil, rerr.Error() diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff_test.go index eafd42a070b12..c8c1103b0196f 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff_test.go @@ -294,26 +294,57 @@ func TestListLB(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - az := GetTestCloud(ctrl) - mockLBClient := az.LoadBalancerClient.(*mockloadbalancerclient.MockInterface) - mockLBClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, &retry.Error{HTTPStatusCode: http.StatusInternalServerError}) + tests := []struct { + clientErr *retry.Error + expectedErr error + }{ + { + clientErr: &retry.Error{HTTPStatusCode: http.StatusInternalServerError}, + expectedErr: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: "), + }, + { + clientErr: &retry.Error{HTTPStatusCode: http.StatusNotFound}, + expectedErr: nil, + }, + } + for _, test := range tests { + az := GetTestCloud(ctrl) + mockLBClient := az.LoadBalancerClient.(*mockloadbalancerclient.MockInterface) + mockLBClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, test.clientErr) + + pips, err := az.ListLB(&v1.Service{}) + assert.Equal(t, test.expectedErr, err) + assert.Empty(t, pips) + } - pips, err := az.ListLB(&v1.Service{}) - assert.Equal(t, fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: "), err) - assert.Empty(t, pips) } func TestListPIP(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - az := GetTestCloud(ctrl) - mockPIPClient := az.PublicIPAddressesClient.(*mockpublicipclient.MockInterface) - mockPIPClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, &retry.Error{HTTPStatusCode: http.StatusInternalServerError}) + tests := []struct { + clientErr *retry.Error + expectedErr error + }{ + { + clientErr: &retry.Error{HTTPStatusCode: http.StatusInternalServerError}, + expectedErr: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: "), + }, + { + clientErr: &retry.Error{HTTPStatusCode: http.StatusNotFound}, + expectedErr: nil, + }, + } + for _, test := range tests { + az := GetTestCloud(ctrl) + mockPIPClient := az.PublicIPAddressesClient.(*mockpublicipclient.MockInterface) + mockPIPClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, test.clientErr) - pips, err := az.ListPIP(&v1.Service{}, az.ResourceGroup) - assert.Equal(t, fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: "), err) - assert.Empty(t, pips) + pips, err := az.ListPIP(&v1.Service{}, az.ResourceGroup) + assert.Equal(t, test.expectedErr, err) + assert.Empty(t, pips) + } } func TestCreateOrUpdatePIP(t *testing.T) {