Пример #1
0
 public static void deleteServices(KubernetesClient client, Logger logger, Filter<Service> filter)
     throws MultiException {
   List<Throwable> errors = new ArrayList<>();
   for (Service service : client.getServices().getItems()) {
     if (filter.matches(service)) {
       try {
         logger.info("Deleting service:" + getId(service));
         client.deleteService(getId(service));
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   if (!errors.isEmpty()) {
     throw new MultiException("Error while deleting services", errors);
   }
 }
Пример #2
0
 public static void deletePods(KubernetesClient client, Logger logger, Filter<Pod> filter)
     throws MultiException {
   List<Throwable> errors = new ArrayList<>();
   for (Pod pod : client.getPods().getItems()) {
     if (filter.matches(pod)) {
       try {
         logger.info("Deleting pod:" + getId(pod));
         client.deletePod(getId(pod));
       } catch (Exception e) {
         errors.add(e);
       }
     }
   }
   if (!errors.isEmpty()) {
     throw new MultiException("Error while deleting pods", errors);
   }
 }
Пример #3
0
 @Override
 public List<Service> getServices() throws KubernetesClientException {
   try {
     return kubernetesClient.getServices().getItems();
   } catch (Exception e) {
     String msg = "Could not retrieve kubernetes services";
     log.error(msg, e);
     throw new KubernetesClientException(msg, e);
   }
 }
Пример #4
0
 @Override
 public void deletePod(String podId) throws KubernetesClientException {
   try {
     kubernetesClient.deletePod(podId);
   } catch (Exception e) {
     String message = String.format("Could not delete kubernetes pod: [pod-id] %s", podId);
     log.error(message, e);
     throw new KubernetesClientException(message, e);
   }
 }
Пример #5
0
 @Override
 public List<Pod> getPods() throws KubernetesClientException {
   try {
     return kubernetesClient.getPods().getItems();
   } catch (Exception e) {
     String msg = "Error while retrieving kubernetes pods.";
     log.error(msg, e);
     throw new KubernetesClientException(msg, e);
   }
 }
Пример #6
0
 @Override
 public Pod getPod(String podId) throws KubernetesClientException {
   try {
     return kubernetesClient.getPod(podId);
   } catch (Exception e) {
     String msg = String.format("Could not retrieve kubernetes pod: [pod-id] %s", podId);
     log.error(msg, e);
     throw new KubernetesClientException(msg, e);
   }
 }
Пример #7
0
 public static List<Service> findServices(KubernetesClient client, Filter<Service> filter)
     throws MultiException {
   List<Service> services = new ArrayList<>();
   for (Service service : client.getServices().getItems()) {
     if (filter.matches(service)) {
       services.add(service);
     }
   }
   return services;
 }
Пример #8
0
 public static List<Pod> findPods(KubernetesClient client, Filter<Pod> filter)
     throws MultiException {
   List<Pod> pods = new ArrayList<>();
   for (Pod pod : client.getPods().getItems()) {
     if (filter.matches(pod)) {
       pods.add(pod);
     }
   }
   return pods;
 }
Пример #9
0
 @Override
 public Service getService(String serviceId) throws KubernetesClientException {
   try {
     return kubernetesClient.getService(serviceId);
   } catch (Exception e) {
     String msg =
         String.format("Could not retrieve kubernetes service: [service-id] %s", serviceId);
     log.error(msg, e);
     throw new KubernetesClientException(msg, e);
   }
 }
Пример #10
0
  public static void deleteReplicationControllers(
      KubernetesClient client, Logger logger, Filter<ReplicationController> filter)
      throws MultiException {
    List<Throwable> errors = new ArrayList<>();
    for (ReplicationController replicationController :
        client.getReplicationControllers().getItems()) {
      if (filter.matches(replicationController)) {
        try {
          logger.info("Deleting replication controller:" + getId(replicationController));
          client.deleteReplicationController(getId(replicationController));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    if (!errors.isEmpty()) {
      throw new MultiException("Error while deleting replication controllers", errors);
    }
  }
Пример #11
0
 public static List<ReplicationController> findReplicationControllers(
     KubernetesClient client, Filter<ReplicationController> filter) throws MultiException {
   List<ReplicationController> replicationControllers = new ArrayList<>();
   for (ReplicationController replicationController :
       client.getReplicationControllers().getItems()) {
     if (filter.matches(replicationController)) {
       replicationControllers.add(replicationController);
     }
   }
   return replicationControllers;
 }
Пример #12
0
  public static void displaySessionStatus(KubernetesClient client, Session session)
      throws MultiException {
    Map<String, String> labels = Collections.singletonMap(Constants.ARQ_KEY, session.getId());
    Filter<Pod> podFilter = KubernetesHelper.createPodFilter(labels);
    Filter<Service> serviceFilter = KubernetesHelper.createServiceFilter(labels);
    Filter<ReplicationController> replicationControllerFilter =
        KubernetesHelper.createReplicationControllerFilter(labels);

    for (ReplicationController replicationController :
        client.getReplicationControllers().getItems()) {
      if (replicationControllerFilter.matches(replicationController)) {
        session.getLogger().info("Replication controller:" + getId(replicationController));
      }
    }

    for (Pod pod : client.getPods().getItems()) {
      if (podFilter.matches(pod)) {
        session
            .getLogger()
            .info("Pod:" + getId(pod) + " Status:" + pod.getCurrentState().getStatus());
      }
    }
    for (Service service : client.getServices().getItems()) {
      if (serviceFilter.matches(service)) {
        session
            .getLogger()
            .info(
                "Service:"
                    + getId(service)
                    + " IP:"
                    + getPortalIP(service)
                    + " Port:"
                    + getPort(service));
      }
    }
  }
Пример #13
0
  public static void cleanupAllMatching(
      KubernetesClient client, String key, Session session, List<Throwable> errors)
      throws MultiException {
    Map<String, String> labels = Collections.singletonMap(key, session.getId());
    Filter<Pod> podFilter = KubernetesHelper.createPodFilter(labels);
    Filter<Service> serviceFilter = KubernetesHelper.createServiceFilter(labels);
    Filter<ReplicationController> replicationControllerFilter =
        KubernetesHelper.createReplicationControllerFilter(labels);

    /** Lets use a loop to ensure we really do delete all the matching resources */
    for (int i = 0; i < 10; i++) {
      try {
        deleteReplicationControllers(client, session.getLogger(), replicationControllerFilter);
      } catch (MultiException e) {
        errors.addAll(Arrays.asList(e.getCauses()));
      }

      try {
        deletePods(client, session.getLogger(), podFilter);
      } catch (MultiException e) {
        errors.addAll(Arrays.asList(e.getCauses()));
      }

      try {
        deleteServices(client, session.getLogger(), serviceFilter);
      } catch (MultiException e) {
        errors.addAll(Arrays.asList(e.getCauses()));
      }

      // lets see if there are any matching podList left
      PodList podList = client.getPods();
      List<Pod> filteredPods = Filters.filter(notNullList(podList.getItems()), podFilter);
      if (filteredPods.isEmpty()) {
        return;
      } else {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
Пример #14
0
  @Override
  public void deleteService(String serviceId) throws KubernetesClientException {

    try {
      if (log.isDebugEnabled()) {
        log.debug(String.format("Deleting kubernetes service: [service-id] %s", serviceId));
      }

      kubernetesClient.deleteService(serviceId);

      if (log.isDebugEnabled()) {
        log.debug(
            String.format("Kubernetes service deleted successfully: [service-id] %s", serviceId));
      }
    } catch (Exception e) {
      String msg = String.format("Could not delete kubernetes service: [service-id] %s", serviceId);
      log.error(msg, e);
      throw new KubernetesClientException(msg, e);
    }
  }
Пример #15
0
  /**
   * Create new pod
   *
   * @param podId Identifier of the pod
   * @param podLabel Pod name to be used by the pod label
   * @param dockerImage Docker image to be used by the pod
   * @param cpu Number of cpu cores
   * @param memory Memory allocation in megabytes
   * @param ports Ports exposed by the pod
   * @param environmentVariables Environment variables to be passed to the pod
   * @throws KubernetesClientException
   */
  @Override
  public void createPod(
      String podId,
      String podLabel,
      String dockerImage,
      int cpu,
      int memory,
      List<ContainerPort> ports,
      List<EnvVar> environmentVariables)
      throws KubernetesClientException {

    try {
      int memoryInMB = 1024 * 1024 * memory;
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Creating kubernetes pod: [pod-id] %s [pod-name] %s [docker-image] %s "
                    + "[cpu] %d [memory] %d MB [ports] %s",
                podId, podLabel, dockerImage, cpu, memoryInMB, ports));
      }

      // Create pod definition
      Pod pod = new Pod();
      pod.setApiVersion(Pod.ApiVersion.V_1);
      pod.setKind(KubernetesConstants.KIND_POD);

      pod.setSpec(new PodSpec());
      pod.setMetadata(new ObjectMeta());

      pod.getMetadata().setName(podId);

      Map<String, String> labels = new HashMap<String, String>();
      labels.put(KubernetesConstants.LABEL_NAME, podLabel);
      pod.getMetadata().setLabels(labels);

      // Set container template
      Container containerTemplate = new Container();
      containerTemplate.setName(podLabel);
      containerTemplate.setImage(dockerImage);
      containerTemplate.setEnv(environmentVariables);
      List<Container> containerTemplates = new ArrayList<Container>();
      containerTemplates.add(containerTemplate);
      pod.getSpec().setContainers(containerTemplates);

      // Set resource limits
      ResourceRequirements resources = new ResourceRequirements();
      Map<String, Quantity> limits = new HashMap<String, Quantity>();
      limits.put(KubernetesConstants.RESOURCE_CPU, new Quantity(String.valueOf(cpu)));
      limits.put(KubernetesConstants.RESOURCE_MEMORY, new Quantity(String.valueOf(memoryInMB)));
      resources.setLimits(limits);
      containerTemplate.setResources(resources);

      containerTemplate.setPorts(ports);
      containerTemplate.setImagePullPolicy(KubernetesConstants.POLICY_PULL_IF_NOT_PRESENT);
      if (environmentVariables != null) {
        containerTemplate.setEnv(environmentVariables);
      }

      // Invoke the api to create the pod
      kubernetesClient.createPod(pod);

      if (log.isDebugEnabled()) {
        log.debug(String.format("Kubernetes pod created successfully: [pod-id] %s", podId));
      }
    } catch (Exception e) {
      String msg = String.format("Could not create kubernetes pod: [pod-id] %s", podId);
      log.error(msg, e);
      throw new KubernetesClientException(msg, e);
    }
  }
Пример #16
0
  /**
   * Create kubernetes service
   *
   * @param serviceId Service id
   * @param serviceLabel Service name to be used by the label name
   * @param nodePort Port to be exposed by the kubernetes node
   * @param containerPortName Container port name defined in the port label
   * @param containerPort Container port
   * @param sessionAffinity Session affinity configuration
   * @throws KubernetesClientException
   */
  @Override
  public void createService(
      String serviceId,
      String serviceLabel,
      int nodePort,
      String containerPortName,
      int containerPort,
      String sessionAffinity)
      throws KubernetesClientException {

    try {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Creating kubernetes service: [service-id] %s [service-name] %s [service-port] %d "
                    + "[container-port-name] %s",
                serviceId, serviceLabel, nodePort, containerPortName));
      }

      // Create service definition
      Service service = new Service();
      service.setSpec(new ServiceSpec());
      service.setMetadata(new ObjectMeta());

      service.setApiVersion(Service.ApiVersion.V_1);
      service.setKind(KubernetesConstants.KIND_SERVICE);

      service.getMetadata().setName(serviceId);
      service.getSpec().setSessionAffinity(sessionAffinity);
      service.getSpec().setType(KubernetesConstants.NODE_PORT);

      // Set port
      List<ServicePort> ports = new ArrayList<ServicePort>();
      ServicePort port = new ServicePort();
      port.setName(containerPortName);
      port.setPort(containerPort);
      port.setTargetPort(new IntOrString(containerPort));
      port.setNodePort(nodePort);
      ports.add(port);
      service.getSpec().setPorts(ports);

      // Set label
      Map<String, String> labels = new HashMap<String, String>();
      labels.put(KubernetesConstants.LABEL_NAME, serviceLabel);
      service.getMetadata().setLabels(labels);

      // Set service selector
      Map<String, String> selector = new HashMap<String, String>();
      selector.put(KubernetesConstants.LABEL_NAME, serviceLabel);
      service.getSpec().setSelector(selector);

      // Invoke the api to create the service
      kubernetesClient.createService(service);

      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Kubernetes service created successfully: [service-id] %s [service-name] %s "
                    + "[node-port] %d [container-port-name] %s [container-port] %d",
                serviceId, serviceLabel, nodePort, containerPortName, containerPort));
      }
    } catch (Exception e) {
      String message =
          String.format(
              "Could not create kubernetes service: [service-id] %s [service-name] %s "
                  + "[node-port] %d [container-port-name] %s [container-port] %d",
              serviceId, serviceLabel, nodePort, containerPortName, containerPort);
      log.error(message, e);
      throw new KubernetesClientException(message, e);
    }
  }