Skip to content

Commit

Permalink
Merge pull request #169 from flux-framework/add/pod-return-output
Browse files Browse the repository at this point in the history
allow stream output in python sdk to return lines
  • Loading branch information
vsoch committed May 16, 2023
2 parents 101aed5 + 4b04964 commit f320113
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hack/python-sdk/template/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if __name__ == "__main__":
setup(
name="fluxoperator",
version="0.0.23",
version="0.0.24",
author="Vanessasaurus",
author_email="vsoch@users.noreply.github.com",
maintainer="Vanessasaurus",
Expand Down
1 change: 1 addition & 0 deletions sdk/python/v1alpha1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pip. Only major versions will be released as tags on Github.

## [0.0.x](https://github.com/flux-framework/flux-operator/tree/main/sdk/python/v2alpha1) (0.0.x)
- stream output should return lines too, if desired (0.0.24)
- custom installRoot for flux (0.0.23)
- add status size variable (0.0.22)
- support maxSize to allow cluster scaling
Expand Down
32 changes: 26 additions & 6 deletions sdk/python/v1alpha1/fluxoperator/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,27 @@ def core_v1(self):
self._core_v1 = core_v1_api.CoreV1Api()
return self._core_v1

def stream_output(self, filename, name=None, namespace=None, pod=None, stdout=True, timestamps=False):
def stream_output(
self,
filename,
name=None,
namespace=None,
pod=None,
stdout=True,
return_output=True,
timestamps=False,
):
"""
Stream output, optionally printing also to stdout.
Also return the output to the user.
"""
namespace = namespace or self.namespace
pod = pod or self.get_broker_pod(name=name, namespace=namespace).metadata.name
watcher = watch.Watch()

# Stream output to file (should we return output too?)
# Stream output to file and return it if desired!
lines = []
with open(filename, "w") as fd:
for line in watcher.stream(
self.core_v1.read_namespaced_pod_log,
Expand All @@ -254,6 +266,12 @@ def stream_output(self, filename, name=None, namespace=None, pod=None, stdout=Tr
fd.write(line.strip() + "\n")
if stdout:
print(line)
if return_output:
lines.append(line)

# I can imagine cases where we wouldn't want to keep it
if return_output:
return lines

def kubectl_exec(self, command, pod=None, quiet=False, namespace=None, name=None):
"""
Expand Down Expand Up @@ -302,7 +320,7 @@ def get_pods(self, namespace, name=None):

# Not found - it was deleted
except kubernetes.client.exceptions.ApiException:
return V1PodList(items=[])
return V1PodList(items=[])
except:
time.sleep(2)
return self.get_pods(namespace, name)
Expand Down Expand Up @@ -379,18 +397,20 @@ def wait_pods(
pod_list = self.get_pods(name=name, namespace=namespace)

for pod in pod_list.items:
logger.debug(f"{pod.metadata.name} is in phase {pod.status.phase}")
print(f"{pod.metadata.name} is in phase {pod.status.phase}")

# Don't include the cert generator pod
if "cert-generator" in pod.metadata.name:
continue

# Ignore services pod
if pod.metadata.name.endswith('-services'):
if pod.metadata.name.endswith("-services"):
continue
if pod.status.phase not in states:
time.sleep(retry_seconds)
elif pod.status.phase not in ["Terminating", "Succeeded"]:
continue

if pod.status.phase not in ["Terminating"]:
ready.add(pod.metadata.name)

if not quiet:
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/v1alpha1/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if __name__ == "__main__":
setup(
name="fluxoperator",
version="0.0.23",
version="0.0.24",
author="Vanessasaurus",
author_email="vsoch@users.noreply.github.com",
maintainer="Vanessasaurus",
Expand Down

0 comments on commit f320113

Please sign in to comment.