@@ -16,6 +16,7 @@ import (
16
16
_ "k8s.io/client-go/plugin/pkg/client/auth"
17
17
"k8s.io/client-go/rest"
18
18
"k8s.io/client-go/tools/clientcmd"
19
+ "k8s.io/client-go/util/retry"
19
20
"math/rand"
20
21
"os"
21
22
"text/template"
@@ -229,34 +230,51 @@ func (m *KubeBootstrapTokenManager) createOrUpdateToken(token *bootstraptoken.Bo
229
230
resourceName := fmt .Sprintf (m .Opts .BootstrapToken .Name , token .Id ())
230
231
resourceNs := m .Opts .BootstrapToken .Namespace
231
232
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
239
241
}
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
249
265
return err
250
266
}
251
- } else {
252
- // error
267
+
268
+ return nil
269
+ })
270
+ if err != nil {
253
271
return err
254
272
}
255
273
256
274
if syncToCloud {
257
275
m .cloudProvider .StoreToken (token )
258
276
} else {
259
- contextLogger .Infof ("not syncing token to cloud, not needed" )
277
+ contextLogger .Debug ("not syncing token to cloud, not needed" )
260
278
}
261
279
262
280
m .prometheus .token .WithLabelValues (token .Id ()).Set (1 )
0 commit comments