1

How can I read a file from a deployment's pods? More specifically, I want fabric8's kubernetes java client equivalent of the following command:

kubectl exec --stdin --tty deployment/otel-collector -n logcollection -- cat /otel-output/json-out.json

PS: you can ignore the --stdin and --tty flags.

lprakashv
  • 1,121
  • 10
  • 19

1 Answers1

0

It's fairly simple, to read file from a Pod you can use the following syntax (see also fabric8 documentation):

try (InputStream is = client.pods().inNamespace(currentNamespace).withName(pod1.getMetadata().getName()).file("/msg").read())  {
  String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
}
mario
  • 9,858
  • 1
  • 26
  • 42
  • I want to know if I can do it without pod name. I am trying to get the files with just the deployment. – lprakashv Nov 26 '21 at 17:18
  • Well, it could be difficult to achieve as one deployment may manage larger set of pods, so in such case in which of those pods do you want to `cat` your file ? – mario Nov 26 '21 at 18:23