使用fabric8java库列出pod文件夹中的所有文件

zengzsys  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(371)

有人能告诉我如何使用fabric8库列出kubernetes pod目录中的所有文件吗?
我已经在codota.com上查看了一些代码示例,但是还没有找到一个对我有帮助的示例。我要做的是获取所有的文件名,比如说pod的/tmp目录,并将这些文件名保存在一个字符串中。类似ls/tmp的东西并保存输出。

1l5u6lss

1l5u6lss1#

您可以使用 writingOutput() 这样地:

try (KubernetesClient client = new DefaultKubernetesClient()) {
    final CountDownLatch execLatch = new CountDownLatch(1);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ExecWatch execWatch = client.pods().inNamespace("default").withName("spring-boot-camel-1-hl6xf")
            .writingOutput(out).withTTY().usingListener(new ExecListener() {
                @Override
                public void onOpen(Response response) {
                    System.out.println("Shell was opened");
                }

                @Override
                public void onFailure(Throwable throwable, Response response) {
                    System.out.println("Shell barfed");
                    execLatch.countDown();
                }

                @Override
                public void onClose(int i, String s) {
                    execLatch.countDown();
                }
            }).exec("ls", "/tmp");

    execLatch.await(5, TimeUnit.SECONDS);
    execWatch.close();
    System.out.println(out.toString());
} catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
    ie.printStackTrace();
}

当我运行这段代码时,我能够看到所需的输出。例如,考虑我的集群在默认命名空间中的这种状态:

~ : $ kubectl get pods
NAME                                     READY   STATUS         RESTARTS   AGE
guide-getting-started-86898fd854-gdxwz   0/1     ErrImagePull   0          2d
quarkus-s2i-1-build                      0/1     Completed      0          2d
quarkus-s2i-2-build                      0/1     Completed      0          2d
spring-boot-camel-1-hl6xf                1/1     Running        0          2d
spring-boot-camel-s2i-1-build            0/1     Completed      0          2d
spring-boot-crd-s2i-1-build              0/1     Completed      0          11d
web-ui-1-build                           0/1     Error          0          4d
~ : $ kubectl exec spring-boot-camel-1-hl6xf ls /tmp
hsperfdata_jboss
hsperfdata_root
ks-script-3QMvMi
scripts
src
undertow-docbase.7492892578802280637.8081
undertow-docbase.8399650292115427382.8080
yum.log
~ : $

这是我在fabric8 java客户端中运行代码后得到的输出:

/usr/java/jdk-14.0.1/bin/java -javaagent:/opt/idea-IC-202.6397.94/lib/idea_rt.jar=36149:/opt/idea-IC-202.6397.94/bin -Dfile.encoding=UTF-8 -classpath /home/rohaan/work/repos/kubernetes-client-demo/target/classes:/home/rohaan/.m2/repository/io/fabric8/kubernetes-client/4.10.3/kubernetes-client-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-core/4.10.3/kubernetes-model-core-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-common/4.10.3/kubernetes-model-common-4.10.3.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/module/jackson-module-jaxb-annotations/2.10.3/jackson-module-jaxb-annotations-2.10.3.jar:/home/rohaan/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/2.3.2/jakarta.xml.bind-api-2.3.2.jar:/home/rohaan/.m2/repository/jakarta/activation/jakarta.activation-api/1.2.1/jakarta.activation-api-1.2.1.jar:/home/rohaan/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/home/rohaan/.m2/repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-rbac/4.10.3/kubernetes-model-rbac-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-admissionregistration/4.10.3/kubernetes-model-admissionregistration-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-apps/4.10.3/kubernetes-model-apps-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-autoscaling/4.10.3/kubernetes-model-autoscaling-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-apiextensions/4.10.3/kubernetes-model-apiextensions-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-batch/4.10.3/kubernetes-model-batch-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-certificates/4.10.3/kubernetes-model-certificates-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-coordination/4.10.3/kubernetes-model-coordination-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-discovery/4.10.3/kubernetes-model-discovery-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-events/4.10.3/kubernetes-model-events-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-extensions/4.10.3/kubernetes-model-extensions-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-networking/4.10.3/kubernetes-model-networking-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-metrics/4.10.3/kubernetes-model-metrics-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-policy/4.10.3/kubernetes-model-policy-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-scheduling/4.10.3/kubernetes-model-scheduling-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-settings/4.10.3/kubernetes-model-settings-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/kubernetes-model-storageclass/4.10.3/kubernetes-model-storageclass-4.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/openshift-model/4.10.3/openshift-model-4.10.3.jar:/home/rohaan/.m2/repository/com/squareup/okhttp3/okhttp/3.12.12/okhttp-3.12.12.jar:/home/rohaan/.m2/repository/com/squareup/okio/okio/1.15.0/okio-1.15.0.jar:/home/rohaan/.m2/repository/com/squareup/okhttp3/logging-interceptor/3.12.12/logging-interceptor-3.12.12.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.10.3/jackson-dataformat-yaml-2.10.3.jar:/home/rohaan/.m2/repository/org/yaml/snakeyaml/1.24/snakeyaml-1.24.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.10.3/jackson-datatype-jsr310-2.10.3.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.3/jackson-annotations-2.10.3.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.3/jackson-databind-2.10.3.jar:/home/rohaan/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.10.3/jackson-core-2.10.3.jar:/home/rohaan/.m2/repository/io/fabric8/zjsonpatch/0.3.0/zjsonpatch-0.3.0.jar:/home/rohaan/.m2/repository/com/github/mifmif/generex/1.0.2/generex-1.0.2.jar:/home/rohaan/.m2/repository/dk/brics/automaton/automaton/1.11-8/automaton-1.11-8.jar:/home/rohaan/.m2/repository/io/fabric8/openshift-client/4.10.3/openshift-client-4.10.3.jar:/home/rohaan/.m2/repository/org/json/json/20190722/json-20190722.jar:/home/rohaan/.m2/repository/org/slf4j/slf4j-simple/1.7.28/slf4j-simple-1.7.28.jar:/home/rohaan/.m2/repository/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar io.fabric8.ExecDemoSaveOutput
Shell was opened
hsperfdata_jboss  src
hsperfdata_root   undertow-docbase.7492892578802280637.8081
ks-script-3QMvMi  undertow-docbase.8399650292115427382.8080
scripts       yum.log

Process finished with exit code 0

相关问题