Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,8 @@ public ConfigKey<?>[] getConfigKeys() {
KubernetesControlNodeInstallReattempts,
KubernetesWorkerNodeInstallAttemptWait,
KubernetesWorkerNodeInstallReattempts,
KubernetesEtcdNodeStartPort
KubernetesEtcdNodeStartPort,
KubernetesClusterExternalCloudProviderEnabled
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public interface KubernetesClusterService extends PluggableService, Configurable
"Start port for Port forwarding rules for etcd nodes",
true,
KubernetesServiceEnabled.key());
static final ConfigKey<Boolean> KubernetesClusterExternalCloudProviderEnabled = new ConfigKey<Boolean>("Advanced", Boolean.class,
"cloud.kubernetes.cluster.external.cloud.provider.enabled",
"false",
"Whether new Kubernetes clusters on isolated and VPC networks run their kubelets with --cloud-provider=external, so the CloudStack " +
"Kubernetes provider initialises the nodes. Needs a Kubernetes ISO whose provider manifest tolerates the uninitialized taint " +
"(v1.1.0 or later), and the provider must reach endpoint.url without cluster DNS, so use an IP address there or a provider " +
"manifest with hostNetwork. Otherwise nodes stay unschedulable. Only applies when a cluster is created.",
true,
ConfigKey.Scope.Zone,
KubernetesServiceEnabled.key());

KubernetesCluster findById(final Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public class KubernetesClusterActionWorker {
public static final String CKS_CLUSTER_SECURITY_GROUP_NAME = "CKSSecurityGroup";
public static final String CKS_SECURITY_GROUP_DESCRIPTION = "Security group for CKS nodes";
public static final String CKS_CONFIG_PATH = "/usr/share/cloudstack-management/cks";
// Only clusters created with the cloud controller manager wired up get the kubelet flag, so nodes
// added to older clusters are not left tainted waiting for a CCM that may never initialise them.
public static final String EXTERNAL_CLOUD_PROVIDER_DETAIL = "externalCloudProvider";
protected static final String KUBELET_CLOUD_PROVIDER_ARG_KEY = "{{ k8s.kubelet.cloud.provider.arg }}";
// Kubelets 1.29+ leave a node without an InternalIP until the CCM sets one unless --node-ip is given
protected static final String KUBELET_CLOUD_PROVIDER_ARGS = " --cloud-provider=external$(ip -4 route get 1.1.1.1 | awk '{for (i = 1; i < NF; i++) if ($i == \"src\") print \" --node-ip=\" $(i + 1)}')";

protected Logger logger = LogManager.getLogger(getClass());

Expand Down Expand Up @@ -782,6 +788,10 @@ protected boolean createCloudStackSecret(String[] keys) {
String projectId = projectService.findByProjectAccountId(account.getId()).getUuid();
command = String.format("%s -p '%s'", command, projectId);
}
DataCenterVO zone = dataCenterDao.findById(kubernetesCluster.getZoneId());
if (zone != null) {
command = String.format("%s -z '%s'", command, zone.getName().replace("'", "'\\''"));
}
Pair<Boolean, String> result = SshHelper.sshExecute(publicIpAddress, sshPort, getControlNodeLoginUser(),
pkFile, null, command, 10000, 10000, 60000);
return result.first();
Expand Down Expand Up @@ -876,6 +886,15 @@ protected boolean taintControlNodes() {
return false;
}

protected boolean isExternalCloudProviderEnabled() {
KubernetesClusterDetailsVO detail = kubernetesClusterDetailsDao.findDetail(kubernetesCluster.getId(), EXTERNAL_CLOUD_PROVIDER_DETAIL);
return detail != null && Boolean.parseBoolean(detail.getValue());
}

protected String replaceKubeletCloudProviderArg(String k8sConfig) {
return k8sConfig.replace(KUBELET_CLOUD_PROVIDER_ARG_KEY, isExternalCloudProviderEnabled() ? KUBELET_CLOUD_PROVIDER_ARGS : "");
}

protected boolean deployProvider() {
Network network = networkDao.findById(kubernetesCluster.getNetworkId());
// Since the provider creates IP addresses, don't deploy it unless the underlying network supports it
Expand All @@ -901,8 +920,7 @@ protected boolean deployProvider() {
copyScripts(publicIpAddress, sshPort);

if (!createCloudStackSecret(keys)) {
logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup keys for Kubernetes cluster %s",
kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
throw new CloudRuntimeException(String.format("Failed to setup keys for Kubernetes cluster %s", kubernetesCluster.getName()));
}

// If at first you don't succeed ...
Expand All @@ -915,7 +933,7 @@ protected boolean deployProvider() {
return true;
} catch (Exception e) {
String msg = String.format("Failed to deploy kubernetes provider: %s : %s", kubernetesCluster.getName(), e.getMessage());
logAndThrow(Level.ERROR, msg);
logAndThrow(Level.ERROR, msg, e);
return false;
}
}
Expand Down Expand Up @@ -1051,6 +1069,7 @@ public String getKubernetesNodeConfig(final String joinIp, final boolean ejectIs
k8sNodeConfig = k8sNodeConfig.replace(routerIpKey, routerIp);
k8sNodeConfig = k8sNodeConfig.replace(installWaitTime, String.valueOf(waitTime));
k8sNodeConfig = k8sNodeConfig.replace(installReattemptsCount, String.valueOf(reattempts));
k8sNodeConfig = replaceKubeletCloudProviderArg(k8sNodeConfig);

k8sNodeConfig = updateKubeConfigWithRegistryDetails(k8sNodeConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private Pair<String, String> getKubernetesControlNodeConfig(final String control
k8sControlNodeConfig = k8sControlNodeConfig.replace(k8sCertificate, KubernetesClusterUtil.generateClusterHACertificateKey(kubernetesCluster));
k8sControlNodeConfig = k8sControlNodeConfig.replace(externalCniPlugin, String.valueOf(externalCni));
k8sControlNodeConfig = k8sControlNodeConfig.replace(setupCsiDriver, String.valueOf(setupCsi));
k8sControlNodeConfig = replaceKubeletCloudProviderArg(k8sControlNodeConfig);

k8sControlNodeConfig = updateKubeConfigWithRegistryDetails(k8sControlNodeConfig);

Expand Down Expand Up @@ -356,6 +357,7 @@ private String getKubernetesAdditionalControlNodeConfig(final String joinIp, fin
k8sControlNodeConfig = k8sControlNodeConfig.replace(clusterTokenKey, KubernetesClusterUtil.generateClusterToken(kubernetesCluster));
k8sControlNodeConfig = k8sControlNodeConfig.replace(clusterHACertificateKey, KubernetesClusterUtil.generateClusterHACertificateKey(kubernetesCluster));
k8sControlNodeConfig = k8sControlNodeConfig.replace(ejectIsoKey, String.valueOf(ejectIso));
k8sControlNodeConfig = replaceKubeletCloudProviderArg(k8sControlNodeConfig);
k8sControlNodeConfig = updateKubeConfigWithRegistryDetails(k8sControlNodeConfig);

return k8sControlNodeConfig;
Expand Down Expand Up @@ -789,6 +791,9 @@ public boolean startKubernetesClusterOnCreate(Long domainId, Long accountId, Lon
} catch (ManagementServerException e) {
logTransitStateAndThrow(Level.ERROR, String.format("Failed to start Kubernetes cluster : %s as its network cannot be started", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.CreateFailed, e);
}
if (!manager.isDirectAccess(network) && KubernetesClusterService.KubernetesClusterExternalCloudProviderEnabled.valueIn(kubernetesCluster.getZoneId())) {
kubernetesClusterDetailsDao.addDetail(kubernetesCluster.getId(), EXTERNAL_CLOUD_PROVIDER_DETAIL, "true", false);
}
Pair<String, Integer> publicIpSshPort = new Pair<>(null, null);
try {
publicIpSshPort = getKubernetesClusterServerIpSshPort(null, true);
Expand Down Expand Up @@ -886,11 +891,15 @@ public boolean startKubernetesClusterOnCreate(Long domainId, Long accountId, Lon
if (!isKubernetesClusterKubeConfigAvailable(startTimeoutTime)) {
logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to retrieve kube-config for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
}
try {
deployProvider();
} catch (RuntimeException e) {
logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s as unable to deploy the CloudStack Kubernetes provider", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed, e);
}
if (!isKubernetesClusterDashboardServiceRunning(true, startTimeoutTime)) {
logTransitStateAndThrow(Level.ERROR, String.format("Failed to setup Kubernetes cluster : %s in usable state as unable to get Dashboard service running for the cluster", kubernetesCluster.getName()), kubernetesCluster.getId(),KubernetesCluster.Event.OperationFailed);
}
taintControlNodes();
deployProvider();
if (kubernetesCluster.isCsiEnabled()) {
deployCsiDriver();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ write_files:
mkdir -p /etc/systemd/system/kubelet.service.d
sed "s:/usr/bin:/opt/bin:g" ${BINARIES_DIR}/10-kubeadm.conf > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/default/kubelet
echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd{{ k8s.kubelet.cloud.provider.arg }}" > /etc/default/kubelet

output=`ls ${BINARIES_DIR}/docker/`
if [ "$output" != "" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ write_files:
mkdir -p /etc/systemd/system/kubelet.service.d
sed "s:/usr/bin:/opt/bin:g" ${BINARIES_DIR}/10-kubeadm.conf > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/default/kubelet
echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd{{ k8s.kubelet.cloud.provider.arg }}" > /etc/default/kubelet

output=`ls ${BINARIES_DIR}/docker/`
if [ "$output" != "" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ write_files:
mkdir -p /etc/systemd/system/kubelet.service.d
sed "s:/usr/bin:/opt/bin:g" ${BINARIES_DIR}/10-kubeadm.conf > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/default/kubelet
echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd{{ k8s.kubelet.cloud.provider.arg }}" > /etc/default/kubelet

output=`ls ${BINARIES_DIR}/docker/`
if [ "$output" != "" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Arguments:
-k, --key string API Key
-s, --secret string Secret Key
-p, --project string Project ID
-z, --zone string Zone name
Other arguments:
-h, --help Display this help message and exit
Examples:
Expand All @@ -37,6 +38,7 @@ API_KEY=""
SECRET_KEY=""
PROJECT_ID=""
PROJECT=""
ZONE=""
while [ -n "$1" ]; do
case "$1" in
-h | --help)
Expand All @@ -59,6 +61,11 @@ while [ -n "$1" ]; do
shift 2
PROJECT="project-id = $PROJECT_ID"
;;
-z | --zone)
ZONE_NAME=${2//\\/\\\\}
ZONE="zone = \"${ZONE_NAME//\"/\\\"}\""
shift 2
;;
-*|*)
echo "ERROR: no such option $1. -h or --help for help"
exit 1
Expand All @@ -71,6 +78,7 @@ api-url = $API_URL
api-key = $API_KEY
secret-key = $SECRET_KEY
$PROJECT
$ZONE
EOF

# Create secret if not exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ if [ -e /opt/provider/provider.yaml ]; then
exit 0
else
mkdir -p /opt/provider
PROVIDER_URL="https://raw.githubusercontent.com/apache/cloudstack-kubernetes-provider/main/deployment.yaml"
PROVIDER_VERSION="v1.3.0"
PROVIDER_URL="https://raw.githubusercontent.com/apache/cloudstack-kubernetes-provider/${PROVIDER_VERSION}/deployment.yaml"
PROVIDER_IMAGE="apache/cloudstack-kubernetes-provider:${PROVIDER_VERSION}"
provider_conf_file="/opt/provider/provider.yaml"
curl -sSL ${PROVIDER_URL} -o ${provider_conf_file}
if [ $? -ne 0 ]; then
provider_tmp_file="${provider_conf_file}.tmp.$$"
trap 'rm -f "$provider_tmp_file"' EXIT
if ! curl -fsSL "$PROVIDER_URL" -o "$provider_tmp_file"; then
echo "Unable to connect to the internet to download the provider deployment and image"
exit 1
else
/opt/bin/kubectl apply -f /opt/provider/provider.yaml
exit 0
fi
# The v1.3.0 release manifest still references the v1.2.0 image
sed -i "s|image: apache/cloudstack-kubernetes-provider:.*|image: ${PROVIDER_IMAGE}|" "$provider_tmp_file"
if ! grep -qF "image: ${PROVIDER_IMAGE}" "$provider_tmp_file"; then
echo "The provider deployment does not reference ${PROVIDER_IMAGE}"
exit 1
fi
mv -f "$provider_tmp_file" "$provider_conf_file"
/opt/bin/kubectl apply -f /opt/provider/provider.yaml
exit 0
fi
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,32 @@ public void testGetVpcTierKubernetesPublicIpValid() {
IpAddress result = actionWorker.getVpcTierKubernetesPublicIp(mockNetworkForGetVpcTierKubernetesPublicIpTest());
Assert.assertNotNull(result);
}

private void mockExternalCloudProviderDetail(String value) {
KubernetesClusterDetailsVO detail = value == null ? null :
new KubernetesClusterDetailsVO(DEFAULT_ID, KubernetesClusterActionWorker.EXTERNAL_CLOUD_PROVIDER_DETAIL, value, false);
Mockito.when(kubernetesClusterDetailsDao.findDetail(DEFAULT_ID, KubernetesClusterActionWorker.EXTERNAL_CLOUD_PROVIDER_DETAIL)).thenReturn(detail);
}

@Test
public void testReplaceKubeletCloudProviderArgEnabled() {
mockExternalCloudProviderDetail("true");
String config = "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" + KubernetesClusterActionWorker.KUBELET_CLOUD_PROVIDER_ARG_KEY;
String result = actionWorker.replaceKubeletCloudProviderArg(config);
Assert.assertTrue(result.startsWith("KUBELET_EXTRA_ARGS=--cgroup-driver=systemd --cloud-provider=external"));
Assert.assertFalse(result.contains("{{"));
}

@Test
public void testReplaceKubeletCloudProviderArgClusterWithoutDetail() {
mockExternalCloudProviderDetail(null);
String config = "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" + KubernetesClusterActionWorker.KUBELET_CLOUD_PROVIDER_ARG_KEY;
Assert.assertEquals("KUBELET_EXTRA_ARGS=--cgroup-driver=systemd", actionWorker.replaceKubeletCloudProviderArg(config));
}

@Test
public void testIsExternalCloudProviderEnabledFalseValue() {
mockExternalCloudProviderDetail("false");
Assert.assertFalse(actionWorker.isExternalCloudProviderEnabled());
}
}
9 changes: 7 additions & 2 deletions scripts/util/create-kubernetes-binaries-iso.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ echo "Downloading kubernetes cluster autoscaler ${AUTOSCALER_URL}"
autoscaler_conf_file="${working_dir}/autoscaler.yaml"
curl -sSL ${AUTOSCALER_URL} -o ${autoscaler_conf_file}

PROVIDER_URL="https://raw.githubusercontent.com/apache/cloudstack-kubernetes-provider/main/deployment.yaml"
PROVIDER_VERSION="v1.3.0"
PROVIDER_URL="https://raw.githubusercontent.com/apache/cloudstack-kubernetes-provider/${PROVIDER_VERSION}/deployment.yaml"
PROVIDER_IMAGE="apache/cloudstack-kubernetes-provider:${PROVIDER_VERSION}"
echo "Downloading kubernetes cluster provider ${PROVIDER_URL}"
provider_conf_file="${working_dir}/provider.yaml"
curl -sSL ${PROVIDER_URL} -o ${provider_conf_file}
curl -fsSL ${PROVIDER_URL} -o ${provider_conf_file}
# The v1.3.0 release manifest still references the v1.2.0 image
sed -i "s|image: apache/cloudstack-kubernetes-provider:.*|image: ${PROVIDER_IMAGE}|" ${provider_conf_file}
grep -q "image: ${PROVIDER_IMAGE}" ${provider_conf_file}

csi_conf_file="${working_dir}/manifest.yaml"
echo "Including CloudStack CSI Driver manifest"
Expand Down
Loading