Search in sources :

Example 6 with AffinityBuilder

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();
}
Also used : AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) Affinity(io.fabric8.kubernetes.api.model.Affinity)

Example 7 with AffinityBuilder

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));
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) TolerationBuilder(io.fabric8.kubernetes.api.model.TolerationBuilder) HashMap(java.util.HashMap) AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) Kafka(io.strimzi.api.kafka.model.Kafka) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) Toleration(io.fabric8.kubernetes.api.model.Toleration) Affinity(io.fabric8.kubernetes.api.model.Affinity) NodeSelectorTermBuilder(io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 8 with AffinityBuilder

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();
}
Also used : AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) Affinity(io.fabric8.kubernetes.api.model.Affinity)

Example 9 with AffinityBuilder

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();
}
Also used : AffinityBuilder(io.fabric8.kubernetes.api.model.AffinityBuilder) Affinity(io.fabric8.kubernetes.api.model.Affinity)

Example 10 with AffinityBuilder

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;
}
Also used : NodeSelectorRequirementBuilder(io.fabric8.kubernetes.api.model.NodeSelectorRequirementBuilder) ArrayList(java.util.ArrayList) NodeSelectorRequirement(io.fabric8.kubernetes.api.model.NodeSelectorRequirement) NodeSelectorTerm(io.fabric8.kubernetes.api.model.NodeSelectorTerm) NodeSelectorTermBuilder(io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder)

Aggregations

AffinityBuilder (io.fabric8.kubernetes.api.model.AffinityBuilder)22 Affinity (io.fabric8.kubernetes.api.model.Affinity)20 NodeSelectorTermBuilder (io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder)18 Toleration (io.fabric8.kubernetes.api.model.Toleration)14 TolerationBuilder (io.fabric8.kubernetes.api.model.TolerationBuilder)14 ParallelTest (io.strimzi.test.annotations.ParallelTest)14 Kafka (io.strimzi.api.kafka.model.Kafka)12 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)10 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)8 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)8 TopologySpreadConstraint (io.fabric8.kubernetes.api.model.TopologySpreadConstraint)8 TopologySpreadConstraintBuilder (io.fabric8.kubernetes.api.model.TopologySpreadConstraintBuilder)8 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)8 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)6 PodSecurityContextBuilder (io.fabric8.kubernetes.api.model.PodSecurityContextBuilder)6 Quantity (io.fabric8.kubernetes.api.model.Quantity)6 PodDisruptionBudget (io.fabric8.kubernetes.api.model.policy.v1.PodDisruptionBudget)6 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)5 HostAliasBuilder (io.fabric8.kubernetes.api.model.HostAliasBuilder)5 Reconciliation (io.strimzi.operator.common.Reconciliation)5