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

The following examples show how to use io.fabric8.kubernetes.api.model.LabelSelectorRequirementBuilder. 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: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testNetworkPolicyPeers() {
    NetworkPolicyPeer peer1 = new NetworkPolicyPeerBuilder()
            .withNewPodSelector()
                .withMatchExpressions(new LabelSelectorRequirementBuilder().withKey("my-key1").withValues("my-value1").build())
            .endPodSelector()
            .build();

    NetworkPolicyPeer peer2 = new NetworkPolicyPeerBuilder()
            .withNewNamespaceSelector()
                .withMatchExpressions(new LabelSelectorRequirementBuilder().withKey("my-key2").withValues("my-value2").build())
            .endNamespaceSelector()
            .build();

    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
                .editKafka()
                    .withNewListeners()
                        .withNewPlain()
                            .withNetworkPolicyPeers(peer1)
                        .endPlain()
                        .withNewTls()
                            .withNetworkPolicyPeers(peer2)
                        .endTls()
                        .withNewKafkaListenerExternalRoute()
                            .withNetworkPolicyPeers(peer1, peer2)
                        .endKafkaListenerExternalRoute()
                    .endListeners()
                .endKafka()
            .endSpec()
            .build();
    KafkaCluster k = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Network Policies
    NetworkPolicy np = k.generateNetworkPolicy(true);

    List<NetworkPolicyIngressRule> rules = np.getSpec().getIngress().stream().filter(ing -> ing.getPorts().get(0).getPort().equals(new IntOrString(KafkaCluster.CLIENT_PORT))).collect(Collectors.toList());
    assertThat(rules.size(), is(1));
    assertThat(rules.get(0).getFrom().get(0), is(peer1));

    rules = np.getSpec().getIngress().stream().filter(ing -> ing.getPorts().get(0).getPort().equals(new IntOrString(KafkaCluster.CLIENT_TLS_PORT))).collect(Collectors.toList());
    assertThat(rules.size(), is(1));
    assertThat(rules.get(0).getFrom().get(0), is(peer2));

    rules = np.getSpec().getIngress().stream().filter(ing -> ing.getPorts().get(0).getPort().equals(new IntOrString(KafkaCluster.EXTERNAL_PORT))).collect(Collectors.toList());
    assertThat(rules.size(), is(1));
    assertThat(rules.get(0).getFrom().size(), is(2));
    assertThat(rules.get(0).getFrom().contains(peer1), is(true));
    assertThat(rules.get(0).getFrom().contains(peer2), is(true));
}