/**
   * 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()));
            }
          }
        }
      }
    }
  }
Exemple #2
0
  public static void handleClusterTerminatingEvent(ClusterStatusClusterTerminatingEvent event) {

    TopologyManager.acquireWriteLock();

    try {
      Topology topology = TopologyManager.getTopology();
      Cluster cluster =
          topology.getService(event.getServiceName()).getCluster(event.getClusterId());

      if (!cluster.isStateTransitionValid(ClusterStatus.Terminating, event.getInstanceId())) {
        log.error(
            "Invalid state transfer from "
                + cluster.getStatus(event.getInstanceId())
                + " to "
                + ClusterStatus.Terminating);
      }
      ClusterInstance context = cluster.getInstanceContexts(event.getInstanceId());
      if (context == null) {
        log.warn(
            "Cluster Instance Context is not found for [cluster] "
                + event.getClusterId()
                + " [instance-id] "
                + event.getInstanceId());
        return;
      }
      ClusterStatus status = ClusterStatus.Terminating;
      if (context.isStateTransitionValid(status)) {
        context.setStatus(status);
        log.info("Cluster Terminating started for " + cluster.getClusterId());
        TopologyManager.updateTopology(topology);
        // publishing data
        ClusterInstanceTerminatingEvent clusterTerminaingEvent =
            new ClusterInstanceTerminatingEvent(
                event.getAppId(),
                event.getServiceName(),
                event.getClusterId(),
                event.getInstanceId());

        TopologyEventPublisher.sendClusterTerminatingEvent(clusterTerminaingEvent);

        // Remove kubernetes services if available
        ClusterContext clusterContext =
            CloudControllerContext.getInstance().getClusterContext(event.getClusterId());
        if (StringUtils.isNotBlank(clusterContext.getKubernetesClusterId())) {
          KubernetesIaas.removeKubernetesServices(event.getAppId(), event.getClusterId());
        }
      } else {
        log.error(
            String.format(
                "Cluster state transition is not valid: [cluster-id] %s "
                    + " [instance-id] %s [current-status] %s [status-requested] %s",
                event.getClusterId(), event.getInstanceId(), context.getStatus(), status));
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }
  /**
   * Terminate a container by member id
   *
   * @param memberId
   * @return
   * @throws MemberTerminationFailedException
   */
  public MemberContext terminateContainer(String memberId) throws MemberTerminationFailedException {
    Lock lock = null;
    try {
      lock = CloudControllerContext.getInstance().acquireMemberContextWriteLock();
      handleNullObject(memberId, "Could not terminate container, member id is null");

      MemberContext memberContext =
          CloudControllerContext.getInstance().getMemberContextOfMemberId(memberId);
      handleNullObject(
          memberContext,
          "Could not terminate container, member context not found: [member-id] " + memberId);

      String clusterId = memberContext.getClusterId();
      handleNullObject(
          clusterId, "Could not terminate container, cluster id is null: [member-id] " + memberId);

      ClusterContext clusterContext =
          CloudControllerContext.getInstance().getClusterContext(clusterId);
      handleNullObject(
          clusterContext,
          String.format(
              "Could not terminate container, cluster context not found: "
                  + "[cluster-id] %s [member-id] %s",
              clusterId, memberId));

      String kubernetesClusterId = clusterContext.getKubernetesClusterId();
      handleNullObject(
          kubernetesClusterId,
          String.format(
              "Could not terminate container, kubernetes cluster "
                  + "context id is null: [cluster-id] %s [member-id] %s",
              clusterId, memberId));

      KubernetesClusterContext kubernetesClusterContext =
          CloudControllerContext.getInstance().getKubernetesClusterContext(kubernetesClusterId);
      handleNullObject(
          kubernetesClusterContext,
          String.format(
              "Could not terminate container, kubernetes cluster "
                  + "context not found: [cluster-id] %s [member-id] %s",
              clusterId, memberId));
      KubernetesApiClient kubApi = kubernetesClusterContext.getKubApi();

      try {
        log.info(
            String.format(
                "Removing kubernetes pod: [application] %s [cartridge] %s [member] %s [pod] %s",
                memberContext.getApplicationId(),
                memberContext.getCartridgeType(),
                memberContext.getMemberId(),
                memberContext.getKubernetesPodId()));

        // Remove pod
        kubApi.deletePod(memberContext.getKubernetesPodId());
        // Persist changes
        CloudControllerContext.getInstance().persist();

        log.info(
            String.format(
                "Kubernetes pod removed successfully: [application] %s [cartridge] %s "
                    + "[member] %s [pod] %s",
                memberContext.getApplicationId(),
                memberContext.getCartridgeType(),
                memberContext.getMemberId(),
                memberContext.getKubernetesPodId()));
      } catch (KubernetesClientException ignore) {
        // we can't do nothing here
        log.warn(
            String.format("Could not delete pod: [pod-id] %s", memberContext.getKubernetesPodId()));
      }
      return memberContext;
    } finally {
      if (lock != null) {
        CloudControllerContext.getInstance().releaseWriteLock(lock);
      }
    }
  }
  /**
   * 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);
      }
    }
  }