kubernetes-client/java

The sample "AttachExample" cannot retrieve anything.

Closed this issue · 0 comments

This is my code.
He wrote it according to the example.

    public static void main(String[] args) throws IOException, ApiException, InterruptedException {
        KubeConfig kubeConfig = KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath));
        ApiClient client = ClientBuilder.kubeconfig(kubeConfig).build();
        io.kubernetes.client.openapi.Configuration.setDefaultApiClient(client);
//        Exec exec = new Exec();
//        Process proc = exec.exec("test", "busybox-test", new String[]{"/bin/sh", "-c", "ls"}, true, true);
//        String output = new String(proc.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
//        System.out.println("====" + output);
        Attach attach = new Attach();
//        Attach.AttachResult result = attach.attach("default", "nginx-demo", true);
        Attach.AttachResult result = attach.newConnectionBuilder("default", "nginx-demo").setStdout(true).setTty(true).setStderr(true).connect();

        new Thread(
                new Runnable() {
                    public void run() {
                        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                        OutputStream output = result.getStandardInputStream();
                        try {
                            while (true) {
                                String line = in.readLine();
                                output.write(line.getBytes());
                                output.write('\n');
                                output.flush();
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                })
                .start();

        new Thread(
                new Runnable() {
                    public void run() {
                        try {
                            Streams.copy(result.getStandardOutputStream(), System.out);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }