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));
}
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));
}
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;
}
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);
}
}
Aggregations