use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class Exec method exec.
/**
* Method executes external command
*
* @param command arguments for command
* @param envVars
* @param timeout timeout for execution
* @param logToOutput log output or not
* @param throwErrors look for errors in output and throws exception if true
* @return execution results
*/
public static ExecResult exec(String input, List<String> command, Set<EnvVar> envVars, int timeout, boolean logToOutput, boolean throwErrors) {
int ret = 1;
ExecResult execResult;
try {
Exec executor = new Exec();
LOGGER.info("Command: {}", String.join(" ", command));
ret = executor.execute(input, command, envVars, timeout);
synchronized (LOCK) {
if (logToOutput) {
LOGGER.info("RETURN code: {}", ret);
if (!executor.out().isEmpty()) {
LOGGER.info("======STDOUT START=======");
LOGGER.info("{}", cutExecutorLog(executor.out()));
LOGGER.info("======STDOUT END======");
}
if (!executor.err().isEmpty()) {
LOGGER.info("======STDERR START=======");
LOGGER.info("{}", cutExecutorLog(executor.err()));
LOGGER.info("======STDERR END======");
}
}
}
execResult = new ExecResult(ret, executor.out(), executor.err());
if (throwErrors && ret != 0) {
String msg = "`" + join(" ", command) + "` got status code " + ret + " and stderr:\n------\n" + executor.stdErr + "\n------\nand stdout:\n------\n" + executor.stdOut + "\n------";
Matcher matcher = ERROR_PATTERN.matcher(executor.err());
KubeClusterException t = null;
if (matcher.find()) {
switch(matcher.group(1)) {
case "NotFound":
t = new KubeClusterException.NotFound(execResult, msg);
break;
case "AlreadyExists":
t = new KubeClusterException.AlreadyExists(execResult, msg);
break;
default:
break;
}
}
matcher = INVALID_PATTERN.matcher(executor.err());
if (matcher.find()) {
t = new KubeClusterException.InvalidResource(execResult, msg);
}
if (t == null) {
t = new KubeClusterException(execResult, msg);
}
throw t;
}
return new ExecResult(ret, executor.out(), executor.err());
} catch (IOException | ExecutionException e) {
throw new KubeClusterException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new KubeClusterException(e);
}
}
use of org.bf2.srs.fleetmanager.it.executor.Exec 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());
}
});
}
use of org.bf2.srs.fleetmanager.it.executor.Exec 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);
}
}
use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class SuiteUnitTest method testExecutorError.
@SequentialTest
void testExecutorError() {
ExecBuilder command = Exec.builder().withCommand("ppppeeeepppaaa", "jenda").logToOutput(false).throwErrors(true).timeout(60);
assertThrows(KubeClusterException.class, command::exec);
}
use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class SuiteUnitTest method testExecutor.
@ParallelTest
void testExecutor() {
ExecResult result = Exec.builder().withCommand("ls", System.getProperty("user.dir")).logToOutput(false).throwErrors(true).timeout(60).exec();
assertTrue(result.exitStatus());
}
Aggregations