Skip to content

Commit 8dac16a

Browse files
committed
Fix CKS HA clusters
1 parent 0ba729c commit 8dac16a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterStartWorker.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ private Pair<String, String> getKubernetesControlNodeConfig(final String control
161161
final String certSans = "{{ k8s_control.server_ips }}";
162162
final String k8sCertificate = "{{ k8s_control.certificate_key }}";
163163
final String externalCniPlugin = "{{ k8s.external.cni.plugin }}";
164+
final String isHaCluster = "{{ k8s.ha.cluster }}";
165+
final String publicIP = "{{ k8s.public.ip }}";
164166

165167
final List<String> addresses = new ArrayList<>();
166168
addresses.add(controlNodeIp);
@@ -202,7 +204,7 @@ private Pair<String, String> getKubernetesControlNodeConfig(final String control
202204
CLUSTER_API_PORT,
203205
KubernetesClusterUtil.generateClusterHACertificateKey(kubernetesCluster));
204206
}
205-
initArgs += String.format("--apiserver-cert-extra-sans=%s", controlNodeIp);
207+
initArgs += String.format("--apiserver-cert-extra-sans=%s", String.join(",", addresses));
206208
initArgs += String.format(" --kubernetes-version=%s", getKubernetesClusterVersion().getSemanticVersion());
207209
k8sControlNodeConfig = k8sControlNodeConfig.replace(clusterInitArgsKey, initArgs);
208210
k8sControlNodeConfig = k8sControlNodeConfig.replace(ejectIsoKey, String.valueOf(ejectIso));
@@ -212,6 +214,8 @@ private Pair<String, String> getKubernetesControlNodeConfig(final String control
212214
k8sControlNodeConfig = k8sControlNodeConfig.replace(certSans, String.format("- %s", serverIp));
213215
k8sControlNodeConfig = k8sControlNodeConfig.replace(k8sCertificate, KubernetesClusterUtil.generateClusterHACertificateKey(kubernetesCluster));
214216
k8sControlNodeConfig = k8sControlNodeConfig.replace(externalCniPlugin, String.valueOf(externalCni));
217+
k8sControlNodeConfig = k8sControlNodeConfig.replace(isHaCluster, String.valueOf(kubernetesCluster.getControlNodeCount() > 1));
218+
k8sControlNodeConfig = k8sControlNodeConfig.replace(publicIP, publicIpAddress);
215219

216220
k8sControlNodeConfig = updateKubeConfigWithRegistryDetails(k8sControlNodeConfig);
217221

@@ -309,6 +313,8 @@ private String getKubernetesAdditionalControlNodeConfig(final String joinIp, fin
309313
final String ejectIsoKey = "{{ k8s.eject.iso }}";
310314
final String installWaitTime = "{{ k8s.install.wait.time }}";
311315
final String installReattemptsCount = "{{ k8s.install.reattempts.count }}";
316+
final String isHaCluster = "{{ k8s.ha.cluster }}";
317+
final String publicIP = "{{ k8s.public.ip }}";
312318

313319
final Long waitTime = KubernetesClusterService.KubernetesControlNodeInstallAttemptWait.value();
314320
final Long reattempts = KubernetesClusterService.KubernetesControlNodeInstallReattempts.value();
@@ -328,6 +334,8 @@ private String getKubernetesAdditionalControlNodeConfig(final String joinIp, fin
328334
k8sControlNodeConfig = k8sControlNodeConfig.replace(clusterTokenKey, KubernetesClusterUtil.generateClusterToken(kubernetesCluster));
329335
k8sControlNodeConfig = k8sControlNodeConfig.replace(clusterHACertificateKey, KubernetesClusterUtil.generateClusterHACertificateKey(kubernetesCluster));
330336
k8sControlNodeConfig = k8sControlNodeConfig.replace(ejectIsoKey, String.valueOf(ejectIso));
337+
k8sControlNodeConfig = k8sControlNodeConfig.replace(isHaCluster, String.valueOf(kubernetesCluster.getControlNodeCount() > 1));
338+
k8sControlNodeConfig = k8sControlNodeConfig.replace(publicIP, publicIpAddress);
331339
k8sControlNodeConfig = updateKubeConfigWithRegistryDetails(k8sControlNodeConfig);
332340

333341
return k8sControlNodeConfig;
@@ -426,7 +434,7 @@ private UserVm createKubernetesAdditionalControlNode(final String joinIp, final
426434
String hostName = String.format("%s-control-%s", kubernetesClusterNodeNamePrefix, suffix);
427435
String k8sControlNodeConfig = null;
428436
try {
429-
k8sControlNodeConfig = getKubernetesAdditionalControlNodeConfig(joinIp, Hypervisor.HypervisorType.VMware.equals(clusterTemplate.getHypervisorType()));
437+
k8sControlNodeConfig = getKubernetesAdditionalControlNodeConfig(publicIpAddress, Hypervisor.HypervisorType.VMware.equals(clusterTemplate.getHypervisorType()));
430438
} catch (IOException e) {
431439
logAndThrow(Level.ERROR, "Failed to read Kubernetes control configuration file", e);
432440
}

plugins/integrations/kubernetes-service/src/main/resources/conf/k8s-control-node-add.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ write_files:
225225
exit 0
226226
fi
227227
228+
HA_CLUSTER={{ k8s.ha.cluster }}
229+
CLUSTER_PUBLIC_IP={{ k8s.public.ip }}
230+
228231
if [[ $(systemctl is-active setup-kube-system) != "inactive" ]]; then
229232
echo "setup-kube-system is running!"
230233
exit 1
@@ -242,6 +245,10 @@ write_files:
242245
cp -i /etc/kubernetes/admin.conf /root/.kube/config
243246
chown $(id -u):$(id -g) /root/.kube/config
244247
248+
if [[ "$HA_CLUSTER" == "true" ]]; then
249+
sed -i -E "s|(server:\\s*).*|\\1https://${CLUSTER_PUBLIC_IP}:6443|" /root/.kube/config
250+
fi
251+
245252
sudo touch /home/cloud/success
246253
echo "true" > /home/cloud/success
247254

plugins/integrations/kubernetes-service/src/main/resources/conf/k8s-control-node.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ write_files:
275275
echo "Already provisioned!"
276276
exit 0
277277
fi
278+
279+
HA_CLUSTER={{ k8s.ha.cluster }}
280+
CLUSTER_PUBLIC_IP={{ k8s.public.ip }}
278281
279282
if [[ "$PATH" != *:/opt/bin && "$PATH" != *:/opt/bin:* ]]; then
280283
export PATH=$PATH:/opt/bin
@@ -319,6 +322,10 @@ write_files:
319322
cp -i /etc/kubernetes/admin.conf /root/.kube/config
320323
chown $(id -u):$(id -g) /root/.kube/config
321324
echo export PATH=\$PATH:/opt/bin >> /root/.bashrc
325+
326+
if [[ "$HA_CLUSTER" == "true" ]]; then
327+
sed -i -E "s|(server:\\s*).*|\\1https://${CLUSTER_PUBLIC_IP}:6443|" /root/.kube/config
328+
fi
322329
323330
if [ -d "$K8S_CONFIG_SCRIPTS_COPY_DIR" ]; then
324331
### Network, dashboard configs available offline ###

0 commit comments

Comments
 (0)