Search in sources :

Example 1 with V1LabelSelectorRequirement

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

the class LabelSelectorTest method parseWithLabelsAndExpressionsShouldWork.

@Test
public void parseWithLabelsAndExpressionsShouldWork() throws IllegalArgumentException {
    HashMap<String, String> labels = new HashMap<String, String>() {

        {
            put("app1", "foo");
            put("app2", "bar");
        }
    };
    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).matchLabels(labels);
    LabelSelector labelSelector = LabelSelector.parse(v1LabelSelector);
    HashMap<String, String> testSelector = new HashMap<String, String>() {

        {
            put("app1", "foo");
            put("app2", "bar");
            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 2 with V1LabelSelectorRequirement

use of io.kubernetes.client.openapi.models.V1LabelSelectorRequirement 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 3 with V1LabelSelectorRequirement

use of io.kubernetes.client.openapi.models.V1LabelSelectorRequirement 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 4 with V1LabelSelectorRequirement

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

the class LabelSelector method parse.

/**
 * Transform a V1LabelSelector to LabelSelector
 *
 * @param ps V1LabelSelector from spec
 * @return the transformed LabelSelector
 */
public static LabelSelector parse(V1LabelSelector ps) throws IllegalArgumentException {
    int matcherNum = 0;
    if (ps.getMatchLabels() != null)
        matcherNum = ps.getMatchLabels().size();
    if (ps.getMatchExpressions() != null)
        matcherNum = ps.getMatchExpressions().size();
    List<LabelMatcher> matchers = new ArrayList<>(matcherNum);
    if (ps.getMatchLabels() != null) {
        for (Map.Entry<String, String> entry : ps.getMatchLabels().entrySet()) {
            LabelMatcher m = EqualityMatcher.equal(entry.getKey(), entry.getValue());
            matchers.add(m);
        }
    }
    if (ps.getMatchExpressions() != null) {
        for (V1LabelSelectorRequirement expr : ps.getMatchExpressions()) {
            switch(expr.getOperator()) {
                case LABEL_SELECTOR_OP_IN:
                    matchers.add(SetMatcher.in(expr.getKey(), expr.getValues().toArray(new String[0])));
                    break;
                case LABEL_SELECTOR_OP_NOT_IN:
                    matchers.add(SetMatcher.notIn(expr.getKey(), expr.getValues().toArray(new String[0])));
                    break;
                case LABEL_SELECTOR_OP_EXISTS:
                    matchers.add(SetMatcher.exists(expr.getKey()));
                    break;
                case LABEL_SELECTOR_OP_DOES_NOT_EXIST:
                    matchers.add(SetMatcher.notExists(expr.getKey()));
                    break;
                default:
                    throw new IllegalArgumentException(expr.getOperator() + " is not a valid pod selector operator");
            }
        }
    }
    return LabelSelector.and(matchers.toArray(matchers.toArray(new LabelMatcher[0])));
}
Also used : V1LabelSelectorRequirement(io.kubernetes.client.openapi.models.V1LabelSelectorRequirement) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 5 with V1LabelSelectorRequirement

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

V1LabelSelectorRequirement (io.kubernetes.client.openapi.models.V1LabelSelectorRequirement)5 V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)4 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 V1PodAffinity (io.kubernetes.client.openapi.models.V1PodAffinity)1 V1PodAffinityTerm (io.kubernetes.client.openapi.models.V1PodAffinityTerm)1 V1PodAntiAffinity (io.kubernetes.client.openapi.models.V1PodAntiAffinity)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1