@@ -43,6 +43,8 @@ import (
43
43
"k8s.io/kubernetes/pkg/serviceaccount"
44
44
)
45
45
46
+ const ServiceServingCASecretKey = "service-ca.crt"
47
+
46
48
// RemoveTokenBackoff is the recommended (empirical) retry interval for removing
47
49
// a secret reference from a service account when the secret is deleted. It is
48
50
// exported for use by custom secret controllers.
@@ -71,6 +73,9 @@ type TokensControllerOptions struct {
71
73
72
74
// AutoGenerate decides the auto-generation of secret-based token for service accounts.
73
75
AutoGenerate bool
76
+
77
+ // This CA will be added in the secrets of service accounts
78
+ ServiceServingCA []byte
74
79
}
75
80
76
81
// NewTokensController returns a new *TokensController.
@@ -81,9 +86,10 @@ func NewTokensController(serviceAccounts informers.ServiceAccountInformer, secre
81
86
}
82
87
83
88
e := & TokensController {
84
- client : cl ,
85
- token : options .TokenGenerator ,
86
- rootCA : options .RootCA ,
89
+ client : cl ,
90
+ token : options .TokenGenerator ,
91
+ rootCA : options .RootCA ,
92
+ serviceServingCA : options .ServiceServingCA ,
87
93
88
94
syncServiceAccountQueue : workqueue .NewNamedRateLimitingQueue (workqueue .DefaultControllerRateLimiter (), "serviceaccount_tokens_service" ),
89
95
syncSecretQueue : workqueue .NewNamedRateLimitingQueue (workqueue .DefaultControllerRateLimiter (), "serviceaccount_tokens_secret" ),
@@ -139,7 +145,8 @@ type TokensController struct {
139
145
client clientset.Interface
140
146
token serviceaccount.TokenGenerator
141
147
142
- rootCA []byte
148
+ rootCA []byte
149
+ serviceServingCA []byte
143
150
144
151
serviceAccounts listersv1.ServiceAccountLister
145
152
// updatedSecrets is a wrapper around the shared cache which allows us to record
@@ -411,6 +418,9 @@ func (e *TokensController) ensureReferencedToken(serviceAccount *v1.ServiceAccou
411
418
if e .rootCA != nil && len (e .rootCA ) > 0 {
412
419
secret .Data [v1 .ServiceAccountRootCAKey ] = e .rootCA
413
420
}
421
+ if e .serviceServingCA != nil && len (e .serviceServingCA ) > 0 {
422
+ secret .Data [ServiceServingCASecretKey ] = e .serviceServingCA
423
+ }
414
424
415
425
// Save the secret
416
426
createdToken , err := e .client .CoreV1 ().Secrets (serviceAccount .Namespace ).Create (context .TODO (), secret , metav1.CreateOptions {})
@@ -504,22 +514,23 @@ func (e *TokensController) hasReferencedToken(serviceAccount *v1.ServiceAccount)
504
514
return false , nil
505
515
}
506
516
507
- func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool ) {
517
+ func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool , bool ) {
508
518
caData := secret .Data [v1 .ServiceAccountRootCAKey ]
509
519
needsCA := len (e .rootCA ) > 0 && ! bytes .Equal (caData , e .rootCA )
520
+ needsServiceServingCA := len (e .serviceServingCA ) > 0 && bytes .Compare (secret .Data [ServiceServingCASecretKey ], e .serviceServingCA ) != 0
510
521
511
522
needsNamespace := len (secret .Data [v1 .ServiceAccountNamespaceKey ]) == 0
512
523
513
524
tokenData := secret .Data [v1 .ServiceAccountTokenKey ]
514
525
needsToken := len (tokenData ) == 0
515
526
516
- return needsCA , needsNamespace , needsToken
527
+ return needsCA , needsServiceServingCA , needsNamespace , needsToken
517
528
}
518
529
519
530
// generateTokenIfNeeded populates the token data for the given Secret if not already set
520
531
func (e * TokensController ) generateTokenIfNeeded (serviceAccount * v1.ServiceAccount , cachedSecret * v1.Secret ) ( /* retry */ bool , error ) {
521
532
// Check the cached secret to see if changes are needed
522
- if needsCA , needsNamespace , needsToken := e .secretUpdateNeeded (cachedSecret ); ! needsCA && ! needsToken && ! needsNamespace {
533
+ if needsCA , needsServiceServingCA , needsNamespace , needsToken := e .secretUpdateNeeded (cachedSecret ); ! needsCA && ! needsServiceServingCA && ! needsToken && ! needsNamespace {
523
534
return false , nil
524
535
}
525
536
@@ -538,8 +549,8 @@ func (e *TokensController) generateTokenIfNeeded(serviceAccount *v1.ServiceAccou
538
549
return false , nil
539
550
}
540
551
541
- needsCA , needsNamespace , needsToken := e .secretUpdateNeeded (liveSecret )
542
- if ! needsCA && ! needsToken && ! needsNamespace {
552
+ needsCA , needsServiceServingCA , needsNamespace , needsToken := e .secretUpdateNeeded (liveSecret )
553
+ if ! needsCA && ! needsServiceServingCA && ! needsToken && ! needsNamespace {
543
554
return false , nil
544
555
}
545
556
@@ -554,6 +565,9 @@ func (e *TokensController) generateTokenIfNeeded(serviceAccount *v1.ServiceAccou
554
565
if needsCA {
555
566
liveSecret .Data [v1 .ServiceAccountRootCAKey ] = e .rootCA
556
567
}
568
+ if needsServiceServingCA {
569
+ liveSecret .Data [ServiceServingCASecretKey ] = e .serviceServingCA
570
+ }
557
571
// Set the namespace
558
572
if needsNamespace {
559
573
liveSecret .Data [v1 .ServiceAccountNamespaceKey ] = []byte (liveSecret .Namespace )
0 commit comments