2017-05-26 21:13:41 +00:00
|
|
|
/*
|
2017-05-31 18:53:02 +00:00
|
|
|
Copyright 2017 The Kubernetes Authors.
|
2017-05-26 21:13:41 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/Azure/go-autorest/autorest"
|
2017-06-02 22:35:20 +00:00
|
|
|
"github.com/golang/glog"
|
2017-05-26 21:13:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
operationPollInterval = 3 * time.Second
|
|
|
|
operationPollTimeoutDuration = time.Hour
|
2017-05-31 18:53:02 +00:00
|
|
|
backoffRetries = 12
|
|
|
|
backoffExponent = 2
|
|
|
|
backoffDuration = 1 * time.Second
|
|
|
|
backoffJitter = 1.0
|
2017-05-26 21:13:41 +00:00
|
|
|
)
|
|
|
|
|
2017-05-31 18:53:02 +00:00
|
|
|
var azAPIBackoff = wait.Backoff{
|
|
|
|
Steps: backoffRetries,
|
|
|
|
Factor: backoffExponent,
|
|
|
|
Duration: backoffDuration,
|
|
|
|
Jitter: backoffJitter,
|
|
|
|
}
|
|
|
|
|
2017-05-26 21:13:41 +00:00
|
|
|
// CreateOrUpdateSGWithRetry invokes az.SecurityGroupsClient.CreateOrUpdate with exponential backoff retry
|
2017-05-31 19:03:22 +00:00
|
|
|
func (az *Cloud) CreateOrUpdateSGWithRetry(sg network.SecurityGroup) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.SecurityGroupsClient.CreateOrUpdate(az.ResourceGroup, *sg.Name, sg, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdateLBWithRetry invokes az.LoadBalancerClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdateLBWithRetry(lb network.LoadBalancer) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.LoadBalancerClient.CreateOrUpdate(az.ResourceGroup, *lb.Name, lb, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdatePIPWithRetry invokes az.PublicIPAddressesClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdatePIPWithRetry(pip network.PublicIPAddress) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.PublicIPAddressesClient.CreateOrUpdate(az.ResourceGroup, *pip.Name, pip, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdateInterfaceWithRetry invokes az.PublicIPAddressesClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdateInterfaceWithRetry(nic network.Interface) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.InterfacesClient.CreateOrUpdate(az.ResourceGroup, *nic.Name, nic, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePublicIPWithRetry invokes az.PublicIPAddressesClient.Delete with exponential backoff retry
|
|
|
|
func (az *Cloud) DeletePublicIPWithRetry(pipName string) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.PublicIPAddressesClient.Delete(az.ResourceGroup, pipName, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLBWithRetry invokes az.LoadBalancerClient.Delete with exponential backoff retry
|
|
|
|
func (az *Cloud) DeleteLBWithRetry(lbName string) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.LoadBalancerClient.Delete(az.ResourceGroup, lbName, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdateRouteTableWithRetry invokes az.RouteTablesClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdateRouteTableWithRetry(routeTable network.RouteTable) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.RouteTablesClient.CreateOrUpdate(az.ResourceGroup, az.RouteTableName, routeTable, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdateRouteWithRetry invokes az.RoutesClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdateRouteWithRetry(route network.Route) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.RoutesClient.CreateOrUpdate(az.ResourceGroup, az.RouteTableName, *route.Name, route, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRouteWithRetry invokes az.RoutesClient.Delete with exponential backoff retry
|
|
|
|
func (az *Cloud) DeleteRouteWithRetry(routeName string) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.RoutesClient.Delete(az.ResourceGroup, az.RouteTableName, routeName, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrUpdateVMWithRetry invokes az.VirtualMachinesClient.CreateOrUpdate with exponential backoff retry
|
|
|
|
func (az *Cloud) CreateOrUpdateVMWithRetry(vmName string, newVM compute.VirtualMachine) error {
|
2017-05-31 18:53:02 +00:00
|
|
|
return wait.ExponentialBackoff(azAPIBackoff, func() (bool, error) {
|
|
|
|
az.operationPollRateLimiter.Accept()
|
2017-05-26 21:13:41 +00:00
|
|
|
resp, err := az.VirtualMachinesClient.CreateOrUpdate(az.ResourceGroup, vmName, newVM, nil)
|
|
|
|
return processRetryResponse(resp, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-02 22:35:20 +00:00
|
|
|
// A wait.ConditionFunc function to deal with common HTTP backoff response conditions
|
2017-05-26 21:13:41 +00:00
|
|
|
func processRetryResponse(resp autorest.Response, err error) (bool, error) {
|
|
|
|
if isSuccessHTTPResponse(resp) {
|
2017-06-02 22:35:20 +00:00
|
|
|
glog.V(2).Infof("backoff: success, HTTP response=%d", resp.StatusCode)
|
2017-05-26 21:13:41 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if shouldRetryAPIRequest(resp, err) {
|
2017-06-02 22:35:20 +00:00
|
|
|
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
|
2017-05-26 21:13:41 +00:00
|
|
|
}
|
|
|
|
// Fall-through: stop periodic backoff, return error object from most recent request
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetryAPIRequest determines if the response from an HTTP request suggests periodic retry behavior
|
|
|
|
func shouldRetryAPIRequest(resp autorest.Response, err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2017-06-01 20:58:11 +00:00
|
|
|
// HTTP 4xx or 5xx suggests we should retry
|
|
|
|
if 399 < resp.StatusCode && resp.StatusCode < 600 {
|
2017-05-26 21:13:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSuccessHTTPResponse determines if the response from an HTTP request suggests success
|
|
|
|
func isSuccessHTTPResponse(resp autorest.Response) bool {
|
2017-05-31 18:53:02 +00:00
|
|
|
// HTTP 2xx suggests a successful response
|
2017-06-01 20:58:11 +00:00
|
|
|
if 199 < resp.StatusCode && resp.StatusCode < 300 {
|
2017-05-26 21:13:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|