@@ -18,6 +18,7 @@ package remote
18
18
19
19
import (
20
20
"context"
21
+ "crypto/rsa"
21
22
"fmt"
22
23
"os"
23
24
"sync"
@@ -48,6 +49,7 @@ import (
48
49
49
50
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
50
51
"sigs.k8s.io/cluster-api/feature"
52
+ "sigs.k8s.io/cluster-api/util/certs"
51
53
"sigs.k8s.io/cluster-api/util/conditions"
52
54
)
53
55
@@ -166,12 +168,23 @@ func (t *ClusterCacheTracker) GetRESTConfig(ctc context.Context, cluster client.
166
168
return accessor .config , nil
167
169
}
168
170
171
+ // GetEtcdClientCertificateKey returns a cached certificate key to be used for generating certificates for accessing etcd in the given cluster.
172
+ func (t * ClusterCacheTracker ) GetEtcdClientCertificateKey (ctx context.Context , cluster client.ObjectKey ) (* rsa.PrivateKey , error ) {
173
+ accessor , err := t .getClusterAccessor (ctx , cluster , t .indexes ... )
174
+ if err != nil {
175
+ return nil , err
176
+ }
177
+
178
+ return accessor .etcdClientCertificateKey , nil
179
+ }
180
+
169
181
// clusterAccessor represents the combination of a delegating client, cache, and watches for a remote cluster.
170
182
type clusterAccessor struct {
171
- cache * stoppableCache
172
- client client.Client
173
- watches sets.Set [string ]
174
- config * rest.Config
183
+ cache * stoppableCache
184
+ client client.Client
185
+ watches sets.Set [string ]
186
+ config * rest.Config
187
+ etcdClientCertificateKey * rsa.PrivateKey
175
188
}
176
189
177
190
// clusterAccessorExists returns true if a clusterAccessor exists for cluster.
@@ -335,11 +348,20 @@ func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster cl
335
348
return nil , err
336
349
}
337
350
351
+ // Generating a new private key to be used for generating temporary certificates to connect to
352
+ // etcd on the target cluster.
353
+ // NOTE: Generating a private key is an expensive operation, so we store it in the cluster accessor.
354
+ etcdKey , err := certs .NewPrivateKey ()
355
+ if err != nil {
356
+ return nil , errors .Wrapf (err , "error creating etcd client key for remote cluster %q" , cluster .String ())
357
+ }
358
+
338
359
return & clusterAccessor {
339
- cache : cache ,
340
- config : config ,
341
- client : delegatingClient ,
342
- watches : sets.Set [string ]{},
360
+ cache : cache ,
361
+ config : config ,
362
+ client : delegatingClient ,
363
+ watches : sets.Set [string ]{},
364
+ etcdClientCertificateKey : etcdKey ,
343
365
}, nil
344
366
}
345
367
0 commit comments