Java Code Examples for io.kubernetes.client.openapi.models.V1PodList#getItems()

The following examples show how to use io.kubernetes.client.openapi.models.V1PodList#getItems() . 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: KubeConfigFileClientExample.java    From java with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws IOException, ApiException {

    // file path to your KubeConfig
    String kubeConfigPath = "~/.kube/config";

    // loading the out-of-cluster config, a kubeconfig from file-system
    ApiClient client =
        ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();

    // set the global default api-client to the in-cluster one from above
    Configuration.setDefaultApiClient(client);

    // the CoreV1Api loads default api-client from global configuration.
    CoreV1Api api = new CoreV1Api();

    // invokes the CoreV1Api client
    V1PodList list =
        api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
      System.out.println(item.getMetadata().getName());
    }
  }
 
Example 2
Source File: InClusterClientExample.java    From java with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, ApiException {

    // loading the in-cluster config, including:
    //   1. service-account CA
    //   2. service-account bearer-token
    //   3. service-account namespace
    //   4. master endpoints(ip, port) from pre-set environment variables
    ApiClient client = ClientBuilder.cluster().build();

    // if you prefer not to refresh service account token, please use:
    // ApiClient client = ClientBuilder.oldCluster().build();

    // set the global default api-client to the in-cluster one from above
    Configuration.setDefaultApiClient(client);

    // the CoreV1Api loads default api-client from global configuration.
    CoreV1Api api = new CoreV1Api();

    // invokes the CoreV1Api client
    V1PodList list =
        api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
      System.out.println(item.getMetadata().getName());
    }
  }
 
Example 3
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 4
Source File: Example.java    From java with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, ApiException {
  ApiClient client = Config.defaultClient();
  Configuration.setDefaultApiClient(client);

  CoreV1Api api = new CoreV1Api();
  V1PodList list =
      api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
  for (V1Pod item : list.getItems()) {
    System.out.println(item.getMetadata().getName());
  }
}
 
Example 5
Source File: FluentExample.java    From java with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, ApiException {
  ApiClient client = Config.defaultClient();
  Configuration.setDefaultApiClient(client);

  CoreV1Api api = new CoreV1Api();

  V1Pod pod =
      new V1PodBuilder()
          .withNewMetadata()
          .withName("apod")
          .endMetadata()
          .withNewSpec()
          .addNewContainer()
          .withName("www")
          .withImage("nginx")
          .endContainer()
          .endSpec()
          .build();

  api.createNamespacedPod("default", pod, null, null, null);

  V1Pod pod2 =
      new V1Pod()
          .metadata(new V1ObjectMeta().name("anotherpod"))
          .spec(
              new V1PodSpec()
                  .containers(Arrays.asList(new V1Container().name("www").image("nginx"))));

  api.createNamespacedPod("default", pod2, null, null, null);

  V1PodList list =
      api.listNamespacedPod("default", null, null, null, null, null, null, null, null, null);
  for (V1Pod item : list.getItems()) {
    System.out.println(item.getMetadata().getName());
  }
}
 
Example 6
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;
  }