Search in sources :

Example 6 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.

the class K8sClient method deployPod.

/**
 * Deploy a pod. This ignores exception when the pod has already been deployed.
 * @param namespace Namespace.
 * @param pod Pod details.
 * @return Future which is completed once the deployemnt has been triggered.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<V1Pod> deployPod(final String namespace, final V1Pod pod) {
    CoreV1Api api = new CoreV1Api();
    K8AsyncCallback<V1Pod> callback = new K8AsyncCallback<>("createPod-" + pod.getMetadata().getName());
    api.createNamespacedPodAsync(namespace, pod, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER, callback);
    return exceptionallyExpecting(callback.getFuture(), isConflict, null);
}
Also used : V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) SneakyThrows(lombok.SneakyThrows)

Example 7 with CoreV1Api

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

the class YamlExample method main.

public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException {
    V1Pod pod = new V1PodBuilder().withNewMetadata().withName("apod").endMetadata().withNewSpec().addNewContainer().withName("www").withImage("nginx").withNewResources().withLimits(new HashMap<>()).endResources().endContainer().endSpec().build();
    System.out.println(Yaml.dump(pod));
    V1Service svc = new V1ServiceBuilder().withNewMetadata().withName("aservice").endMetadata().withNewSpec().withSessionAffinity(V1ServiceSpec.SessionAffinityEnum.CLIENTIP).withType(V1ServiceSpec.TypeEnum.NODEPORT).addNewPort().withProtocol(V1ServicePort.ProtocolEnum.TCP).withName("client").withPort(8008).withNodePort(8080).withTargetPort(new IntOrString(8080)).endPort().endSpec().build();
    System.out.println(Yaml.dump(svc));
    // Read yaml configuration file, and deploy it
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // See issue #474. Not needed at most cases, but it is needed if you are using war
    // packging or running this on JUnit.
    Yaml.addModelMap("v1", "Service", V1Service.class);
    // Example yaml file can be found in $REPO_DIR/test-svc.yaml
    File file = new File("test-svc.yaml");
    V1Service yamlSvc = (V1Service) Yaml.load(file);
    // Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of
    // CoreV1API
    CoreV1Api api = new CoreV1Api();
    V1Service createResult = api.createNamespacedService("default", yamlSvc, null, null, null, null);
    System.out.println(createResult);
    V1Service deleteResult = api.deleteNamespacedService(yamlSvc.getMetadata().getName(), "default", null, null, null, null, null, new V1DeleteOptions());
    System.out.println(deleteResult);
}
Also used : V1DeleteOptions(io.kubernetes.client.openapi.models.V1DeleteOptions) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) IntOrString(io.kubernetes.client.custom.IntOrString) V1ServiceBuilder(io.kubernetes.client.openapi.models.V1ServiceBuilder) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1Service(io.kubernetes.client.openapi.models.V1Service) ApiClient(io.kubernetes.client.openapi.ApiClient) File(java.io.File) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 8 with CoreV1Api

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

the class LeaderElectorTest method deleteConfigMapLockResource.

private void deleteConfigMapLockResource() throws Exception {
    try {
        CoreV1Api coreV1Api = new CoreV1Api(apiClient);
        coreV1Api.deleteNamespacedConfigMap(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
    } catch (ApiException ex) {
        if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
            throw ex;
        }
    }
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Example 9 with CoreV1Api

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

the class LeaderElectorTest method deleteEndpointsLockResource.

private void deleteEndpointsLockResource() throws Exception {
    try {
        CoreV1Api coreV1Api = new CoreV1Api(apiClient);
        coreV1Api.deleteNamespacedEndpoints(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
    } catch (ApiException ex) {
        if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
            throw ex;
        }
    }
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Example 10 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api 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)

Aggregations

CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)48 V1Pod (io.kubernetes.client.openapi.models.V1Pod)18 ApiClient (io.kubernetes.client.openapi.ApiClient)16 Test (org.junit.Test)16 ApiException (io.kubernetes.client.openapi.ApiException)13 V1PodList (io.kubernetes.client.openapi.models.V1PodList)13 SneakyThrows (lombok.SneakyThrows)12 IOException (java.io.IOException)11 V1Namespace (io.kubernetes.client.openapi.models.V1Namespace)10 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)10 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)9 Watch (io.kubernetes.client.util.Watch)9 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)8 JSON (io.kubernetes.client.openapi.JSON)6 V1Status (io.kubernetes.client.openapi.models.V1Status)6 OkHttpClient (okhttp3.OkHttpClient)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5