Move schema and upgrade for 4.20

This commit is contained in:
nvazquez 2024-06-05 12:11:01 -03:00
parent 8ca90135d6
commit 27f7a16a84
No known key found for this signature in database
GPG Key ID: 656E1BCC8CB54F84
4 changed files with 120 additions and 115 deletions

View File

@ -27,9 +27,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -78,7 +76,6 @@ public class Upgrade41810to41900 extends DbUpgradeAbstractImpl implements DbUpgr
migrateBackupDates(conn);
addIndexes(conn);
addRemoverAccountIdForeignKeyToQuarantinedIps(conn);
updateKubernetesClusterNodeVersions(conn);
}
@Override
@ -267,93 +264,4 @@ public class Upgrade41810to41900 extends DbUpgradeAbstractImpl implements DbUpgr
private void addRemoverAccountIdForeignKeyToQuarantinedIps(Connection conn) {
DbUpgradeUtils.addForeignKey(conn, "quarantined_ips", "remover_account_id", "account", "id");
}
private Map<Long, String> getKubernetesClusterIdsAndVersion(Connection conn) {
String listKubernetesClusters = "SELECT c.id, v.semantic_version FROM `cloud`.`kubernetes_cluster` c JOIN `cloud`.`kubernetes_supported_version` v ON (c.kubernetes_version_id = v.id) WHERE c.removed is NULL;";
Map<Long, String> clusterAndVersion = new HashMap<>();
try {
PreparedStatement pstmt = conn.prepareStatement(listKubernetesClusters);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
clusterAndVersion.put(rs.getLong(1), rs.getString(2));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
String errMsg = String.format("Failed to get all the kubernetes cluster ids due to: %s", e.getMessage());
LOG.error(errMsg);
throw new CloudRuntimeException(errMsg, e);
}
return clusterAndVersion;
}
private List<Long> getKubernetesClusterVmMapIds(Connection conn, Long cksClusterId) {
List<Long> kubernetesClusterVmIds = new ArrayList<>();
String getKubernetesClustersVmMap = "SELECT id FROM `cloud`.`kubernetes_cluster_vm_map` WHERE cluster_id = %s;";
try {
PreparedStatement pstmt = conn.prepareStatement(String.format(getKubernetesClustersVmMap, cksClusterId));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
kubernetesClusterVmIds.add(rs.getLong(1));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
String errMsg = String.format("Failed to get the kubernetes cluster vm map IDs for kubernetes cluster with id: %s," +
" due to: %s", cksClusterId, e.getMessage());
LOG.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
return kubernetesClusterVmIds;
}
private void updateKubernetesNodeVersion(Connection conn, List<Long> kubernetesClusterVmIds, Long cksClusterId, String cksVersion) {
String updateKubernetesNodeVersion = "UPDATE `cloud`.`kubernetes_cluster_vm_map` set kubernetes_node_version = ? WHERE id = ?;";
for (Long nodeVmId : kubernetesClusterVmIds) {
try {
PreparedStatement pstmt = conn.prepareStatement(updateKubernetesNodeVersion);
pstmt.setString(1, cksVersion);
pstmt.setLong(2, nodeVmId);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
String errMsg = String.format("Failed to update the node version for kubernetes cluster nodes for the" +
" kubernetes cluster with id: %s," +
" due to: %s", cksClusterId, e.getMessage());
LOG.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
}
}
private void updateKubernetesNodeVersions(Connection conn, Map<Long, String> clusterAndVersion) {
List<Long> kubernetesClusterVmIds;
for (Map.Entry<Long, String> clusterVersionEntry : clusterAndVersion.entrySet()) {
try {
Long cksClusterId = clusterVersionEntry.getKey();
String cksVersion = clusterVersionEntry.getValue();
LOG.debug(String.format("Adding CKS version %s to existing CKS cluster %s nodes", cksVersion, cksClusterId));
kubernetesClusterVmIds = getKubernetesClusterVmMapIds(conn, cksClusterId);
updateKubernetesNodeVersion(conn, kubernetesClusterVmIds, cksClusterId, cksVersion);
} catch (Exception e) {
String errMsg = String.format("Failed to update the node version for kubernetes cluster nodes for the" +
" kubernetes cluster with id: %s," +
" due to: %s", clusterVersionEntry.getKey(), e.getMessage());
LOG.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
}
}
private void updateKubernetesClusterNodeVersions(Connection conn) {
//get list of all non removed kubernetes clusters
try {
Map<Long, String> clusterAndVersion = getKubernetesClusterIdsAndVersion(conn);
updateKubernetesNodeVersions(conn, clusterAndVersion);
} catch (Exception e) {
String errMsg = "Failed to update kubernetes cluster nodes version";
LOG.error(errMsg);
throw new CloudRuntimeException(errMsg, e);
}
}
}

View File

@ -18,6 +18,13 @@ package com.cloud.upgrade.dao;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cloud.upgrade.SystemVmTemplateRegistration;
import com.cloud.utils.exception.CloudRuntimeException;
@ -53,6 +60,7 @@ public class Upgrade41900to42000 extends DbUpgradeAbstractImpl implements DbUpgr
@Override
public void performDataMigration(Connection conn) {
updateKubernetesClusterNodeVersions(conn);
}
@Override
@ -80,4 +88,93 @@ public class Upgrade41900to42000 extends DbUpgradeAbstractImpl implements DbUpgr
throw new CloudRuntimeException("Failed to find / register SystemVM template(s)");
}
}
private Map<Long, String> getKubernetesClusterIdsAndVersion(Connection conn) {
String listKubernetesClusters = "SELECT c.id, v.semantic_version FROM `cloud`.`kubernetes_cluster` c JOIN `cloud`.`kubernetes_supported_version` v ON (c.kubernetes_version_id = v.id) WHERE c.removed is NULL;";
Map<Long, String> clusterAndVersion = new HashMap<>();
try {
PreparedStatement pstmt = conn.prepareStatement(listKubernetesClusters);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
clusterAndVersion.put(rs.getLong(1), rs.getString(2));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
String errMsg = String.format("Failed to get all the kubernetes cluster ids due to: %s", e.getMessage());
logger.error(errMsg);
throw new CloudRuntimeException(errMsg, e);
}
return clusterAndVersion;
}
private List<Long> getKubernetesClusterVmMapIds(Connection conn, Long cksClusterId) {
List<Long> kubernetesClusterVmIds = new ArrayList<>();
String getKubernetesClustersVmMap = "SELECT id FROM `cloud`.`kubernetes_cluster_vm_map` WHERE cluster_id = %s;";
try {
PreparedStatement pstmt = conn.prepareStatement(String.format(getKubernetesClustersVmMap, cksClusterId));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
kubernetesClusterVmIds.add(rs.getLong(1));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
String errMsg = String.format("Failed to get the kubernetes cluster vm map IDs for kubernetes cluster with id: %s," +
" due to: %s", cksClusterId, e.getMessage());
logger.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
return kubernetesClusterVmIds;
}
private void updateKubernetesNodeVersion(Connection conn, List<Long> kubernetesClusterVmIds, Long cksClusterId, String cksVersion) {
String updateKubernetesNodeVersion = "UPDATE `cloud`.`kubernetes_cluster_vm_map` set kubernetes_node_version = ? WHERE id = ?;";
for (Long nodeVmId : kubernetesClusterVmIds) {
try {
PreparedStatement pstmt = conn.prepareStatement(updateKubernetesNodeVersion);
pstmt.setString(1, cksVersion);
pstmt.setLong(2, nodeVmId);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
String errMsg = String.format("Failed to update the node version for kubernetes cluster nodes for the" +
" kubernetes cluster with id: %s," +
" due to: %s", cksClusterId, e.getMessage());
logger.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
}
}
private void updateKubernetesNodeVersions(Connection conn, Map<Long, String> clusterAndVersion) {
List<Long> kubernetesClusterVmIds;
for (Map.Entry<Long, String> clusterVersionEntry : clusterAndVersion.entrySet()) {
try {
Long cksClusterId = clusterVersionEntry.getKey();
String cksVersion = clusterVersionEntry.getValue();
logger.debug(String.format("Adding CKS version %s to existing CKS cluster %s nodes", cksVersion, cksClusterId));
kubernetesClusterVmIds = getKubernetesClusterVmMapIds(conn, cksClusterId);
updateKubernetesNodeVersion(conn, kubernetesClusterVmIds, cksClusterId, cksVersion);
} catch (Exception e) {
String errMsg = String.format("Failed to update the node version for kubernetes cluster nodes for the" +
" kubernetes cluster with id: %s," +
" due to: %s", clusterVersionEntry.getKey(), e.getMessage());
logger.error(errMsg, e);
throw new CloudRuntimeException(errMsg, e);
}
}
}
private void updateKubernetesClusterNodeVersions(Connection conn) {
//get list of all non removed kubernetes clusters
try {
Map<Long, String> clusterAndVersion = getKubernetesClusterIdsAndVersion(conn);
updateKubernetesNodeVersions(conn, clusterAndVersion);
} catch (Exception e) {
String errMsg = "Failed to update kubernetes cluster nodes version";
logger.error(errMsg);
throw new CloudRuntimeException(errMsg, e);
}
}
}

View File

@ -323,26 +323,3 @@ CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.quarantined_ips', 'remover_account_i
-- Explicitly add support for VMware 8.0b (8.0.0.2), 8.0c (8.0.0.3)
INSERT IGNORE INTO `cloud`.`hypervisor_capabilities` (uuid, hypervisor_type, hypervisor_version, max_guests_limit, security_group_enabled, max_data_volumes_limit, max_hosts_per_cluster, storage_motion_supported, vm_snapshot_enabled) values (UUID(), 'VMware', '8.0.0.2', 1024, 0, 59, 64, 1, 1);
INSERT IGNORE INTO `cloud`.`hypervisor_capabilities` (uuid, hypervisor_type, hypervisor_version, max_guests_limit, security_group_enabled, max_data_volumes_limit, max_hosts_per_cluster, storage_motion_supported, vm_snapshot_enabled) values (UUID(), 'VMware', '8.0.0.3', 1024, 0, 59, 64, 1, 1);
-- Add for_cks column to the vm_template table
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.vm_template','for_cks', 'int(1) unsigned DEFAULT "0" COMMENT "if true, the template can be used for CKS cluster deployment"');
-- Add support for different node types service offerings on CKS clusters
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','control_service_offering_id', 'bigint unsigned COMMENT "service offering ID for Control Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','worker_service_offering_id', 'bigint unsigned COMMENT "service offering ID for Worker Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_service_offering_id', 'bigint unsigned COMMENT "service offering ID for etcd Nodes"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_node_count', 'bigint unsigned COMMENT "number of etcd nodes to be deployed for the Kubernetes cluster"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','control_template_id', 'bigint unsigned COMMENT "template id to be used for Control Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','worker_template_id', 'bigint unsigned COMMENT "template id to be used for Worker Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_template_id', 'bigint unsigned COMMENT "template id to be used for etcd Nodes"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','etcd_node', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the VM is an etcd node"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','external_node', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the node was imported into the Kubernetes cluster"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','manual_upgrade', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the node is marked for manual upgrade and excluded from the Kubernetes cluster upgrade operation"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','kubernetes_node_version', 'varchar(40) COMMENT "version of k8s the cluster node is on"');
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__control_service_offering_id` FOREIGN KEY `fk_cluster__control_service_offering_id`(`control_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__worker_service_offering_id` FOREIGN KEY `fk_cluster__worker_service_offering_id`(`worker_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__etcd_service_offering_id` FOREIGN KEY `fk_cluster__etcd_service_offering_id`(`etcd_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__control_template_id` FOREIGN KEY `fk_cluster__control_template_id`(`control_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__worker_template_id` FOREIGN KEY `fk_cluster__worker_template_id`(`worker_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__etcd_template_id` FOREIGN KEY `fk_cluster__etcd_template_id`(`etcd_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;

View File

@ -79,3 +79,26 @@ CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_email_configuration`(
PRIMARY KEY (`account_id`, `email_template_id`),
CONSTRAINT `FK_quota_email_configuration_account_id` FOREIGN KEY (`account_id`) REFERENCES `cloud_usage`.`quota_account`(`account_id`),
CONSTRAINT `FK_quota_email_configuration_email_template_id` FOREIGN KEY (`email_template_id`) REFERENCES `cloud_usage`.`quota_email_templates`(`id`));
-- Add for_cks column to the vm_template table
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.vm_template','for_cks', 'int(1) unsigned DEFAULT "0" COMMENT "if true, the template can be used for CKS cluster deployment"');
-- Add support for different node types service offerings on CKS clusters
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','control_service_offering_id', 'bigint unsigned COMMENT "service offering ID for Control Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','worker_service_offering_id', 'bigint unsigned COMMENT "service offering ID for Worker Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_service_offering_id', 'bigint unsigned COMMENT "service offering ID for etcd Nodes"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_node_count', 'bigint unsigned COMMENT "number of etcd nodes to be deployed for the Kubernetes cluster"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','control_template_id', 'bigint unsigned COMMENT "template id to be used for Control Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','worker_template_id', 'bigint unsigned COMMENT "template id to be used for Worker Node(s)"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster','etcd_template_id', 'bigint unsigned COMMENT "template id to be used for etcd Nodes"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','etcd_node', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the VM is an etcd node"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','external_node', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the node was imported into the Kubernetes cluster"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','manual_upgrade', 'tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT "indicates if the node is marked for manual upgrade and excluded from the Kubernetes cluster upgrade operation"');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.kubernetes_cluster_vm_map','kubernetes_node_version', 'varchar(40) COMMENT "version of k8s the cluster node is on"');
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__control_service_offering_id` FOREIGN KEY `fk_cluster__control_service_offering_id`(`control_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__worker_service_offering_id` FOREIGN KEY `fk_cluster__worker_service_offering_id`(`worker_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__etcd_service_offering_id` FOREIGN KEY `fk_cluster__etcd_service_offering_id`(`etcd_service_offering_id`) REFERENCES `service_offering`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__control_template_id` FOREIGN KEY `fk_cluster__control_template_id`(`control_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__worker_template_id` FOREIGN KEY `fk_cluster__worker_template_id`(`worker_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;
ALTER TABLE `cloud`.`kubernetes_cluster` ADD CONSTRAINT `fk_cluster__etcd_template_id` FOREIGN KEY `fk_cluster__etcd_template_id`(`etcd_template_id`) REFERENCES `vm_template`(`id`) ON DELETE CASCADE;