Skip to content

Commit d439765

Browse files
committed
feat: priority to reuse apiserver-etcd-client if present
1 parent 93adf87 commit d439765

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

controlplane/kubeadm/internal/cluster.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,27 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, cluster *clusterv1.
137137
if err != nil {
138138
return nil, err
139139
}
140+
// If the apiserver-etcd-client certificate exists, use it.
141+
// Otherwise, try to generate a new etcd client certificate for the controllers.
142+
clientCert, err := m.getAPIServerEtcdClientCert(ctx, clusterKey)
143+
if !apierrors.IsNotFound(err) {
144+
return nil, err
145+
}
146+
if keyData == nil {
147+
return nil, fmt.Errorf("missing keyData in etcd CA bundle %s/%s and no apiserver-etcd-client certificate found", clusterKey.Namespace, fmt.Sprintf("%s-etcd", clusterKey.Name))
148+
}
140149

141-
// If the CA key is defined, the cluster is using a managed etcd, and so we can generate a new
142-
// etcd client certificate for the controllers.
143-
// Otherwise the cluster is using an external etcd; in this case the only option to connect to etcd is to re-use
144-
// the apiserver-etcd-client certificate.
145-
// 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)
146-
var clientCert tls.Certificate
147-
if keyData != nil {
148-
// Get client cert from cache if possible, otherwise generate it and add it to the cache.
149-
// Note: The caching assumes that the etcd CA is not rotated during the lifetime of a Cluster.
150-
if entry, ok := m.ClientCertCache.Has(ClientCertEntry{Cluster: clusterKey, ClusterUID: cluster.UID, EncryptionAlgorithm: keyEncryptionAlgorithm}.Key()); ok {
151-
clientCert = *entry.ClientCert
152-
} else {
153-
// The client cert expires after 10 years, but that's okay as the cache has a TTL of 1 day.
154-
clientCert, err = generateClientCert(crtData, keyData, keyEncryptionAlgorithm)
155-
if err != nil {
156-
return nil, err
157-
}
158-
m.ClientCertCache.Add(ClientCertEntry{Cluster: clusterKey, ClusterUID: cluster.UID, ClientCert: &clientCert, EncryptionAlgorithm: keyEncryptionAlgorithm})
159-
}
150+
// Get client cert from cache if possible, otherwise generate it and add it to the cache.
151+
// Note: The caching assumes that the etcd CA is not rotated during the lifetime of a Cluster.
152+
if entry, ok := m.ClientCertCache.Has(ClientCertEntry{Cluster: clusterKey, ClusterUID: cluster.UID, EncryptionAlgorithm: keyEncryptionAlgorithm}.Key()); ok {
153+
clientCert = *entry.ClientCert
160154
} else {
161-
clientCert, err = m.getAPIServerEtcdClientCert(ctx, clusterKey)
155+
// The client cert expires after 10 years, but that's okay as the cache has a TTL of 1 day.
156+
clientCert, err = generateClientCert(crtData, keyData, keyEncryptionAlgorithm)
162157
if err != nil {
163158
return nil, err
164159
}
160+
m.ClientCertCache.Add(ClientCertEntry{Cluster: clusterKey, ClusterUID: cluster.UID, ClientCert: &clientCert, EncryptionAlgorithm: keyEncryptionAlgorithm})
165161
}
166162

167163
caPool := x509.NewCertPool()

0 commit comments

Comments
 (0)