Search in sources :

Example 46 with ApiClient

use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.

the class InClusterClientExample method main.

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, null);
    for (V1Pod item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 47 with ApiClient

use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.

the class LeaderElectionExample method main.

public static void main(String[] args) throws Exception {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // New
    String appNamespace = "default";
    String appName = "leader-election-foobar";
    // Anything unique
    String lockHolderIdentityName = UUID.randomUUID().toString();
    EndpointsLock lock = new EndpointsLock(appNamespace, appName, lockHolderIdentityName);
    LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(lock, Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(2000));
    try (LeaderElector leaderElector = new LeaderElector(leaderElectionConfig)) {
        leaderElector.run(() -> {
            System.out.println("Do something when getting leadership.");
        }, () -> {
            System.out.println("Do something when losing leadership.");
        });
    }
}
Also used : EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) ApiClient(io.kubernetes.client.openapi.ApiClient) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)

Example 48 with ApiClient

use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.

the class MetricsExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    Metrics metrics = new Metrics(client);
    NodeMetricsList list = metrics.getNodeMetrics();
    for (NodeMetrics item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
        System.out.println("------------------------------");
        for (String key : item.getUsage().keySet()) {
            System.out.println("\t" + key);
            System.out.println("\t" + item.getUsage().get(key));
        }
        System.out.println();
    }
    for (PodMetrics item : metrics.getPodMetrics("default").getItems()) {
        System.out.println(item.getMetadata().getName());
        System.out.println("------------------------------");
        if (item.getContainers() == null) {
            continue;
        }
        for (ContainerMetrics container : item.getContainers()) {
            System.out.println(container.getName());
            System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
            for (String key : container.getUsage().keySet()) {
                System.out.println("\t" + key);
                System.out.println("\t" + container.getUsage().get(key));
            }
            System.out.println();
        }
    }
}
Also used : Metrics(io.kubernetes.client.Metrics) PodMetrics(io.kubernetes.client.custom.PodMetrics) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) ContainerMetrics(io.kubernetes.client.custom.ContainerMetrics) NodeMetricsList(io.kubernetes.client.custom.NodeMetricsList) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) PodMetrics(io.kubernetes.client.custom.PodMetrics) ApiClient(io.kubernetes.client.openapi.ApiClient) ContainerMetrics(io.kubernetes.client.custom.ContainerMetrics)

Example 49 with ApiClient

use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.

the class ParseExample method main.

public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException {
    if (args.length < 2) {
        System.err.println("Usage: ParseExample <file-name> <class-name e.g. V1Pod>");
        System.exit(1);
    }
    ApiClient client = Config.defaultClient();
    FileReader json = new FileReader(args[0]);
    Object obj = client.getJSON().getGson().fromJson(json, Class.forName("io.kubernetes.client.models." + args[1]));
    String output = client.getJSON().getGson().toJson(obj);
    // Test round tripping...
    Object obj2 = client.getJSON().getGson().fromJson(new StringReader(output), Class.forName("io.kubernetes.client.models." + args[1]));
    String output2 = client.getJSON().getGson().toJson(obj2);
    // Validate round trip
    if (!output.equals(output2)) {
        System.err.println("Error, expected:\n" + output + "\nto equal\n" + output2);
        System.exit(2);
    }
    System.out.println(output);
}
Also used : StringReader(java.io.StringReader) FileReader(java.io.FileReader) ApiClient(io.kubernetes.client.openapi.ApiClient)

Example 50 with ApiClient

use of io.kubernetes.client.openapi.ApiClient in project java by kubernetes-client.

the class PortForwardExample method main.

public static void main(String[] args) throws IOException, ApiException, InterruptedException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    PortForward forward = new PortForward();
    List<Integer> ports = new ArrayList<>();
    int localPort = 8080;
    int targetPort = 8080;
    ports.add(targetPort);
    final PortForward.PortForwardResult result = forward.forward("default", "camera-viz-7949dbf7c6-lpxkd", ports);
    System.out.println("Forwarding!");
    ServerSocket ss = new ServerSocket(localPort);
    final Socket s = ss.accept();
    System.out.println("Connected!");
    new Thread(new Runnable() {

        public void run() {
            try {
                Streams.copy(result.getInputStream(targetPort), s.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {

        public void run() {
            try {
                Streams.copy(s.getInputStream(), result.getOutboundStream(targetPort));
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }).start();
    Thread.sleep(10 * 1000);
    System.exit(0);
}
Also used : ArrayList(java.util.ArrayList) PortForward(io.kubernetes.client.PortForward) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ApiClient(io.kubernetes.client.openapi.ApiClient) ApiException(io.kubernetes.client.openapi.ApiException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

ApiClient (io.kubernetes.client.openapi.ApiClient)61 Test (org.junit.Test)28 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)13 V1Pod (io.kubernetes.client.openapi.models.V1Pod)12 IOException (java.io.IOException)11 V1PodList (io.kubernetes.client.openapi.models.V1PodList)9 ClientBuilder (io.kubernetes.client.util.ClientBuilder)6 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)5 Before (org.junit.Before)5 ApiException (io.kubernetes.client.openapi.ApiException)4 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)3 V1Job (io.kubernetes.client.openapi.models.V1Job)3 V1JobList (io.kubernetes.client.openapi.models.V1JobList)3 V1Namespace (io.kubernetes.client.openapi.models.V1Namespace)3 OkHttpClient (okhttp3.OkHttpClient)3 NodeMetrics (io.kubernetes.client.custom.NodeMetrics)2 PodMetrics (io.kubernetes.client.custom.PodMetrics)2 V1Patch (io.kubernetes.client.custom.V1Patch)2 LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)2 LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)2