@@ -10,6 +10,7 @@ import (
10
10
"github.com/webdevops/kube-bootstrap-token-manager/cloudprovider"
11
11
"github.com/webdevops/kube-bootstrap-token-manager/config"
12
12
corev1 "k8s.io/api/core/v1"
13
+ "k8s.io/apimachinery/pkg/api/errors"
13
14
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
15
"k8s.io/client-go/kubernetes"
15
16
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -229,11 +230,15 @@ func (m *KubeBootstrapTokenManager) createOrUpdateToken(token *bootstraptoken.Bo
229
230
resourceNs := m .Opts .BootstrapToken .Namespace
230
231
231
232
resource , err := m .k8sClient .CoreV1 ().Secrets (resourceNs ).Get (m .ctx , resourceName , v1.GetOptions {})
232
- if resource == nil && err != nil {
233
- return err
234
- }
235
-
236
- if resource == nil || resource .UID == "" {
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
239
+ }
240
+ } else if errors .IsNotFound (err ) {
241
+ // create
237
242
resource = & corev1.Secret {}
238
243
resource .SetName (resourceName )
239
244
resource .SetNamespace (resourceNs )
@@ -244,11 +249,8 @@ func (m *KubeBootstrapTokenManager) createOrUpdateToken(token *bootstraptoken.Bo
244
249
return err
245
250
}
246
251
} else {
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
+ // error
253
+ return err
252
254
}
253
255
254
256
if syncToCloud {
0 commit comments