Skip to content

Commit 69d4f28

Browse files
authored
fix: use exponential backoff for csr approvals (#35)
1 parent 6a6d400 commit 69d4f28

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

actions/approve_csr_handler.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (h *approveCSRHandler) Handle(ctx context.Context, data interface{}) error
4343
log := h.log.WithField("node_name", req.NodeName)
4444

4545
b := backoff.WithContext(
46-
backoff.WithMaxRetries(backoff.NewConstantBackOff(h.retryAfter), h.maxRetries),
46+
newApproveCSRExponentialBackoff(),
4747
ctx,
4848
)
4949
return backoff.RetryNotify(func() error {
@@ -60,6 +60,9 @@ func (h *approveCSRHandler) handle(ctx context.Context, log logrus.FieldLogger,
6060
log.Debug("getting initial csr")
6161
cert, err := h.getInitialNodeCSR(ctx, req.NodeName)
6262
if err != nil {
63+
if errors.Is(err, context.DeadlineExceeded) {
64+
return backoff.Permanent(fmt.Errorf("getting initial csr: %w", err))
65+
}
6366
return fmt.Errorf("getting initial csr: %w", err)
6467
}
6568

@@ -116,3 +119,11 @@ func (h *approveCSRHandler) getInitialNodeCSR(ctx context.Context, nodeName stri
116119
}
117120
}
118121
}
122+
123+
func newApproveCSRExponentialBackoff() *backoff.ExponentialBackOff {
124+
b := backoff.NewExponentialBackOff()
125+
b.Multiplier = 2
126+
b.MaxElapsedTime = 4 * time.Minute
127+
b.Reset()
128+
return b
129+
}

actions/approve_csr_handler_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
)
2424

2525
func TestApproveCSRHandler(t *testing.T) {
26-
r := require.New(t)
2726
log := logrus.New()
2827
log.SetLevel(logrus.DebugLevel)
2928

3029
t.Run("approve v1 csr successfully", func(t *testing.T) {
30+
r := require.New(t)
31+
3132
csr := &certv1.CertificateSigningRequest{
3233
TypeMeta: metav1.TypeMeta{},
3334
ObjectMeta: metav1.ObjectMeta{
@@ -99,6 +100,8 @@ AiAHVYZXHxxspoV0hcfn2Pdsl89fIPCOFy/K1PqSUR6QNAIgYdt51ZbQt9rgM2BD
99100
})
100101

101102
t.Run("approve v1beta1 csr successfully", func(t *testing.T) {
103+
r := require.New(t)
104+
102105
csr := &certv1beta1.CertificateSigningRequest{
103106
TypeMeta: metav1.TypeMeta{},
104107
ObjectMeta: metav1.ObjectMeta{
@@ -158,6 +161,8 @@ AiAHVYZXHxxspoV0hcfn2Pdsl89fIPCOFy/K1PqSUR6QNAIgYdt51ZbQt9rgM2BD
158161
})
159162

160163
t.Run("return timeout error when no initial csr found for node", func(t *testing.T) {
164+
r := require.New(t)
165+
161166
client := fake.NewSimpleClientset()
162167
h := &approveCSRHandler{
163168
log: log,
@@ -171,3 +176,14 @@ AiAHVYZXHxxspoV0hcfn2Pdsl89fIPCOFy/K1PqSUR6QNAIgYdt51ZbQt9rgM2BD
171176
r.EqualError(err, "getting initial csr: context deadline exceeded")
172177
})
173178
}
179+
180+
func TestApproveCSRExponentialBackoff(t *testing.T) {
181+
r := require.New(t)
182+
b := newApproveCSRExponentialBackoff()
183+
var sum time.Duration
184+
for i := 0; i < 10; i++ {
185+
tmp := b.NextBackOff()
186+
sum += tmp
187+
}
188+
r.Truef(100 < sum.Seconds(), "actual elapsed seconds %s", sum.Seconds())
189+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ require (
138138
gopkg.in/inf.v0 v0.9.1 // indirect
139139
gopkg.in/ini.v1 v1.66.2 // indirect
140140
gopkg.in/yaml.v2 v2.4.0 // indirect
141-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
141+
gopkg.in/yaml.v3 v3.0.0 // indirect
142142
k8s.io/apiextensions-apiserver v0.23.5 // indirect
143143
k8s.io/component-base v0.23.5 // indirect
144144
k8s.io/klog/v2 v2.30.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,8 +1785,9 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
17851785
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
17861786
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17871787
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1788-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
17891788
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1789+
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
1790+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17901791
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
17911792
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
17921793
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=

0 commit comments

Comments
 (0)