Skip to content

koordinator-sh/client-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Koordinator Java Client

Java client for the Koordinator API.

Usage

Add this dependency to your project's POM:

<dependency>
    <groupId>sh.koordinator</groupId>
    <artifactId>client-java</artifactId>
    <version>1.3.0</version>
</dependency>

Note that this package has not been uploaded to the maven official repository. Currently, you should manually download this repo and package it to use.

You should also add the dependency of Kubernetes official java SDK:

<dependency>
    <groupId>io.kubernetes</groupId>
    <artifactId>client-java</artifactId>
    <version>16.0.2</version>
</dependency>

Manually package

At first generate the JAR by executing:

mvn package

Then manually install the following JARs:

  • target/client-java-1.3.0.jar

Getting Started

package sh.koordinator.scheduling.models;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import io.kubernetes.client.custom.Quantity;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.models.V1Container;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1PodSpec;
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
import io.kubernetes.client.openapi.models.V1ResourceRequirements;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import io.kubernetes.client.util.generic.KubernetesApiResponse;
import org.junit.Test;
import sh.koordinator.scheduling.models.V1alpha1ReservationSpec.AllocatePolicyEnum;

public class V1alpha1ReservationTest {
    private static final String API_GROUP = "scheduling.koordinator.sh";
    private static final String API_VERSION = "v1alpha1";
    private static final String RESOURCE_PLURAL = "reservations";
    private static final String API_VERSION_DETAIL = API_GROUP + "/" + API_VERSION;
    private static final String API_KIND = "Reservation";

    @Test
    public void testCreateReservation() throws IOException {
        ApiClient apiClient = Config.defaultClient();
        GenericKubernetesApi<V1alpha1Reservation, V1alpha1ReservationList> reservationClient = new GenericKubernetesApi<>(
            V1alpha1Reservation.class,
            V1alpha1ReservationList.class,
            API_GROUP,
            API_VERSION,
            RESOURCE_PLURAL,
            apiClient);
        V1ObjectMeta objectMeta = new V1ObjectMeta().name("test-reservation");
        V1alpha1ReservationSpecOwners owners = new V1alpha1ReservationSpecOwners()
            .labelSelector(
                new V1alpha1ReservationSpecLabelSelector()
                    .matchLabels(
                        new HashMap<String, String>(){{
                            put("app", "test");
                        }}
                    )
            );
        List<V1Container> containerList = new ArrayList<>(
            Collections.singletonList(
                new V1Container()
                    .name("main")
                    .resources(
                        new V1ResourceRequirements()
                            .limits(
                                new HashMap<String, Quantity>() {{
                                    put("cpu", Quantity.fromString("2"));
                                    put("memory", Quantity.fromString("4Gi"));
                                }}
                            )
                    )
            )
        );
        V1alpha1ReservationSpec reservationSpec = new V1alpha1ReservationSpec()
            .allocateOnce(false)
            .addOwnersItem(owners)
            .allocatePolicy(AllocatePolicyEnum.RESTRICTED)
            .template(new V1PodTemplateSpec().spec(new V1PodSpec().containers(containerList)));
        V1alpha1Reservation reservation = new V1alpha1Reservation()
            .apiVersion(API_VERSION_DETAIL)
            .kind(API_KIND)
            .metadata(objectMeta)
            .spec(reservationSpec);
        KubernetesApiResponse<V1alpha1Reservation> response = reservationClient.create(reservation);
        System.out.println(response.getStatus());
        System.out.println(response.getObject());
        assert reservation.getMetadata() != null;
        reservationClient.delete(reservation.getMetadata().getName());
    }
}

About

Java client library for Koordinator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages