Java源码示例:io.fabric8.kubernetes.api.model.Toleration

示例1
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;
}
 
示例2
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;
}
 
示例3
@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")));
}
 
示例4
@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));
}
 
示例5
@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")));
}
 
示例6
@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")));
}
 
示例7
@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")));
}
 
示例8
@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();
    }
}
 
示例9
@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();
    }
}
 
示例10
@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();
    }
}
 
示例11
@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();
    }
}
 
示例12
@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();
    }
}
 
示例13
@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;
}
 
示例14
@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;
}
 
示例15
@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;
}
 
示例16
@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;
}
 
示例17
@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;
}
 
示例18
@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));
}
 
示例19
@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));
}
 
示例20
@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));
}
 
示例21
@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));
}
 
示例22
@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"));
}
 
示例23
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例24
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例25
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例26
@Deprecated
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例27
@Description("The pod's tolerations.")
@KubeLink(group = "core", version = "v1", kind = "toleration")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List<Toleration> getTolerations() {
    return tolerations;
}
 
示例28
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例29
public void setTolerations(List<Toleration> tolerations) {
    this.tolerations = tolerations;
}
 
示例30
/**
     * 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;
    }