|
36 | 36 |
|
37 | 37 | import javax.inject.Inject; |
38 | 38 |
|
| 39 | +import com.cloud.kubernetes.cluster.KubernetesClusterDetailsVO; |
39 | 40 | import com.cloud.kubernetes.cluster.KubernetesClusterHelper.KubernetesClusterNodeType; |
| 41 | +import com.cloud.kubernetes.cluster.KubernetesClusterService; |
| 42 | +import com.cloud.kubernetes.cluster.utils.KubernetesClusterUtil; |
40 | 43 | import com.cloud.network.rules.FirewallManager; |
41 | 44 | import com.cloud.network.rules.RulesService; |
42 | 45 | import com.cloud.network.rules.dao.PortForwardingRulesDao; |
43 | 46 | import com.cloud.offering.NetworkOffering; |
44 | 47 | import com.cloud.offerings.dao.NetworkOfferingDao; |
| 48 | +import com.cloud.user.SSHKeyPairVO; |
45 | 49 | import com.cloud.utils.db.TransactionCallbackWithException; |
46 | 50 | import com.cloud.utils.net.Ip; |
| 51 | +import org.apache.cloudstack.api.ApiConstants; |
47 | 52 | import org.apache.cloudstack.api.BaseCmd; |
48 | 53 | import org.apache.cloudstack.api.command.user.firewall.CreateFirewallRuleCmd; |
49 | 54 | import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd; |
@@ -166,6 +171,79 @@ protected void init() { |
166 | 171 | kubernetesClusterNodeNamePrefix = getKubernetesClusterNodeNamePrefix(); |
167 | 172 | } |
168 | 173 |
|
| 174 | + private String getKubernetesNodeConfig(final String joinIp, final boolean ejectIso) throws IOException { |
| 175 | + String k8sNodeConfig = readResourceFile("/conf/k8s-node.yml"); |
| 176 | + final String sshPubKey = "{{ k8s.ssh.pub.key }}"; |
| 177 | + final String joinIpKey = "{{ k8s_control_node.join_ip }}"; |
| 178 | + final String clusterTokenKey = "{{ k8s_control_node.cluster.token }}"; |
| 179 | + final String ejectIsoKey = "{{ k8s.eject.iso }}"; |
| 180 | + final String installWaitTime = "{{ k8s.install.wait.time }}"; |
| 181 | + final String installReattemptsCount = "{{ k8s.install.reattempts.count }}"; |
| 182 | + |
| 183 | + final Long waitTime = KubernetesClusterService.KubernetesWorkerNodeInstallAttemptWait.value(); |
| 184 | + final Long reattempts = KubernetesClusterService.KubernetesWorkerNodeInstallReattempts.value(); |
| 185 | + String pubKey = "- \"" + configurationDao.getValue("ssh.publickey") + "\""; |
| 186 | + String sshKeyPair = kubernetesCluster.getKeyPair(); |
| 187 | + if (StringUtils.isNotEmpty(sshKeyPair)) { |
| 188 | + SSHKeyPairVO sshkp = sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), sshKeyPair); |
| 189 | + if (sshkp != null) { |
| 190 | + pubKey += "\n - \"" + sshkp.getPublicKey() + "\""; |
| 191 | + } |
| 192 | + } |
| 193 | + k8sNodeConfig = k8sNodeConfig.replace(sshPubKey, pubKey); |
| 194 | + k8sNodeConfig = k8sNodeConfig.replace(joinIpKey, joinIp); |
| 195 | + k8sNodeConfig = k8sNodeConfig.replace(clusterTokenKey, KubernetesClusterUtil.generateClusterToken(kubernetesCluster)); |
| 196 | + k8sNodeConfig = k8sNodeConfig.replace(ejectIsoKey, String.valueOf(ejectIso)); |
| 197 | + k8sNodeConfig = k8sNodeConfig.replace(installWaitTime, String.valueOf(waitTime)); |
| 198 | + k8sNodeConfig = k8sNodeConfig.replace(installReattemptsCount, String.valueOf(reattempts)); |
| 199 | + k8sNodeConfig = updateKubeConfigWithRegistryDetails(k8sNodeConfig); |
| 200 | + |
| 201 | + return k8sNodeConfig; |
| 202 | + } |
| 203 | + |
| 204 | + protected String updateKubeConfigWithRegistryDetails(String k8sConfig) { |
| 205 | + /* genarate /etc/containerd/config.toml file on the nodes only if Kubernetes cluster is created to |
| 206 | + * use docker private registry */ |
| 207 | + String registryUsername = null; |
| 208 | + String registryPassword = null; |
| 209 | + String registryUrl = null; |
| 210 | + |
| 211 | + List<KubernetesClusterDetailsVO> details = kubernetesClusterDetailsDao.listDetails(kubernetesCluster.getId()); |
| 212 | + for (KubernetesClusterDetailsVO detail : details) { |
| 213 | + if (detail.getName().equals(ApiConstants.DOCKER_REGISTRY_USER_NAME)) { |
| 214 | + registryUsername = detail.getValue(); |
| 215 | + } |
| 216 | + if (detail.getName().equals(ApiConstants.DOCKER_REGISTRY_PASSWORD)) { |
| 217 | + registryPassword = detail.getValue(); |
| 218 | + } |
| 219 | + if (detail.getName().equals(ApiConstants.DOCKER_REGISTRY_URL)) { |
| 220 | + registryUrl = detail.getValue(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + if (StringUtils.isNoneEmpty(registryUsername, registryPassword, registryUrl)) { |
| 225 | + // Update runcmd in the cloud-init configuration to run a script that updates the containerd config with provided registry details |
| 226 | + String runCmd = "- bash -x /opt/bin/setup-containerd"; |
| 227 | + |
| 228 | + String registryEp = registryUrl.split("://")[1]; |
| 229 | + k8sConfig = k8sConfig.replace("- containerd config default > /etc/containerd/config.toml", runCmd); |
| 230 | + final String registryUrlKey = "{{registry.url}}"; |
| 231 | + final String registryUrlEpKey = "{{registry.url.endpoint}}"; |
| 232 | + final String registryAuthKey = "{{registry.token}}"; |
| 233 | + final String registryUname = "{{registry.username}}"; |
| 234 | + final String registryPsswd = "{{registry.password}}"; |
| 235 | + |
| 236 | + final String usernamePasswordKey = registryUsername + ":" + registryPassword; |
| 237 | + String base64Auth = Base64.encodeBase64String(usernamePasswordKey.getBytes(com.cloud.utils.StringUtils.getPreferredCharset())); |
| 238 | + k8sConfig = k8sConfig.replace(registryUrlKey, registryUrl); |
| 239 | + k8sConfig = k8sConfig.replace(registryUrlEpKey, registryEp); |
| 240 | + k8sConfig = k8sConfig.replace(registryUname, registryUsername); |
| 241 | + k8sConfig = k8sConfig.replace(registryPsswd, registryPassword); |
| 242 | + k8sConfig = k8sConfig.replace(registryAuthKey, base64Auth); |
| 243 | + } |
| 244 | + return k8sConfig; |
| 245 | + } |
| 246 | + |
169 | 247 | protected DeployDestination plan(final long nodesCount, final DataCenter zone, final ServiceOffering offering) throws InsufficientServerCapacityException { |
170 | 248 | final int cpu_requested = offering.getCpu() * offering.getSpeed(); |
171 | 249 | final long ram_requested = offering.getRamSize() * 1024L * 1024L; |
|
0 commit comments