Search in sources :

Example 41 with ApiClient

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

the class ClientBuilderTest method testDefaultClientReadsKubeConfigMultiple.

@Test
public void testDefaultClientReadsKubeConfigMultiple() throws Exception {
    final String kubeConfigEnv = KUBECONFIG_FILE_PATH + File.pathSeparator + "/non-existent";
    String path = withEnvironmentVariable("KUBECONFIG", kubeConfigEnv).execute(() -> {
        final ApiClient client = ClientBuilder.defaultClient();
        return client.getBasePath();
    });
    assertEquals("http://kubeconfig.dir.com", path);
}
Also used : ApiClient(io.kubernetes.client.openapi.ApiClient) Test(org.junit.Test)

Example 42 with ApiClient

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

the class ClientCertificateAuthenticationTest method testValidOldECCertificates.

@Test
public void testValidOldECCertificates() throws Exception {
    try {
        final ApiClient client = new ApiClient();
        final byte[] certificate = Files.readAllBytes(Paths.get(CLIENT_EC_CERT_PATH));
        final byte[] key = Files.readAllBytes(Paths.get(CLIENT_EC_KEY_OLD_PATH));
        new ClientCertificateAuthentication(certificate, key).provide(client);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ApiClient(io.kubernetes.client.openapi.ApiClient) Test(org.junit.Test)

Example 43 with ApiClient

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

the class DynamicClientExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient apiClient = ClientBuilder.standard().build();
    // retrieving the latest state of the default namespace
    DynamicKubernetesApi dynamicApi = new DynamicKubernetesApi("", "v1", "namespaces", apiClient);
    DynamicKubernetesObject defaultNamespace = dynamicApi.get("default").throwsApiException().getObject();
    // attaching a "foo=bar" label to the default namespace
    defaultNamespace.setMetadata(defaultNamespace.getMetadata().putLabelsItem("foo", "bar"));
    DynamicKubernetesObject updatedDefaultNamespace = dynamicApi.update(defaultNamespace).throwsApiException().getObject();
    System.out.println(updatedDefaultNamespace);
    apiClient.getHttpClient().connectionPool().evictAll();
}
Also used : DynamicKubernetesApi(io.kubernetes.client.util.generic.dynamic.DynamicKubernetesApi) DynamicKubernetesObject(io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject) ApiClient(io.kubernetes.client.openapi.ApiClient)

Example 44 with ApiClient

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

the class ExecExample method main.

public static void main(String[] args) throws IOException, ApiException, InterruptedException, ParseException {
    final Options options = new Options();
    options.addOption(new Option("p", "pod", true, "The name of the pod"));
    options.addOption(new Option("n", "namespace", true, "The namespace of the pod"));
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);
    String podName = cmd.getOptionValue("p", "nginx-dbddb74b8-s4cx5");
    String namespace = cmd.getOptionValue("n", "default");
    args = cmd.getArgs();
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    Exec exec = new Exec();
    boolean tty = System.console() != null;
    // final Process proc = exec.exec("default", "nginx-4217019353-k5sn9", new String[]
    // {"sh", "-c", "echo foo"}, true, tty);
    final Process proc = exec.exec(namespace, podName, args.length == 0 ? new String[] { "sh" } : args, true, tty);
    Thread in = new Thread(new Runnable() {

        public void run() {
            try {
                Streams.copy(System.in, proc.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    in.start();
    Thread out = new Thread(new Runnable() {

        public void run() {
            try {
                Streams.copy(proc.getInputStream(), System.out);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    out.start();
    proc.waitFor();
    // wait for any last output; no need to wait for input thread
    out.join();
    proc.destroy();
    System.exit(proc.exitValue());
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) ApiClient(io.kubernetes.client.openapi.ApiClient) Exec(io.kubernetes.client.Exec) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 45 with ApiClient

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

the class FluentExample method main.

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

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