From 5a06ad2d0fb431eade61bbd10d04ff0dd7823944 Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Thu, 24 May 2018 14:43:44 +0800 Subject: [PATCH] Add reason message logs for non-exist resources --- .../providers/azure/azure_vmss_cache.go | 6 ++-- .../providers/azure/azure_wrap.go | 31 ++++++++++++------- .../providers/azure/azure_wrap_test.go | 2 +- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/cloudprovider/providers/azure/azure_vmss_cache.go b/pkg/cloudprovider/providers/azure/azure_vmss_cache.go index e224845f30c..5542b7ca0b8 100644 --- a/pkg/cloudprovider/providers/azure/azure_vmss_cache.go +++ b/pkg/cloudprovider/providers/azure/azure_vmss_cache.go @@ -67,12 +67,13 @@ func (ss *scaleSet) newVmssCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() result, err := ss.VirtualMachineScaleSetsClient.Get(ctx, ss.ResourceGroup, key) - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Virtual machine scale set %q not found with message: %q", key, message) return nil, nil } @@ -147,12 +148,13 @@ func (ss *scaleSet) newVmssVMCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() result, err := ss.VirtualMachineScaleSetVMsClient.Get(ctx, ss.ResourceGroup, ssName, instanceID) - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Virtual machine scale set VM %q not found with message: %q", key, message) return nil, nil } diff --git a/pkg/cloudprovider/providers/azure/azure_wrap.go b/pkg/cloudprovider/providers/azure/azure_wrap.go index 2c30e287220..53c17871e88 100644 --- a/pkg/cloudprovider/providers/azure/azure_wrap.go +++ b/pkg/cloudprovider/providers/azure/azure_wrap.go @@ -25,6 +25,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-12-01/compute" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network" "github.com/Azure/go-autorest/autorest" + "github.com/golang/glog" "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/cloudprovider" @@ -40,18 +41,18 @@ var ( // checkExistsFromError inspects an error and returns a true if err is nil, // false if error is an autorest.Error with StatusCode=404 and will return the // error back if error is another status code or another type of error. -func checkResourceExistsFromError(err error) (bool, error) { +func checkResourceExistsFromError(err error) (bool, string, error) { if err == nil { - return true, nil + return true, "", nil } v, ok := err.(autorest.DetailedError) if !ok { - return false, err + return false, "", err } if v.StatusCode == http.StatusNotFound { - return false, nil + return false, err.Error(), nil } - return false, v + return false, "", v } // If it is StatusNotFound return nil, @@ -104,15 +105,17 @@ func (az *Cloud) getPublicIPAddress(pipResourceGroup string, pipName string) (pi } var realErr error + var message string ctx, cancel := getContextWithCancel() defer cancel() pip, err = az.PublicIPAddressesClient.Get(ctx, resourceGroup, pipName, "") - exists, realErr = checkResourceExistsFromError(err) + exists, message, realErr = checkResourceExistsFromError(err) if realErr != nil { return pip, false, realErr } if !exists { + glog.V(2).Infof("Public IP %q not found with message: %q", pipName, message) return pip, false, nil } @@ -121,6 +124,7 @@ func (az *Cloud) getPublicIPAddress(pipResourceGroup string, pipName string) (pi func (az *Cloud) getSubnet(virtualNetworkName string, subnetName string) (subnet network.Subnet, exists bool, err error) { var realErr error + var message string var rg string if len(az.VnetResourceGroup) > 0 { @@ -132,12 +136,13 @@ func (az *Cloud) getSubnet(virtualNetworkName string, subnetName string) (subnet ctx, cancel := getContextWithCancel() defer cancel() subnet, err = az.SubnetsClient.Get(ctx, rg, virtualNetworkName, subnetName, "") - exists, realErr = checkResourceExistsFromError(err) + exists, message, realErr = checkResourceExistsFromError(err) if realErr != nil { return subnet, false, realErr } if !exists { + glog.V(2).Infof("Subnet %q not found with message: %q", subnetName, message) return subnet, false, nil } @@ -181,12 +186,13 @@ func (az *Cloud) newVMCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() vm, err := az.VirtualMachinesClient.Get(ctx, az.ResourceGroup, key, compute.InstanceView) - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Virtual machine %q not found with message: %q", key, message) return nil, nil } @@ -202,12 +208,13 @@ func (az *Cloud) newLBCache() (*timedCache, error) { defer cancel() lb, err := az.LoadBalancerClient.Get(ctx, az.ResourceGroup, key, "") - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Load balancer %q not found with message: %q", key, message) return nil, nil } @@ -222,12 +229,13 @@ func (az *Cloud) newNSGCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() nsg, err := az.SecurityGroupsClient.Get(ctx, az.ResourceGroup, key, "") - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Security group %q not found with message: %q", key, message) return nil, nil } @@ -242,12 +250,13 @@ func (az *Cloud) newRouteTableCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() rt, err := az.RouteTablesClient.Get(ctx, az.ResourceGroup, key, "") - exists, realErr := checkResourceExistsFromError(err) + exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr } if !exists { + glog.V(2).Infof("Route table %q not found with message: %q", key, message) return nil, nil } diff --git a/pkg/cloudprovider/providers/azure/azure_wrap_test.go b/pkg/cloudprovider/providers/azure/azure_wrap_test.go index 380194ba9c3..5ab090c2a47 100644 --- a/pkg/cloudprovider/providers/azure/azure_wrap_test.go +++ b/pkg/cloudprovider/providers/azure/azure_wrap_test.go @@ -42,7 +42,7 @@ func TestExtractNotFound(t *testing.T) { } for _, test := range tests { - exists, err := checkResourceExistsFromError(test.err) + exists, _, err := checkResourceExistsFromError(test.err) if test.exists != exists { t.Errorf("expected: %v, saw: %v", test.exists, exists) }