Skip to content

Commit 4df4094

Browse files
author
qliang
committed
let etcdclient use GetRESTConfigFromSecret to get kubeconfig
1 parent 4852b67 commit 4df4094

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

controllers/clustercache/cluster_accessor.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,19 @@ func (ca *clusterAccessor) GetRESTConfig(ctx context.Context) (*rest.Config, err
414414
return ca.lockedState.connection.restConfig, nil
415415
}
416416

417+
func (ca *clusterAccessor) GetRESTConfigFromSecret(ctx context.Context) (*rest.Config, error) {
418+
ca.rLock(ctx)
419+
defer ca.rUnlock(ctx)
420+
421+
log := ctrl.LoggerFrom(ctx)
422+
log.V(6).Info("Creating REST config")
423+
restConfig, err := createRESTConfig(ctx, ca.config.Client, ca.config.SecretClient, ca.cluster)
424+
if err != nil {
425+
return nil, err
426+
}
427+
return restConfig, nil
428+
}
429+
417430
func (ca *clusterAccessor) GetClientCertificatePrivateKey(ctx context.Context) *rsa.PrivateKey {
418431
ca.rLock(ctx)
419432
defer ca.rUnlock(ctx)

controllers/clustercache/cluster_cache.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ type ClusterCache interface {
137137
// If there is no connection to the workload cluster ErrClusterNotConnected will be returned.
138138
GetRESTConfig(ctx context.Context, cluster client.ObjectKey) (*rest.Config, error)
139139

140+
// GetRESTConfigFromSecret returns a REST config for the given cluster.
141+
// It reads the REST config from kubeconfig secret directly.
142+
GetRESTConfigFromSecret(ctx context.Context, cluster client.ObjectKey) (*rest.Config, error)
143+
140144
// GetClientCertificatePrivateKey returns a private key that is generated once for a cluster
141145
// and can then be used to generate client certificates. This is e.g. used in KCP to generate a client
142146
// cert to communicate with etcd.
@@ -384,6 +388,14 @@ func (cc *clusterCache) GetRESTConfig(ctx context.Context, cluster client.Object
384388
return accessor.GetRESTConfig(ctx)
385389
}
386390

391+
func (cc *clusterCache) GetRESTConfigFromSecret(ctx context.Context, cluster client.ObjectKey) (*rest.Config, error) {
392+
accessor := cc.getClusterAccessor(cluster)
393+
if accessor == nil {
394+
return nil, errors.Wrapf(ErrClusterNotConnected, "error getting REST config")
395+
}
396+
return accessor.GetRESTConfigFromSecret(ctx)
397+
}
398+
387399
func (cc *clusterCache) GetClientCertificatePrivateKey(ctx context.Context, cluster client.ObjectKey) (*rsa.PrivateKey, error) {
388400
accessor := cc.getClusterAccessor(cluster)
389401
if accessor == nil {

controlplane/kubeadm/internal/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (m *Management) GetMachinePoolsForCluster(ctx context.Context, cluster *clu
9999
func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.ObjectKey) (WorkloadCluster, error) {
100100
// TODO(chuckha): Inject this dependency.
101101
// TODO(chuckha): memoize this function. The workload client only exists as long as a reconciliation loop.
102-
restConfig, err := m.ClusterCache.GetRESTConfig(ctx, clusterKey)
102+
restConfig, err := m.ClusterCache.GetRESTConfigFromSecret(ctx, clusterKey)
103103
if err != nil {
104104
return nil, &RemoteClusterConnectionError{Name: clusterKey.String(), Err: err}
105105
}

0 commit comments

Comments
 (0)