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

The following examples show how to use io.fabric8.kubernetes.api.model.Toleration. 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: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
List<Toleration> getTolerations(Map<String, String> kubernetesDeployerProperties) {
	List<Toleration> tolerations = new ArrayList<>();

	KubernetesDeployerProperties deployerProperties = bindProperties(kubernetesDeployerProperties,
			this.propertyPrefix + ".tolerations", "tolerations" );

	deployerProperties.getTolerations().forEach(toleration -> tolerations.add(
			new Toleration(toleration.getEffect(), toleration.getKey(), toleration.getOperator(),
					toleration.getTolerationSeconds(), toleration.getValue())));

	this.properties.getTolerations().stream()
			.filter(toleration -> tolerations.stream()
					.noneMatch(existing -> existing.getKey().equals(toleration.getKey())))
			.collect(Collectors.toList())
			.forEach(toleration -> tolerations.add(new Toleration(toleration.getEffect(), toleration.getKey(),
					toleration.getOperator(), toleration.getTolerationSeconds(), toleration.getValue())));

	return tolerations;
}
 
Example #2
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void deployWithGlobalTolerations() {
	AppDefinition definition = new AppDefinition("app-test", null);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.tolerations",
			"[{key: 'test', value: 'true', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}, "
					+ "{key: 'test2', value: 'false', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}]");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

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

	assertNotNull(podSpec.getTolerations());
	assertThat(podSpec.getTolerations().size() == 2);
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test", "Equal", 5L, "true")));
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test2", "Equal", 5L, "false")));
}
 
Example #3
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 #4
Source File: PodTemplateUtilsTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCombineAllTolerations() {
    PodSpec podSpec1 = new PodSpec();
    Pod pod1 = new Pod();
    Toleration toleration1 = new Toleration("effect1", "key1", "oper1", Long.parseLong("1"), "val1");
    Toleration toleration2 = new Toleration("effect2", "key2", "oper2", Long.parseLong("2"), "val2");
    podSpec1.setTolerations(asList(toleration1, toleration2));
    pod1.setSpec(podSpec1);
    pod1.setMetadata(new ObjectMeta());

    PodSpec podSpec2 = new PodSpec();
    Pod pod2 = new Pod();
    Toleration toleration3 = new Toleration("effect3", "key3", "oper3", Long.parseLong("3"), "val3");
    Toleration toleration4 = new Toleration("effect4", "key4", "oper4", Long.parseLong("4"), "val4");
    podSpec2.setTolerations(asList(toleration3, toleration4));
    pod2.setSpec(podSpec2);
    pod2.setMetadata(new ObjectMeta());

    Pod result = combine(pod1, pod2);
    assertThat(result.getSpec().getTolerations(), containsInAnyOrder(toleration1, toleration2, toleration3, toleration4));
}
 
Example #5
Source File: EntityOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@DeprecatedProperty(movedToPath = "spec.template.pod.tolerations")
@Deprecated
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #6
Source File: KafkaClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@DeprecatedProperty(movedToPath = "spec.kafka.template.pod.tolerations")
@Deprecated
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #7
Source File: KafkaMirrorMakerSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@DeprecatedProperty(movedToPath = "spec.template.pod.tolerations")
@Deprecated
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #8
Source File: AbstractKafkaConnectSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_NULL)
@DeprecatedProperty(movedToPath = "spec.template.pod.tolerations")
@Deprecated
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #9
Source File: ZookeeperClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@DeprecatedProperty(movedToPath = "spec.zookeeper.template.pod.tolerations")
@Deprecated
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #10
Source File: KafkaMirrorMakerCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static List<Toleration> tolerations(KafkaMirrorMakerSpec spec) {
    if (spec.getTemplate() != null
            && spec.getTemplate().getPod() != null
            && spec.getTemplate().getPod().getTolerations() != null) {
        if (spec.getTolerations() != null) {
            log.warn("Tolerations given on both spec.tolerations and spec.template.pod.tolerations; latter takes precedence");
        }
        return spec.getTemplate().getPod().getTolerations();
    } else {
        return spec.getTolerations();
    }
}
 
Example #11
Source File: EntityOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static List<Toleration> tolerations(EntityOperatorSpec entityOperatorSpec) {
    if (entityOperatorSpec.getTemplate() != null
            && entityOperatorSpec.getTemplate().getPod() != null
            && entityOperatorSpec.getTemplate().getPod().getTolerations() != null) {
        if (entityOperatorSpec.getTolerations() != null) {
            log.warn("Tolerations given on both spec.entityOperator.tolerations and spec.entityOperator.template.pod.tolerations; latter takes precedence");
        }
        return entityOperatorSpec.getTemplate().getPod().getTolerations();
    } else {
        return entityOperatorSpec.getTolerations();
    }
}
 
Example #12
Source File: KafkaConnectCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static <C extends KafkaConnectCluster> List<Toleration> tolerations(KafkaConnectSpec spec) {
    if (spec.getTemplate() != null
            && spec.getTemplate().getPod() != null
            && spec.getTemplate().getPod().getTolerations() != null) {
        if (spec.getTolerations() != null) {
            log.warn("Tolerations given on both spec.tolerations and spec.template.pod.tolerations; latter takes precedence");
        }
        return spec.getTemplate().getPod().getTolerations();
    } else {
        return spec.getTolerations();
    }
}
 
Example #13
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static List<Toleration> tolerations(KafkaClusterSpec kafkaClusterSpec) {
    if (kafkaClusterSpec.getTemplate() != null
            && kafkaClusterSpec.getTemplate().getPod() != null
            && kafkaClusterSpec.getTemplate().getPod().getTolerations() != null) {
        if (kafkaClusterSpec.getTolerations() != null) {
            log.warn("Tolerations given on both spec.kafka.tolerations and spec.kafka.template.pod.tolerations; latter takes precedence");
        }
        return kafkaClusterSpec.getTemplate().getPod().getTolerations();
    } else {
        return kafkaClusterSpec.getTolerations();
    }
}
 
Example #14
Source File: ZookeeperCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
static List<Toleration> tolerations(ZookeeperClusterSpec zookeeperClusterSpec) {
    if (zookeeperClusterSpec.getTemplate() != null
            && zookeeperClusterSpec.getTemplate().getPod() != null
            && zookeeperClusterSpec.getTemplate().getPod().getTolerations() != null) {
        if (zookeeperClusterSpec.getAffinity() != null) {
            log.warn("Tolerations given on both spec.zookeeper.tolerations and spec.zookeeper.template.pod.tolerations; latter takes precedence");
        }
        return zookeeperClusterSpec.getTemplate().getPod().getTolerations();
    } else {
        return zookeeperClusterSpec.getTolerations();
    }
}
 
Example #15
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void deployWithDuplicateGlobalToleration() {
	AppDefinition definition = new AppDefinition("app-test", null);

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	KubernetesDeployerProperties.Toleration toleration1 = new KubernetesDeployerProperties.Toleration();
	toleration1.setEffect("NoSchedule");
	toleration1.setKey("test");
	toleration1.setOperator("Equal");
	toleration1.setTolerationSeconds(5L);
	toleration1.setValue("false");

	kubernetesDeployerProperties.getTolerations().add(toleration1);

	KubernetesDeployerProperties.Toleration toleration2 = new KubernetesDeployerProperties.Toleration();
	toleration2.setEffect("NoSchedule");
	toleration2.setKey("test");
	toleration2.setOperator("Equal");
	toleration2.setTolerationSeconds(5L);
	toleration2.setValue("true");

	kubernetesDeployerProperties.getTolerations().add(toleration2);

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

	assertNotNull(podSpec.getTolerations());
	assertThat(podSpec.getTolerations().size() == 1);
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test2", "Equal", 5L, "true")));
}
 
Example #16
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void deployWithDuplicateTolerationKeyPropertyOverride() {
	AppDefinition definition = new AppDefinition("app-test", null);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.tolerations",
			"[{key: 'test', value: 'true', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}]");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties.Toleration toleration = new KubernetesDeployerProperties.Toleration();
	toleration.setEffect("NoSchedule");
	toleration.setKey("test");
	toleration.setOperator("Equal");
	toleration.setTolerationSeconds(5L);
	toleration.setValue("false");

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.getTolerations().add(toleration);

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

	assertNotNull(podSpec.getTolerations());
	assertThat(podSpec.getTolerations().size() == 1);
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test2", "Equal", 5L, "false")));
}
 
Example #17
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void deployWithTolerationPropertyOverride() {
	AppDefinition definition = new AppDefinition("app-test", null);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.tolerations",
			"[{key: 'test', value: 'true', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}, "
					+ "{key: 'test2', value: 'false', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}]");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties.Toleration toleration = new KubernetesDeployerProperties.Toleration();
	toleration.setEffect("NoSchedule");
	toleration.setKey("test");
	toleration.setOperator("Equal");
	toleration.setTolerationSeconds(5L);
	toleration.setValue("false");

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.getTolerations().add(toleration);

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

	assertNotNull(podSpec.getTolerations());
	assertThat(podSpec.getTolerations().size() == 2);
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test", "Equal", 5L, "true")));
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test2", "Equal", 5L, "false")));
}
 
Example #18
Source File: PodTemplateUtils.java    From kubernetes-plugin with Apache License 2.0 4 votes vote down vote up
/**
     * Combines a Pod with its parent.
     * @param parent        The parent Pod (nullable).
     * @param template      The child Pod
     */
    public static Pod combine(Pod parent, Pod template) {
        Preconditions.checkNotNull(template, "Pod template should not be null");
        if (parent == null) {
            return template;
        }

        LOGGER.finest(() -> "Combining pods, parent: " + Serialization.asYaml(parent) + " template: " + Serialization.asYaml(template));

        Map<String, String> nodeSelector = mergeMaps(parent.getSpec().getNodeSelector(),
                template.getSpec().getNodeSelector());
        String serviceAccount = Strings.isNullOrEmpty(template.getSpec().getServiceAccount())
                ? parent.getSpec().getServiceAccount()
                : template.getSpec().getServiceAccount();

        Boolean hostNetwork = template.getSpec().getHostNetwork() != null
                ? template.getSpec().getHostNetwork()
                : parent.getSpec().getHostNetwork();

        Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(),
                template.getMetadata().getAnnotations());
        Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(), template.getMetadata().getLabels());

        Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
        imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
        imagePullSecrets.addAll(template.getSpec().getImagePullSecrets());

        // Containers
        Map<String, Container> combinedContainers = new HashMap<>();
        Map<String, Container> parentContainers = parent.getSpec().getContainers().stream()
                .collect(toMap(c -> c.getName(), c -> c));
        combinedContainers.putAll(parentContainers);
        combinedContainers.putAll(template.getSpec().getContainers().stream()
                .collect(toMap(c -> c.getName(), c -> combine(parentContainers.get(c.getName()), c))));

        // Volumes
        List<Volume> combinedVolumes = combineVolumes(parent.getSpec().getVolumes(), template.getSpec().getVolumes());

        // Tolerations
        List<Toleration> combinedTolerations = Lists.newLinkedList();
        Optional.ofNullable(parent.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
        Optional.ofNullable(template.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);

//        WorkspaceVolume workspaceVolume = template.isCustomWorkspaceVolumeEnabled() && template.getWorkspaceVolume() != null ? template.getWorkspaceVolume() : parent.getWorkspaceVolume();

        //Tool location node properties
//        List<ToolLocationNodeProperty> toolLocationNodeProperties = new ArrayList<>();
//        toolLocationNodeProperties.addAll(parent.getNodeProperties());
//        toolLocationNodeProperties.addAll(template.getNodeProperties());

        MetadataNested<PodBuilder> metadataBuilder = new PodBuilder(parent).withNewMetadataLike(parent.getMetadata()) //
                .withAnnotations(podAnnotations).withLabels(podLabels);
        if (!Strings.isNullOrEmpty(template.getMetadata().getName())) {
            metadataBuilder.withName(template.getMetadata().getName());
        }
        if (!Strings.isNullOrEmpty(template.getMetadata().getNamespace())) {
            metadataBuilder.withNamespace(template.getMetadata().getNamespace());
        }

        SpecNested<PodBuilder> specBuilder = metadataBuilder.endMetadata() //
                .withNewSpecLike(parent.getSpec()) //
                .withNodeSelector(nodeSelector) //
                .withServiceAccount(serviceAccount) //
                .withHostNetwork(hostNetwork) //
                .withContainers(Lists.newArrayList(combinedContainers.values())) //
                .withVolumes(combinedVolumes) //
                .withTolerations(combinedTolerations) //
                .withImagePullSecrets(Lists.newArrayList(imagePullSecrets));


        // Security context
        specBuilder.editOrNewSecurityContext()
                .withRunAsUser(
                        template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsUser() != null ? template.getSpec().getSecurityContext().getRunAsUser() : (
                                parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsUser() != null ? parent.getSpec().getSecurityContext().getRunAsUser() : null
                        )
                )
                .withRunAsGroup(
                        template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsGroup() != null ? template.getSpec().getSecurityContext().getRunAsGroup() : (
                                parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsGroup() != null ? parent.getSpec().getSecurityContext().getRunAsGroup() : null
                        )
                )
                .endSecurityContext();

        // podTemplate.setLabel(label);
//        podTemplate.setEnvVars(combineEnvVars(parent, template));
//        podTemplate.setWorkspaceVolume(workspaceVolume);
//        podTemplate.setNodeProperties(toolLocationNodeProperties);
//        podTemplate.setNodeUsageMode(nodeUsageMode);
//        podTemplate.setYaml(template.getYaml() == null ? parent.getYaml() : template.getYaml());

        Pod pod = specBuilder.endSpec().build();
        LOGGER.finest(() -> "Pods combined: " + Serialization.asYaml(pod));
        return pod;
    }
 
Example #19
Source File: ZookeeperClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #20
Source File: PodTemplate.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #21
Source File: PodTemplate.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List<Toleration> getTolerations() {
    return tolerations;
}
 
Example #22
Source File: KafkaClusterSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #23
Source File: KubernetesToleration.java    From flink with Apache License 2.0 4 votes vote down vote up
private KubernetesToleration(Toleration toleration) {
	super(toleration);
}
 
Example #24
Source File: EntityOperatorSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #25
Source File: KafkaMirrorMakerSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #26
Source File: AbstractKafkaConnectSpec.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
Example #27
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"));
}
 
Example #28
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 #29
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 #30
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));
}