Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 5d847c1

Browse files
committed
Kubernetes: ensure ARM operations have appropriate timeout value (#3646)
1 parent da4127d commit 5d847c1

File tree

9 files changed

+45
-12
lines changed

9 files changed

+45
-12
lines changed

cmd/deploy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ func autofillApimodel(dc *deployCmd) error {
286286
dc.containerService.Properties.LinuxProfile.SSH.PublicKeys = []api.PublicKey{{KeyData: publicKey}}
287287
}
288288

289-
ctx := context.Background()
289+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
290+
defer cancel()
290291
_, err = dc.client.EnsureResourceGroup(ctx, dc.resourceGroup, dc.location, nil)
291292
if err != nil {
292293
return err
@@ -414,9 +415,11 @@ func (dc *deployCmd) run() error {
414415
}
415416

416417
deploymentSuffix := dc.random.Int31()
418+
cx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
419+
defer cancel()
417420

418421
if res, err := dc.client.DeployTemplate(
419-
context.Background(),
422+
cx,
420423
dc.resourceGroup,
421424
fmt.Sprintf("%s-%d", dc.resourceGroup, deploymentSuffix),
422425
templateJSON,

cmd/scale.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (sc *scaleCmd) load(cmd *cobra.Command) error {
132132
return errors.Wrap(err, "failed to get client")
133133
}
134134

135-
ctx := context.Background()
135+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
136+
defer cancel()
136137
_, err = sc.client.EnsureResourceGroup(ctx, sc.resourceGroupName, sc.location, nil)
137138
if err != nil {
138139
return err
@@ -209,7 +210,8 @@ func (sc *scaleCmd) run(cmd *cobra.Command, args []string) error {
209210
return errors.Wrap(err, "failed to load existing container service")
210211
}
211212

212-
ctx := context.Background()
213+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
214+
defer cancel()
213215
orchestratorInfo := sc.containerService.Properties.OrchestratorProfile
214216
var currentNodeCount, highestUsedIndex, index, winPoolIndex int
215217
winPoolIndex = -1

cmd/upgrade.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func (uc *upgradeCmd) loadCluster(cmd *cobra.Command) error {
122122
return errors.Wrap(err, "Failed to get client")
123123
}
124124

125-
ctx := context.Background()
125+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
126+
defer cancel()
126127
_, err = uc.client.EnsureResourceGroup(ctx, uc.resourceGroupName, uc.location, nil)
127128
if err != nil {
128129
return errors.Wrap(err, "Error ensuring resource group")

pkg/acsengine/tenantid.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/http"
66
"regexp"
7+
"time"
78

89
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
910
"github.com/pkg/errors"
@@ -22,7 +23,9 @@ func GetTenantID(resourceManagerEndpoint string, subscriptionID string) (string,
2223
// we expect this request to fail (err != nil), but we are only interested
2324
// in headers, so surface the error if the Response is not present (i.e.
2425
// network error etc)
25-
subs, err := c.Get(context.Background(), subscriptionID)
26+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
27+
defer cancel()
28+
subs, err := c.Get(ctx, subscriptionID)
2629
if subs.Response.Response == nil {
2730
return "", errors.Wrap(err, "Request failed")
2831
}

pkg/armhelpers/azureclient.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string)
106106
}
107107
}
108108

109-
client := &autorest.Client{}
109+
client := &autorest.Client{
110+
PollingDuration: 1 * time.Hour,
111+
}
110112

111113
deviceCode, err := adal.InitiateDeviceAuth(client, *oauthConfig, acsEngineClientID, env.ServiceManagementEndpoint)
112114
if err != nil {
@@ -301,6 +303,21 @@ func getClient(env azure.Environment, subscriptionID, tenantID string, armSpt *a
301303
c.deploymentsClient.PollingDelay = time.Second * 5
302304
c.resourcesClient.PollingDelay = time.Second * 5
303305

306+
// Set permissive timeouts to accommodate long-running operations
307+
c.deploymentsClient.PollingDuration = time.Hour * 1
308+
c.deploymentOperationsClient.PollingDuration = time.Hour * 1
309+
c.applicationsClient.PollingDuration = time.Hour * 1
310+
c.authorizationClient.PollingDuration = time.Hour * 1
311+
c.disksClient.PollingDuration = time.Hour * 1
312+
c.groupsClient.PollingDuration = time.Hour * 1
313+
c.interfacesClient.PollingDuration = time.Hour * 1
314+
c.providersClient.PollingDuration = time.Hour * 1
315+
c.resourcesClient.PollingDuration = time.Hour * 1
316+
c.storageAccountsClient.PollingDuration = time.Hour * 1
317+
c.virtualMachineScaleSetsClient.PollingDuration = time.Hour * 1
318+
c.virtualMachineScaleSetVMsClient.PollingDuration = time.Hour * 1
319+
c.virtualMachinesClient.PollingDuration = time.Hour * 1
320+
304321
graphAuthorizer := autorest.NewBearerAuthorizer(graphSpt)
305322
c.applicationsClient.Authorizer = graphAuthorizer
306323
c.servicePrincipalsClient.Authorizer = graphAuthorizer
@@ -310,7 +327,8 @@ func getClient(env azure.Environment, subscriptionID, tenantID string, armSpt *a
310327

311328
// EnsureProvidersRegistered checks if the AzureClient is registered to required resource providers and, if not, register subscription to providers
312329
func (az *AzureClient) EnsureProvidersRegistered(subscriptionID string) error {
313-
ctx := context.Background()
330+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
331+
defer cancel()
314332
registeredProviders, err := az.providersClient.List(ctx, to.Int32Ptr(100), "")
315333
if err != nil {
316334
return err

pkg/armhelpers/deploymentError.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"strings"
9+
"time"
910

1011
"github.com/Azure/acs-engine/pkg/api"
1112
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
@@ -58,7 +59,8 @@ func (e *DeploymentValidationError) Error() string {
5859

5960
// DeployTemplateSync deploys the template and returns ArmError
6061
func DeployTemplateSync(az ACSEngineClient, logger *logrus.Entry, resourceGroupName, deploymentName string, template map[string]interface{}, parameters map[string]interface{}) error {
61-
ctx := context.Background()
62+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
63+
defer cancel()
6264
deploymentExtended, err := az.DeployTemplate(ctx, resourceGroupName, deploymentName, template, parameters)
6365
if err == nil {
6466
return nil

pkg/operations/deletevm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package operations
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/Azure/acs-engine/pkg/armhelpers"
89
"github.com/Azure/acs-engine/pkg/armhelpers/utils"
@@ -17,7 +18,8 @@ const (
1718

1819
// CleanDeleteVirtualMachine deletes a VM and any associated OS disk
1920
func CleanDeleteVirtualMachine(az armhelpers.ACSEngineClient, logger *log.Entry, subscriptionID, resourceGroup, name string) error {
20-
ctx := context.Background()
21+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
22+
defer cancel()
2123
logger.Infof("fetching VM: %s/%s", resourceGroup, name)
2224
vm, err := az.GetVirtualMachine(ctx, resourceGroup, name)
2325
if err != nil {

pkg/operations/kubernetesupgrade/upgradecluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ func (uc *UpgradeCluster) UpgradeCluster(subscriptionID uuid.UUID, kubeConfig, r
145145
func (uc *UpgradeCluster) getClusterNodeStatus(subscriptionID uuid.UUID, resourceGroup string) error {
146146
targetOrchestratorTypeVersion := fmt.Sprintf("%s:%s", uc.DataModel.Properties.OrchestratorProfile.OrchestratorType, uc.DataModel.Properties.OrchestratorProfile.OrchestratorVersion)
147147

148-
ctx := context.Background()
148+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
149+
defer cancel()
149150
for vmScaleSetPage, err := uc.Client.ListVirtualMachineScaleSets(ctx, resourceGroup); vmScaleSetPage.NotDone(); err = vmScaleSetPage.Next() {
150151
if err != nil {
151152
return err

pkg/operations/kubernetesupgrade/upgrader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (ku *Upgrader) Init(translator *i18n.Translator, logger *logrus.Entry, clus
5454

5555
// RunUpgrade runs the upgrade pipeline
5656
func (ku *Upgrader) RunUpgrade() error {
57-
ctx := context.Background()
57+
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Minute)
58+
defer cancel()
5859
if err := ku.upgradeMasterNodes(ctx); err != nil {
5960
return err
6061
}

0 commit comments

Comments
 (0)