使用Fabric8 Java库列出Pod文件夹中的所有文件。

huangapple go评论74阅读模式
英文:

List all files in a pod folder using Fabric8 java library

问题

可以有人指导我如何使用Fabric8库列出Kubernetes Pod目录中的所有文件吗?

我已经在codota.com上查看了代码示例,但尚未找到能帮助我的示例。我想做的是获取所有文件名,比如在我的Pod的/tmp目录中,并将这些名称保存在一个字符串中。就像执行 ls /tmp 并将输出保存起来一样。

英文:

Can someone please point me in the right direction on how to list all files in a kubernetes pod directory using the Fabric8 library?

I have looked at the codota.com for code examples, but have yet to find a example that would help me.
What i want to do is get all the filenames, in lets say the /tmp directory of my pod, and save the names in a string.
Something like ls /tmp and save the output.

答案1

得分: 3

你可以像这样使用 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/ro

<details>
<summary>英文:</summary>

You can easily pipe output using `writingOutput()` like this:

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(&quot;Shell barfed&quot;);
                execLatch.countDown();
            }

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

execLatch.await(5, TimeUnit.SECONDS);
execWatch.close();
System.out.println(out.toString());

} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
ie.printStackTrace();
}


When I run this code, I&#39;m able to see desired output. For example consider this state of my cluster in default namespace:

~ : $ 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
~ : $

This is the output I get after running code in fabric8 java client:

/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


</details>



huangapple
  • 本文由 发表于 2020年8月21日 16:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定