io.kubernetes.client.openapi.models.V1Pod Java Examples

The following examples show how to use io.kubernetes.client.openapi.models.V1Pod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PodWatchUtils.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * a test method to see whether kubernetes java client can connect to kubernetes master
 * and get the pod list
 */
public static void testGetPodList(String namespace) {
  if (apiClient == null || coreApi == null) {
    createApiInstances();
  }

  LOG.info("Getting the pod list for the namespace: " + namespace);
  V1PodList list = null;
  try {
    list = coreApi.listNamespacedPod(
        namespace, null, null, null, null, null, null, null, null, null);
  } catch (ApiException e) {
    String logMessage = "Exception when getting the pod list: \n"
        + "exCode: " + e.getCode() + "\n"
        + "responseBody: " + e.getResponseBody();
    LOG.log(Level.SEVERE, logMessage, e);
    throw new RuntimeException(e);
  }

  LOG.info("Number of pods in the received list: " + list.getItems().size());
  for (V1Pod item : list.getItems()) {
    LOG.info(item.getMetadata().getName());
  }
}
 
Example #2
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
/**
 * GC orphaned pods on nodes that are no longer valid/available.
 */
private void gcOrphanedPodsWithoutValidNodes(List<V1Node> nodes, List<V1Pod> pods) {
    Set<String> currentNodeNames = nodes.stream().map(n -> n.getMetadata().getName()).collect(Collectors.toSet());
    List<V1Pod> orphanedPodsWithoutValidNodesToGc = pods.stream()
            .filter(p -> {
                String nodeName = p.getSpec().getNodeName();
                return StringExt.isNotEmpty(nodeName) && !currentNodeNames.contains(nodeName);
            })
            .collect(Collectors.toList());

    logger.info("Attempting to GC {} orphaned pods: {} without valid nodes", orphanedPodsWithoutValidNodesToGc.size(),
            orphanedPodsWithoutValidNodesToGc);
    orphanedPodsWithoutValidNodesToGcGauge.set(orphanedPodsWithoutValidNodesToGc.size());
    for (V1Pod pod : orphanedPodsWithoutValidNodesToGc) {
        gcPod(pod);
    }
    logger.info("Finished orphaned pod GC without valid nodes");
}
 
Example #3
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
/**
 * GC pods past deletion timestamp timeout.
 */
private void gcPodsPastDeletionTimestamp(List<V1Pod> pods) {
    List<V1Pod> podsPastDeletionTimestampToGc = pods.stream()
            .filter(p -> {
                DateTime deletionTimestamp = p.getMetadata().getDeletionTimestamp();
                return deletionTimestamp != null &&
                        clock.isPast(deletionTimestamp.getMillis() + directKubeConfiguration.getDeleteGracePeriodSeconds()
                                + directKubeConfiguration.getPodTerminationGcTimeoutMs());
            })
            .collect(Collectors.toList());

    logger.info("Attempting to GC {} pods: {} past deletion timestamp", podsPastDeletionTimestampToGc.size(),
            podsPastDeletionTimestampToGc);
    podsPastDeletionTimestampToGcGauge.set(podsPastDeletionTimestampToGc.size());
    for (V1Pod pod : podsPastDeletionTimestampToGc) {
        gcPod(pod);
    }
    logger.info("Finished pods past deletion timestamp GC");
}
 
Example #4
Source File: CacheTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacheStore() {
  if (this.obj == null) {
    // skip null object storing test b/c it should be checked before invoking cache
    return;
  }

  cache.replace(Arrays.asList(this.obj), "0");

  cache.delete(this.obj);

  V1Pod pod = ((V1Pod) this.obj);
  List indexedObjectList = cache.byIndex(mockIndexName, this.index);
  assertEquals(0, indexedObjectList.size());
  assertEquals(null, pod.getMetadata().getClusterName());

  cache.add(this.obj);

  // replace cached object w/ null value
  String newClusterName = "test_cluster";
  pod.getMetadata().setClusterName(newClusterName);
  cache.update(this.obj);

  assertEquals(1, cache.list().size());
  assertEquals(newClusterName, pod.getMetadata().getClusterName());
}
 
Example #5
Source File: CacheTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiIndexFuncCacheStore() {
  String testIndexFuncName = "test-idx-func";
  Cache<V1Pod> podCache = new Cache<>();
  podCache.addIndexFunc(
      testIndexFuncName,
      (V1Pod pod) -> {
        return Arrays.asList(pod.getSpec().getNodeName());
      });

  V1Pod testPod =
      new V1Pod()
          .metadata(new V1ObjectMeta().namespace("ns").name("n"))
          .spec(new V1PodSpec().nodeName("node1"));
  podCache.add(testPod);

  List<V1Pod> namespaceIndexedPods = podCache.byIndex(Caches.NAMESPACE_INDEX, "ns");
  assertEquals(1, namespaceIndexedPods.size());

  List<V1Pod> nodeNameIndexedPods = podCache.byIndex(testIndexFuncName, "node1");
  assertEquals(1, nodeNameIndexedPods.size());
}
 
Example #6
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
/**
 * GC pods in Pending phase with a deletion timestamp.
 */
private void gcPendingPodsWithDeletionTimestamp(List<V1Pod> pods) {
    List<V1Pod> pendingPodsWithDeletionTimestampToGc = pods.stream()
            .filter(p -> {
                DateTime deletionTimestamp = p.getMetadata().getDeletionTimestamp();
                return p.getStatus().getPhase().equalsIgnoreCase(PENDING) && deletionTimestamp != null;
            })
            .collect(Collectors.toList());

    logger.info("Attempting to GC {} pending pods: {} with deletion timestamp", pendingPodsWithDeletionTimestampToGc.size(),
            pendingPodsWithDeletionTimestampToGc);
    pendingPodsWithDeletionTimestampToGcGauge.set(pendingPodsWithDeletionTimestampToGc.size());
    for (V1Pod pod : pendingPodsWithDeletionTimestampToGc) {
        gcPod(pod);
        publishContainerEvent(pod.getMetadata().getName(), Finished, REASON_TASK_KILLED, "", Optional.empty());
    }
    logger.info("Finished pending pods with deletion timestamp GC");
}
 
Example #7
Source File: DefaultKubeJobManagementReconciler.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private void reconcile() {
    if (!mesosConfiguration.isReconcilerEnabled()) {
        logger.info("Skipping the job management / Kube reconciliation cycle: reconciler disabled");
        return;
    }
    if (!kubeApiFacade.getNodeInformer().hasSynced() || !kubeApiFacade.getPodInformer().hasSynced()) {
        logger.info("Skipping the job management / Kube reconciliation cycle: Kube informers not ready (node={}, pod={})",
                kubeApiFacade.getNodeInformer().hasSynced(), kubeApiFacade.getPodInformer().hasSynced()
        );
        return;
    }

    List<V1Node> nodes = kubeApiFacade.getNodeInformer().getIndexer().list();
    List<V1Pod> pods = kubeApiFacade.getPodInformer().getIndexer().list();
    List<Task> tasks = v3JobOperations.getTasks();

    Map<String, V1Node> nodesById = nodes.stream().collect(Collectors.toMap(
            node -> node.getMetadata().getName(),
            Function.identity()
    ));
    Map<String, Task> currentTasks = tasks.stream().collect(Collectors.toMap(Task::getId, Function.identity()));
    Set<String> currentPodNames = pods.stream().map(p -> p.getMetadata().getName()).collect(Collectors.toSet());

    transitionOrphanedTasks(currentTasks, currentPodNames, nodesById);
}
 
Example #8
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private void reconcileNodesAndPods() {
    if (!mesosConfiguration.isReconcilerEnabled() || !kubeApiFacade.getNodeInformer().hasSynced() || !kubeApiFacade.getPodInformer().hasSynced()) {
        return;
    }

    List<V1Node> nodes = kubeApiFacade.getNodeInformer().getIndexer().list();
    List<V1Pod> pods = kubeApiFacade.getPodInformer().getIndexer().list();
    List<Task> tasks = v3JobOperations.getTasks();
    Map<String, Task> currentTasks = tasks.stream().collect(Collectors.toMap(Task::getId, Function.identity()));

    gcTimedOutNodes(nodes);
    gcOrphanedPodsWithoutValidNodes(nodes, pods);
    gcTerminalPods(pods, currentTasks);
    gcUnknownPods(pods, currentTasks);
    gcPodsPastDeletionTimestamp(pods);
    gcPendingPodsWithDeletionTimestamp(pods);
}
 
Example #9
Source File: StubbedDirectKubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<V1Pod> launchTask(Job job, Task task) {
    return Mono.fromCallable(() -> {
        if (nextLaunchError != null) {
            try {
                throw nextLaunchError;
            } finally {
                nextLaunchError = null;
            }
        }
        V1Pod v1Pod = new V1Pod()
                .metadata(new V1ObjectMeta()
                        .name(task.getId())
                );
        podHoldersByTaskId.put(task.getId(), v1Pod);
        return v1Pod;
    });
}
 
Example #10
Source File: KubeUtil.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
public static Optional<TitusExecutorDetails> getTitusExecutorDetails(V1Pod pod) {
    Map<String, String> annotations = pod.getMetadata().getAnnotations();
    if (!Strings.isNullOrEmpty(annotations.get("IpAddress"))) {
        TitusExecutorDetails titusExecutorDetails = new TitusExecutorDetails(
                Collections.emptyMap(),
                new TitusExecutorDetails.NetworkConfiguration(
                        Boolean.parseBoolean(annotations.getOrDefault("IsRoutableIp", "true")),
                        annotations.getOrDefault("IpAddress", "UnknownIpAddress"),
                        annotations.get("EniIPv6Address"),
                        annotations.getOrDefault("EniIpAddress", "UnknownEniIpAddress"),
                        annotations.getOrDefault("EniId", "UnknownEniId"),
                        annotations.getOrDefault("ResourceId", "UnknownResourceId")
                )
        );
        return Optional.of(titusExecutorDetails);
    }
    return Optional.empty();
}
 
Example #11
Source File: YamlTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateTime() {
  try {
    String strInput =
        "apiVersion: v1\n"
            + "kind: Pod\n"
            + "metadata:\n"
            + "  creationTimestamp: 2018-09-06T15:12:24Z";

    V1Pod pod = Yaml.loadAs(strInput, V1Pod.class);

    assertEquals(
        "Incorrect value loaded for creationTimestamp",
        "2018-09-06T15:12:24.000Z",
        new String(pod.getMetadata().getCreationTimestamp().toString().getBytes(), UTF_8));

  } catch (Exception ex) {
    assertNull("Unexpected exception: " + ex.toString(), ex);
  }
}
 
Example #12
Source File: PatchUtilsTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrategicMergePatchPod() throws ApiException {
  CoreV1Api coreV1Api = new CoreV1Api(client);
  stubFor(
      patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo"))
          .withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json")
                  .withBody("{}")));

  PatchUtils.patch(
      V1Pod.class,
      () ->
          coreV1Api.patchNamespacedPodCall(
              "foo", "default", new V1Patch("[]"), null, null, null, null, null),
      V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
      client);

  verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
 
Example #13
Source File: DeltaFIFOTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeltaFIFOResync() {
  V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
  Cache cache = new Cache();
  DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);

  // sync after add
  cache.add(foo1);
  deltaFIFO.resync();

  Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> deltas =
      deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));

  assertEquals(1, deltas.size());
  assertEquals(foo1, deltas.peekLast().getRight());
  assertEquals(DeltaFIFO.DeltaType.Sync, deltas.peekLast().getLeft());
}
 
Example #14
Source File: DeltaFIFOTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeltaFIFOReplaceWithDeleteDeltaIn() throws InterruptedException {
  V1Pod oldPod = new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1"));
  V1Pod newPod = new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo2"));

  Cache mockCache = mock(Cache.class);
  doReturn(oldPod).when(mockCache).getByKey(Caches.deletionHandlingMetaNamespaceKeyFunc(oldPod));
  DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, mockCache);

  deltaFIFO.delete(oldPod);
  deltaFIFO.replace(Lists.newArrayList(newPod), "0");

  deltaFIFO.pop(
      (deltas) -> {
        assertEquals(DeltaFIFO.DeltaType.Deleted, deltas.getFirst().getLeft());
        assertEquals(oldPod, deltas.getFirst().getRight());
      });

  deltaFIFO.pop(
      (deltas) -> {
        assertEquals(DeltaFIFO.DeltaType.Sync, deltas.getFirst().getLeft());
        assertEquals(newPod, deltas.getFirst().getRight());
      });
}
 
Example #15
Source File: ListerTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testListerBasic() {
  Cache<V1Pod> podCache = new Cache<>();

  Lister<V1Pod> namespacedPodLister = new Lister<>(podCache, "default");
  List<V1Pod> emptyPodList = namespacedPodLister.list();
  assertEquals(0, emptyPodList.size());

  podCache.replace(
      Arrays.asList(
          new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default")),
          new V1Pod().metadata(new V1ObjectMeta().name("foo2").namespace("default")),
          new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default"))),
      "0");
  List<V1Pod> namespacedPodList = namespacedPodLister.list();
  assertEquals(3, namespacedPodList.size());

  Lister<V1Pod> allNamespacedPodLister = new Lister<>(podCache);
  List<V1Pod> allPodList = allNamespacedPodLister.list();
  assertEquals(3, allPodList.size());

  namespacedPodList = allNamespacedPodLister.namespace("default").list();
  assertEquals(3, namespacedPodList.size());
}
 
Example #16
Source File: K8sClient.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Download logs of the specified pod.
 *
 * @param fromPod Pod logs to be copied.
 * @param toFile Destination file of the logs.
 * @return A Future which completes once the download operation completes.
 */
public CompletableFuture<Void> downloadLogs(final V1Pod fromPod, final String toFile) {

    final AtomicInteger retryCount = new AtomicInteger(0);
    return Retry.withExpBackoff(LOG_DOWNLOAD_INIT_DELAY_MS, 10, LOG_DOWNLOAD_RETRY_COUNT, RETRY_MAX_DELAY_MS)
                .retryingOn(TestFrameworkException.class)
                .throwingOn(RuntimeException.class)
                .runInExecutor(() -> {
                    final String podName = fromPod.getMetadata().getName();
                    log.debug("Download logs from pod {}", podName);
                    try {
                        @Cleanup
                        InputStream logStream = logUtility.streamNamespacedPodLog(fromPod);
                        // On every retry this method attempts to download the complete pod logs from from K8s api-server. Due to the
                        // amount of logs for a pod and the K8s cluster configuration it can so happen that the K8s api-server can
                        // return truncated logs. Hence, every retry attempt does not overwrite the previously downloaded logs for
                        // the pod.
                        String logFile = toFile + "-" + retryCount.incrementAndGet() + ".log";
                        Files.copy(logStream, Paths.get(logFile));
                        log.debug("Logs downloaded from pod {} to {}", podName, logFile);
                    } catch (ApiException | IOException e) {
                        log.warn("Retryable error while downloading logs from pod {}. Error message: {} ", podName, e.getMessage());
                        throw new TestFrameworkException(TestFrameworkException.Type.RequestFailed, "Error while downloading logs");
                    }
                }, executor);
}
 
Example #17
Source File: DefaultDirectKubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<V1Pod> launchTask(Job job, Task task) {
    return Mono.fromCallable(() -> {
        Stopwatch timer = Stopwatch.createStarted();
        try {
            V1Pod v1Pod = taskToPodConverter.apply(job, task);
            logger.info("creating pod: {}", v1Pod);

            fitKubeInjection.ifPresent(i -> i.beforeImmediate(KubeFitAction.ErrorKind.POD_CREATE_ERROR.name()));

            kubeApiFacade.getCoreV1Api().createNamespacedPod(KUBERNETES_NAMESPACE, v1Pod, null, null, null);
            pods.putIfAbsent(task.getId(), v1Pod);

            metrics.launchSuccess(task, v1Pod, timer.elapsed(TimeUnit.MILLISECONDS));

            return v1Pod;
        } catch (Exception e) {
            logger.error("Unable to create pod with error: {}", KubeUtil.toErrorDetails(e), e);

            metrics.launchError(task, e, timer.elapsed(TimeUnit.MILLISECONDS));

            throw new IllegalStateException("Unable to launch a task " + task.getId(), e);
        }
    }).subscribeOn(apiClientScheduler).timeout(Duration.ofMillis(configuration.getKubeApiClientTimeoutMs()));
}
 
Example #18
Source File: PatchUtilsTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMergePatchPod() throws ApiException {
  CoreV1Api coreV1Api = new CoreV1Api(client);
  stubFor(
      patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo"))
          .withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json")
                  .withBody("{}")));

  PatchUtils.patch(
      V1Pod.class,
      () ->
          coreV1Api.patchNamespacedPodCall(
              "foo", "default", new V1Patch("[]"), null, null, null, null, null),
      V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH,
      client);

  verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
 
Example #19
Source File: PatchUtilsTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonPatchPod() throws ApiException {
  CoreV1Api coreV1Api = new CoreV1Api(client);
  stubFor(
      patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo"))
          .withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_JSON_PATCH))
          .willReturn(
              aResponse()
                  .withStatus(200)
                  .withHeader("Content-Type", "application/json")
                  .withBody("{}")));

  PatchUtils.patch(
      V1Pod.class,
      () ->
          coreV1Api.patchNamespacedPodCall(
              "foo", "default", new V1Patch("[]"), null, null, null, null, null),
      V1Patch.PATCH_FORMAT_JSON_PATCH,
      client);

  verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
 
Example #20
Source File: DefaultKubeApiFacade.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private SharedIndexInformer<V1Pod> createPodInformer(SharedInformerFactory sharedInformerFactory) {
    return sharedInformerFactory.sharedIndexInformerFor(
            (CallGeneratorParams params) -> coreV1Api.listNamespacedPodCall(
                    KUBERNETES_NAMESPACE,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    params.resourceVersion,
                    params.timeoutSeconds,
                    params.watch,
                    null
            ),
            V1Pod.class,
            V1PodList.class,
            configuration.getKubeApiServerIntegratorRefreshIntervalMs()
    );
}
 
Example #21
Source File: KubernetesCoordinator.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<RemoteInstance> queryRemoteNodes() {

    List<V1Pod> pods = NamespacedPodListInformer.INFORMER.listPods().orElseGet(this::selfPod);

    if (log.isDebugEnabled()) {
        List<String> uidList = pods
            .stream()
            .map(item -> item.getMetadata().getUid())
            .collect(Collectors.toList());
        log.debug("[kubernetes cluster pods uid list]:{}", uidList.toString());
    }

    if (port == -1) {
        port = manager.find(CoreModule.NAME).provider().getService(ConfigService.class).getGRPCPort();
    }

    return pods.stream()
               .map(pod -> new RemoteInstance(
                   new Address(pod.getStatus().getPodIP(), port, pod.getMetadata().getUid().equals(uid))))
               .collect(Collectors.toList());

}
 
Example #22
Source File: K8sClient.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Method to fetch the status of all pods which match a label.
 * @param namespace Namespace on which the pod(s) reside.
 * @param labelName Name of the label.
 * @param labelValue Value of the label.
 * @return Future representing the list of pod status.
 */
public CompletableFuture<List<V1PodStatus>> getStatusOfPodWithLabel(final String namespace, final String labelName, final String labelValue) {
    return getPodsWithLabel(namespace, labelName, labelValue)
            .thenApply(v1PodList -> {
                List<V1Pod> podList = v1PodList.getItems();
                log.debug("{} pod(s) found with label {}={}.", podList.size(), labelName, labelValue);
                return podList.stream().map(V1Pod::getStatus).collect(Collectors.toList());
            });
}
 
Example #23
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private void launchTasksConcurrently(List<TaskInfoRequest> requests) {
    List<Mono<Void>> podAddActions = new ArrayList<>(requests.size());
    for (TaskInfoRequest request : requests) {
        V1Pod v1Pod = taskInfoToPod(request);
        Mono<Void> podAddAction = KubeUtil
                .<V1Pod>toReact(handler -> kubeApiFacade.getCoreV1Api().createNamespacedPodAsync(
                        KUBERNETES_NAMESPACE, v1Pod, null, null, null, handler
                ))
                .doOnSubscribe(subscription -> {
                    launchTaskCounter.increment();
                    logger.info("creating pod: {}", v1Pod);
                    podSizeMetrics.record(KubeUtil.estimatePodSize(v1Pod));
                })
                .timeout(Duration.ofMillis(directKubeConfiguration.getKubeApiClientTimeoutMs()))
                .ignoreElement()
                .cast(Void.class)
                .onErrorResume(error -> {
                    logger.error("Unable to create pod with error: {}", KubeUtil.toErrorDetails(error), error);
                    return Mono.empty();
                });
        podAddActions.add(podAddAction);
    }

    try {
        Flux.mergeSequentialDelayError(Flux.fromIterable(podAddActions),
                directKubeConfiguration.getPodCreateConcurrencyLimit(),
                Queues.XS_BUFFER_SIZE
        ).blockLast();
    } catch (Exception e) {
        logger.error("Async pod create error: {}", KubeUtil.toErrorDetails(e), e);
    }
}
 
Example #24
Source File: DefaultTaskToPodConverter.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public V1Pod apply(Job<?> job, Task task) {
    String taskId = task.getId();
    TitanProtos.ContainerInfo containerInfo = buildContainerInfo(job, task);
    Map<String, String> annotations = KubeUtil.createPodAnnotations(job, task, containerInfo.toByteArray(),
            containerInfo.getPassthroughAttributesMap(), configuration.isJobDescriptorAnnotationEnabled());

    V1ObjectMeta metadata = new V1ObjectMeta()
            .name(taskId)
            .annotations(annotations)
            .labels(ImmutableMap.of(
                    KubeConstants.POD_LABEL_JOB_ID, job.getId(),
                    KubeConstants.POD_LABEL_TASK_ID, taskId
            ));

    V1Container container = new V1Container()
            .name(taskId)
            .image("imageIsInContainerInfo")
            .resources(buildV1ResourceRequirements(job.getJobDescriptor().getContainer().getContainerResources()));

    V1PodSpec spec = new V1PodSpec()
            .schedulerName(configuration.getKubeSchedulerName())
            .containers(Collections.singletonList(container))
            .terminationGracePeriodSeconds(POD_TERMINATION_GRACE_PERIOD_SECONDS)
            .restartPolicy(NEVER_RESTART_POLICY)
            .affinity(podAffinityFactory.buildV1Affinity(job, task))
            .tolerations(taintTolerationFactory.buildV1Toleration(job, task))
            .topologySpreadConstraints(buildTopologySpreadConstraints(job));

    return new V1Pod().metadata(metadata).spec(spec);
}
 
Example #25
Source File: ControllerWatchTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnAdd() {
  WorkQueue<Request> workQueue = new DefaultWorkQueue<>();
  DefaultControllerWatch<V1Pod> controllerWatch =
      new DefaultControllerWatch(
          V1Pod.class, workQueue, Controllers.defaultReflectiveKeyFunc(), Duration.ZERO);
  controllerWatch.getResourceEventHandler().onAdd(testPod);
  assertEquals(1, workQueue.length());

  controllerWatch.setOnAddFilterPredicate((V1Pod addedPod) -> false);
  controllerWatch.getResourceEventHandler().onAdd(testPod);
  assertEquals(1, workQueue.length());
}
 
Example #26
Source File: ControllerWatchTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnDelete() {
  WorkQueue<Request> workQueue = new DefaultWorkQueue<>();
  DefaultControllerWatch<V1Pod> controllerWatch =
      new DefaultControllerWatch(
          V1Pod.class, workQueue, Controllers.defaultReflectiveKeyFunc(), Duration.ZERO);
  controllerWatch.getResourceEventHandler().onDelete(testPod, false);
  assertEquals(1, workQueue.length());

  controllerWatch.setOnDeleteFilterPredicate((V1Pod newPod, Boolean stateUnknown) -> false);
  controllerWatch.getResourceEventHandler().onDelete(testPod, false);
  assertEquals(1, workQueue.length());
}
 
Example #27
Source File: PodWatchUtils.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
   * get the IP of the node where the pod with that name is running
   */
  public static String getNodeIP(String namespace, String jobID, String podIP) {

    if (apiClient == null || coreApi == null) {
      createApiInstances();
    }

    // this is better but it does not work with another installation
//    String podNameLabel = "statefulset.kubernetes.io/pod-name=" + podName;
    String workerRoleLabel = KubernetesUtils.workerPodLabelSelector(jobID);

    V1PodList podList = null;
    try {
      podList = coreApi.listNamespacedPod(
          namespace, null, null, null, null, workerRoleLabel, null, null, null, null);
    } catch (ApiException e) {
      LOG.log(Level.SEVERE, "Exception when getting PodList.", e);
      throw new RuntimeException(e);
    }

    for (V1Pod pod : podList.getItems()) {
      if (podIP.equals(pod.getStatus().getPodIP())) {
        return pod.getStatus().getHostIP();
      }
    }

    return null;
  }
 
Example #28
Source File: DeltaFIFOTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeltaFIFODedup() {
  V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
  Cache cache = new Cache();
  DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
  Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> deltas;

  // add-delete dedup
  deltaFIFO.add(foo1);
  deltaFIFO.delete(foo1);
  deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
  assertEquals(DeltaFIFO.DeltaType.Deleted, deltas.peekLast().getLeft());
  assertEquals(foo1, deltas.peekLast().getRight());
  assertEquals(DeltaFIFO.DeltaType.Added, deltas.peekFirst().getLeft());
  assertEquals(foo1, deltas.peekFirst().getRight());
  assertEquals(2, deltas.size());
  deltaFIFO.getItems().remove(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));

  // add-delete-delete dedup
  deltaFIFO.add(foo1);
  deltaFIFO.delete(foo1);
  deltaFIFO.delete(foo1);
  deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
  assertEquals(DeltaFIFO.DeltaType.Deleted, deltas.peekLast().getLeft());
  assertEquals(foo1, deltas.peekLast().getRight());
  assertEquals(DeltaFIFO.DeltaType.Added, deltas.peekFirst().getLeft());
  assertEquals(foo1, deltas.peekFirst().getRight());
  assertEquals(2, deltas.size());
  deltaFIFO.getItems().remove(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
}
 
Example #29
Source File: PodLogsTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotFound() throws ApiException, IOException {
  V1Pod pod =
      new V1Pod()
          .metadata(new V1ObjectMeta().name(podName).namespace(namespace))
          .spec(
              new V1PodSpec()
                  .containers(Arrays.asList(new V1Container().name(container).image("nginx"))));

  stubFor(
      get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log"))
          .willReturn(
              aResponse()
                  .withStatus(404)
                  .withHeader("Content-Type", "text/plain")
                  .withBody("Not Found")));

  PodLogs logs = new PodLogs(client);
  boolean thrown = false;
  try {
    logs.streamNamespacedPodLog(pod);
  } catch (ApiException ex) {
    assertEquals(404, ex.getCode());
    thrown = true;
  }
  assertEquals(thrown, true);
  verify(
      getRequestedFor(
              urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log"))
          .withQueryParam("container", equalTo(container))
          .withQueryParam("follow", equalTo("true"))
          .withQueryParam("pretty", equalTo("false"))
          .withQueryParam("previous", equalTo("false"))
          .withQueryParam("timestamps", equalTo("false")));
}
 
Example #30
Source File: Exec.java    From java with Apache License 2.0 5 votes vote down vote up
public Process execute() throws ApiException, IOException {
  if (container == null) {
    CoreV1Api api = new CoreV1Api(apiClient);
    V1Pod pod = api.readNamespacedPod(name, namespace, "false", null, null);
    container = pod.getSpec().getContainers().get(0).getName();
  }

  ExecProcess exec = new ExecProcess(apiClient);
  WebSocketStreamHandler handler = exec.getHandler();
  WebSockets.stream(makePath(), "GET", apiClient, handler);

  return exec;
}