Skip to content

Commit 6fdc338

Browse files
committed
Add retry
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 524e7fc commit 6fdc338

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

manager/manager.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
_ "k8s.io/client-go/plugin/pkg/client/auth"
1717
"k8s.io/client-go/rest"
1818
"k8s.io/client-go/tools/clientcmd"
19+
"k8s.io/client-go/util/retry"
1920
"math/rand"
2021
"os"
2122
"text/template"
@@ -229,34 +230,51 @@ func (m *KubeBootstrapTokenManager) createOrUpdateToken(token *bootstraptoken.Bo
229230
resourceName := fmt.Sprintf(m.Opts.BootstrapToken.Name, token.Id())
230231
resourceNs := m.Opts.BootstrapToken.Namespace
231232

232-
resource, err := m.k8sClient.CoreV1().Secrets(resourceNs).Get(m.ctx, resourceName, v1.GetOptions{})
233-
if err == nil {
234-
// update
235-
contextLogger.Infof("updating existing bootstrap token \"%s\" with expiration %s", resourceName, token.ExpirationString())
236-
resource = m.updateTokenData(resource, token)
237-
if _, err := m.k8sClient.CoreV1().Secrets(resourceNs).Update(m.ctx, resource, v1.UpdateOptions{}); err != nil {
238-
return err
233+
err := retry.OnError(retry.DefaultRetry, func(err error) bool {
234+
switch {
235+
case errors.IsServerTimeout(err):
236+
return true
237+
case errors.IsConflict(err):
238+
return true
239+
case errors.IsTimeout(err):
240+
return true
239241
}
240-
} else if errors.IsNotFound(err) {
241-
// create
242-
resource = &corev1.Secret{}
243-
resource.SetName(resourceName)
244-
resource.SetNamespace(resourceNs)
245-
246-
contextLogger.Infof("creating new bootstrap token \"%s\" with expiration %s", resourceName, token.ExpirationString())
247-
resource = m.updateTokenData(resource, token)
248-
if _, err := m.k8sClient.CoreV1().Secrets(resourceNs).Create(m.ctx, resource, v1.CreateOptions{}); err != nil {
242+
return false
243+
}, func() error {
244+
resource, err := m.k8sClient.CoreV1().Secrets(resourceNs).Get(m.ctx, resourceName, v1.GetOptions{})
245+
if err == nil {
246+
// update
247+
contextLogger.Infof("updating existing bootstrap token \"%s\" with expiration %s", resourceName, token.ExpirationString())
248+
resource = m.updateTokenData(resource, token)
249+
if _, err := m.k8sClient.CoreV1().Secrets(resourceNs).Update(m.ctx, resource, v1.UpdateOptions{}); err != nil {
250+
return err
251+
}
252+
} else if errors.IsNotFound(err) {
253+
// create
254+
resource = &corev1.Secret{}
255+
resource.SetName(resourceName)
256+
resource.SetNamespace(resourceNs)
257+
258+
contextLogger.Infof("creating new bootstrap token \"%s\" with expiration %s", resourceName, token.ExpirationString())
259+
resource = m.updateTokenData(resource, token)
260+
if _, err := m.k8sClient.CoreV1().Secrets(resourceNs).Create(m.ctx, resource, v1.CreateOptions{}); err != nil {
261+
return err
262+
}
263+
} else {
264+
// error
249265
return err
250266
}
251-
} else {
252-
// error
267+
268+
return nil
269+
})
270+
if err != nil {
253271
return err
254272
}
255273

256274
if syncToCloud {
257275
m.cloudProvider.StoreToken(token)
258276
} else {
259-
contextLogger.Infof("not syncing token to cloud, not needed")
277+
contextLogger.Debug("not syncing token to cloud, not needed")
260278
}
261279

262280
m.prometheus.token.WithLabelValues(token.Id()).Set(1)

0 commit comments

Comments
 (0)