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);
}
}
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));
}
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());
}
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());
}
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();
}
}
}
Aggregations