Java Code Examples for io.kubernetes.client.openapi.apis.CoreV1Api#listPodForAllNamespaces()

The following examples show how to use io.kubernetes.client.openapi.apis.CoreV1Api#listPodForAllNamespaces() . 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: TokenFileAuthenticationTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void testTokenProvided() throws IOException, ApiException {
  stubFor(
      get(urlPathEqualTo("/api/v1/pods")).willReturn(okForContentType("application/json", "{}")));
  CoreV1Api api = new CoreV1Api();

  api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
  WireMock.verify(
      1,
      getRequestedFor(urlPathEqualTo("/api/v1/pods"))
          .withHeader("Authorization", equalTo("Bearer token1")));

  this.auth.setFile(SERVICEACCOUNT_TOKEN2_PATH);
  api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
  WireMock.verify(
      2,
      getRequestedFor(urlPathEqualTo("/api/v1/pods"))
          .withHeader("Authorization", equalTo("Bearer token1")));

  this.auth.setExpiry(Instant.now().minusSeconds(1));
  api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
  WireMock.verify(
      1,
      getRequestedFor(urlPathEqualTo("/api/v1/pods"))
          .withHeader("Authorization", equalTo("Bearer token2")));
}
 
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: DemoApplication.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/hello")
public String hello() throws Exception {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);

    CoreV1Api api = new CoreV1Api();
    V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);

    List<String> rlt = new ArrayList<>();
    rlt.add(new Date().toString());
    rlt.addAll(list.getItems().stream().map(value -> value.getMetadata().getNamespace() + ":" + value.getMetadata().getName()).collect(Collectors.toList()));
    return new Gson().toJson(rlt);
}