@@ -13,30 +13,27 @@ import (
13
13
)
14
14
15
15
type TokenGetter struct {
16
- client corev1.ServiceAccountsGetter
17
- expirationDuration time.Duration
18
- removeAfterExpiredDuration time.Duration
19
- tokens map [types.NamespacedName ]* authenticationv1.TokenRequestStatus
20
- mu sync.RWMutex
16
+ client corev1.ServiceAccountsGetter
17
+ expirationDuration time.Duration
18
+ tokens map [types.NamespacedName ]* authenticationv1.TokenRequestStatus
19
+ mu sync.RWMutex
21
20
}
22
21
23
22
type TokenGetterOption func (* TokenGetter )
24
23
25
24
const (
26
- RotationThresholdPercentage = 10
27
- DefaultExpirationDuration = 5 * time .Minute
28
- DefaultRemoveAfterExpiredDuration = 90 * time .Minute
25
+ rotationThresholdFraction = 0.1
26
+ DefaultExpirationDuration = 5 * time .Minute
29
27
)
30
28
31
29
// Returns a token getter that can fetch tokens given a service account.
32
30
// The token getter also caches tokens which helps reduce the number of requests to the API Server.
33
31
// In case a cached token is expiring a fresh token is created.
34
32
func NewTokenGetter (client corev1.ServiceAccountsGetter , options ... TokenGetterOption ) * TokenGetter {
35
33
tokenGetter := & TokenGetter {
36
- client : client ,
37
- expirationDuration : DefaultExpirationDuration ,
38
- removeAfterExpiredDuration : DefaultRemoveAfterExpiredDuration ,
39
- tokens : map [types.NamespacedName ]* authenticationv1.TokenRequestStatus {},
34
+ client : client ,
35
+ expirationDuration : DefaultExpirationDuration ,
36
+ tokens : map [types.NamespacedName ]* authenticationv1.TokenRequestStatus {},
40
37
}
41
38
42
39
for _ , opt := range options {
@@ -52,12 +49,6 @@ func WithExpirationDuration(expirationDuration time.Duration) TokenGetterOption
52
49
}
53
50
}
54
51
55
- func WithRemoveAfterExpiredDuration (removeAfterExpiredDuration time.Duration ) TokenGetterOption {
56
- return func (tg * TokenGetter ) {
57
- tg .removeAfterExpiredDuration = removeAfterExpiredDuration
58
- }
59
- }
60
-
61
52
// Get returns a token from the cache if available and not expiring, otherwise creates a new token
62
53
func (t * TokenGetter ) Get (ctx context.Context , key types.NamespacedName ) (string , error ) {
63
54
t .mu .RLock ()
@@ -69,8 +60,8 @@ func (t *TokenGetter) Get(ctx context.Context, key types.NamespacedName) (string
69
60
expireTime = token .ExpirationTimestamp .Time
70
61
}
71
62
72
- // Create a new token if the cached token expires within DurationPercentage of expirationDuration from now
73
- rotationThresholdAfterNow := metav1 .Now ().Add (t .expirationDuration * (RotationThresholdPercentage / 100 ))
63
+ // Create a new token if the cached token expires within rotationThresholdFraction of expirationDuration from now
64
+ rotationThresholdAfterNow := metav1 .Now ().Add (time . Duration ( float64 ( t .expirationDuration ) * (rotationThresholdFraction ) ))
74
65
if expireTime .Before (rotationThresholdAfterNow ) {
75
66
var err error
76
67
token , err = t .getToken (ctx , key )
@@ -82,8 +73,8 @@ func (t *TokenGetter) Get(ctx context.Context, key types.NamespacedName) (string
82
73
t .mu .Unlock ()
83
74
}
84
75
85
- // Delete tokens that have been expired for more than ExpiredDuration
86
- t .reapExpiredTokens (t . removeAfterExpiredDuration )
76
+ // Delete tokens that have expired
77
+ t .reapExpiredTokens ()
87
78
88
79
return token .Token , nil
89
80
}
@@ -92,19 +83,19 @@ func (t *TokenGetter) getToken(ctx context.Context, key types.NamespacedName) (*
92
83
req , err := t .client .ServiceAccounts (key .Namespace ).CreateToken (ctx ,
93
84
key .Name ,
94
85
& authenticationv1.TokenRequest {
95
- Spec : authenticationv1.TokenRequestSpec {ExpirationSeconds : ptr.To [int64 ](int64 (t .expirationDuration ))},
86
+ Spec : authenticationv1.TokenRequestSpec {ExpirationSeconds : ptr.To [int64 ](int64 (t .expirationDuration / time . Second ))},
96
87
}, metav1.CreateOptions {})
97
88
if err != nil {
98
89
return nil , err
99
90
}
100
91
return & req .Status , nil
101
92
}
102
93
103
- func (t * TokenGetter ) reapExpiredTokens (expiredDuration time. Duration ) {
94
+ func (t * TokenGetter ) reapExpiredTokens () {
104
95
t .mu .Lock ()
105
96
defer t .mu .Unlock ()
106
97
for key , token := range t .tokens {
107
- if metav1 .Now ().Sub (token .ExpirationTimestamp .Time ) > expiredDuration {
98
+ if metav1 .Now ().Sub (token .ExpirationTimestamp .Time ) > 0 {
108
99
delete (t .tokens , key )
109
100
}
110
101
}
0 commit comments