Ensure service deleted when the Azure resource group has been deleted

This commit is contained in:
Pengfei Ni 2021-04-09 14:12:00 +08:00
parent a5489431cf
commit 5c93d76158
2 changed files with 49 additions and 12 deletions

View File

@ -275,6 +275,9 @@ func (az *Cloud) ListLB(service *v1.Service) ([]network.LoadBalancer, error) {
rgName := az.getLoadBalancerResourceGroup() rgName := az.getLoadBalancerResourceGroup()
allLBs, rerr := az.LoadBalancerClient.List(ctx, rgName) allLBs, rerr := az.LoadBalancerClient.List(ctx, rgName)
if rerr != nil { if rerr != nil {
if rerr.IsNotFound() {
return nil, nil
}
az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", rerr.Error().Error()) az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", rerr.Error().Error())
klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", rgName, rerr) klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", rgName, rerr)
return nil, rerr.Error() 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) allPIPs, rerr := az.PublicIPAddressesClient.List(ctx, pipResourceGroup)
if rerr != nil { if rerr != nil {
if rerr.IsNotFound() {
return nil, nil
}
az.Event(service, v1.EventTypeWarning, "ListPublicIPs", rerr.Error().Error()) az.Event(service, v1.EventTypeWarning, "ListPublicIPs", rerr.Error().Error())
klog.Errorf("PublicIPAddressesClient.List(%v) failure with err=%v", pipResourceGroup, rerr) klog.Errorf("PublicIPAddressesClient.List(%v) failure with err=%v", pipResourceGroup, rerr)
return nil, rerr.Error() return nil, rerr.Error()

View File

@ -294,26 +294,57 @@ func TestListLB(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
az := GetTestCloud(ctrl) tests := []struct {
mockLBClient := az.LoadBalancerClient.(*mockloadbalancerclient.MockInterface) clientErr *retry.Error
mockLBClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, &retry.Error{HTTPStatusCode: http.StatusInternalServerError}) expectedErr error
}{
{
clientErr: &retry.Error{HTTPStatusCode: http.StatusInternalServerError},
expectedErr: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: <nil>"),
},
{
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: <nil>"), err)
assert.Empty(t, pips)
} }
func TestListPIP(t *testing.T) { func TestListPIP(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
az := GetTestCloud(ctrl) tests := []struct {
mockPIPClient := az.PublicIPAddressesClient.(*mockpublicipclient.MockInterface) clientErr *retry.Error
mockPIPClient.EXPECT().List(gomock.Any(), az.ResourceGroup).Return(nil, &retry.Error{HTTPStatusCode: http.StatusInternalServerError}) expectedErr error
}{
{
clientErr: &retry.Error{HTTPStatusCode: http.StatusInternalServerError},
expectedErr: fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: <nil>"),
},
{
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) pips, err := az.ListPIP(&v1.Service{}, az.ResourceGroup)
assert.Equal(t, fmt.Errorf("Retriable: false, RetryAfter: 0s, HTTPStatusCode: 500, RawError: <nil>"), err) assert.Equal(t, test.expectedErr, err)
assert.Empty(t, pips) assert.Empty(t, pips)
}
} }
func TestCreateOrUpdatePIP(t *testing.T) { func TestCreateOrUpdatePIP(t *testing.T) {