Search in sources :

Example 6 with KubeClient

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

the class KeycloakOperatorManager method installKeycloak.

public static CompletableFuture<Void> installKeycloak(KubeClient kubeClient) throws Exception {
    if (SystemTestEnvironment.INSTALL_KEYCLOAK) {
        LOGGER.info("Installing Keycloak : {}", OPERATOR_NS);
        kubeClient.client().namespaces().createOrReplace(new NamespaceBuilder().withNewMetadata().withName(OPERATOR_NS).endMetadata().build());
        SecurityUtils.TlsConfig tls = SecurityUtils.getTLSConfig(OPERATOR_NS + ".svc");
        Secret keycloakCert = new SecretBuilder().withNewMetadata().withName("sso-x509-https-secret").withNamespace(OPERATOR_NS).endMetadata().withType("kubernetes.io/tls").withData(Map.of("tls.crt", new String(Base64.getEncoder().encode(tls.getCert().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8), "tls.key", new String(Base64.getEncoder().encode(tls.getKey().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8))).build();
        kubeClient.client().secrets().inNamespace(OPERATOR_NS).createOrReplace(keycloakCert);
        List<String> keycloakInstallFiles = Arrays.asList("https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/service_account.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/role_binding.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/role.yaml", "https://raw.githubusercontent.com/keycloak/keycloak-operator/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/cluster_roles/cluster_role_binding.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/cluster_roles/cluster_role.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakbackups_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakclients_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakrealms_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloaks_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakusers_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/operator.yaml");
        for (String urlString : keycloakInstallFiles) {
            URL url = new URL(urlString);
            INSTALLED_RESOURCES.add(kubeClient.client().load(url.openStream()).get().get(0));
        }
        for (HasMetadata resource : INSTALLED_RESOURCES) {
            resource.getMetadata().setNamespace(OPERATOR_NS);
            kubeClient.client().resource(resource).inNamespace(OPERATOR_NS).createOrReplace();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("apply", "-f", Paths.get(Environment.SUITE_ROOT, "src", "main", "resources", "keycloak.yml").toAbsolutePath().toString());
        LOGGER.info("Done installing Keycloak : {}", OPERATOR_NS);
        return TestUtils.asyncWaitFor("Keycloak instance ready", 1_000, 600_000, () -> TestUtils.isPodReady(KubeClient.getInstance().client().pods().inNamespace(OPERATOR_NS).list().getItems().stream().filter(pod -> pod.getMetadata().getName().contains("keycloak-0")).findFirst().orElse(null)));
    } else {
        LOGGER.info("Keycloak is not installed suite will use values from env vars for oauth");
        return CompletableFuture.completedFuture(null);
    }
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) SecurityUtils(org.bf2.systemtest.framework.SecurityUtils) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) URL(java.net.URL)

Example 7 with KubeClient

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

the class StrimziOperatorManager method doInstall.

protected CompletableFuture<Void> doInstall(KubeClient kubeClient) throws IOException {
    LOGGER.info("Installing Strimzi : {} version: {}", operatorNs, version);
    Namespace namespace = new NamespaceBuilder().withNewMetadata().withName(operatorNs).endMetadata().build();
    kubeClient.client().namespaces().createOrReplace(namespace);
    URL url = new URL(String.format(STRIMZI_URL_FORMAT, version));
    // modify namespaces, convert rolebinding to clusterrolebindings, update deployment if needed
    String crbID = UUID.randomUUID().toString().substring(0, 5);
    kubeClient.apply(operatorNs, url.openStream(), i -> {
        if (i instanceof Namespaced) {
            i.getMetadata().setNamespace(operatorNs);
        }
        if (i instanceof ClusterRoleBinding) {
            ClusterRoleBinding crb = (ClusterRoleBinding) i;
            crb.getSubjects().forEach(sbj -> sbj.setNamespace(operatorNs));
            crb.getMetadata().setName(crb.getMetadata().getName() + "." + operatorNs);
            clusterWideResourceDeleters.add(unused -> {
                kubeClient.client().rbac().clusterRoleBindings().withName(crb.getMetadata().getName()).delete();
            });
        } else if (i instanceof RoleBinding) {
            RoleBinding rb = (RoleBinding) i;
            rb.getSubjects().forEach(sbj -> sbj.setNamespace(operatorNs));
            ClusterRoleBinding crb = new ClusterRoleBindingBuilder().withNewMetadata().withName(rb.getMetadata().getName() + "-all-ns-" + crbID).withAnnotations(rb.getMetadata().getAnnotations()).withLabels(rb.getMetadata().getLabels()).endMetadata().withRoleRef(rb.getRoleRef()).withSubjects(rb.getSubjects()).build();
            LOGGER.info("Creating {} named {}", crb.getKind(), crb.getMetadata().getName());
            kubeClient.client().rbac().clusterRoleBindings().createOrReplace(crb);
            clusterWideResourceDeleters.add(unused -> {
                kubeClient.client().rbac().clusterRoleBindings().withName(crb.getMetadata().getName()).delete();
            });
        } else if (i instanceof Deployment && "strimzi-cluster-operator".equals(i.getMetadata().getName())) {
            modifyDeployment((Deployment) i);
        }
        return i;
    });
    LOGGER.info("Done installing Strimzi : {}", operatorNs);
    return TestUtils.asyncWaitFor("Strimzi operator ready", 1_000, FleetShardOperatorManager.INSTALL_TIMEOUT_MS, () -> isReady(kubeClient, operatorNs, version));
}
Also used : Namespaced(io.fabric8.kubernetes.api.model.Namespaced) GithubApiClient(org.bf2.systemtest.api.github.GithubApiClient) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) KubeClient(org.bf2.test.k8s.KubeClient) Map(java.util.Map) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) LinkedList(java.util.LinkedList) TestUtils(org.bf2.test.TestUtils) Pod(io.fabric8.kubernetes.api.model.Pod) SystemTestEnvironment(org.bf2.systemtest.framework.SystemTestEnvironment) IOException(java.io.IOException) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) UUID(java.util.UUID) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) Namespaced(io.fabric8.kubernetes.api.model.Namespaced) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Readiness(io.fabric8.kubernetes.client.internal.readiness.Readiness) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) URL(java.net.URL)

Example 8 with KubeClient

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

the class AssertUtils method assertManagedKafka.

public static void assertManagedKafka(ManagedKafka mk) {
    KubeClient kube = KubeClient.getInstance();
    assertNotNull(ManagedKafkaResourceType.getOperation().inNamespace(mk.getMetadata().getNamespace()).withName(mk.getMetadata().getName()).get());
    assertTrue(kube.client().pods().inNamespace(mk.getMetadata().getNamespace()).list().getItems().size() > 0);
    assertEquals("Running", ManagedKafkaResourceType.getCanaryPod(mk).getStatus().getPhase());
    assertEquals("Running", ManagedKafkaResourceType.getAdminApiPod(mk).getStatus().getPhase());
    assertEquals(3, ManagedKafkaResourceType.getKafkaPods(mk).size());
    if (!ManagedKafkaResourceType.isDevKafka(mk)) {
        assertEquals(1, ManagedKafkaResourceType.getKafkaExporterPods(mk).size());
    }
    assertEquals(3, ManagedKafkaResourceType.getZookeeperPods(mk).size());
}
Also used : KubeClient(org.bf2.test.k8s.KubeClient)

Example 9 with KubeClient

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

the class OMB method createWorker.

private void createWorker(String jvmOpts, String name, Node node) throws IOException {
    KubeClient kubeClient = ombCluster.kubeClient();
    DeploymentBuilder deploymentBuilder = new DeploymentBuilder().editOrNewMetadata().withName(name).withNamespace(Constants.OMB_NAMESPACE).addToLabels("app", "worker").endMetadata().editOrNewSpec().withReplicas(1).editOrNewSelector().addToMatchLabels("worker", name).endSelector().editOrNewTemplate().editOrNewMetadata().addToLabels("worker", name).addToLabels("app", "worker").endMetadata().editOrNewSpec().addNewContainer().withName("worker").withImage(Constants.OMB_WORKER_IMAGE).withResources(new ResourceRequirementsBuilder().withLimits(getResourceLimits()).withRequests(getResourceLimits()).build()).addToCommand("sh", "-c").addToEnv(new EnvVar("_JAVA_OPTIONS", jvmOpts, null)).addToEnv(envVars.toArray(new EnvVar[0])).addToArgs("cd /tmp/src; ./bin/benchmark-worker").addToPorts(new ContainerPortBuilder().withContainerPort(8080).build(), new ContainerPortBuilder().withContainerPort(8081).build()).withLivenessProbe(new ProbeBuilder().withInitialDelaySeconds(10).withHttpGet(new HTTPGetActionBuilder().withPort(new IntOrString(8080)).withPath("counters-stats").build()).build()).addNewVolumeMount().withName("ca").withMountPath("/cert").withReadOnly(true).endVolumeMount().endContainer().withTerminationGracePeriodSeconds(15L).addNewVolume().withName("ca").editOrNewSecret().withSecretName("ext-listener-crt").endSecret().endVolume().endSpec().endTemplate().endSpec();
    if (node != null) {
        deploymentBuilder.editSpec().editTemplate().editSpec().withNodeSelector(Collections.singletonMap("kubernetes.io/hostname", node.getMetadata().getLabels().get("kubernetes.io/hostname"))).endSpec().endTemplate().endSpec();
    }
    kubeClient.client().apps().deployments().inNamespace(Constants.OMB_NAMESPACE).createOrReplace(deploymentBuilder.build());
    kubeClient.client().services().inNamespace(Constants.OMB_NAMESPACE).createOrReplace(new ServiceBuilder().editOrNewMetadata().withName(name).withNamespace(Constants.OMB_NAMESPACE).addToLabels("app", "worker").endMetadata().editOrNewSpec().addToSelector("worker", name).addNewPort().withPort(80).withTargetPort(new IntOrString(8080)).withProtocol("TCP").endPort().endSpec().build());
    kubeClient.client().adapt(OpenShiftClient.class).routes().inNamespace(Constants.OMB_NAMESPACE).createOrReplace(new RouteBuilder().editOrNewMetadata().withName(name).withNamespace(Constants.OMB_NAMESPACE).withAnnotations(Map.of("haproxy.router.openshift.io/timeout", "360s")).addToLabels("app", "worker").addToLabels("app.kubernetes.io/name", name).endMetadata().editOrNewSpec().editOrNewTo().withKind("Service").withName(name).endTo().endSpec().build());
}
Also used : HTTPGetActionBuilder(io.fabric8.kubernetes.api.model.HTTPGetActionBuilder) ProbeBuilder(io.fabric8.kubernetes.api.model.ProbeBuilder) KubeClient(org.bf2.test.k8s.KubeClient) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ResourceRequirementsBuilder(io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 10 with KubeClient

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

the class TestExceptionCallbackListener method storeClusterInfo.

/**
 * Stores cluster specific information in case of failed test in test callback
 *
 * @param cluster
 * @param logPath
 * @throws IOException
 */
private void storeClusterInfo(KubeClusterResource cluster, Path logPath) throws IOException {
    Files.createDirectories(logPath);
    LOGGER.info("Storing cluster info for {}", cluster.kubeClient().client().getConfiguration().getMasterUrl());
    Files.writeString(logPath.resolve("describe_cluster.log"), cluster.cmdKubeClient().exec(false, false, "describe", "nodes").out());
    Files.writeString(logPath.resolve("events.log"), cluster.cmdKubeClient().exec(false, false, "get", "events", "--all-namespaces").out());
    ExecutorService executorService = Executors.newFixedThreadPool(4);
    try {
        KubeClient kubeClient = cluster.kubeClient();
        cluster.kubeClient().client().namespaces().list().getItems().stream().filter(ns -> checkAnnotation(ns, Constants.ORG_BF2_KAFKA_PERFORMANCE_COLLECTPODLOG)).forEach(ns -> {
            try {
                Files.writeString(logPath.resolve(String.format("describe_%s_pods.log", ns.getMetadata().getName())), cluster.cmdKubeClient().exec(false, false, "describe", "pods", "-n", ns.getMetadata().getName()).out());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            NonNamespaceOperation<Pod, PodList, PodResource<Pod>> podsOp = kubeClient.client().pods().inNamespace(ns.getMetadata().getName());
            List<Pod> pods = podsOp.list().getItems();
            for (Pod p : pods) {
                try {
                    List<Container> containers = podsOp.withName(p.getMetadata().getName()).get().getSpec().getContainers();
                    for (Container c : containers) {
                        executorService.submit(() -> {
                            Path filePath = logPath.resolve(String.format("%s_%s.log", p.getMetadata().getName(), c.getName()));
                            try {
                                Files.writeString(filePath, podsOp.withName(p.getMetadata().getName()).inContainer(c.getName()).getLog());
                            } catch (IOException e) {
                                LOGGER.warn("Cannot write file {}", filePath, e);
                            }
                        });
                    }
                } catch (Exception ex) {
                    LOGGER.warn("Cannot access logs from pod {} ", p.getMetadata().getName(), ex);
                }
                p.getStatus().getContainerStatuses().stream().filter(cs -> cs.getRestartCount() > 0).forEach(cs -> {
                    executorService.submit(() -> {
                        Path filePath = logPath.resolve(String.format("%s_%s_terminated.log", p.getMetadata().getName(), cs.getName()));
                        try {
                            Files.writeString(filePath, podsOp.withName(p.getMetadata().getName()).inContainer(cs.getName()).terminated().getLog());
                        } catch (IOException e) {
                            LOGGER.warn("Cannot write file {}", filePath, e);
                        }
                    });
                });
            }
        });
    } finally {
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.HOURS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
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) Path(java.nio.file.Path) PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Pod(io.fabric8.kubernetes.api.model.Pod) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Container(io.fabric8.kubernetes.api.model.Container) KubeClient(org.bf2.test.k8s.KubeClient) ExecutorService(java.util.concurrent.ExecutorService)

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