Search in sources :

Example 1 with Exec

use of io.kubernetes.client.Exec in project java by kubernetes-client.

the class ExecExample method main.

public static void main(String[] args) throws IOException, ApiException, InterruptedException {
    String podName = "nginx-4217019353-k5sn9";
    String namespace = "default";
    List<String> commands = new ArrayList<>();
    int len = args.length;
    if (len >= 1)
        podName = args[0];
    if (len >= 2)
        namespace = args[1];
    for (int i = 2; i < len; i++) {
        commands.add(args[i]);
    }
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    Exec exec = new Exec();
    boolean tty = System.console() != null;
    // final Process proc = exec.exec("default", "nginx-4217019353-k5sn9", new String[]
    // {"sh", "-c", "echo foo"}, true, tty);
    final Process proc = exec.exec(namespace, podName, commands.isEmpty() ? new String[] { "sh" } : commands.toArray(new String[commands.size()]), true, tty);
    Thread in = new Thread(new Runnable() {

        public void run() {
            try {
                ByteStreams.copy(System.in, proc.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    in.start();
    Thread out = new Thread(new Runnable() {

        public void run() {
            try {
                ByteStreams.copy(proc.getInputStream(), System.out);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    out.start();
    proc.waitFor();
    // wait for any last output; no need to wait for input thread
    out.join();
    proc.destroy();
    System.exit(proc.exitValue());
}
Also used : Exec(io.kubernetes.client.Exec) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ApiClient(io.kubernetes.client.ApiClient)

Aggregations

ApiClient (io.kubernetes.client.ApiClient)1 Exec (io.kubernetes.client.Exec)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1