Commit 5042cea8 authored by Pengfei Ni's avatar Pengfei Ni

Use new clients for vmss cache

parent 3b7cc3dd
...@@ -17,13 +17,17 @@ limitations under the License. ...@@ -17,13 +17,17 @@ limitations under the License.
package azure package azure
import ( import (
"k8s.io/apimachinery/pkg/util/wait" "context"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/compute" "github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/network" "github.com/Azure/azure-sdk-for-go/arm/network"
computepreview "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-12-01/compute"
"github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
) )
// requestBackoff if backoff is disabled in cloud provider it // requestBackoff if backoff is disabled in cloud provider it
...@@ -345,6 +349,15 @@ func (az *Cloud) CreateOrUpdateVMWithRetry(vmName string, newVM compute.VirtualM ...@@ -345,6 +349,15 @@ func (az *Cloud) CreateOrUpdateVMWithRetry(vmName string, newVM compute.VirtualM
}) })
} }
// UpdateVmssVMWithRetry invokes az.VirtualMachineScaleSetVMsClient.Update with exponential backoff retry
func (az *Cloud) UpdateVmssVMWithRetry(ctx context.Context, resourceGroupName string, VMScaleSetName string, instanceID string, parameters computepreview.VirtualMachineScaleSetVM) error {
return wait.ExponentialBackoff(az.requestBackoff(), func() (bool, error) {
resp, err := az.VirtualMachineScaleSetVMsClient.Update(ctx, resourceGroupName, VMScaleSetName, instanceID, parameters)
glog.V(10).Infof("VirtualMachinesClient.CreateOrUpdate(%s,%s): end", VMScaleSetName, instanceID)
return processHTTPRetryResponse(resp, err)
})
}
// A wait.ConditionFunc function to deal with common HTTP backoff response conditions // A wait.ConditionFunc function to deal with common HTTP backoff response conditions
func processRetryResponse(resp autorest.Response, err error) (bool, error) { func processRetryResponse(resp autorest.Response, err error) (bool, error) {
if isSuccessHTTPResponse(resp) { if isSuccessHTTPResponse(resp) {
...@@ -380,3 +393,36 @@ func isSuccessHTTPResponse(resp autorest.Response) bool { ...@@ -380,3 +393,36 @@ func isSuccessHTTPResponse(resp autorest.Response) bool {
} }
return false return false
} }
func shouldRetryHTTPRequest(resp *http.Response, err error) bool {
if err != nil {
return true
}
if resp != nil {
// HTTP 4xx or 5xx suggests we should retry
if 399 < resp.StatusCode && resp.StatusCode < 600 {
return true
}
}
return false
}
func processHTTPRetryResponse(resp *http.Response, err error) (bool, error) {
if resp != nil {
// HTTP 2xx suggests a successful response
if 199 < resp.StatusCode && resp.StatusCode < 300 {
return true, nil
}
}
if shouldRetryHTTPRequest(resp, err) {
glog.Errorf("backoff: failure, will retry, HTTP response=%d, err=%v", resp.StatusCode, err)
// suppress the error object so that backoff process continues
return false, nil
}
// Fall-through: stop periodic backoff
return true, nil
}
...@@ -140,6 +140,10 @@ type azVirtualMachinesClient struct { ...@@ -140,6 +140,10 @@ type azVirtualMachinesClient struct {
rateLimiter flowcontrol.RateLimiter rateLimiter flowcontrol.RateLimiter
} }
func getContextWithCancel() (context.Context, context.CancelFunc) {
return context.WithCancel(context.Background())
}
func newAzVirtualMachinesClient(config *azClientConfig) *azVirtualMachinesClient { func newAzVirtualMachinesClient(config *azClientConfig) *azVirtualMachinesClient {
virtualMachinesClient := compute.NewVirtualMachinesClient(config.subscriptionID) virtualMachinesClient := compute.NewVirtualMachinesClient(config.subscriptionID)
virtualMachinesClient.BaseURI = config.resourceManagerEndpoint virtualMachinesClient.BaseURI = config.resourceManagerEndpoint
......
...@@ -58,7 +58,9 @@ func (ss *scaleSet) extractVmssVMName(name string) (string, string, error) { ...@@ -58,7 +58,9 @@ func (ss *scaleSet) extractVmssVMName(name string) (string, string, error) {
func (ss *scaleSet) newVmssCache() (*timedCache, error) { func (ss *scaleSet) newVmssCache() (*timedCache, error) {
getter := func(key string) (interface{}, error) { getter := func(key string) (interface{}, error) {
result, err := ss.VirtualMachineScaleSetsClient.Get(ss.ResourceGroup, key) ctx, cancel := getContextWithCancel()
defer cancel()
result, err := ss.VirtualMachineScaleSetsClient.Get(ctx, ss.ResourceGroup, key)
exists, realErr := checkResourceExistsFromError(err) exists, realErr := checkResourceExistsFromError(err)
if realErr != nil { if realErr != nil {
return nil, realErr return nil, realErr
...@@ -76,14 +78,14 @@ func (ss *scaleSet) newVmssCache() (*timedCache, error) { ...@@ -76,14 +78,14 @@ func (ss *scaleSet) newVmssCache() (*timedCache, error) {
func (ss *scaleSet) newNodeNameToScaleSetMappingCache() (*timedCache, error) { func (ss *scaleSet) newNodeNameToScaleSetMappingCache() (*timedCache, error) {
getter := func(key string) (interface{}, error) { getter := func(key string) (interface{}, error) {
scaleSetNames, err := ss.listScaleSetsWithRetry() scaleSetNames, err := ss.listScaleSets()
if err != nil { if err != nil {
return nil, err return nil, err
} }
localCache := make(nodeNameToScaleSetMapping) localCache := make(nodeNameToScaleSetMapping)
for _, ssName := range scaleSetNames { for _, ssName := range scaleSetNames {
vms, err := ss.listScaleSetVMsWithRetry(ssName) vms, err := ss.listScaleSetVMs(ssName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -136,7 +138,9 @@ func (ss *scaleSet) newVmssVMCache() (*timedCache, error) { ...@@ -136,7 +138,9 @@ func (ss *scaleSet) newVmssVMCache() (*timedCache, error) {
return nil, nil return nil, nil
} }
result, err := ss.VirtualMachineScaleSetVMsClient.Get(ss.ResourceGroup, ssName, instanceID) ctx, cancel := getContextWithCancel()
defer cancel()
result, err := ss.VirtualMachineScaleSetVMsClient.Get(ctx, ss.ResourceGroup, ssName, instanceID)
exists, realErr := checkResourceExistsFromError(err) exists, realErr := checkResourceExistsFromError(err)
if realErr != nil { if realErr != nil {
return nil, realErr return nil, realErr
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment