Search in sources :

Example 56 with ApiClient

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

the class ExampleTest method exactUrlOnly.

@Test
public void exactUrlOnly() throws IOException, ApiException {
    ApiClient client = new ApiClient();
    client.setBasePath("http://localhost:" + PORT);
    Configuration.setDefaultApiClient(client);
    V1Namespace ns1 = new V1Namespace().metadata(new V1ObjectMeta().name("name"));
    stubFor(get(urlEqualTo("/api/v1/namespaces/name")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(client.getJSON().serialize(ns1))));
    CoreV1Api api = new CoreV1Api();
    V1Namespace ns2 = api.readNamespace("name", null);
    assertEquals(ns1, ns2);
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 57 with ApiClient

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

the class Example method main.

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, 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 58 with ApiClient

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

the class ExpandedExample method main.

/**
 * Main method
 *
 * @param args
 */
public static void main(String[] args) {
    try {
        ApiClient client = Config.defaultClient();
        // To change the context of k8s cluster, you can use
        // io.kubernetes.client.util.KubeConfig
        Configuration.setDefaultApiClient(client);
        COREV1_API = new CoreV1Api(client);
        // ScaleUp/ScaleDown the Deployment pod
        // Please change the name of Deployment?
        System.out.println("----- Scale Deployment Start -----");
        scaleDeployment("account-service", 5);
        // List all of the namaspaces and pods
        List<String> nameSpaces = getAllNameSpaces();
        nameSpaces.stream().forEach(namespace -> {
            try {
                System.out.println("----- " + namespace + " -----");
                getNamespacedPod(namespace).stream().forEach(System.out::println);
            } catch (ApiException ex) {
                LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex);
            }
        });
        // Print all of the Services
        System.out.println("----- Print list all Services Start -----");
        List<String> services = getServices();
        services.stream().forEach(System.out::println);
        System.out.println("----- Print list all Services End -----");
        // Print log of specific pod. In this example show the first pod logs.
        System.out.println("----- Print Log of Specific Pod Start -----");
        String firstPodName = getPods().get(0);
        printLog(DEFAULT_NAME_SPACE, firstPodName);
        System.out.println("----- Print Log of Specific Pod End -----");
    } catch (ApiException | IOException ex) {
        LOGGER.warn("Exception had occured ", ex);
    }
}
Also used : IOException(java.io.IOException) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Example 59 with ApiClient

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

the class AttachExample method main.

public static void main(String[] args) throws IOException, ApiException, InterruptedException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    Attach attach = new Attach();
    final Attach.AttachResult result = attach.attach("default", "nginx-4217019353-k5sn9", true);
    new Thread(new Runnable() {

        public void run() {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            OutputStream output = result.getStandardInputStream();
            try {
                while (true) {
                    String line = in.readLine();
                    output.write(line.getBytes());
                    output.write('\n');
                    output.flush();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {

        public void run() {
            try {
                Streams.copy(result.getStandardOutputStream(), System.out);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }).start();
    Thread.sleep(10 * 1000);
    result.close();
    System.exit(0);
}
Also used : InputStreamReader(java.io.InputStreamReader) Attach(io.kubernetes.client.Attach) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ApiClient(io.kubernetes.client.openapi.ApiClient)

Example 60 with ApiClient

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

the class ControllerExample method main.

public static void main(String[] args) throws IOException {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    // instantiating an informer-factory, and there should be only one informer-factory
    // globally.
    SharedInformerFactory informerFactory = new SharedInformerFactory();
    // registering node-informer into the informer-factory.
    SharedIndexInformer<V1Node> nodeInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    informerFactory.startAllRegisteredInformers();
    EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api);
    // nodeReconciler prints node information on events
    NodePrintingReconciler nodeReconciler = new NodePrintingReconciler(nodeInformer, eventBroadcaster.newRecorder(new V1EventSource().host("localhost").component("node-printer")));
    // Use builder library to construct a default controller.
    Controller controller = ControllerBuilder.defaultBuilder(informerFactory).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue).withWorkQueueKeyFunc((V1Node node) -> new Request(node.getMetadata().getName())).withOnAddFilter((V1Node createdNode) -> createdNode.getMetadata().getName().startsWith(// optional, set onAdd filter
    "docker-")).withOnUpdateFilter((V1Node oldNode, V1Node newNode) -> newNode.getMetadata().getName().startsWith(// optional, set onUpdate filter
    "docker-")).withOnDeleteFilter((V1Node deletedNode, Boolean stateUnknown) -> deletedNode.getMetadata().getName().startsWith(// optional, set onDelete filter
    "docker-")).build()).withReconciler(// required, set the actual reconciler
    nodeReconciler).withName(// optional, set name for controller
    "node-printing-controller").withWorkerCount(// optional, set worker thread count
    4).withReadyFunc(// optional, only starts controller when the
    nodeInformer::hasSynced).build();
    // Use builder library to manage one or multiple controllers.
    ControllerManager controllerManager = ControllerBuilder.controllerManagerBuilder(informerFactory).addController(controller).build();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(new EndpointsLock("kube-system", "leader-election", "foo"), Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(5000))), controllerManager);
    leaderElectingController.run();
}
Also used : EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) V1NodeList(io.kubernetes.client.openapi.models.V1NodeList) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) Result(io.kubernetes.client.extended.controller.reconciler.Result) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) V1Node(io.kubernetes.client.openapi.models.V1Node) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) ApiClient(io.kubernetes.client.openapi.ApiClient) Request(io.kubernetes.client.extended.controller.reconciler.Request) Lister(io.kubernetes.client.informer.cache.Lister) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) Duration(java.time.Duration) EventRecorder(io.kubernetes.client.extended.event.legacy.EventRecorder) EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) EventType(io.kubernetes.client.extended.event.EventType) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) Controller(io.kubernetes.client.extended.controller.Controller) ControllerBuilder(io.kubernetes.client.extended.controller.builder.ControllerBuilder) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) Request(io.kubernetes.client.extended.controller.reconciler.Request) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) ApiClient(io.kubernetes.client.openapi.ApiClient) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

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