Improve network rules cleanup on failure adding external nodes to CKS cluster

This commit is contained in:
Nicolas Vazquez 2024-05-23 12:47:45 -03:00 committed by nvazquez
parent 4b4a675788
commit aca606e510
No known key found for this signature in database
GPG Key ID: 656E1BCC8CB54F84
2 changed files with 10 additions and 5 deletions

View File

@ -250,6 +250,10 @@ public class KubernetesClusterAddWorker extends KubernetesClusterActionWorker {
revertNetworkRules(network, nodeId, sshStartPort);
return new Pair<>( false, nodeIndex);
} catch (Exception e) {
String errMsg = String.format("Unexpected exception while trying to add the external node %s to the Kubernetes cluster %s: %s",
nodeId, kubernetesCluster.getName(), e.getMessage());
LOGGER.error(errMsg, e);
revertNetworkRules(network, nodeId, sshStartPort);
throw new CloudRuntimeException(e);
}
return new Pair<>(true, ++nodeIndex);
@ -305,12 +309,15 @@ public class KubernetesClusterAddWorker extends KubernetesClusterActionWorker {
}
private void revertNetworkRules(Network network, long vmId, int port) {
LOGGER.debug(String.format("Reverting network rules for VM ID %s on network %s", vmId, network.getName()));
FirewallRuleVO ruleVO = firewallRulesDao.findByNetworkIdAndPorts(network.getId(), port, port);
if (Objects.isNull(network.getVpcId())) {
LOGGER.debug(String.format("Removing firewall rule %s", ruleVO.getId()));
firewallService.revokeIngressFirewallRule(ruleVO.getId(), true);
}
List<PortForwardingRuleVO> pfRules = portForwardingRulesDao.listByVm(vmId);
for (PortForwardingRuleVO pfRule : pfRules) {
LOGGER.debug(String.format("Removing port forwarding rule %s", pfRule.getId()));
rulesService.revokePortForwardingRule(pfRule.getId(), true);
}
}

View File

@ -19,7 +19,6 @@ package org.apache.cloudstack.api.command.user.kubernetes.cluster;
import com.cloud.kubernetes.cluster.KubernetesClusterEventTypes;
import com.cloud.kubernetes.cluster.KubernetesClusterService;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandResourceType;
@ -109,14 +108,13 @@ public class AddNodesToKubernetesClusterCmd extends BaseAsyncCmd {
@Override
public void execute() {
try {
if (!kubernetesClusterService.addNodesToKubernetesCluster(this)) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to add node(s) Kubernetes cluster ID: %d", getClusterId()));
}
kubernetesClusterService.addNodesToKubernetesCluster(this);
final KubernetesClusterResponse response = kubernetesClusterService.createKubernetesClusterResponse(getClusterId());
response.setResponseName(getCommandName());
setResponseObject(response);
} catch (Exception e) {
throw new CloudRuntimeException(String.format("Failed to add nodes to cluster due to: %s", e.getLocalizedMessage()), e);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to add nodes to cluster ID %s due to: %s",
getClusterId(), e.getLocalizedMessage()), e);
}
}