@@ -41,6 +41,8 @@ import (
41
41
"k8s.io/kubernetes/pkg/serviceaccount"
42
42
)
43
43
44
+ const ServiceServingCASecretKey = "service-ca.crt"
45
+
44
46
// RemoveTokenBackoff is the recommended (empirical) retry interval for removing
45
47
// a secret reference from a service account when the secret is deleted. It is
46
48
// exported for use by custom secret controllers.
@@ -66,6 +68,9 @@ type TokensControllerOptions struct {
66
68
// MaxRetries controls the maximum number of times a particular key is retried before giving up
67
69
// If zero, a default max is used
68
70
MaxRetries int
71
+
72
+ // This CA will be added in the secrets of service accounts
73
+ ServiceServingCA []byte
69
74
}
70
75
71
76
// NewTokensController returns a new *TokensController.
@@ -76,9 +81,10 @@ func NewTokensController(serviceAccounts informers.ServiceAccountInformer, secre
76
81
}
77
82
78
83
e := & TokensController {
79
- client : cl ,
80
- token : options .TokenGenerator ,
81
- rootCA : options .RootCA ,
84
+ client : cl ,
85
+ token : options .TokenGenerator ,
86
+ rootCA : options .RootCA ,
87
+ serviceServingCA : options .ServiceServingCA ,
82
88
83
89
syncServiceAccountQueue : workqueue .NewTypedRateLimitingQueueWithConfig (
84
90
workqueue .DefaultTypedControllerRateLimiter [serviceAccountQueueKey ](),
@@ -134,7 +140,8 @@ type TokensController struct {
134
140
client clientset.Interface
135
141
token serviceaccount.TokenGenerator
136
142
137
- rootCA []byte
143
+ rootCA []byte
144
+ serviceServingCA []byte
138
145
139
146
serviceAccounts listersv1.ServiceAccountLister
140
147
// updatedSecrets is a wrapper around the shared cache which allows us to record
@@ -352,22 +359,23 @@ func (e *TokensController) deleteToken(ns, name string, uid types.UID) ( /*retry
352
359
return true , err
353
360
}
354
361
355
- func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool ) {
362
+ func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool , bool ) {
356
363
caData := secret .Data [v1 .ServiceAccountRootCAKey ]
357
364
needsCA := len (e .rootCA ) > 0 && ! bytes .Equal (caData , e .rootCA )
365
+ needsServiceServingCA := len (e .serviceServingCA ) > 0 && bytes .Compare (secret .Data [ServiceServingCASecretKey ], e .serviceServingCA ) != 0
358
366
359
367
needsNamespace := len (secret .Data [v1 .ServiceAccountNamespaceKey ]) == 0
360
368
361
369
tokenData := secret .Data [v1 .ServiceAccountTokenKey ]
362
370
needsToken := len (tokenData ) == 0
363
371
364
- return needsCA , needsNamespace , needsToken
372
+ return needsCA , needsServiceServingCA , needsNamespace , needsToken
365
373
}
366
374
367
375
// generateTokenIfNeeded populates the token data for the given Secret if not already set
368
376
func (e * TokensController ) generateTokenIfNeeded (logger klog.Logger , serviceAccount * v1.ServiceAccount , cachedSecret * v1.Secret ) ( /* retry */ bool , error ) {
369
377
// Check the cached secret to see if changes are needed
370
- if needsCA , needsNamespace , needsToken := e .secretUpdateNeeded (cachedSecret ); ! needsCA && ! needsToken && ! needsNamespace {
378
+ if needsCA , needsServiceServingCA , needsNamespace , needsToken := e .secretUpdateNeeded (cachedSecret ); ! needsCA && ! needsServiceServingCA && ! needsToken && ! needsNamespace {
371
379
return false , nil
372
380
}
373
381
@@ -386,8 +394,8 @@ func (e *TokensController) generateTokenIfNeeded(logger klog.Logger, serviceAcco
386
394
return false , nil
387
395
}
388
396
389
- needsCA , needsNamespace , needsToken := e .secretUpdateNeeded (liveSecret )
390
- if ! needsCA && ! needsToken && ! needsNamespace {
397
+ needsCA , needsServiceServingCA , needsNamespace , needsToken := e .secretUpdateNeeded (liveSecret )
398
+ if ! needsCA && ! needsServiceServingCA && ! needsToken && ! needsNamespace {
391
399
return false , nil
392
400
}
393
401
@@ -402,6 +410,9 @@ func (e *TokensController) generateTokenIfNeeded(logger klog.Logger, serviceAcco
402
410
if needsCA {
403
411
liveSecret .Data [v1 .ServiceAccountRootCAKey ] = e .rootCA
404
412
}
413
+ if needsServiceServingCA {
414
+ liveSecret .Data [ServiceServingCASecretKey ] = e .serviceServingCA
415
+ }
405
416
// Set the namespace
406
417
if needsNamespace {
407
418
liveSecret .Data [v1 .ServiceAccountNamespaceKey ] = []byte (liveSecret .Namespace )
0 commit comments