Search code examples
javakubernetesfabric8

List all files in a pod folder using Fabric8 java library


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.


Solution

  • 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("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();
    }
    

    When I run this code, I'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