Is there any way to get the author of a gcp cloud run job execution in python?
For example when I run gcloud run jobs executions list, I can see the author that created it (RUN BY column):
JOB EXECUTION REGION RUNNING COMPLETE CREATED RUN BY
✔ my-job my-job-lj2op us-west1 0 1 / 1 2024-11-09 12:00:00 UTC person@email.com
If I run the command in debug verbosity, I can see that the api used is GET /apis/run.googleapis.com/v1/namespaces/oxa-dev-loc-data-connector/executions?alt=json&limit=100 HTTP/1.1, however if I use the python sdk run_v2.ListExecutionsRequest call, no author field is present. Is there an alternate way to get it via the python sdk?
An alternative to --verbosity=debug is --log-http which shows the underlying REST calls, requests and responses. You can then find the documentation using Google's APIs Explorer for e.g. Cloud Run.
gcloud run jobs execution list calls run/v1 (!) namespaces.executions.list which returns Response which is a list of Execution and includes various metadata.annotations:
E.g.:
metadata:
annotations:
run.googleapis.com/client-name: gcloud
run.googleapis.com/client-version: 499.0.0
run.googleapis.com/creator: {USER}
run.googleapis.com/execution-environment: gen2
run.googleapis.com/lastModifier: {USER}
run.googleapis.com/operation-id: 12345678-1234-1234-1234-1234567890ab
I'm unsure whether 'RUN BY' maps to creator or lastModifier but...
gcloud run jobs executions list \
--project=${PROJECT} \
--region=${REGION} \
--format='yaml(metadata.annotations["run.googleapis.com/creator"])'
metadata:
annotations:
run.googleapis.com/creator: my@email.com
Or, if you'd prefer a more generic (JSON) solution:
gcloud run jobs executions list \
--project=${PROJECT} \
--region=${REGION} \
--format=json \
| jq -r '.[].metadata.annotations["run.googleapis.com/creator"]'
Curiously, annotations aren't (!) surfaced by the run/v2 which underpins the Python Cloud Client Library for Cloud Run (gcloud uses run/v1) and uses a different variant of Execution (which retains annotations but these are {}) and no other field appears to replicate the creator annotation:
requirements.txt:
google-cloud-run==0.10.10
main.py:
import os
from google.cloud.run import ExecutionsClient
from google.cloud.run_v2 import ListExecutionsRequest,Execution
from google.cloud.run_v2.services.executions.pagers import ListExecutionsPager
project = os.getenv("PROJECT")
location = os.getenv("LOCATION")
client = ExecutionsClient()
request = ListExecutionsRequest(
parent=f"projects/{project}/locations/{location}/jobs/-"
)
response=client.list_executions(
request=request
)
for execution in response:
response: ListExecutionsPager
execution: Execution
print(execution.annotations)
You could use the API Client Library for Cloud Run to force run/v1 but I'm unclear as to the deprecation of run/v1!?
requirements.txt:
google-api-python-client==2.151.0
main.py:
import os
from googleapiclient.discovery import build
project = os.getenv("PROJECT")
service = build('run', 'v1')
request = service.namespaces().executions().list(
parent=f"namespaces/{project}"
)
response = request.execute()
for execution in response["items"]:
print(execution["metadata"]["annotations"]["run.googleapis.com/creator"])