|
37 | 37 | import com.cloud.kubernetes.cluster.KubernetesClusterVmMap; |
38 | 38 | import com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO; |
39 | 39 | import com.cloud.network.IpAddress; |
40 | | -import com.cloud.network.Network; |
41 | 40 | import com.cloud.network.dao.NetworkVO; |
42 | 41 | import com.cloud.network.rules.FirewallRule; |
43 | 42 | import com.cloud.user.Account; |
|
48 | 47 | import com.cloud.vm.ReservationContext; |
49 | 48 | import com.cloud.vm.ReservationContextImpl; |
50 | 49 | import com.cloud.vm.UserVmVO; |
| 50 | +import com.cloud.vm.VMInstanceVO; |
51 | 51 |
|
52 | 52 | public class KubernetesClusterDestroyWorker extends KubernetesClusterResourceModifierActionWorker { |
53 | 53 |
|
@@ -129,7 +129,7 @@ private void destroyKubernetesClusterNetwork() throws ManagementServerException |
129 | 129 |
|
130 | 130 | private void deleteKubernetesClusterNetworkRules() throws ManagementServerException { |
131 | 131 | NetworkVO network = networkDao.findById(kubernetesCluster.getNetworkId()); |
132 | | - if (network == null || !Network.GuestType.Isolated.equals(network.getGuestType())) { |
| 132 | + if (network == null) { |
133 | 133 | return; |
134 | 134 | } |
135 | 135 | List<Long> removedVmIds = new ArrayList<>(); |
@@ -189,17 +189,39 @@ private void validateClusterVMsDestroyed() { |
189 | 189 | public boolean destroy() throws CloudRuntimeException { |
190 | 190 | init(); |
191 | 191 | validateClusterSate(); |
192 | | - if (LOGGER.isInfoEnabled()) { |
193 | | - LOGGER.info(String.format("Destroying Kubernetes cluster ID: %s", kubernetesCluster.getUuid())); |
194 | | - } |
195 | | - stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.DestroyRequested); |
196 | 192 | this.clusterVMs = kubernetesClusterVmMapDao.listByClusterId(kubernetesCluster.getId()); |
197 | | - boolean vmsDestroyed = destroyClusterVMs(); |
198 | 193 | boolean cleanupNetwork = true; |
199 | 194 | final KubernetesClusterDetailsVO clusterDetails = kubernetesClusterDetailsDao.findDetail(kubernetesCluster.getId(), "networkCleanup"); |
200 | 195 | if (clusterDetails != null) { |
201 | 196 | cleanupNetwork = Boolean.parseBoolean(clusterDetails.getValue()); |
202 | 197 | } |
| 198 | + if (cleanupNetwork) { // if network has additional VM, cannot proceed with cluster destroy |
| 199 | + NetworkVO network = networkDao.findById(kubernetesCluster.getNetworkId()); |
| 200 | + if (network == null) { |
| 201 | + logAndThrow(Level.ERROR, String.format("Failed to find network for Kubernetes cluster ID: %s", kubernetesCluster.getUuid())); |
| 202 | + } |
| 203 | + List<VMInstanceVO> networkVMs = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(network.getId(), null); |
| 204 | + if (networkVMs.size() > clusterVMs.size()) { |
| 205 | + logAndThrow(Level.ERROR, String.format("Network ID: %s for Kubernetes cluster ID: %s has instances using it which are not part of the Kubernetes cluster", kubernetesCluster.getUuid())); |
| 206 | + } |
| 207 | + for (VMInstanceVO vm : networkVMs) { |
| 208 | + boolean vmFoundInKubernetesCluster = false; |
| 209 | + for (KubernetesClusterVmMap clusterVM : clusterVMs) { |
| 210 | + if (vm.getId() == clusterVM.getVmId()) { |
| 211 | + vmFoundInKubernetesCluster = true; |
| 212 | + break; |
| 213 | + } |
| 214 | + } |
| 215 | + if (!vmFoundInKubernetesCluster) { |
| 216 | + logAndThrow(Level.ERROR, String.format("VM ID: %s which is not a part of Kubernetes cluster ID: %s is using its network ID: %s", vm.getUuid(), kubernetesCluster.getUuid(), network.getUuid())); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + if (LOGGER.isInfoEnabled()) { |
| 221 | + LOGGER.info(String.format("Destroying Kubernetes cluster ID: %s", kubernetesCluster.getUuid())); |
| 222 | + } |
| 223 | + stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.DestroyRequested); |
| 224 | + boolean vmsDestroyed = destroyClusterVMs(); |
203 | 225 | // if there are VM's that were not expunged, we can not delete the network |
204 | 226 | if (vmsDestroyed) { |
205 | 227 | if (cleanupNetwork) { |
|
0 commit comments