Search in sources :

Example 6 with V1LabelSelector

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

the class LabelSelectorTest method parseWithExpressionsShouldWork.

@Test
public void parseWithExpressionsShouldWork() throws IllegalArgumentException {
    List<V1LabelSelectorRequirement> exprs = new LinkedList<V1LabelSelectorRequirement>() {

        {
            add(new V1LabelSelectorRequirement().key("key1").values(new LinkedList<String>() {

                {
                    add("value1");
                    add("value2");
                }
            }).operator(LabelSelector.LABEL_SELECTOR_OP_IN));
            add(new V1LabelSelectorRequirement().key("key2").values(new LinkedList<String>() {

                {
                    add("value3");
                    add("value4");
                }
            }).operator(LabelSelector.LABEL_SELECTOR_OP_NOT_IN));
            add(new V1LabelSelectorRequirement().key("key3").operator(LabelSelector.LABEL_SELECTOR_OP_EXISTS));
            add(new V1LabelSelectorRequirement().key("key4").operator(LabelSelector.LABEL_SELECTOR_OP_DOES_NOT_EXIST));
        }
    };
    V1LabelSelector v1LabelSelector = new V1LabelSelector().matchExpressions(exprs);
    LabelSelector labelSelector = LabelSelector.parse(v1LabelSelector);
    HashMap<String, String> testSelector = new HashMap<String, String>() {

        {
            put("key1", "value1");
            put("key2", "value5");
            put("key3", "");
        }
    };
    Assert.assertTrue(labelSelector.test(testSelector));
}
Also used : HashMap(java.util.HashMap) V1LabelSelectorRequirement(io.kubernetes.client.openapi.models.V1LabelSelectorRequirement) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 7 with V1LabelSelector

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

the class LabelSelectorTest method parseWithWrongOpShouldThrowIllegalArgumentException.

@Test
public void parseWithWrongOpShouldThrowIllegalArgumentException() {
    List<V1LabelSelectorRequirement> exprs = new LinkedList<V1LabelSelectorRequirement>() {

        {
            add(new V1LabelSelectorRequirement().key("key1").values(new LinkedList<String>() {

                {
                    add("value1");
                    add("value2");
                }
            }).operator("WrongOp"));
        }
    };
    V1LabelSelector v1LabelSelector = new V1LabelSelector().matchExpressions(exprs);
    assertThrows(IllegalArgumentException.class, () -> LabelSelector.parse(v1LabelSelector));
}
Also used : V1LabelSelectorRequirement(io.kubernetes.client.openapi.models.V1LabelSelectorRequirement) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with V1LabelSelector

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

the class RequestObjectBuilder method createStatefulSetForWorkers.

/**
 * create StatefulSet object for a job
 */
public static V1StatefulSet createStatefulSetForWorkers(ComputeResource computeResource) {
    if (config == null) {
        LOG.severe("RequestObjectBuilder.init method has not been called.");
        return null;
    }
    String statefulSetName = KubernetesUtils.createWorkersStatefulSetName(jobID, computeResource.getIndex());
    V1StatefulSet statefulSet = new V1StatefulSet();
    // set labels for the worker stateful set
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // worker statefulset
    labels.put("t2-wss", jobID);
    // construct metadata and set for jobID setting
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(statefulSetName);
    meta.setLabels(labels);
    statefulSet.setMetadata(meta);
    // construct JobSpec and set
    V1StatefulSetSpec setSpec = new V1StatefulSetSpec();
    setSpec.serviceName(KubernetesUtils.createServiceName(jobID));
    // pods will be started in parallel
    // by default they are started sequentially
    setSpec.setPodManagementPolicy("Parallel");
    int numberOfPods = computeResource.getInstances();
    setSpec.setReplicas(numberOfPods);
    // add selector for the job
    V1LabelSelector selector = new V1LabelSelector();
    selector.putMatchLabelsItem("t2-wp", jobID);
    setSpec.setSelector(selector);
    // construct the pod template
    V1PodTemplateSpec template = constructPodTemplate(computeResource);
    setSpec.setTemplate(template);
    statefulSet.setSpec(setSpec);
    return statefulSet;
}
Also used : V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1StatefulSetSpec(io.kubernetes.client.openapi.models.V1StatefulSetSpec) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector)

Example 9 with V1LabelSelector

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

the class RequestObjectBuilder method setUniformMappingAffinity.

public static void setUniformMappingAffinity(V1Affinity affinity) {
    String mappingType = KubernetesContext.workerMappingUniform(config);
    String key = "t2-wp";
    String operator = "In";
    String serviceLabel = jobID;
    List<String> values = Arrays.asList(serviceLabel);
    V1LabelSelectorRequirement labelRequirement = new V1LabelSelectorRequirement();
    labelRequirement.setKey(key);
    labelRequirement.setOperator(operator);
    labelRequirement.setValues(values);
    V1LabelSelector labelSelector = new V1LabelSelector();
    labelSelector.addMatchExpressionsItem(labelRequirement);
    V1PodAffinityTerm affinityTerm = new V1PodAffinityTerm();
    affinityTerm.setLabelSelector(labelSelector);
    affinityTerm.setTopologyKey("kubernetes.io/hostname");
    if ("all-same-node".equalsIgnoreCase(mappingType)) {
        V1PodAffinity podAffinity = new V1PodAffinity();
        podAffinity.requiredDuringSchedulingIgnoredDuringExecution(Arrays.asList(affinityTerm));
        affinity.setPodAffinity(podAffinity);
    } else if ("all-separate-nodes".equalsIgnoreCase(mappingType)) {
        V1PodAntiAffinity podAntiAffinity = new V1PodAntiAffinity();
        podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution(Arrays.asList(affinityTerm));
        affinity.setPodAntiAffinity(podAntiAffinity);
    }
}
Also used : V1PodAffinity(io.kubernetes.client.openapi.models.V1PodAffinity) V1PodAntiAffinity(io.kubernetes.client.openapi.models.V1PodAntiAffinity) V1LabelSelectorRequirement(io.kubernetes.client.openapi.models.V1LabelSelectorRequirement) V1PodAffinityTerm(io.kubernetes.client.openapi.models.V1PodAffinityTerm) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector)

Aggregations

V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)9 V1LabelSelectorRequirement (io.kubernetes.client.openapi.models.V1LabelSelectorRequirement)4 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)4 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 V1StatefulSet (io.kubernetes.client.openapi.models.V1StatefulSet)3 V1StatefulSetSpec (io.kubernetes.client.openapi.models.V1StatefulSetSpec)3 IntOrString (io.kubernetes.client.custom.IntOrString)1 V1Patch (io.kubernetes.client.custom.V1Patch)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 ApiException (io.kubernetes.client.openapi.ApiException)1 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)1 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)1 V1DeploymentBuilder (io.kubernetes.client.openapi.models.V1DeploymentBuilder)1 V1DeploymentSpec (io.kubernetes.client.openapi.models.V1DeploymentSpec)1 V1PodAffinity (io.kubernetes.client.openapi.models.V1PodAffinity)1