Hi i am trying to use this fun to run a linux comm...
# getting-started
l
Hi i am trying to use this fun to run a linux command inside a k8s pod using fabric8io kubernetes client
Copy code
fun execIntoContainer(podName: String, containerId: String, cmd: String) : String {
    val future = CompletableFuture<Int>()
    val listener = CompletableExecListener(future)
    val out = ByteArrayOutputStream()
    val error = ByteArrayOutputStream()
    val command = cmd.split(" ").toTypedArray()
    val exec = kubernetesClient.pods().withName(podName).inContainer(containerId)
        .writingOutput(out)
        .writingError(error)
        .usingListener(listener)
        .exec(*command)
    future.get(10, TimeUnit.SECONDS)
    exec.close()
    if (error.toString().isEmpty()) {
        return out.toString()
     }
        throw RuntimeException("Error from pod: $error")
    }
it seems works just for one command. when i add a grep for example (ls -ltr | grep t) it gave me an error and seems like the issue is with the split part
Copy code
cmd.split(" ").toTypedArray()
Copy code
org.opentest4j.AssertionFailedError: Unexpected exception thrown: java.lang.RuntimeException: Error from pod: ls: cannot access '|': No such file or directory
ls: cannot access 'grep': No such file or directory
ls: cannot access 't': No such file or directory
any idea how can i use the split?
s
Not really a Kotlin issue, more a detail of the Java API you’re using (or of Java in general). The pipe is a shell operator and this isn’t a shell. If you want to use a pipe, you can try to
exec
a shell and pass your command to it. e.g.
Copy code
val command = arrayOf("/bin/sh", "-c", cmd)
l
amazing you are right i thought to upload a bash script file to the pod using this API and to execute it but this one is much better 10xxx