use of io.fabric8.kubernetes.api.model.AffinityBuilder in project strimzi by strimzi.
the class KafkaConnectCluster method getMergedAffinity.
/**
* Returns a combined affinity: Adding the affinity needed for the "kafka-rack" to the {@link #getUserAffinity()}.
*/
@Override
protected Affinity getMergedAffinity() {
Affinity userAffinity = getUserAffinity();
AffinityBuilder builder = new AffinityBuilder(userAffinity == null ? new Affinity() : userAffinity);
if (rack != null) {
builder = ModelUtils.populateAffinityBuilderWithRackLabelSelector(builder, userAffinity, rack.getTopologyKey());
}
return builder.build();
}
use of io.fabric8.kubernetes.api.model.AffinityBuilder in project strimzi by strimzi.
the class JmxTransTest method testTemplate.
@ParallelTest
public void testTemplate() {
Map<String, String> depLabels = TestUtils.map("l1", "v1", "l2", "v2", Labels.KUBERNETES_PART_OF_LABEL, "custom-part", Labels.KUBERNETES_MANAGED_BY_LABEL, "custom-managed-by");
Map<String, String> expectedDepLabels = new HashMap<>(depLabels);
expectedDepLabels.remove(Labels.KUBERNETES_MANAGED_BY_LABEL);
Map<String, String> depAnots = TestUtils.map("a1", "v1", "a2", "v2");
Map<String, String> podLabels = TestUtils.map("l3", "v3", "l4", "v4");
Map<String, String> podAnots = TestUtils.map("a3", "v3", "a4", "v4");
Map<String, String> saLabels = TestUtils.map("l5", "v5", "l6", "v6");
Map<String, String> saAnots = TestUtils.map("a5", "v5", "a6", "v6");
Affinity affinity = new AffinityBuilder().withNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(new NodeSelectorTermBuilder().addNewMatchExpression().withKey("key1").withOperator("In").withValues("value1", "value2").endMatchExpression().build()).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity().build();
List<Toleration> tolerations = singletonList(new TolerationBuilder().withEffect("NoExecute").withKey("key1").withOperator("Equal").withValue("value1").build());
Kafka resource = new KafkaBuilder(kafkaAssembly).editSpec().editOrNewJmxTrans().editOrNewTemplate().withNewDeployment().withNewMetadata().withLabels(depLabels).withAnnotations(depAnots).endMetadata().endDeployment().withNewPod().withNewMetadata().withLabels(podLabels).withAnnotations(podAnots).endMetadata().withPriorityClassName("top-priority").withSchedulerName("my-scheduler").withAffinity(affinity).withTolerations(tolerations).withEnableServiceLinks(false).endPod().withNewServiceAccount().withNewMetadata().withLabels(saLabels).withAnnotations(saAnots).endMetadata().endServiceAccount().endTemplate().endJmxTrans().endSpec().build();
JmxTrans jmxTrans = JmxTrans.fromCrd(Reconciliation.DUMMY_RECONCILIATION, resource, VERSIONS);
// Check Deployment
Deployment dep = jmxTrans.generateDeployment(null, null);
assertThat(dep.getMetadata().getLabels(), hasEntries(expectedDepLabels));
assertThat(dep.getMetadata().getAnnotations(), hasEntries(depAnots));
assertThat(dep.getSpec().getTemplate().getSpec().getPriorityClassName(), is("top-priority"));
assertThat(dep.getSpec().getTemplate().getSpec().getAffinity(), is(affinity));
assertThat(dep.getSpec().getTemplate().getSpec().getTolerations(), is(tolerations));
// Check Pods
assertThat(dep.getSpec().getTemplate().getMetadata().getLabels(), hasEntries(podLabels));
assertThat(dep.getSpec().getTemplate().getMetadata().getAnnotations(), hasEntries(podAnots));
assertThat(dep.getSpec().getTemplate().getSpec().getSchedulerName(), is("my-scheduler"));
assertThat(dep.getSpec().getTemplate().getSpec().getEnableServiceLinks(), is(false));
// Check Service Account
ServiceAccount sa = jmxTrans.generateServiceAccount();
assertThat(sa.getMetadata().getLabels().entrySet().containsAll(saLabels.entrySet()), is(true));
assertThat(sa.getMetadata().getAnnotations().entrySet().containsAll(saAnots.entrySet()), is(true));
}
use of io.fabric8.kubernetes.api.model.AffinityBuilder in project strimzi-kafka-operator by strimzi.
the class KafkaCluster method getMergedAffinity.
/**
* Returns a combined affinity: Adding the affinity needed for the "kafka-rack" to the {@link #getUserAffinity()}.
*/
@Override
protected Affinity getMergedAffinity() {
Affinity userAffinity = getUserAffinity();
AffinityBuilder builder = new AffinityBuilder(userAffinity == null ? new Affinity() : userAffinity);
if (rack != null) {
// If there's a rack config, we need to add a podAntiAffinity to spread the brokers among the racks
builder = builder.editOrNewPodAntiAffinity().addNewPreferredDuringSchedulingIgnoredDuringExecution().withWeight(100).withNewPodAffinityTerm().withTopologyKey(rack.getTopologyKey()).withNewLabelSelector().addToMatchLabels(Labels.STRIMZI_CLUSTER_LABEL, cluster).addToMatchLabels(Labels.STRIMZI_NAME_LABEL, name).endLabelSelector().endPodAffinityTerm().endPreferredDuringSchedulingIgnoredDuringExecution().endPodAntiAffinity();
builder = ModelUtils.populateAffinityBuilderWithRackLabelSelector(builder, userAffinity, rack.getTopologyKey());
}
return builder.build();
}
use of io.fabric8.kubernetes.api.model.AffinityBuilder in project strimzi-kafka-operator by strimzi.
the class KafkaConnectCluster method getMergedAffinity.
/**
* Returns a combined affinity: Adding the affinity needed for the "kafka-rack" to the {@link #getUserAffinity()}.
*/
@Override
protected Affinity getMergedAffinity() {
Affinity userAffinity = getUserAffinity();
AffinityBuilder builder = new AffinityBuilder(userAffinity == null ? new Affinity() : userAffinity);
if (rack != null) {
builder = ModelUtils.populateAffinityBuilderWithRackLabelSelector(builder, userAffinity, rack.getTopologyKey());
}
return builder.build();
}
use of io.fabric8.kubernetes.api.model.AffinityBuilder in project strimzi-kafka-operator by strimzi.
the class ModelUtils method populateAffinityBuilderWithRackLabelSelector.
/**
* @param builder the builder which is used to populate the node affinity
* @param userAffinity the userAffinity which is defined by the user
* @param topologyKey the topology key which is used to select the node
* @return the AffinityBuilder which has the node selector with topology key which is needed to make sure
* the pods are scheduled only on nodes with the rack label
*/
public static AffinityBuilder populateAffinityBuilderWithRackLabelSelector(AffinityBuilder builder, Affinity userAffinity, String topologyKey) {
// We need to add node affinity to make sure the pods are scheduled only on nodes with the rack label
NodeSelectorRequirement selector = new NodeSelectorRequirementBuilder().withOperator("Exists").withKey(topologyKey).build();
if (userAffinity != null && userAffinity.getNodeAffinity() != null && userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution() != null && userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution().getNodeSelectorTerms() != null) {
// User has specified some Node Selector Terms => we should enhance them
List<NodeSelectorTerm> oldTerms = userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution().getNodeSelectorTerms();
List<NodeSelectorTerm> enhancedTerms = new ArrayList<>(oldTerms.size());
for (NodeSelectorTerm term : oldTerms) {
NodeSelectorTerm enhancedTerm = new NodeSelectorTermBuilder(term).addToMatchExpressions(selector).build();
enhancedTerms.add(enhancedTerm);
}
builder = builder.editOrNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(enhancedTerms).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity();
} else {
// User has not specified any selector terms => we add our own
builder = builder.editOrNewNodeAffinity().editOrNewRequiredDuringSchedulingIgnoredDuringExecution().addNewNodeSelectorTerm().withMatchExpressions(selector).endNodeSelectorTerm().endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity();
}
return builder;
}
Aggregations