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

The following examples show how to use io.fabric8.kubernetes.api.model.NodeSelectorTerm. 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 testNodeAffinityGlobalProperty() {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	NodeSelectorTerm nodeSelectorTerm = new NodeSelectorTerm();
	nodeSelectorTerm.setMatchExpressions(Arrays.asList(new NodeSelectorRequirementBuilder()
			.withKey("kubernetes.io/e2e-az-name")
			.withOperator("In")
			.withValues("e2e-az1", "e2e-az2")
			.build()));
	NodeSelectorTerm nodeSelectorTerm2 = new NodeSelectorTerm();
	nodeSelectorTerm2.setMatchExpressions(Arrays.asList(new NodeSelectorRequirementBuilder()
			.withKey("another-node-label-key")
			.withOperator("In")
			.withValues("another-node-label-value2")
			.build()));
	PreferredSchedulingTerm preferredSchedulingTerm = new PreferredSchedulingTerm(nodeSelectorTerm2, 1);
	NodeAffinity nodeAffinity = new AffinityBuilder()
			.withNewNodeAffinity()
			.withNewRequiredDuringSchedulingIgnoredDuringExecution()
			.withNodeSelectorTerms(nodeSelectorTerm)
			.endRequiredDuringSchedulingIgnoredDuringExecution()
			.withPreferredDuringSchedulingIgnoredDuringExecution(preferredSchedulingTerm)
			.endNodeAffinity()
			.buildNodeAffinity();

	kubernetesDeployerProperties.setNodeAffinity(nodeAffinity);

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

	NodeAffinity nodeAffinityTest = podSpec.getAffinity().getNodeAffinity();
	assertNotNull("Node affinity should not be null", nodeAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", nodeAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, nodeAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #2
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testNodeAffinityPropertyOverrideGlobal() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.affinity.nodeAffinity",
			"{ requiredDuringSchedulingIgnoredDuringExecution:" +
					"  { nodeSelectorTerms:" +
					"    [ { matchExpressions:" +
					"        [ { key: 'kubernetes.io/e2e-az-name', " +
					"            operator: 'In'," +
					"            values:" +
					"            [ 'e2e-az1', 'e2e-az2']}]}]}, " +
					"  preferredDuringSchedulingIgnoredDuringExecution:" +
					"  [ { weight: 1," +
					"      preference:" +
					"      { matchExpressions:" +
					"        [ { key: 'another-node-label-key'," +
					"            operator: 'In'," +
					"            values:" +
					"            [ 'another-node-label-value' ]}]}}]}");

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

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	NodeSelectorTerm nodeSelectorTerm = new NodeSelectorTerm();
	nodeSelectorTerm.setMatchExpressions(Arrays.asList(new NodeSelectorRequirementBuilder()
			.withKey("kubernetes.io/e2e-az-name")
			.withOperator("In")
			.withValues("e2e-az1", "e2e-az2")
			.build()));
	NodeAffinity nodeAffinity = new AffinityBuilder()
			.withNewNodeAffinity()
			.withNewRequiredDuringSchedulingIgnoredDuringExecution()
			.withNodeSelectorTerms(nodeSelectorTerm)
			.endRequiredDuringSchedulingIgnoredDuringExecution()
			.endNodeAffinity()
			.buildNodeAffinity();

	kubernetesDeployerProperties.setNodeAffinity(nodeAffinity);

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

	NodeAffinity nodeAffinityTest = podSpec.getAffinity().getNodeAffinity();
	assertNotNull("Node affinity should not be null", nodeAffinityTest);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", nodeAffinityTest.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, nodeAffinityTest.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}
 
Example #3
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

        // We also need to add node affinity to make sure the pods are scheduled only on nodes with the rack label
        NodeSelectorRequirement selector = new NodeSelectorRequirementBuilder()
                .withNewOperator("Exists")
                .withNewKey(rack.getTopologyKey())
                .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.build();
}