@@ -32,6 +32,7 @@ import (
3232
3333 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3434 "sigs.k8s.io/cluster-api/controllers/clustercache"
35+ "sigs.k8s.io/cluster-api/util/cache"
3536 "sigs.k8s.io/cluster-api/util/collections"
3637 "sigs.k8s.io/cluster-api/util/secret"
3738)
@@ -53,6 +54,18 @@ type Management struct {
5354 EtcdDialTimeout time.Duration
5455 EtcdCallTimeout time.Duration
5556 EtcdLogger * zap.Logger
57+ ClientCertCache cache.Cache [ClientCertEntry ]
58+ }
59+
60+ // ClientCertEntry is an Entry for the Cache that stores the client cert.
61+ type ClientCertEntry struct {
62+ Cluster client.ObjectKey
63+ ClientCert * tls.Certificate
64+ }
65+
66+ // Key returns the cache key of a ClientCertEntry.
67+ func (r ClientCertEntry ) Key () string {
68+ return r .Cluster .String ()
5669}
5770
5871// RemoteClusterConnectionError represents a failure to connect to a remote cluster.
@@ -126,14 +139,18 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.O
126139 // TODO: consider if we can detect if we are using external etcd in a more explicit way (e.g. looking at the config instead of deriving from the existing certificates)
127140 var clientCert tls.Certificate
128141 if keyData != nil {
129- clientKey , err := m .ClusterCache .GetClientCertificatePrivateKey (ctx , clusterKey )
130- if err != nil {
131- return nil , err
132- }
133-
134- clientCert , err = generateClientCert (crtData , keyData , clientKey )
135- if err != nil {
136- return nil , err
142+ // Get client cert from cache if possible, otherwise generate it and add it to the cache.
143+ // TODO: When we implement ClusterConfiguration.EncryptionAlgorithm we should add it to
144+ // the ClientCertEntries and make it part of the key.
145+ if entry , ok := m .ClientCertCache .Has (ClientCertEntry {Cluster : clusterKey }.Key ()); ok {
146+ clientCert = * entry .ClientCert
147+ } else {
148+ // The client cert expires after 10 years, but that's okay as the cache has a TTL of 1 day.
149+ clientCert , err = generateClientCert (crtData , keyData )
150+ if err != nil {
151+ return nil , err
152+ }
153+ m .ClientCertCache .Add (ClientCertEntry {Cluster : clusterKey , ClientCert : & clientCert })
137154 }
138155 } else {
139156 clientCert , err = m .getAPIServerEtcdClientCert (ctx , clusterKey )
0 commit comments