Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MGDSTRM-10130 addressing test inconsistency #835

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.fabric8.kubernetes.api.model.ResourceRequirements;
import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.TolerationBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
Expand Down Expand Up @@ -526,7 +527,10 @@ private void buildIngressController(String name, String domain,
builder
.editSpec()
.editOrNewNodePlacement()
.addNewToleration("NoSchedule", "kas-fleetshard-ingress", "Exists", null, null)
.withTolerations(new TolerationBuilder().withEffect("NoSchedule")
.withKey("kas-fleetshard-ingress")
.withOperator("Exists")
.build())
.endNodePlacement()
.endSpec();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.api.model.RouteBuilder;
import io.fabric8.openshift.api.model.RouteTargetReferenceBuilder;
Expand All @@ -25,16 +24,14 @@
import io.quarkus.test.kubernetes.client.KubernetesServerTestResource;
import io.quarkus.test.kubernetes.client.KubernetesTestServer;
import io.strimzi.api.kafka.model.Kafka;
import org.bf2.common.ManagedKafkaAgentResourceClient;
import org.bf2.common.OperandUtils;
import org.bf2.operator.operands.AbstractKafkaCluster;
import org.bf2.operator.operands.KafkaCluster;
import org.bf2.operator.resources.v1alpha1.ManagedKafka;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaAgentBuilder;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaAgent;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaBuilder;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaSpecBuilder;
import org.bf2.operator.resources.v1alpha1.ProfileBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -61,7 +58,7 @@
@QuarkusTest
class IngressControllerManagerTest {

private static final double ZONE_PERCENTAGE = 1d/3;
static final double ZONE_PERCENTAGE = 1d/3;

@Inject
IngressControllerManager ingressControllerManager;
Expand Down Expand Up @@ -90,22 +87,6 @@ void testIngressControllerCreationWithNoZones() {
checkDefaultReplicaCount(0, "Expected 0 replicas because there are 0 nodes");
}

@Test
void testIngressControllerNodePlacement() {
useProfileLabels();

ingressControllerManager.reconcileIngressControllers();

List<IngressController> ingressControllers = openShiftClient.operator().ingressControllers().inNamespace(IngressControllerManager.INGRESS_OPERATOR_NAMESPACE).list().getItems();
String tolerations = Serialization.asYaml(ingressControllers.get(0).getSpec().getNodePlacement().getTolerations());

// the additional toleration will cause the deployment to roll
assertEquals("---\n"
+ "- effect: \"NoSchedule\"\n"
+ " key: \"kas-fleetshard-ingress\"\n"
+ " operator: \"Exists\"\n", tolerations);
}

@Test
void testReplicaReduction() {
openShiftClient.resourceList((List)buildNodes(12)).createOrReplace();
Expand Down Expand Up @@ -280,29 +261,6 @@ void testIngressControllerReplicaCountsMultiUnit() {
assertEquals(2, ingressControllerManager.numReplicasForZone(new LongSummaryStatistics(1, 0, Quantity.getAmountInBytes(Quantity.parse("50Mi")).longValue(), Quantity.getAmountInBytes(Quantity.parse("50Mi")).longValue()*24), new LongSummaryStatistics(1, 0, Quantity.getAmountInBytes(Quantity.parse("100Mi")).longValue(), Quantity.getAmountInBytes(Quantity.parse("100Mi")).longValue()*24), 0, ZONE_PERCENTAGE));
}

@Test
void testIngressControllerReplicaCountsWithoutCollocation() {
useProfileLabels();

// should only be 2 replicas for 60 standard instances
long ingress = 50000000;
assertEquals(2, ingressControllerManager.numReplicasForZone(new LongSummaryStatistics(1, 0, ingress, ingress*60), new LongSummaryStatistics(1, 0, ingress*2, ingress*120), 0, ZONE_PERCENTAGE));
}

private void useProfileLabels() {
InformerManager mockInformerManager = Mockito.mock(InformerManager.class);
QuarkusMock.installMockForType(mockInformerManager, InformerManager.class);
// enable the profile
Mockito.when(mockInformerManager.getLocalAgent()).thenReturn(new ManagedKafkaAgentBuilder()
.withNewMetadata()
.withName(ManagedKafkaAgentResourceClient.RESOURCE_NAME)
.endMetadata()
.withNewSpec()
.addToCapacity("standard", new ProfileBuilder().withMaxNodes(6).build())
.endSpec()
.build());
}

private List<Node> buildNodes(int nodeCount) {
return IntStream.range(0, nodeCount).mapToObj(i ->
new NodeBuilder()
Expand Down Expand Up @@ -501,6 +459,8 @@ void cleanup() {
ingressControllerManager.getRouteMatchLabels().clear();
openShiftClient.resources(Node.class).delete();
openShiftClient.resources(Kafka.class).inAnyNamespace().delete();
openShiftClient.resources(ManagedKafka.class).inAnyNamespace().delete();
openShiftClient.resources(ManagedKafkaAgent.class).inAnyNamespace().delete();
openShiftClient.resources(IngressController.class).inNamespace(IngressControllerManager.INGRESS_OPERATOR_NAMESPACE).delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.bf2.operator.managers;

import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.operator.v1.IngressController;
import io.fabric8.openshift.client.OpenShiftClient;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.kubernetes.client.KubernetesServerTestResource;
import io.quarkus.test.kubernetes.client.KubernetesTestServer;
import org.bf2.common.ManagedKafkaAgentResourceClient;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaAgentBuilder;
import org.bf2.operator.resources.v1alpha1.ProfileBuilder;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Inject;

import java.util.List;
import java.util.LongSummaryStatistics;

import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTestResource(KubernetesServerTestResource.class)
@QuarkusTest
public class IngressControllerManagerWithProfilesTest {

@Inject
IngressControllerManager ingressControllerManager;

@Inject
OpenShiftClient openShiftClient;

@KubernetesTestServer
KubernetesServer kubernetesServer;

@InjectMock
InformerManager informerManager;

@Test
void testIngressControllerNodePlacement() {
useProfileLabels();

ingressControllerManager.reconcileIngressControllers();

List<IngressController> ingressControllers = openShiftClient.operator().ingressControllers().inNamespace(IngressControllerManager.INGRESS_OPERATOR_NAMESPACE).list().getItems();
String tolerations = Serialization.asYaml(ingressControllers.get(0).getSpec().getNodePlacement().getTolerations());

// the additional toleration will cause the deployment to roll
assertEquals("---\n"
+ "- effect: \"NoSchedule\"\n"
+ " key: \"kas-fleetshard-ingress\"\n"
+ " operator: \"Exists\"\n", tolerations);
}

@Test
void testIngressControllerReplicaCountsWithoutCollocation() {
useProfileLabels();

// should only be 2 replicas for 60 standard instances
long ingress = 50000000;
assertEquals(2, ingressControllerManager.numReplicasForZone(new LongSummaryStatistics(1, 0, ingress, ingress*60), new LongSummaryStatistics(1, 0, ingress*2, ingress*120), 0, IngressControllerManagerTest.ZONE_PERCENTAGE));
}

void useProfileLabels() {
// enable the profile
Mockito.when(informerManager.getLocalAgent()).thenReturn(new ManagedKafkaAgentBuilder()
.withNewMetadata()
.withName(ManagedKafkaAgentResourceClient.RESOURCE_NAME)
.endMetadata()
.withNewSpec()
.addToCapacity("standard", new ProfileBuilder().withMaxNodes(6).build())
.endSpec()
.build());
}

}