Java Code Examples for io.fabric8.kubernetes.api.model.LabelSelector#setMatchExpressions()

The following examples show how to use io.fabric8.kubernetes.api.model.LabelSelector#setMatchExpressions() . 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());
}