Search in sources :

Example 1 with KubeClient

use of org.bf2.test.k8s.KubeClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ManagedKafkaResourceType method readiness.

@Override
public Predicate<ManagedKafka> readiness(KubeClient client) {
    AtomicInteger count = new AtomicInteger();
    Set<String> messages = Collections.synchronizedSet(new LinkedHashSet<>());
    return mk -> {
        if (mk == null) {
            throw new IllegalStateException("ManagedKafka is null");
        }
        ManagedKafkaCondition mkc = getCondition(mk.getStatus(), ManagedKafkaCondition.Type.Ready).orElse(null);
        if (mkc == null) {
            return false;
        }
        if (ManagedKafkaCondition.Status.True.name().equals(mkc.getStatus())) {
            return true;
        }
        if (ManagedKafkaCondition.Reason.Error.name().equals(mkc.getReason())) {
            if (messages.add(mkc.getMessage())) {
                LOGGER.warn("ManagedKafka {} in error state {}", mk.getMetadata().getName(), mkc.getMessage());
            }
        // throw new IllegalStateException(String.format("ManagedKafka %s in error state %s", mk.getMetadata().getName(), mkc.getMessage()));
        }
        if (count.getAndIncrement() % 15 == 0) {
            ListOptions opts = new ListOptionsBuilder().withFieldSelector("status.phase=Pending").build();
            client.client().pods().inNamespace(mk.getMetadata().getNamespace()).withLabel("strimzi.io/cluster").list(opts).getItems().forEach(ManagedKafkaResourceType::checkUnschedulablePod);
        }
        return false;
    };
}
Also used : KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) Resource(io.fabric8.kubernetes.client.dsl.Resource) KubeClient(org.bf2.test.k8s.KubeClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) LinkedHashSet(java.util.LinkedHashSet) KeycloakInstance(org.bf2.systemtest.framework.KeycloakInstance) SecurityUtils(org.bf2.systemtest.framework.SecurityUtils) Predicate(java.util.function.Predicate) TestUtils(org.bf2.test.TestUtils) Pod(io.fabric8.kubernetes.api.model.Pod) SystemTestEnvironment(org.bf2.systemtest.framework.SystemTestEnvironment) Set(java.util.Set) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) Optional(java.util.Optional) Kafka(io.strimzi.api.kafka.model.Kafka) LogManager(org.apache.logging.log4j.LogManager) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition)

Example 2 with KubeClient

use of org.bf2.test.k8s.KubeClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class DrainCleanerTest method testKafkaAvailabilityDuringClusterUpgrade.

@Test
@Tag(TestTags.PERF)
void testKafkaAvailabilityDuringClusterUpgrade(TestInfo info) throws Exception {
    long throughput = 41943040;
    int numWorkers = 12;
    int topics = 10;
    int messageSize = 1024;
    int partitionsPerTopic = 205;
    int workerProducerRate = 40000;
    ensureClientClusterCapacityForWorkers(omb.getOmbCluster(), numWorkers, WORKER_SIZE, CPU_SIZE);
    workers = omb.deployWorkers(numWorkers);
    ManagedKafkaCapacity capacity = kafkaProvisioner.defaultCapacity(throughput);
    ManagedKafkaDeployment deployCluster = kafkaProvisioner.deployCluster("cluster1", capacity, AdopterProfile.VALUE_PROD);
    String bootstrapHosts = deployCluster.waitUntilReady();
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
    Future<Integer> nodeDrain;
    Future<File> resultDone;
    try {
        nodeDrain = scheduler.schedule(() -> {
            // this thread simulates the OpenShift cluster upgrade
            LOGGER.info("PERFORMING SCHEDULED NODES DRAIN");
            kafkaProvisioner.getKubernetesCluster().kubeClient().getClusterWorkers().forEach(node -> {
                TestUtils.drainNode(kafkaProvisioner.getKubernetesCluster(), node.getMetadata().getName());
                TestUtils.waitUntilAllPodsReady(kafkaProvisioner.getKubernetesCluster(), deployCluster.getManagedKafka().getMetadata().getNamespace());
                TestUtils.setNodeSchedule(kafkaProvisioner.getKubernetesCluster(), node.getMetadata().getName(), true);
            });
            return 0;
        }, 2, TimeUnit.MINUTES);
        resultDone = scheduler.submit(() -> {
            OMBDriver driver = new OMBDriver().setReplicationFactor(3).setTopicConfig("min.insync.replicas=2\n").setCommonConfigWithBootstrapUrl(bootstrapHosts).setProducerConfig("acks=all\n").setConsumerConfig("auto.offset.reset=earliest\nenable.auto.commit=false\n");
            int producerConsumer = numWorkers / 2;
            LOGGER.info("PERFORMING OMB WORKLOAD");
            OMBWorkloadResult result = omb.runWorkload(instanceDir, driver, workers, new OMBWorkload().setName(info.getDisplayName()).setTopics(topics).setPartitionsPerTopic(partitionsPerTopic).setMessageSize(messageSize).setPayloadFile("src/test/resources/payload/payload-1Kb.data").setSubscriptionsPerTopic(1).setConsumerPerSubscription(producerConsumer).setProducersPerTopic(producerConsumer).setProducerRate(workerProducerRate).setConsumerBacklogSizeGB(0).setTestDurationMinutes(15));
            LOGGER.info("{}: results {}", info.getDisplayName(), result.getResultFile());
            LOGGER.info("COMPLETED OMB WORKLOAD");
            return result.getResultFile();
        });
        Integer podTaskRetVal = nodeDrain.get(25, TimeUnit.MINUTES);
        LOGGER.info("Node drain task return value: {}", podTaskRetVal.toString());
        File resultFile = resultDone.get(25, TimeUnit.MINUTES);
        LOGGER.info("Result file: {}", resultFile);
    } finally {
        scheduler.shutdown();
        scheduler.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) KubeClusterResource(org.bf2.performance.framework.KubeClusterResource) TestTags(org.bf2.performance.framework.TestTags) BeforeEach(org.junit.jupiter.api.BeforeEach) ManagedKafkaCapacity(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacity) TestInfo(org.junit.jupiter.api.TestInfo) File(java.io.File) Executors(java.util.concurrent.Executors) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Future(java.util.concurrent.Future) Logger(org.apache.logging.log4j.Logger) BeforeAll(org.junit.jupiter.api.BeforeAll) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Tag(org.junit.jupiter.api.Tag) LogManager(org.apache.logging.log4j.LogManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagedKafkaCapacity(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacity) File(java.io.File) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 3 with KubeClient

use of org.bf2.test.k8s.KubeClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class TestExceptionCallbackListener method afterTestExecution.

@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
    for (KubeClusterResource kubeClusterResource : KubeClusterResource.getCurrentConnectedClusters()) {
        KubeClient kubeClient = kubeClusterResource.kubeClient();
        kubeClient.client().namespaces().list().getItems().stream().filter(ns -> checkAnnotation(ns, Constants.ORG_BF2_PERFORMANCE_CHECKRESTARTEDCONTAINERS)).forEach(ns -> {
            List<Pod> podsWithRestartedContainers = kubeClient.client().pods().inNamespace(ns.getMetadata().getName()).list().getItems().stream().filter(p -> p.getStatus().getContainerStatuses().stream().anyMatch(cs -> cs.getRestartCount() > 0)).collect(Collectors.toList());
            if (!podsWithRestartedContainers.isEmpty()) {
                LOGGER.error("Found {} pod(s) with containers that had restarted at least once", podsWithRestartedContainers.size());
                podsWithRestartedContainers.forEach(p -> {
                    p.getStatus().getContainerStatuses().stream().filter(cs -> cs.getRestartCount() > 0).forEach(cs -> {
                        LOGGER.error("Pod {} container {} restart count {}", p.getMetadata().getName(), cs.getName(), cs.getRestartCount());
                    });
                });
                if (context.getExecutionException().isEmpty()) {
                    // Fail the test
                    throw new RuntimeException("Found {} pod(s) with containers that had restarted at least once");
                }
            }
        });
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Files(java.nio.file.Files) Constants(org.bf2.performance.Constants) LifecycleMethodExecutionExceptionHandler(org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Collectors(java.util.stream.Collectors) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Logger(org.apache.logging.log4j.Logger) KubeClient(org.bf2.test.k8s.KubeClient) PodList(io.fabric8.kubernetes.api.model.PodList) LogManager(org.apache.logging.log4j.LogManager) TestExecutionExceptionHandler(org.junit.jupiter.api.extension.TestExecutionExceptionHandler) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) KubeClient(org.bf2.test.k8s.KubeClient) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 4 with KubeClient

use of org.bf2.test.k8s.KubeClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class LogCollector method saveClusterState.

private static void saveClusterState(Path logpath) throws IOException {
    KubeClient kube = KubeClient.getInstance();
    Files.writeString(logpath.resolve("describe-cluster-nodes.log"), kube.cmdClient().exec(false, false, "describe", "nodes").out());
    Files.writeString(logpath.resolve("all-events.log"), kube.cmdClient().exec(false, false, "get", "events", "--all-namespaces").out());
    Files.writeString(logpath.resolve("pvs.log"), kube.cmdClient().exec(false, false, "describe", "pv").out());
    Files.writeString(logpath.resolve("operator-routes.yml"), kube.cmdClient().exec(false, false, "get", "routes", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
    Files.writeString(logpath.resolve("operator-services.yml"), kube.cmdClient().exec(false, false, "get", "service", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
    Files.writeString(logpath.resolve("kas-fleetshard-operator-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("strimzi-kafka-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app.kubernetes.io/managed-by=strimzi-cluster-operator", "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("managedkafkas.yml"), kube.cmdClient().exec(false, false, "get", "managedkafka", "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("kafkas.yml"), kube.cmdClient().exec(false, false, "get", "kafka", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("pods-managed-by-operator.yml"), kube.cmdClient().exec(false, false, "get", "pods", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("operator-namespace-events.yml"), kube.cmdClient().exec(false, false, "get", "events", "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    Files.writeString(logpath.resolve("operator.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.OPERATOR_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    Files.writeString(logpath.resolve("sync.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.SYNC_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    StrimziOperatorManager.getStrimziOperatorPods().forEach(pod -> {
        try {
            Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.cmdClient().exec(false, false, "logs", pod.getMetadata().getName(), "--tail", "-1", "-n", pod.getMetadata().getNamespace()).out());
        } catch (Exception e) {
            LOGGER.warn("Cannot get logs from pod {} in namespace {}", pod.getMetadata().getName(), pod.getMetadata().getNamespace());
        }
    });
}
Also used : KubeClient(org.bf2.test.k8s.KubeClient) IOException(java.io.IOException)

Example 5 with KubeClient

use of org.bf2.test.k8s.KubeClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class FleetShardOperatorManager method createEndpoint.

public static String createEndpoint(KubeClient kubeClient) {
    String externalEndpointName = SYNC_NAME + "-external";
    if (kubeClient.isGenericKubernetes()) {
        if (kubeClient.client().services().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
            kubeClient.client().services().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--type=LoadBalancer", "--name", externalEndpointName);
        return new ExecBuilder().withCommand("minikube", "service", "--url", externalEndpointName, "-n", OPERATOR_NS).logToOutput(false).exec().out().trim();
    } else {
        OpenShiftClient openShiftClient = kubeClient.client().adapt(OpenShiftClient.class);
        if (openShiftClient.routes().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
            openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--name", externalEndpointName);
        Route r = openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).get();
        return String.format("%s://%s:%d", r.getSpec().getPort().getTargetPort().getStrVal(), r.getSpec().getHost(), r.getSpec().getPort().getTargetPort().getStrVal().equals("http") ? 80 : 443);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ExecBuilder(org.bf2.test.executor.ExecBuilder) Route(io.fabric8.openshift.api.model.Route)

Aggregations

KubeClient (org.bf2.test.k8s.KubeClient)7 List (java.util.List)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 Pod (io.fabric8.kubernetes.api.model.Pod)4 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)4 IOException (java.io.IOException)4 Collectors (java.util.stream.Collectors)4 Container (io.fabric8.kubernetes.api.model.Container)3 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)2 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)2 PodList (io.fabric8.kubernetes.api.model.PodList)2 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)2 PodResource (io.fabric8.kubernetes.client.dsl.PodResource)2 UncheckedIOException (java.io.UncheckedIOException)2 URL (java.net.URL)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Executors (java.util.concurrent.Executors)2 TimeUnit (java.util.concurrent.TimeUnit)2