Skip to content

Commit b94407e

Browse files
authored
Add CircleCI context retries (#467)
1 parent 60949e9 commit b94407e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/aws/aws-lambda-go v1.28.0
1111
github.com/aws/aws-sdk-go v1.40.34
1212
github.com/beamly/go-gocd v0.0.0-20190719193049-383d56afbf92
13+
github.com/cenkalti/backoff/v4 v4.1.2
1314
github.com/gogo/protobuf v1.3.1 // indirect
1415
github.com/google/gofuzz v1.1.0 // indirect
1516
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
9898
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
9999
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
100100
github.com/briandowns/spinner v0.0.0-20181018151057-dd69c579ff20/go.mod h1:hw/JEQBIE+c/BLI4aKM8UU8v+ZqrD3h7HC27kKt8JQU=
101+
github.com/cenkalti/backoff v1.1.0 h1:QnvVp8ikKCDWOsFheytRCoYWYPO/ObCTBGxT19Hc+yE=
102+
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
103+
github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuDPIFo=
104+
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
101105
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
102106
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
103107
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=

pkg/location/circlecicontext.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package location
33
import (
44
"encoding/base64"
55
"net/http"
6+
"time"
67

78
"github.com/ovotech/cloud-key-rotator/pkg/cred"
89

910
"github.com/CircleCI-Public/circleci-cli/api"
1011
"github.com/CircleCI-Public/circleci-cli/settings"
12+
13+
"github.com/cenkalti/backoff/v4"
1114
)
1215

1316
// CircleCIContext type
@@ -78,8 +81,35 @@ func (circleContext CircleCIContext) Write(serviceAccountName string, keyWrapper
7881
func updateCircleCIContext(contextID, envVarName, envVarValue string,
7982
restClient *api.ContextRestClient) (err error) {
8083

81-
if err = restClient.DeleteEnvironmentVariable(contextID, envVarName); err != nil {
84+
maxElapsedTimeSecs := 500
85+
backoffMultiplier := 5
86+
87+
deleteOp := func() error {
88+
logger.Infof("Deleting env var %s on contextID: %s", envVarName, contextID)
89+
return restClient.DeleteEnvironmentVariable(contextID, envVarName)
90+
}
91+
err = callCircleCIWithExpBackoff(deleteOp,
92+
time.Duration(maxElapsedTimeSecs)*time.Second,
93+
float64(backoffMultiplier))
94+
if err != nil {
8295
return
8396
}
84-
return restClient.CreateEnvironmentVariable(contextID, envVarName, envVarValue)
97+
createOp := func() error {
98+
logger.Infof("Creating env var %s on contextID: %s", envVarName, contextID)
99+
return restClient.CreateEnvironmentVariable(contextID, envVarName, envVarValue)
100+
}
101+
err = callCircleCIWithExpBackoff(createOp,
102+
time.Duration(maxElapsedTimeSecs)*time.Second,
103+
float64(backoffMultiplier))
104+
return
105+
}
106+
107+
// callCircleCIWithExpBackoff calls the CircleCI API with exponential backoff
108+
func callCircleCIWithExpBackoff(operation backoff.Operation,
109+
maxElapsedTime time.Duration, multiplier float64) (err error) {
110+
b := backoff.NewExponentialBackOff()
111+
b.MaxElapsedTime = maxElapsedTime
112+
b.Multiplier = multiplier
113+
err = backoff.Retry(operation, b)
114+
return
85115
}

0 commit comments

Comments
 (0)