Search in sources :

Example 11 with V1PodSpec

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

the class DefaultControllerBuilderTest method testBuildWatchEventNotificationShouldWork.

@Test
public void testBuildWatchEventNotificationShouldWork() throws InterruptedException {
    V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("test-pod1")).spec(new V1PodSpec().hostname("hostname1"))));
    stubFor(get(urlPathEqualTo("/api/v1/pods")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
    CoreV1Api api = new CoreV1Api(client);
    SharedIndexInformer<V1Pod> podInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        return api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Pod.class, V1PodList.class);
    List<Request> keyFuncReceivingRequests = new ArrayList<>();
    Function<V1Pod, Request> podKeyFunc = (V1Pod pod) -> {
        // twisting pod name key
        Request request = new Request(pod.getSpec().getHostname() + "/" + pod.getMetadata().getName());
        keyFuncReceivingRequests.add(request);
        return request;
    };
    List<Request> controllerReceivingRequests = new ArrayList<>();
    final Semaphore latch = new Semaphore(1);
    latch.acquire();
    final Controller testController = ControllerBuilder.defaultBuilder(informerFactory).withReconciler(new Reconciler() {

        @Override
        public Result reconcile(Request request) {
            controllerReceivingRequests.add(request);
            latch.release();
            return new Result(false);
        }
    }).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).withWorkQueueKeyFunc(podKeyFunc).build()).build();
    controllerThead.submit(testController::run);
    informerFactory.startAllRegisteredInformers();
    // Wait for the request to be processed.
    latch.acquire(1);
    Request expectedRequest = new Request("hostname1/test-pod1");
    assertEquals(1, keyFuncReceivingRequests.size());
    assertEquals(expectedRequest, keyFuncReceivingRequests.get(0));
    assertEquals(1, controllerReceivingRequests.size());
    assertEquals(expectedRequest, controllerReceivingRequests.get(0));
}
Also used : V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) JSON(io.kubernetes.client.openapi.JSON) Arrays(java.util.Arrays) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Function(java.util.function.Function) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) WireMock(com.github.tomakehurst.wiremock.client.WireMock) ApiClient(io.kubernetes.client.openapi.ApiClient) ArrayList(java.util.ArrayList) Request(io.kubernetes.client.extended.controller.reconciler.Request) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) After(org.junit.After) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Controller(io.kubernetes.client.extended.controller.Controller) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Executors(java.util.concurrent.Executors) ClientBuilder(io.kubernetes.client.util.ClientBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) List(java.util.List) Rule(org.junit.Rule) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) Assert(org.junit.Assert) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Request(io.kubernetes.client.extended.controller.reconciler.Request) ArrayList(java.util.ArrayList) JSON(io.kubernetes.client.openapi.JSON) Semaphore(java.util.concurrent.Semaphore) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 12 with V1PodSpec

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

the class CacheTest method testMultiIndexFuncCacheStore.

@Test
public void testMultiIndexFuncCacheStore() {
    String testIndexFuncName = "test-idx-func";
    Cache<V1Pod> podCache = new Cache<>();
    podCache.addIndexFunc(testIndexFuncName, (V1Pod pod) -> {
        return Arrays.asList(pod.getSpec().getNodeName());
    });
    V1Pod testPod = new V1Pod().metadata(new V1ObjectMeta().namespace("ns").name("n")).spec(new V1PodSpec().nodeName("node1"));
    podCache.add(testPod);
    List<V1Pod> namespaceIndexedPods = podCache.byIndex(Caches.NAMESPACE_INDEX, "ns");
    assertEquals(1, namespaceIndexedPods.size());
    List<V1Pod> nodeNameIndexedPods = podCache.byIndex(testIndexFuncName, "node1");
    assertEquals(1, nodeNameIndexedPods.size());
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Test(org.junit.Test)

Example 13 with V1PodSpec

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

Example 14 with V1PodSpec

use of io.kubernetes.client.openapi.models.V1PodSpec in project twister2 by DSC-SPIDAL.

the class RequestObjectBuilder method constructPodTemplate.

/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate(ComputeResource computeResource) {
    V1PodTemplateSpec template = new V1PodTemplateSpec();
    V1ObjectMeta templateMetaData = new V1ObjectMeta();
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // worker pod
    labels.put("t2-wp", jobID);
    templateMetaData.setLabels(labels);
    template.setMetadata(templateMetaData);
    V1PodSpec podSpec = new V1PodSpec();
    podSpec.setTerminationGracePeriodSeconds(0L);
    ArrayList<V1Volume> volumes = new ArrayList<>();
    V1Volume memoryVolume = new V1Volume();
    memoryVolume.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
    V1EmptyDirVolumeSource volumeSource1 = new V1EmptyDirVolumeSource();
    volumeSource1.setMedium("Memory");
    memoryVolume.setEmptyDir(volumeSource1);
    volumes.add(memoryVolume);
    // create it if the requested disk space is positive
    if (computeResource.getDiskGigaBytes() > 0) {
        double volumeSize = computeResource.getDiskGigaBytes() * computeResource.getWorkersPerPod();
        V1Volume volatileVolume = createVolatileVolume(volumeSize);
        volumes.add(volatileVolume);
    }
    if (SchedulerContext.persistentVolumeRequested(config)) {
        String claimName = jobID;
        V1Volume persistentVolume = createPersistentVolume(claimName);
        volumes.add(persistentVolume);
    }
    // if openmpi is used, we initialize a Secret volume on each pod
    if (SchedulerContext.usingOpenMPI(config)) {
        String secretName = KubernetesContext.secretName(config);
        V1Volume secretVolume = createSecretVolume(secretName);
        volumes.add(secretVolume);
    }
    podSpec.setVolumes(volumes);
    int containersPerPod = computeResource.getWorkersPerPod();
    // if openmpi is used, we initialize only one container for each pod
    if (SchedulerContext.usingOpenMPI(config)) {
        containersPerPod = 1;
    }
    ArrayList<V1Container> containers = new ArrayList<V1Container>();
    for (int i = 0; i < containersPerPod; i++) {
        containers.add(constructContainer(computeResource, i));
    }
    podSpec.setContainers(containers);
    if (computeResource.getIndex() == 0) {
        constructAffinity(podSpec);
    }
    template.setSpec(podSpec);
    return template;
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ArrayList(java.util.ArrayList) V1EmptyDirVolumeSource(io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource) V1Container(io.kubernetes.client.openapi.models.V1Container) V1Volume(io.kubernetes.client.openapi.models.V1Volume) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Aggregations

V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)14 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)10 V1Container (io.kubernetes.client.openapi.models.V1Container)9 Test (org.junit.Test)8 V1Pod (io.kubernetes.client.openapi.models.V1Pod)7 ApiClient (io.kubernetes.client.openapi.ApiClient)4 V1Volume (io.kubernetes.client.openapi.models.V1Volume)4 V1PodList (io.kubernetes.client.openapi.models.V1PodList)3 V1PodSpecBuilder (io.kubernetes.client.openapi.models.V1PodSpecBuilder)3 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 V1Patch (io.kubernetes.client.custom.V1Patch)2 ApiException (io.kubernetes.client.openapi.ApiException)2 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)2 V1EmptyDirVolumeSource (io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource)2 V1VolumeBuilder (io.kubernetes.client.openapi.models.V1VolumeBuilder)2 Function (java.util.function.Function)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)1