Skip to content

Commit 579140b

Browse files
committed
feat: priority to reuse apiserver-etcd-client if present
1 parent af7e245 commit 579140b

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

controlplane/kubeadm/internal/cluster.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,9 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.O
119119
return nil, err
120120
}
121121

122-
// If the CA key is defined, the cluster is using a managed etcd, and so we can generate a new
123-
// etcd client certificate for the controllers.
124-
// Otherwise the cluster is using an external etcd; in this case the only option to connect to etcd is to re-use
125-
// the apiserver-etcd-client certificate.
126-
// 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)
127-
var clientCert tls.Certificate
128-
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
137-
}
138-
} else {
139-
clientCert, err = m.getAPIServerEtcdClientCert(ctx, clusterKey)
140-
if err != nil {
141-
return nil, err
142-
}
122+
clientCert, err := m.getOrGenerateEtcdClientCert(ctx, clusterKey, crtData, keyData)
123+
if err != nil {
124+
return nil, err
143125
}
144126

145127
caPool := x509.NewCertPool()
@@ -158,6 +140,32 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.O
158140
}, nil
159141
}
160142

143+
func (m *Management) getOrGenerateEtcdClientCert(ctx context.Context, clusterKey client.ObjectKey, crtData, keyData []byte) (tls.Certificate, error) {
144+
// If the apiserver-etcd-client doesn't exist, we try to generate a new
145+
// etcd client certificate for the controllers.
146+
// TODO: consider if we can detect if we are in external etcd mode or in external ca in a more explicit way (e.g. looking at the config instead of deriving from the existing certificates)
147+
148+
clientCert, err := m.getAPIServerEtcdClientCert(ctx, clusterKey)
149+
if err == nil {
150+
return clientCert, nil
151+
}
152+
153+
if !apierrors.IsNotFound(err) {
154+
return tls.Certificate{}, err
155+
}
156+
157+
if keyData == nil {
158+
return tls.Certificate{}, fmt.Errorf("missing keyData in etcd CA bundle %s/%s", clusterKey.Namespace, fmt.Sprintf("%s-etcd", clusterKey.Name))
159+
}
160+
161+
clientKey, err := m.ClusterCache.GetClientCertificatePrivateKey(ctx, clusterKey)
162+
if err != nil {
163+
return tls.Certificate{}, err
164+
}
165+
166+
return generateClientCert(crtData, keyData, clientKey)
167+
}
168+
161169
func (m *Management) getEtcdCAKeyPair(ctx context.Context, clusterKey client.ObjectKey) ([]byte, []byte, error) {
162170
etcdCASecret := &corev1.Secret{}
163171
etcdCAObjectKey := client.ObjectKey{

0 commit comments

Comments
 (0)