Search in sources :

Example 6 with ApiClient

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

the class InformerExample method main.

public static void main(String[] args) throws Exception {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    SharedInformerFactory factory = new SharedInformerFactory(apiClient);
    // Node informer
    SharedIndexInformer<V1Node> nodeInformer = factory.sharedIndexInformerFor(// the informer-factory.
    (CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    nodeInformer.addEventHandler(new ResourceEventHandler<V1Node>() {

        @Override
        public void onAdd(V1Node node) {
            System.out.printf("%s node added!\n", node.getMetadata().getName());
        }

        @Override
        public void onUpdate(V1Node oldNode, V1Node newNode) {
            System.out.printf("%s => %s node updated!\n", oldNode.getMetadata().getName(), newNode.getMetadata().getName());
        }

        @Override
        public void onDelete(V1Node node, boolean deletedFinalStateUnknown) {
            System.out.printf("%s node deleted!\n", node.getMetadata().getName());
        }
    });
    factory.startAllRegisteredInformers();
    V1Node nodeToCreate = new V1Node();
    V1ObjectMeta metadata = new V1ObjectMeta();
    metadata.setName("noxu");
    nodeToCreate.setMetadata(metadata);
    V1Node createdNode = coreV1Api.createNode(nodeToCreate, null, null, null, null);
    Thread.sleep(3000);
    Lister<V1Node> nodeLister = new Lister<V1Node>(nodeInformer.getIndexer());
    V1Node node = nodeLister.get("noxu");
    System.out.printf("noxu created! %s\n", node.getMetadata().getCreationTimestamp());
    factory.stopAllRegisteredInformers();
    Thread.sleep(3000);
    System.out.println("informer stopped..");
}
Also used : OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Lister(io.kubernetes.client.informer.cache.Lister) ApiClient(io.kubernetes.client.openapi.ApiClient) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 7 with ApiClient

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

the class KubeConfigFileClientExample method main.

public static void main(String[] args) throws IOException, ApiException {
    // file path to your KubeConfig
    String kubeConfigPath = System.getenv("HOME") + "/.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, 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) FileReader(java.io.FileReader) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 8 with ApiClient

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

the class PrometheusExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // Install an HTTP Interceptor that adds metrics
    Monitoring.installMetrics(client);
    // Install a simple HTTP server to serve prometheus metrics. If you already are serving
    // metrics elsewhere, this is unnecessary.
    Monitoring.startMetricsServer("localhost", 8080);
    CoreV1Api api = new CoreV1Api();
    while (true) {
        // A request that should return 200
        V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        // A request that should return 404
        try {
            V1Pod pod = api.readNamespacedPod("foo", "bar", null);
        } catch (ApiException ex) {
            if (ex.getCode() != 404) {
                throw ex;
            }
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
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) ApiException(io.kubernetes.client.openapi.ApiException)

Example 9 with ApiClient

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

the class ClientBuilder method build.

public ApiClient build() {
    final ApiClient client = new ApiClient();
    client.setHttpClient(client.getHttpClient().newBuilder().protocols(protocols).readTimeout(this.readTimeout).pingInterval(pingInterval).build());
    if (basePath != null) {
        if (basePath.endsWith("/")) {
            basePath = basePath.substring(0, basePath.length() - 1);
        }
        client.setBasePath(basePath);
    }
    client.setVerifyingSsl(verifyingSsl);
    if (authentication != null) {
        if (StringUtils.isNotEmpty(keyStorePassphrase)) {
            if (authentication instanceof KubeconfigAuthentication) {
                if (((KubeconfigAuthentication) authentication).getDelegateAuthentication() instanceof ClientCertificateAuthentication) {
                    ((ClientCertificateAuthentication) (((KubeconfigAuthentication) authentication).getDelegateAuthentication())).setPassphrase(keyStorePassphrase);
                }
            }
        }
        authentication.provide(client);
    }
    // TODO: Add a test to ensure that this works correctly...
    if (caCertBytes != null) {
        client.setSslCaCert(new ByteArrayInputStream(caCertBytes));
    }
    return client;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ApiClient(io.kubernetes.client.openapi.ApiClient) ClientCertificateAuthentication(io.kubernetes.client.util.credentials.ClientCertificateAuthentication) KubeconfigAuthentication(io.kubernetes.client.util.credentials.KubeconfigAuthentication)

Example 10 with ApiClient

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

the class AccessTokenAuthenticationTest method testTokenProvided.

@Test
public void testTokenProvided() {
    final ApiClient client = new ApiClient();
    new AccessTokenAuthentication("token").provide(client);
    assertThat(getApiKeyAuthFromClient(client).getApiKeyPrefix(), is("Bearer"));
    assertThat(getApiKeyAuthFromClient(client).getApiKey(), is("token"));
}
Also used : ApiClient(io.kubernetes.client.openapi.ApiClient) Test(org.junit.Test)

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