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

The following examples show how to use io.fabric8.kubernetes.api.model.TolerationBuilder. 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: DeploymentHandler.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
private List<Toleration> populatePodTolerations(List<PodTolerationModel> podTolerationModels) {
    List<Toleration> tolerations = null;

    if (null != podTolerationModels && podTolerationModels.size() > 0) {
        tolerations = new LinkedList<>();
        for (PodTolerationModel podTolerationModel : podTolerationModels) {
            Toleration toleration = new TolerationBuilder()
                    .withKey(podTolerationModel.getKey())
                    .withOperator(podTolerationModel.getOperator())
                    .withValue(podTolerationModel.getValue())
                    .withEffect(podTolerationModel.getEffect())
                    .withTolerationSeconds((long) podTolerationModel.getTolerationSeconds())
                    .build();

            tolerations.add(toleration);
        }
    }

    return tolerations;
}
 
Example #2
Source File: KubernetesToleration.java    From flink with Apache License 2.0 6 votes vote down vote up
public static KubernetesToleration fromMap(Map<String, String> stringMap) {
	final TolerationBuilder tolerationBuilder = new TolerationBuilder();
	stringMap.forEach((k, v) -> {
		switch (k.toLowerCase()) {
			case "effect":
				tolerationBuilder.withEffect(v);
				break;
			case "key":
				tolerationBuilder.withKey(v);
				break;
			case "operator":
				tolerationBuilder.withOperator(v);
				break;
			case "tolerationseconds":
				tolerationBuilder.withTolerationSeconds(Long.valueOf(v));
				break;
			case "value":
				tolerationBuilder.withValue(v);
				break;
			default:
				LOG.warn("Unrecognized key({}) of toleration, will ignore.", k);
				break;
		}
	});
	return new KubernetesToleration(tolerationBuilder.build());
}
 
Example #3
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));
}
 
Example #4
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 #5
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 #6
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 #7
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"));
}