io.fabric8.kubernetes.api.model.Affinity Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.Affinity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EntityOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static Affinity affinity(EntityOperatorSpec entityOperatorSpec) {
    if (entityOperatorSpec.getTemplate() != null
            && entityOperatorSpec.getTemplate().getPod() != null
            && entityOperatorSpec.getTemplate().getPod().getAffinity() != null) {
        if (entityOperatorSpec.getAffinity() != null) {
            log.warn("Affinity given on both spec.entityOperator.affinity and spec.entityOperator.template.pod.affinity; latter takes precedence");
        }
        return entityOperatorSpec.getTemplate().getPod().getAffinity();
    } else {
        return entityOperatorSpec.getAffinity();
    }
}
 
Example #2
Source File: EntityOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.template.pod.affinity")
@Deprecated
public Affinity getAffinity() {
    return affinity;
}
 
Example #3
Source File: KafkaMirrorMakerSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.template.pod.affinity")
@Deprecated
public Affinity getAffinity() {
    return affinity;
}
 
Example #4
Source File: KafkaClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.kafka.template.pod.affinity")
@Deprecated
public Affinity getAffinity() {
    return affinity;
}
 
Example #5
Source File: AbstractKafkaConnectSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.template.pod.affinity")
@Deprecated
public Affinity getAffinity() {
    return affinity;
}
 
Example #6
Source File: KafkaMirrorMakerCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static Affinity affinity(KafkaMirrorMakerSpec spec) {
    if (spec.getTemplate() != null
            && spec.getTemplate().getPod() != null
            && spec.getTemplate().getPod().getAffinity() != null) {
        if (spec.getAffinity() != null) {
            log.warn("Affinity given on both spec.affinity and spec.template.pod.affinity; latter takes precedence");
        }
        return spec.getTemplate().getPod().getAffinity();
    } else {
        return spec.getAffinity();
    }
}
 
Example #7
Source File: KafkaConnectCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static <C extends KafkaConnectCluster> Affinity affinity(KafkaConnectSpec spec) {
    if (spec.getTemplate() != null
            && spec.getTemplate().getPod() != null
            && spec.getTemplate().getPod().getAffinity() != null) {
        if (spec.getAffinity() != null) {
            log.warn("Affinity given on both spec.affinity and spec.template.pod.affinity; latter takes precedence");
        }
        return spec.getTemplate().getPod().getAffinity();
    } else {
        return spec.getAffinity();
    }
}
 
Example #8
Source File: ZookeeperClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.zookeeper.template.pod.affinity")
@Deprecated
public Affinity getAffinity() {
    return affinity;
}
 
Example #9
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static Affinity affinity(KafkaClusterSpec kafkaClusterSpec) {
    if (kafkaClusterSpec.getTemplate() != null
            && kafkaClusterSpec.getTemplate().getPod() != null
            && kafkaClusterSpec.getTemplate().getPod().getAffinity() != null) {
        if (kafkaClusterSpec.getAffinity() != null) {
            log.warn("Affinity given on both spec.kafka.affinity and spec.kafka.template.pod.affinity; latter takes precedence");
        }
        return kafkaClusterSpec.getTemplate().getPod().getAffinity();
    } else {
        return kafkaClusterSpec.getAffinity();
    }
}
 
Example #10
Source File: ZookeeperCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static Affinity affinity(ZookeeperClusterSpec zookeeperClusterSpec) {
    if (zookeeperClusterSpec.getTemplate() != null
            && zookeeperClusterSpec.getTemplate().getPod() != null
            && zookeeperClusterSpec.getTemplate().getPod().getAffinity() != null) {
        if (zookeeperClusterSpec.getAffinity() != null) {
            log.warn("Affinity given on both spec.zookeeper.affinity and spec.zookeeper.template.pod.affinity; latter takes precedence");
        }
        return zookeeperClusterSpec.getTemplate().getPod().getAffinity();
    } else {
        return zookeeperClusterSpec.getAffinity();
    }
}
 
Example #11
Source File: ZookeeperClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #12
Source File: ExampleCrd.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@KubeLink(group = "core", version = "v1", kind = "affinity")
public Affinity getAffinity() {
    return affinity;
}
 
Example #13
Source File: ExampleCrd.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #14
Source File: ServiceConfig.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #15
Source File: SpecificST.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
@Tag(LOADBALANCER_SUPPORTED)
void testRackAware() {
    String rackKey = "rack-key";
    KafkaResource.kafkaEphemeral(CLUSTER_NAME, 1, 1)
        .editSpec()
            .editKafka()
                .withNewRack()
                    .withTopologyKey(rackKey)
                .endRack()
            .editListeners()
                .withNewKafkaListenerExternalLoadBalancer()
                    .withTls(false)
                .endKafkaListenerExternalLoadBalancer()
            .endListeners()
            .endKafka()
        .endSpec().done();

    Affinity kafkaPodSpecAffinity = kubeClient().getStatefulSet(KafkaResources.kafkaStatefulSetName(CLUSTER_NAME)).getSpec().getTemplate().getSpec().getAffinity();
    NodeSelectorRequirement kafkaPodNodeSelectorRequirement = kafkaPodSpecAffinity.getNodeAffinity()
            .getRequiredDuringSchedulingIgnoredDuringExecution().getNodeSelectorTerms().get(0).getMatchExpressions().get(0);

    assertThat(kafkaPodNodeSelectorRequirement.getKey(), is(rackKey));
    assertThat(kafkaPodNodeSelectorRequirement.getOperator(), is("Exists"));

    PodAffinityTerm kafkaPodAffinityTerm = kafkaPodSpecAffinity.getPodAntiAffinity().getPreferredDuringSchedulingIgnoredDuringExecution().get(0).getPodAffinityTerm();

    assertThat(kafkaPodAffinityTerm.getTopologyKey(), is(rackKey));
    assertThat(kafkaPodAffinityTerm.getLabelSelector().getMatchLabels(), hasEntry("strimzi.io/cluster", CLUSTER_NAME));
    assertThat(kafkaPodAffinityTerm.getLabelSelector().getMatchLabels(), hasEntry("strimzi.io/name", KafkaResources.kafkaStatefulSetName(CLUSTER_NAME)));

    String rackId = cmdKubeClient().execInPod(KafkaResources.kafkaPodName(CLUSTER_NAME, 0), "/bin/bash", "-c", "cat /opt/kafka/init/rack.id").out();
    assertThat(rackId.trim(), is("zone"));

    String brokerRack = cmdKubeClient().execInPod(KafkaResources.kafkaPodName(CLUSTER_NAME, 0), "/bin/bash", "-c", "cat /tmp/strimzi.properties | grep broker.rack").out();
    assertThat(brokerRack.contains("broker.rack=zone"), is(true));

    String uid = kubeClient().getPodUid(KafkaResources.kafkaPodName(CLUSTER_NAME, 0));
    List<Event> events = kubeClient().listEvents(uid);
    assertThat(events, hasAllOfReasons(Scheduled, Pulled, Created, Started));

    BasicExternalKafkaClient basicExternalKafkaClient = new BasicExternalKafkaClient.Builder()
        .withTopicName(TOPIC_NAME)
        .withNamespaceName(NAMESPACE)
        .withClusterName(CLUSTER_NAME)
        .withMessageCount(MESSAGE_COUNT)
        .withConsumerGroupName(CONSUMER_GROUP_NAME + "-" + rng.nextInt(Integer.MAX_VALUE))
        .build();

    basicExternalKafkaClient.verifyProducedAndConsumedMessages(
        basicExternalKafkaClient.sendMessagesPlain(),
        basicExternalKafkaClient.receiveMessagesPlain()
    );
}
 
Example #16
Source File: TopicOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #17
Source File: TopicOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Description("Pod affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
public Affinity getAffinity() {
    return affinity;
}
 
Example #18
Source File: PodTemplate.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #19
Source File: PodTemplate.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Description("The pod's affinity rules.")
@KubeLink(group = "core", version = "v1", kind = "affinity")
@JsonInclude(JsonInclude.Include.NON_NULL)
public Affinity getAffinity() {
    return affinity;
}
 
Example #20
Source File: KafkaClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #21
Source File: ServiceConfig.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public Affinity getAffinity() {
    return affinity;
}
 
Example #22
Source File: EntityOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #23
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
Affinity getAffinityRules(Map<String, String> kubernetesDeployerProperties) {
	Affinity affinity = new Affinity();

	String nodeAffinityPropertyKey = this.propertyPrefix + ".affinity.nodeAffinity";
	String podAffinityPropertyKey = this.propertyPrefix + ".affinity.podAffinity";
	String podAntiAffinityPropertyKey = this.propertyPrefix + ".affinity.podAntiAffinity";

	String nodeAffinityValue = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
			nodeAffinityPropertyKey);
	String podAffinityValue = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
			podAffinityPropertyKey);
	String podAntiAffinityValue = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
			podAntiAffinityPropertyKey);

	if (properties.getNodeAffinity() != null && !StringUtils.hasText(nodeAffinityValue)) {
		affinity.setNodeAffinity(new AffinityBuilder()
				.withNodeAffinity(properties.getNodeAffinity())
				.buildNodeAffinity());
	} else if (StringUtils.hasText(nodeAffinityValue)) {
		KubernetesDeployerProperties nodeAffinityProperties = bindProperties(kubernetesDeployerProperties,
				nodeAffinityPropertyKey, "nodeAffinity");

		affinity.setNodeAffinity(new AffinityBuilder()
				.withNodeAffinity(nodeAffinityProperties.getNodeAffinity())
				.buildNodeAffinity());
	}

	if (properties.getPodAffinity() != null && !StringUtils.hasText(podAffinityValue)) {
		affinity.setPodAffinity(new AffinityBuilder()
				.withPodAffinity(properties.getPodAffinity())
				.buildPodAffinity());
	} else if (StringUtils.hasText(podAffinityValue)) {
		KubernetesDeployerProperties podAffinityProperties = bindProperties(kubernetesDeployerProperties,
				podAffinityPropertyKey, "podAffinity");

		affinity.setPodAffinity(new AffinityBuilder()
				.withPodAffinity(podAffinityProperties.getPodAffinity())
				.buildPodAffinity());
	}

	if (properties.getPodAntiAffinity() != null && !StringUtils.hasText(podAntiAffinityValue)) {
		affinity.setPodAntiAffinity(new AffinityBuilder()
				.withPodAntiAffinity(properties.getPodAntiAffinity())
				.buildPodAntiAffinity());
	} else if (StringUtils.hasText(podAntiAffinityValue)) {
		KubernetesDeployerProperties podAntiAffinityProperties = bindProperties(kubernetesDeployerProperties,
				podAntiAffinityPropertyKey, "podAntiAffinity");

		affinity.setPodAntiAffinity(new AffinityBuilder()
				.withPodAntiAffinity(podAntiAffinityProperties.getPodAntiAffinity())
				.buildPodAntiAffinity());
	}

	return affinity;
}
 
Example #24
Source File: KafkaMirrorMakerSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #25
Source File: AbstractKafkaConnectSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setAffinity(Affinity affinity) {
    this.affinity = affinity;
}
 
Example #26
Source File: JmxTransTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
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");

    Affinity affinity = new AffinityBuilder()
            .withNewNodeAffinity()
                .withNewRequiredDuringSchedulingIgnoredDuringExecution()
                    .withNodeSelectorTerms(new NodeSelectorTermBuilder()
                            .addNewMatchExpression()
                                .withNewKey("key1")
                                .withNewOperator("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()
                            .withNewPriorityClassName("top-priority")
                            .withNewSchedulerName("my-scheduler")
                            .withAffinity(affinity)
                            .withTolerations(tolerations)
                        .endPod()
                    .endTemplate()
                .endJmxTrans()
            .endSpec()
            .build();
    JmxTrans jmxTrans = JmxTrans.fromCrd(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"));
}
 
Example #27
Source File: KafkaExporterTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
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> svcLabels = TestUtils.map("l5", "v5", "l6", "v6");
    Map<String, String> svcAnots = TestUtils.map("a5", "v5", "a6", "v6");

    Affinity affinity = new AffinityBuilder()
            .withNewNodeAffinity()
                .withNewRequiredDuringSchedulingIgnoredDuringExecution()
                    .withNodeSelectorTerms(new NodeSelectorTermBuilder()
                            .addNewMatchExpression()
                                .withNewKey("key1")
                                .withNewOperator("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(ResourceUtils.createKafkaCluster(namespace, cluster, replicas, image, healthDelay, healthTimeout))
            .editSpec()
                .withNewKafkaExporter()
                    .withNewTemplate()
                        .withNewDeployment()
                            .withNewMetadata()
                                .withLabels(depLabels)
                                .withAnnotations(depAnots)
                            .endMetadata()
                        .endDeployment()
                        .withNewPod()
                            .withNewMetadata()
                                .withLabels(podLabels)
                                .withAnnotations(podAnots)
                            .endMetadata()
                            .withNewPriorityClassName("top-priority")
                            .withNewSchedulerName("my-scheduler")
                            .withAffinity(affinity)
                            .withTolerations(tolerations)
                        .endPod()
                        .withNewService()
                            .withNewMetadata()
                                .withLabels(svcLabels)
                                .withAnnotations(svcAnots)
                            .endMetadata()
                        .endService()
                    .endTemplate()
                .endKafkaExporter()
            .endSpec()
            .build();
    KafkaExporter ke = KafkaExporter.fromCrd(resource, VERSIONS);

    // Check Deployment
    Deployment dep = ke.generateDeployment(true, null, null);
    assertThat(dep.getMetadata().getLabels().entrySet().containsAll(expectedDepLabels.entrySet()), is(true));
    assertThat(dep.getMetadata().getAnnotations().entrySet().containsAll(depAnots.entrySet()), is(true));

    // Check Pods
    assertThat(dep.getSpec().getTemplate().getMetadata().getLabels().entrySet().containsAll(podLabels.entrySet()), is(true));
    assertThat(dep.getSpec().getTemplate().getMetadata().getAnnotations().entrySet().containsAll(podAnots.entrySet()), is(true));
    assertThat(dep.getSpec().getTemplate().getSpec().getPriorityClassName(), is("top-priority"));
    assertThat(dep.getSpec().getTemplate().getSpec().getSchedulerName(), is("my-scheduler"));
    assertThat(dep.getSpec().getTemplate().getSpec().getAffinity(), is(affinity));
    assertThat(dep.getSpec().getTemplate().getSpec().getTolerations(), is(tolerations));

    // Check Service
    Service svc = ke.generateService();
    assertThat(svc.getMetadata().getLabels().entrySet().containsAll(svcLabels.entrySet()), is(true));
    assertThat(svc.getMetadata().getAnnotations().entrySet().containsAll(svcAnots.entrySet()), is(true));
}
 
Example #28
Source File: ModelUtilsTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testParsePodTemplate()  {
    Kafka kafka = new KafkaBuilder()
            .withNewMetadata()
                .withName("my-cluster")
                .withNamespace("my-namespace")
            .endMetadata()
            .build();

    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    Affinity affinity = new AffinityBuilder()
            .withNewNodeAffinity()
                .withNewRequiredDuringSchedulingIgnoredDuringExecution()
                    .withNodeSelectorTerms(new NodeSelectorTermBuilder()
                            .addNewMatchExpression()
                                .withNewKey("key1")
                                .withNewOperator("In")
                                .withValues("value1", "value2")
                            .endMatchExpression()
                            .build())
                .endRequiredDuringSchedulingIgnoredDuringExecution()
            .endNodeAffinity()
            .build();

    List<Toleration> tolerations = singletonList(new TolerationBuilder()
            .withEffect("NoExecute")
            .withKey("key1")
            .withOperator("Equal")
            .withValue("value1")
            .build());

    PodTemplate template = new PodTemplateBuilder()
            .withNewMetadata()
            .withAnnotations(Collections.singletonMap("annoKey", "annoValue"))
            .withLabels(Collections.singletonMap("labelKey", "labelValue"))
            .endMetadata()
            .withSecurityContext(new PodSecurityContextBuilder().withFsGroup(123L).withRunAsGroup(456L).withRunAsUser(789L).build())
            .withImagePullSecrets(secret1, secret2)
            .withTerminationGracePeriodSeconds(123)
            .withAffinity(affinity)
            .withTolerations(tolerations)
            .build();

    Model model = new Model(kafka);

    ModelUtils.parsePodTemplate(model, template);
    assertThat(model.templatePodLabels, is(Collections.singletonMap("labelKey", "labelValue")));
    assertThat(model.templatePodAnnotations, is(Collections.singletonMap("annoKey", "annoValue")));
    assertThat(model.templateTerminationGracePeriodSeconds, is(123));
    assertThat(model.templateImagePullSecrets.size(), is(2));
    assertThat(model.templateImagePullSecrets.contains(secret1), is(true));
    assertThat(model.templateImagePullSecrets.contains(secret2), is(true));
    assertThat(model.templateSecurityContext, is(notNullValue()));
    assertThat(model.templateSecurityContext.getFsGroup(), is(Long.valueOf(123)));
    assertThat(model.templateSecurityContext.getRunAsGroup(), is(Long.valueOf(456)));
    assertThat(model.templateSecurityContext.getRunAsUser(), is(Long.valueOf(789)));
    assertThat(model.getUserAffinity(), is(affinity));
    assertThat(model.getTolerations(), is(tolerations));
}
 
Example #29
Source File: CruiseControlTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testTemplate() {
    Map<String, String> depLabels = TestUtils.map("l1", "v1", "l2", "v2");
    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> svcLabels = TestUtils.map("l5", "v5", "l6", "v6");
    Map<String, String> svcAnots = TestUtils.map("a5", "v5", "a6", "v6");

    Affinity affinity = new AffinityBuilder()
            .withNewNodeAffinity()
                .withNewRequiredDuringSchedulingIgnoredDuringExecution()
                    .withNodeSelectorTerms(new NodeSelectorTermBuilder()
                            .addNewMatchExpression()
                                .withNewKey("key1")
                                .withNewOperator("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(ResourceUtils.createKafkaCluster(namespace, cluster, replicas, image, healthDelay, healthTimeout))
            .editSpec()
                .withNewCruiseControl()
                    .withImage(ccImage)
                    .withNewTemplate()
                        .withNewDeployment()
                            .withNewMetadata()
                                .withLabels(depLabels)
                                .withAnnotations(depAnots)
                            .endMetadata()
                        .endDeployment()
                        .withNewPod()
                            .withNewMetadata()
                                .withLabels(podLabels)
                                .withAnnotations(podAnots)
                            .endMetadata()
                            .withNewPriorityClassName("top-priority")
                            .withNewSchedulerName("my-scheduler")
                            .withAffinity(affinity)
                            .withTolerations(tolerations)
                        .endPod()
                        .withNewApiService()
                            .withNewMetadata()
                                .withLabels(svcLabels)
                                .withAnnotations(svcAnots)
                            .endMetadata()
                        .endApiService()
                    .endTemplate()
                .endCruiseControl()
            .endSpec()
            .build();

    CruiseControl cc = CruiseControl.fromCrd(resource, VERSIONS);

    // Check Deployment
    Deployment dep = cc.generateDeployment(true, depAnots, null, null);
    depLabels.putAll(expectedLabels());
    assertThat(dep.getMetadata().getLabels(), is(depLabels));
    assertThat(dep.getMetadata().getAnnotations(), is(depAnots));

    // Check Pods
    podLabels.putAll(expectedLabels());
    assertThat(dep.getSpec().getTemplate().getMetadata().getLabels(), is(podLabels));
    assertThat(dep.getSpec().getTemplate().getMetadata().getAnnotations(), is(podAnots));
    assertThat(dep.getSpec().getTemplate().getSpec().getPriorityClassName(), is("top-priority"));
    assertThat(dep.getSpec().getTemplate().getSpec().getSchedulerName(), is("my-scheduler"));
    assertThat(dep.getSpec().getTemplate().getSpec().getAffinity(), is(affinity));
    assertThat(dep.getSpec().getTemplate().getSpec().getTolerations(), is(tolerations));

    // Check Service
    svcLabels.putAll(expectedLabels());
    Service svc = cc.generateService();
    assertThat(svc.getMetadata().getLabels(), is(svcLabels));
    assertThat(svc.getMetadata().getAnnotations(),  is(svcAnots));
}
 
Example #30
Source File: KafkaBridgeClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
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> svcLabels = TestUtils.map("l5", "v5", "l6", "v6");
    Map<String, String> svcAnots = TestUtils.map("a5", "v5", "a6", "v6");

    Map<String, String> pdbLabels = TestUtils.map("l7", "v7", "l8", "v8");
    Map<String, String> pdbAnots = TestUtils.map("a7", "v7", "a8", "v8");

    Affinity affinity = new AffinityBuilder()
            .withNewNodeAffinity()
                .withNewRequiredDuringSchedulingIgnoredDuringExecution()
                    .withNodeSelectorTerms(new NodeSelectorTermBuilder()
                            .addNewMatchExpression()
                                .withNewKey("key1")
                                .withNewOperator("In")
                                .withValues("value1", "value2")
                            .endMatchExpression()
                            .build())
                .endRequiredDuringSchedulingIgnoredDuringExecution()
            .endNodeAffinity()
            .build();

    List<Toleration> tolerations = singletonList(new TolerationBuilder()
            .withEffect("NoExecute")
            .withKey("key1")
            .withOperator("Equal")
            .withValue("value1")
            .build());

    KafkaBridge resource = new KafkaBridgeBuilder(this.resource)
            .editSpec()
                .withNewTemplate()
                    .withNewDeployment()
                        .withNewMetadata()
                            .withLabels(depLabels)
                            .withAnnotations(depAnots)
                        .endMetadata()
                    .endDeployment()
                    .withNewPod()
                        .withNewMetadata()
                            .withLabels(podLabels)
                            .withAnnotations(podAnots)
                        .endMetadata()
                        .withNewPriorityClassName("top-priority")
                        .withNewSchedulerName("my-scheduler")
                        .withAffinity(affinity)
                        .withTolerations(tolerations)
                    .endPod()
                    .withNewApiService()
                        .withNewMetadata()
                            .withLabels(svcLabels)
                            .withAnnotations(svcAnots)
                        .endMetadata()
                    .endApiService()
                    .withNewPodDisruptionBudget()
                        .withNewMetadata()
                            .withLabels(pdbLabels)
                            .withAnnotations(pdbAnots)
                        .endMetadata()
                    .endPodDisruptionBudget()
                .endTemplate()
            .endSpec()
            .build();
    KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(resource, VERSIONS);

    // Check Deployment
    Deployment dep = kbc.generateDeployment(emptyMap(), true, null, null);
    assertThat(dep.getMetadata().getLabels().entrySet().containsAll(expectedDepLabels.entrySet()), is(true));
    assertThat(dep.getMetadata().getAnnotations().entrySet().containsAll(depAnots.entrySet()), is(true));
    assertThat(dep.getSpec().getTemplate().getSpec().getPriorityClassName(), is("top-priority"));

    // Check Pods
    assertThat(dep.getSpec().getTemplate().getMetadata().getLabels().entrySet().containsAll(podLabels.entrySet()), is(true));
    assertThat(dep.getSpec().getTemplate().getMetadata().getAnnotations().entrySet().containsAll(podAnots.entrySet()), is(true));
    assertThat(dep.getSpec().getTemplate().getSpec().getSchedulerName(), is("my-scheduler"));
    assertThat(dep.getSpec().getTemplate().getSpec().getAffinity(), is(affinity));
    assertThat(dep.getSpec().getTemplate().getSpec().getTolerations(), is(tolerations));

    // Check Service
    Service svc = kbc.generateService();
    assertThat(svc.getMetadata().getLabels().entrySet().containsAll(svcLabels.entrySet()), is(true));
    assertThat(svc.getMetadata().getAnnotations().entrySet().containsAll(svcAnots.entrySet()), is(true));

    // Check PodDisruptionBudget
    PodDisruptionBudget pdb = kbc.generatePodDisruptionBudget();
    assertThat(pdb.getMetadata().getLabels().entrySet().containsAll(pdbLabels.entrySet()), is(true));
    assertThat(pdb.getMetadata().getAnnotations().entrySet().containsAll(pdbAnots.entrySet()), is(true));
}