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

The following examples show how to use io.fabric8.kubernetes.api.model.PodAffinityTerm. 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: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPodAffinityGlobalProperty() {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	LabelSelector labelSelector = new LabelSelector();
	labelSelector.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("security")
			.withOperator("In")
			.withValues("S1")
			.build()));
	PodAffinityTerm podAffinityTerm = new PodAffinityTerm(labelSelector, null, "failure-domain.beta.kubernetes.io/zone");
	LabelSelector labelSelector2 = new LabelSelector();
	labelSelector2.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("security")
			.withOperator("In")
			.withValues("s2")
			.build()));
	PodAffinityTerm podAffinityTerm2 = new PodAffinityTerm(labelSelector2, null, "failure-domain.beta.kubernetes.io/zone");
	WeightedPodAffinityTerm weightedPodAffinityTerm = new WeightedPodAffinityTerm(podAffinityTerm2, 100);
	PodAffinity podAffinity = new AffinityBuilder()
			.withNewPodAffinity()
			.withRequiredDuringSchedulingIgnoredDuringExecution(podAffinityTerm)
			.withPreferredDuringSchedulingIgnoredDuringExecution(weightedPodAffinityTerm)
			.endPodAffinity()
			.buildPodAffinity();

	kubernetesDeployerProperties.setPodAffinity(podAffinity);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodAffinity podAffinityTest = podSpec.getAffinity().getPodAffinity();
	assertNotNull("Pod affinity should not be null", podAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", podAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, podAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #2
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPodAntiAffinityGlobalProperty() {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	LabelSelector labelSelector = new LabelSelector();
	labelSelector.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("app")
			.withOperator("In")
			.withValues("store")
			.build()));
	PodAffinityTerm podAffinityTerm = new PodAffinityTerm(labelSelector, null, "kubernetes.io/hostname");
	LabelSelector labelSelector2 = new LabelSelector();
	labelSelector2.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("security")
			.withOperator("In")
			.withValues("s2")
			.build()));
	PodAffinityTerm podAffinityTerm2 = new PodAffinityTerm(labelSelector2, null, "failure-domain.beta.kubernetes.io/zone");
	WeightedPodAffinityTerm weightedPodAffinityTerm = new WeightedPodAffinityTerm(podAffinityTerm2, 100);
	PodAntiAffinity podAntiAffinity = new AffinityBuilder()
			.withNewPodAntiAffinity()
			.withRequiredDuringSchedulingIgnoredDuringExecution(podAffinityTerm)
			.withPreferredDuringSchedulingIgnoredDuringExecution(weightedPodAffinityTerm)
			.endPodAntiAffinity()
			.buildPodAntiAffinity();

	kubernetesDeployerProperties.setPodAntiAffinity(podAntiAffinity);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodAntiAffinity podAntiAffinityTest = podSpec.getAffinity().getPodAntiAffinity();
	assertNotNull("Pod anti-affinity should not be null", podAntiAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", podAntiAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, podAntiAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #3
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPodAffinityPropertyOverrideGlobal() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.affinity.podAffinity",
			"{ requiredDuringSchedulingIgnoredDuringExecution:" +
					"  { labelSelector:" +
					"    [ { matchExpressions:" +
					"        [ { key: 'security', " +
					"            operator: 'In'," +
					"            values:" +
					"            [ 'S1']}]}], " +
					"     topologyKey: 'failure-domain.beta.kubernetes.io/zone'}, " +
					"  preferredDuringSchedulingIgnoredDuringExecution:" +
					"  [ { weight: 1," +
					"      podAffinityTerm:" +
					"      { labelSelector:" +
					"        { matchExpressions:" +
					"          [ { key: 'security'," +
					"              operator: 'In'," +
					"              values:" +
					"              [ 'S2' ]}]}, " +
					"        topologyKey: 'failure-domain.beta.kubernetes.io/zone'}}]}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	LabelSelector labelSelector = new LabelSelector();
	labelSelector.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("tolerance")
			.withOperator("In")
			.withValues("Reliable")
			.build()));
	PodAffinityTerm podAffinityTerm = new PodAffinityTerm(labelSelector, null, "failure-domain.beta.kubernetes.io/zone");
	PodAffinity podAffinity = new AffinityBuilder()
			.withNewPodAffinity()
			.withRequiredDuringSchedulingIgnoredDuringExecution(podAffinityTerm)
			.endPodAffinity()
			.buildPodAffinity();

	kubernetesDeployerProperties.setPodAffinity(podAffinity);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodAffinity podAffinityTest = podSpec.getAffinity().getPodAffinity();
	assertNotNull("Pod affinity should not be null", podAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", podAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, podAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #4
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPodAntiAffinityPropertyOverrideGlobal() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.affinity.podAntiAffinity",
			"{ requiredDuringSchedulingIgnoredDuringExecution:" +
					"  { labelSelector:" +
					"    [ { matchExpressions:" +
					"        [ { key: 'app', " +
					"            operator: 'In'," +
					"            values:" +
					"            [ 'store']}]}], " +
					"     topologyKey: 'kubernetes.io/hostnam'}, " +
					"  preferredDuringSchedulingIgnoredDuringExecution:" +
					"  [ { weight: 1," +
					"      podAffinityTerm:" +
					"      { labelSelector:" +
					"        { matchExpressions:" +
					"          [ { key: 'security'," +
					"              operator: 'In'," +
					"              values:" +
					"              [ 'S2' ]}]}, " +
					"        topologyKey: 'failure-domain.beta.kubernetes.io/zone'}}]}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	LabelSelector labelSelector = new LabelSelector();
	labelSelector.setMatchExpressions(Arrays.asList(new LabelSelectorRequirementBuilder()
			.withKey("version")
			.withOperator("Equals")
			.withValues("v1")
			.build()));
	PodAffinityTerm podAffinityTerm = new PodAffinityTerm(labelSelector, null, "kubernetes.io/hostnam");
	PodAntiAffinity podAntiAffinity = new AffinityBuilder()
			.withNewPodAntiAffinity()
			.withRequiredDuringSchedulingIgnoredDuringExecution(podAffinityTerm)
			.endPodAntiAffinity()
			.buildPodAntiAffinity();

	kubernetesDeployerProperties.setPodAntiAffinity(podAntiAffinity);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodAntiAffinity podAntiAffinityTest = podSpec.getAffinity().getPodAntiAffinity();
	assertNotNull("Pod anti-affinity should not be null", podAntiAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", podAntiAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, podAntiAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #5
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()
    );
}