Search in sources :

Example 1 with CLI

use of org.infinispan.cli.commands.CLI in project infinispan by infinispan.

the class Shell method exec.

@Override
public CommandResult exec(ContextAwareCommandInvocation invocation) {
    KubernetesClient client = KubernetesContext.getClient(invocation);
    namespace = Kube.getNamespaceOrDefault(client, namespace);
    GenericKubernetesResource infinispan = client.genericKubernetesResources(INFINISPAN_CLUSTER_CRD).inNamespace(namespace).withName(name).get();
    if (infinispan == null) {
        throw Messages.MSG.noSuchService(name, namespace);
    }
    String endpointSecretName = Kube.getProperty(infinispan, "spec", "security", "endpointSecretName");
    String certSecretName = Kube.getProperty(infinispan, "spec", "security", "endpointEncryption", "certSecretName");
    Pod pod;
    if (podName == null) {
        pod = client.pods().inNamespace(namespace).withLabel("infinispan_cr", name).list().getItems().stream().filter(p -> "running".equalsIgnoreCase(p.getStatus().getPhase())).findFirst().orElse(null);
    } else {
        pod = client.pods().inNamespace(namespace).withName(podName).get();
    }
    if (pod == null) {
        throw Messages.MSG.noRunningPodsInService(name);
    }
    // Port forwarding mode
    List<ContainerPort> ports = pod.getSpec().getContainers().get(0).getPorts();
    // Find the `infinispan` port
    ContainerPort containerPort = ports.stream().filter(p -> "infinispan".equals(p.getName())).findFirst().get();
    try (LocalPortForward portForward = client.pods().inNamespace(namespace).withName(pod.getMetadata().getName()).portForward(containerPort.getContainerPort())) {
        StringBuilder connection = new StringBuilder();
        List<String> args = new ArrayList<>();
        if (certSecretName != null) {
            connection.append("https://");
            Secret secret = Kube.getSecret(client, namespace, certSecretName);
            final byte[] cert;
            final String suffix;
            if (secret.getData().containsKey("keystore.p12")) {
                cert = Base64.getDecoder().decode(secret.getData().get("keystore.p12"));
                suffix = ".p12";
                String password = new String(Base64.getDecoder().decode(secret.getData().get("password")));
                args.add("-s");
                args.add(password);
            } else {
                cert = new String(Base64.getDecoder().decode(secret.getData().get("tls.crt"))).getBytes(StandardCharsets.UTF_8);
                suffix = ".pem";
            }
            Path certPath = Files.createTempFile("clitrust", suffix, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
            Files.write(certPath, cert);
            args.add("-t");
            args.add(certPath.toString());
            args.add("--hostname-verifier");
            args.add(".*");
        } else {
            connection.append("http://");
        }
        if (endpointSecretName != null) {
            Secret secret = Kube.getSecret(client, namespace, endpointSecretName);
            Map<String, String> credentials = Kube.decodeOpaqueSecrets(secret);
            if (username == null) {
                if (credentials.size() != 1) {
                    throw Messages.MSG.usernameRequired();
                } else {
                    Map.Entry<String, String> entry = credentials.entrySet().iterator().next();
                    connection.append(entry.getKey());
                    connection.append(':');
                    connection.append(entry.getValue());
                    connection.append('@');
                }
            } else {
                connection.append(username);
                if (credentials.containsKey(username)) {
                    connection.append(':');
                    connection.append(credentials.get(username));
                }
                connection.append('@');
            }
        }
        InetAddress localAddress = portForward.getLocalAddress();
        if (localAddress.getAddress().length == 4) {
            connection.append(localAddress.getHostAddress());
        } else {
            connection.append('[').append(localAddress.getHostAddress()).append(']');
        }
        connection.append(':');
        connection.append(portForward.getLocalPort());
        args.add("-c");
        args.add(connection.toString());
        Messages.CLI.debugf("cli %s", args);
        CLI.main(new DefaultShell(), args.toArray(new String[0]), System.getProperties(), false);
        return CommandResult.SUCCESS;
    } catch (Throwable t) {
        TerminalString error = new TerminalString(Util.getRootCause(t).getLocalizedMessage(), new TerminalColor(Color.RED, Color.DEFAULT, Color.Intensity.BRIGHT));
        invocation.getShell().writeln(error.toString());
        return CommandResult.FAILURE;
    }
}
Also used : CommandDefinition(org.aesh.command.CommandDefinition) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) LocalPortForward(io.fabric8.kubernetes.client.LocalPortForward) Argument(org.aesh.command.option.Argument) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) Map(java.util.Map) CommandResult(org.aesh.command.CommandResult) Path(java.nio.file.Path) DEFAULT_CLUSTER_NAME(org.infinispan.cli.commands.kubernetes.Kube.DEFAULT_CLUSTER_NAME) Option(org.aesh.command.option.Option) ContextAwareCommandInvocation(org.infinispan.cli.impl.ContextAwareCommandInvocation) Files(java.nio.file.Files) Messages(org.infinispan.cli.logging.Messages) Util(org.infinispan.commons.util.Util) Color(org.aesh.readline.terminal.formatting.Color) Pod(io.fabric8.kubernetes.api.model.Pod) CliCommand(org.infinispan.cli.commands.CliCommand) StandardCharsets(java.nio.charset.StandardCharsets) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Base64(java.util.Base64) List(java.util.List) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) CLI(org.infinispan.cli.commands.CLI) KubernetesContext(org.infinispan.cli.impl.KubernetesContext) INFINISPAN_CLUSTER_CRD(org.infinispan.cli.commands.kubernetes.Kube.INFINISPAN_CLUSTER_CRD) DefaultShell(org.infinispan.cli.impl.DefaultShell) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Secret(io.fabric8.kubernetes.api.model.Secret) Path(java.nio.file.Path) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) LocalPortForward(io.fabric8.kubernetes.client.LocalPortForward) Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) Secret(io.fabric8.kubernetes.api.model.Secret) DefaultShell(org.infinispan.cli.impl.DefaultShell) TerminalColor(org.aesh.readline.terminal.formatting.TerminalColor) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Map(java.util.Map) InetAddress(java.net.InetAddress)

Aggregations

ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 Secret (io.fabric8.kubernetes.api.model.Secret)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 LocalPortForward (io.fabric8.kubernetes.client.LocalPortForward)1 InetAddress (java.net.InetAddress)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 PosixFilePermissions (java.nio.file.attribute.PosixFilePermissions)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 List (java.util.List)1 Map (java.util.Map)1 CommandDefinition (org.aesh.command.CommandDefinition)1 CommandResult (org.aesh.command.CommandResult)1 Argument (org.aesh.command.option.Argument)1 Option (org.aesh.command.option.Option)1 Color (org.aesh.readline.terminal.formatting.Color)1