@@ -41,6 +41,8 @@ import (
4141 "k8s.io/kubernetes/pkg/serviceaccount"
4242)
4343
44+ const ServiceServingCASecretKey = "service-ca.crt"
45+
4446// RemoveTokenBackoff is the recommended (empirical) retry interval for removing
4547// a secret reference from a service account when the secret is deleted. It is
4648// exported for use by custom secret controllers.
@@ -66,6 +68,9 @@ type TokensControllerOptions struct {
6668 // MaxRetries controls the maximum number of times a particular key is retried before giving up
6769 // If zero, a default max is used
6870 MaxRetries int
71+
72+ // This CA will be added in the secrets of service accounts
73+ ServiceServingCA []byte
6974}
7075
7176// NewTokensController returns a new *TokensController.
@@ -76,9 +81,10 @@ func NewTokensController(serviceAccounts informers.ServiceAccountInformer, secre
7681 }
7782
7883 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 ,
8288
8389 syncServiceAccountQueue : workqueue .NewTypedRateLimitingQueueWithConfig (
8490 workqueue .DefaultTypedControllerRateLimiter [serviceAccountQueueKey ](),
@@ -134,7 +140,8 @@ type TokensController struct {
134140 client clientset.Interface
135141 token serviceaccount.TokenGenerator
136142
137- rootCA []byte
143+ rootCA []byte
144+ serviceServingCA []byte
138145
139146 serviceAccounts listersv1.ServiceAccountLister
140147 // 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
352359 return true , err
353360}
354361
355- func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool ) {
362+ func (e * TokensController ) secretUpdateNeeded (secret * v1.Secret ) (bool , bool , bool , bool ) {
356363 caData := secret .Data [v1 .ServiceAccountRootCAKey ]
357364 needsCA := len (e .rootCA ) > 0 && ! bytes .Equal (caData , e .rootCA )
365+ needsServiceServingCA := len (e .serviceServingCA ) > 0 && bytes .Compare (secret .Data [ServiceServingCASecretKey ], e .serviceServingCA ) != 0
358366
359367 needsNamespace := len (secret .Data [v1 .ServiceAccountNamespaceKey ]) == 0
360368
361369 tokenData := secret .Data [v1 .ServiceAccountTokenKey ]
362370 needsToken := len (tokenData ) == 0
363371
364- return needsCA , needsNamespace , needsToken
372+ return needsCA , needsServiceServingCA , needsNamespace , needsToken
365373}
366374
367375// generateTokenIfNeeded populates the token data for the given Secret if not already set
368376func (e * TokensController ) generateTokenIfNeeded (logger klog.Logger , serviceAccount * v1.ServiceAccount , cachedSecret * v1.Secret ) ( /* retry */ bool , error ) {
369377 // 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 {
371379 return false , nil
372380 }
373381
@@ -386,8 +394,8 @@ func (e *TokensController) generateTokenIfNeeded(logger klog.Logger, serviceAcco
386394 return false , nil
387395 }
388396
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 {
391399 return false , nil
392400 }
393401
@@ -402,6 +410,9 @@ func (e *TokensController) generateTokenIfNeeded(logger klog.Logger, serviceAcco
402410 if needsCA {
403411 liveSecret .Data [v1 .ServiceAccountRootCAKey ] = e .rootCA
404412 }
413+ if needsServiceServingCA {
414+ liveSecret .Data [ServiceServingCASecretKey ] = e .serviceServingCA
415+ }
405416 // Set the namespace
406417 if needsNamespace {
407418 liveSecret .Data [v1 .ServiceAccountNamespaceKey ] = []byte (liveSecret .Namespace )
0 commit comments