use of io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder in project strimzi by strimzi.
the class CruiseControlTest method testTemplate.
@ParallelTest
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");
Map<String, String> saLabels = TestUtils.map("l7", "v7", "l8", "v8");
Map<String, String> saAnots = TestUtils.map("a7", "v7", "a8", "v8");
Affinity affinity = new AffinityBuilder().withNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(new NodeSelectorTermBuilder().addNewMatchExpression().withKey("key1").withOperator("In").withValues("value1", "value2").endMatchExpression().build()).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity().build();
List<Toleration> tolerations = singletonList(new TolerationBuilder().withEffect("NoExecute").withKey("key1").withOperator("Equal").withValue("value1").build());
HostAlias hostAlias1 = new HostAliasBuilder().withHostnames("my-host-1", "my-host-2").withIp("192.168.1.86").build();
HostAlias hostAlias2 = new HostAliasBuilder().withHostnames("my-host-3").withIp("192.168.1.87").build();
CruiseControlSpec cruiseControlSpec = new CruiseControlSpecBuilder().withImage(ccImage).withNewTemplate().withNewDeployment().withNewMetadata().withLabels(depLabels).withAnnotations(depAnots).endMetadata().endDeployment().withNewPod().withNewMetadata().withLabels(podLabels).withAnnotations(podAnots).endMetadata().withPriorityClassName("top-priority").withSchedulerName("my-scheduler").withHostAliases(hostAlias1, hostAlias2).withAffinity(affinity).withTolerations(tolerations).endPod().withNewApiService().withNewMetadata().withLabels(svcLabels).withAnnotations(svcAnots).endMetadata().withIpFamilyPolicy(IpFamilyPolicy.PREFER_DUAL_STACK).withIpFamilies(IpFamily.IPV6, IpFamily.IPV4).endApiService().withNewServiceAccount().withNewMetadata().withLabels(saLabels).withAnnotations(saAnots).endMetadata().endServiceAccount().endTemplate().build();
Kafka resource = createKafka(cruiseControlSpec);
CruiseControl cc = createCruiseControl(resource);
// 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));
assertThat(dep.getSpec().getTemplate().getSpec().getHostAliases(), containsInAnyOrder(hostAlias1, hostAlias2));
// Check Service
svcLabels.putAll(expectedLabels());
Service svc = cc.generateService();
assertThat(svc.getMetadata().getLabels(), is(svcLabels));
assertThat(svc.getMetadata().getAnnotations(), is(svcAnots));
assertThat(svc.getSpec().getIpFamilyPolicy(), is("PreferDualStack"));
assertThat(svc.getSpec().getIpFamilies(), contains("IPv6", "IPv4"));
// Check Service Account
ServiceAccount sa = cc.generateServiceAccount();
assertThat(sa.getMetadata().getLabels().entrySet().containsAll(saLabels.entrySet()), is(true));
assertThat(sa.getMetadata().getAnnotations().entrySet().containsAll(saAnots.entrySet()), is(true));
}
use of io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder in project strimzi by strimzi.
the class ModelUtilsTest method testParsePodTemplate.
@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().withKey("key1").withOperator("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(Reconciliation.DUMMY_RECONCILIATION, 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));
}
use of io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder in project strimzi by strimzi.
the class ModelUtils method populateAffinityBuilderWithRackLabelSelector.
/**
* @param builder the builder which is used to populate the node affinity
* @param userAffinity the userAffinity which is defined by the user
* @param topologyKey the topology key which is used to select the node
* @return the AffinityBuilder which has the node selector with topology key which is needed to make sure
* the pods are scheduled only on nodes with the rack label
*/
public static AffinityBuilder populateAffinityBuilderWithRackLabelSelector(AffinityBuilder builder, Affinity userAffinity, String topologyKey) {
// We need to add node affinity to make sure the pods are scheduled only on nodes with the rack label
NodeSelectorRequirement selector = new NodeSelectorRequirementBuilder().withOperator("Exists").withKey(topologyKey).build();
if (userAffinity != null && userAffinity.getNodeAffinity() != null && userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution() != null && userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution().getNodeSelectorTerms() != null) {
// User has specified some Node Selector Terms => we should enhance them
List<NodeSelectorTerm> oldTerms = userAffinity.getNodeAffinity().getRequiredDuringSchedulingIgnoredDuringExecution().getNodeSelectorTerms();
List<NodeSelectorTerm> enhancedTerms = new ArrayList<>(oldTerms.size());
for (NodeSelectorTerm term : oldTerms) {
NodeSelectorTerm enhancedTerm = new NodeSelectorTermBuilder(term).addToMatchExpressions(selector).build();
enhancedTerms.add(enhancedTerm);
}
builder = builder.editOrNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(enhancedTerms).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity();
} else {
// User has not specified any selector terms => we add our own
builder = builder.editOrNewNodeAffinity().editOrNewRequiredDuringSchedulingIgnoredDuringExecution().addNewNodeSelectorTerm().withMatchExpressions(selector).endNodeSelectorTerm().endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity();
}
return builder;
}
use of io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder in project strimzi by strimzi.
the class KafkaBridgeClusterTest method testTemplate.
@ParallelTest
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");
Map<String, String> saLabels = TestUtils.map("l9", "v9", "l10", "v10");
Map<String, String> saAnots = TestUtils.map("a9", "v9", "a10", "v10");
Affinity affinity = new AffinityBuilder().withNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(new NodeSelectorTermBuilder().addNewMatchExpression().withKey("key1").withOperator("In").withValues("value1", "value2").endMatchExpression().build()).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity().build();
List<Toleration> tolerations = singletonList(new TolerationBuilder().withEffect("NoExecute").withKey("key1").withOperator("Equal").withValue("value1").build());
TopologySpreadConstraint tsc1 = new TopologySpreadConstraintBuilder().withTopologyKey("kubernetes.io/zone").withMaxSkew(1).withWhenUnsatisfiable("DoNotSchedule").withLabelSelector(new LabelSelectorBuilder().withMatchLabels(singletonMap("label", "value")).build()).build();
TopologySpreadConstraint tsc2 = new TopologySpreadConstraintBuilder().withTopologyKey("kubernetes.io/hostname").withMaxSkew(2).withWhenUnsatisfiable("ScheduleAnyway").withLabelSelector(new LabelSelectorBuilder().withMatchLabels(singletonMap("label", "value")).build()).build();
KafkaBridge resource = new KafkaBridgeBuilder(this.resource).editSpec().withNewTemplate().withNewDeployment().withNewMetadata().withLabels(depLabels).withAnnotations(depAnots).endMetadata().withDeploymentStrategy(DeploymentStrategy.RECREATE).endDeployment().withNewPod().withNewMetadata().withLabels(podLabels).withAnnotations(podAnots).endMetadata().withPriorityClassName("top-priority").withSchedulerName("my-scheduler").withAffinity(affinity).withTolerations(tolerations).withTopologySpreadConstraints(tsc1, tsc2).withEnableServiceLinks(false).withTmpDirSizeLimit("10Mi").endPod().withNewApiService().withNewMetadata().withLabels(svcLabels).withAnnotations(svcAnots).endMetadata().withIpFamilyPolicy(IpFamilyPolicy.PREFER_DUAL_STACK).withIpFamilies(IpFamily.IPV6, IpFamily.IPV4).endApiService().withNewPodDisruptionBudget().withNewMetadata().withLabels(pdbLabels).withAnnotations(pdbAnots).endMetadata().endPodDisruptionBudget().withNewServiceAccount().withNewMetadata().withLabels(saLabels).withAnnotations(saAnots).endMetadata().endServiceAccount().endTemplate().endSpec().build();
KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, 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"));
assertThat(dep.getSpec().getStrategy().getType(), is("Recreate"));
assertThat(dep.getSpec().getStrategy().getRollingUpdate(), is(nullValue()));
// 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));
assertThat(dep.getSpec().getTemplate().getSpec().getTopologySpreadConstraints(), containsInAnyOrder(tsc1, tsc2));
assertThat(dep.getSpec().getTemplate().getSpec().getEnableServiceLinks(), is(false));
assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().get(0).getEmptyDir().getSizeLimit(), is(new Quantity("10Mi")));
// 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));
assertThat(svc.getSpec().getIpFamilyPolicy(), is("PreferDualStack"));
assertThat(svc.getSpec().getIpFamilies(), contains("IPv6", "IPv4"));
// 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));
// Check PodDisruptionBudget V1Beta1
io.fabric8.kubernetes.api.model.policy.v1beta1.PodDisruptionBudget pdbV1Beta1 = kbc.generatePodDisruptionBudgetV1Beta1();
assertThat(pdbV1Beta1.getMetadata().getLabels().entrySet().containsAll(pdbLabels.entrySet()), is(true));
assertThat(pdbV1Beta1.getMetadata().getAnnotations().entrySet().containsAll(pdbAnots.entrySet()), is(true));
// Check Service Account
ServiceAccount sa = kbc.generateServiceAccount();
assertThat(sa.getMetadata().getLabels().entrySet().containsAll(saLabels.entrySet()), is(true));
assertThat(sa.getMetadata().getAnnotations().entrySet().containsAll(saAnots.entrySet()), is(true));
}
use of io.fabric8.kubernetes.api.model.NodeSelectorTermBuilder in project strimzi-kafka-operator by strimzi.
the class KafkaBridgeClusterTest method testTemplate.
@ParallelTest
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");
Map<String, String> saLabels = TestUtils.map("l9", "v9", "l10", "v10");
Map<String, String> saAnots = TestUtils.map("a9", "v9", "a10", "v10");
Affinity affinity = new AffinityBuilder().withNewNodeAffinity().withNewRequiredDuringSchedulingIgnoredDuringExecution().withNodeSelectorTerms(new NodeSelectorTermBuilder().addNewMatchExpression().withKey("key1").withOperator("In").withValues("value1", "value2").endMatchExpression().build()).endRequiredDuringSchedulingIgnoredDuringExecution().endNodeAffinity().build();
List<Toleration> tolerations = singletonList(new TolerationBuilder().withEffect("NoExecute").withKey("key1").withOperator("Equal").withValue("value1").build());
TopologySpreadConstraint tsc1 = new TopologySpreadConstraintBuilder().withTopologyKey("kubernetes.io/zone").withMaxSkew(1).withWhenUnsatisfiable("DoNotSchedule").withLabelSelector(new LabelSelectorBuilder().withMatchLabels(singletonMap("label", "value")).build()).build();
TopologySpreadConstraint tsc2 = new TopologySpreadConstraintBuilder().withTopologyKey("kubernetes.io/hostname").withMaxSkew(2).withWhenUnsatisfiable("ScheduleAnyway").withLabelSelector(new LabelSelectorBuilder().withMatchLabels(singletonMap("label", "value")).build()).build();
KafkaBridge resource = new KafkaBridgeBuilder(this.resource).editSpec().withNewTemplate().withNewDeployment().withNewMetadata().withLabels(depLabels).withAnnotations(depAnots).endMetadata().withDeploymentStrategy(DeploymentStrategy.RECREATE).endDeployment().withNewPod().withNewMetadata().withLabels(podLabels).withAnnotations(podAnots).endMetadata().withPriorityClassName("top-priority").withSchedulerName("my-scheduler").withAffinity(affinity).withTolerations(tolerations).withTopologySpreadConstraints(tsc1, tsc2).withEnableServiceLinks(false).withTmpDirSizeLimit("10Mi").endPod().withNewApiService().withNewMetadata().withLabels(svcLabels).withAnnotations(svcAnots).endMetadata().withIpFamilyPolicy(IpFamilyPolicy.PREFER_DUAL_STACK).withIpFamilies(IpFamily.IPV6, IpFamily.IPV4).endApiService().withNewPodDisruptionBudget().withNewMetadata().withLabels(pdbLabels).withAnnotations(pdbAnots).endMetadata().endPodDisruptionBudget().withNewServiceAccount().withNewMetadata().withLabels(saLabels).withAnnotations(saAnots).endMetadata().endServiceAccount().endTemplate().endSpec().build();
KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, 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"));
assertThat(dep.getSpec().getStrategy().getType(), is("Recreate"));
assertThat(dep.getSpec().getStrategy().getRollingUpdate(), is(nullValue()));
// 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));
assertThat(dep.getSpec().getTemplate().getSpec().getTopologySpreadConstraints(), containsInAnyOrder(tsc1, tsc2));
assertThat(dep.getSpec().getTemplate().getSpec().getEnableServiceLinks(), is(false));
assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().get(0).getEmptyDir().getSizeLimit(), is(new Quantity("10Mi")));
// 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));
assertThat(svc.getSpec().getIpFamilyPolicy(), is("PreferDualStack"));
assertThat(svc.getSpec().getIpFamilies(), contains("IPv6", "IPv4"));
// 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));
// Check PodDisruptionBudget V1Beta1
io.fabric8.kubernetes.api.model.policy.v1beta1.PodDisruptionBudget pdbV1Beta1 = kbc.generatePodDisruptionBudgetV1Beta1();
assertThat(pdbV1Beta1.getMetadata().getLabels().entrySet().containsAll(pdbLabels.entrySet()), is(true));
assertThat(pdbV1Beta1.getMetadata().getAnnotations().entrySet().containsAll(pdbAnots.entrySet()), is(true));
// Check Service Account
ServiceAccount sa = kbc.generateServiceAccount();
assertThat(sa.getMetadata().getLabels().entrySet().containsAll(saLabels.entrySet()), is(true));
assertThat(sa.getMetadata().getAnnotations().entrySet().containsAll(saAnots.entrySet()), is(true));
}
Aggregations