Example #1
0
  public ServiceInstance(Service service) {
    this.service = service;
    this.id = getName(service);
    ServiceSpec spec = KubernetesHelper.getOrCreateSpec(service);
    List<ServicePort> ports = spec.getPorts();
    if (spec.getPortalIP().equals(HEADLESS_PORTAL_IP)) {
      // do nothing service is headless
    } else if (ports != null && !ports.isEmpty()) {
      for (ServicePort servicePort : ports) {
        servicePorts.add(toNamedServicePort(id, servicePort));
      }
    } else {
      throw new IllegalArgumentException(
          "Service: " + id + " doesn't have a valid port configuration.");
    }
    this.selector = KubernetesHelper.getSelector(service);
    Objects.notNull(this.selector, "No selector for service " + id);
    if (selector.isEmpty()) {
      throw new IllegalArgumentException("Empty selector for service " + id);
    }
    this.filter = KubernetesHelper.createPodFilter(selector);

    // TODO should we use some service metadata to choose the load balancer?
    this.loadBalancer = new RoundRobinLoadBalancer();
  }
Example #2
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();
        }
      }
    }
  }
Example #3
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));
      }
    }
  }