/** * Remove kubernetes services if available for application cluster. * * @param applicationId * @param clusterId */ public static void removeKubernetesServices(String applicationId, String clusterId) { ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(clusterId); if (clusterContext != null) { String kubernetesClusterId = clusterContext.getKubernetesClusterId(); if (org.apache.commons.lang3.StringUtils.isNotBlank(kubernetesClusterId)) { KubernetesClusterContext kubernetesClusterContext = CloudControllerContext.getInstance().getKubernetesClusterContext(kubernetesClusterId); if (kubernetesClusterContext != null) { KubernetesApiClient kubernetesApiClient = kubernetesClusterContext.getKubApi(); for (KubernetesService kubernetesService : clusterContext.getKubernetesServices()) { log.info( String.format( "Deleting kubernetes service: [application-id] %s " + "[service-id] %s", applicationId, kubernetesService.getId())); try { kubernetesApiClient.deleteService(kubernetesService.getId()); } catch (KubernetesClientException e) { log.error( String.format( "Could not delete kubernetes service: [application-id] %s " + "[service-id] %s", applicationId, kubernetesService.getId())); } } } } } }
/** * Terminate all the containers belong to a cluster by cluster id. * * @param clusterId * @return * @throws InvalidClusterException */ public MemberContext[] terminateContainers(String clusterId) throws InvalidClusterException { Lock lock = null; try { lock = CloudControllerContext.getInstance().acquireMemberContextWriteLock(); ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(clusterId); handleNullObject( clusterContext, "Could not terminate containers, cluster not found: [cluster-id] " + clusterId); String kubernetesClusterId = clusterContext.getKubernetesClusterId(); handleNullObject( kubernetesClusterId, "Could not terminate containers, kubernetes cluster id not found: " + "[cluster-id] " + clusterId); KubernetesClusterContext kubClusterContext = CloudControllerContext.getInstance().getKubernetesClusterContext(kubernetesClusterId); handleNullObject( kubClusterContext, "Could not terminate containers, kubernetes cluster not found: " + "[kubernetes-cluster-id] " + kubernetesClusterId); KubernetesApiClient kubApi = kubClusterContext.getKubApi(); // Remove kubernetes services List<KubernetesService> kubernetesServices = clusterContext.getKubernetesServices(); if (kubernetesServices != null) { for (KubernetesService kubernetesService : kubernetesServices) { try { kubApi.deleteService(kubernetesService.getId()); int allocatedPort = kubernetesService.getPort(); kubClusterContext.deallocatePort(allocatedPort); } catch (KubernetesClientException e) { log.error("Could not remove kubernetes service: [cluster-id] " + clusterId, e); } } } List<MemberContext> memberContextsRemoved = new ArrayList<MemberContext>(); List<MemberContext> memberContexts = CloudControllerContext.getInstance().getMemberContextsOfClusterId(clusterId); if (memberContexts != null) { for (MemberContext memberContext : memberContexts) { try { MemberContext memberContextRemoved = terminateContainer(memberContext.getMemberId()); memberContextsRemoved.add(memberContextRemoved); } catch (MemberTerminationFailedException e) { String message = "Could not terminate container: [member-id] " + memberContext.getMemberId(); log.error(message); } } } // Persist changes CloudControllerContext.getInstance().persist(); return memberContextsRemoved.toArray(new MemberContext[memberContextsRemoved.size()]); } finally { if (lock != null) { CloudControllerContext.getInstance().releaseWriteLock(lock); } } }