Search code examples
pythonkubernetesopenshift

OpenShift Client Python - Extracting resource details


So I'm following the OpenShift Client Python docs and I can use a script I created to successfully list resources like pods, jobs, deployments etc.

I'm trying to understand how to extract some more detailed information from these resources such as age, completions or 'ready' status. I can't find any instructions on how to do this in the docs but there's a chance I'm missing something.

I've tried:

namespace = ['testNamespace']
for ns in namespace:
    with oc.project(ns):

        print('Jobs in {}: \n{}'.format(oc.get_project_name(), '\n'.join(oc.selector('jobs').names())))

        print('Ages of jobs in {}: \n{}'.format(oc.get_project_name(), '\n'.join(oc.selector('jobs').ages())))

But it says that Selector has no 'ages' attribute. Any advice would be much appreciated if anyone has experience with the client.


Solution

  • The quickstart shows you how to access information about objects (see e.g. how it access the metadata.ownerReferences attribute of a pod).

    Using that example, we can look at the at the creationTimestamp of the jobs like this:

    import sys
    import openshift_client as oc
    
    for ns in sys.argv[1:]:
        with oc.project(ns):
            jobs = oc.selector("jobs")
            print("Jobs in {}: \n{}".format(ns, "\n".join(oc.selector("jobs").names())))
    
            for job in jobs.objects():
                print(f"Job {job.name()} create at {job.model.metadata.creationTimestamp}")
    

    Given that kubectl shows me:

    $ kubectl -n example-namespace get job
    NAME                                         COMPLETIONS   DURATION   AGE
    daily-openshift-metrics-collector-28534320   1/1           15s        2d4h
    daily-openshift-metrics-collector-28535760   1/1           10s        28h
    daily-openshift-metrics-collector-28537200   1/1           16s        4h31m
    produce-report-28488300                      1/1           43s        34d
    produce-report-28532940                      1/1           64s        3d3h
    

    Running the above code will produce something like:

    $ python ocexample.py example-namespace
    Jobs in example-namespace: 
    daily-openshift-metrics-collector-28534320
    daily-openshift-metrics-collector-28535760
    daily-openshift-metrics-collector-28537200
    produce-report-28488300
    produce-report-28532940
    Job daily-openshift-metrics-collector-28534320 create at 2024-04-02T12:00:00Z
    Job daily-openshift-metrics-collector-28535760 create at 2024-04-03T12:00:00Z
    Job daily-openshift-metrics-collector-28537200 create at 2024-04-04T12:00:00Z
    Job produce-report-28488300 create at 2024-03-01T13:00:00Z
    Job produce-report-28532940 create at 2024-04-01T13:00:00Z
    

    It looks like you're trying to get the "age" attribute, but if you look at the job api specification, there is no such attribute. The age is something you need to calculate. Maybe like this:

    #!/usr/bin/python
    
    import sys
    import openshift_client as oc
    import datetime
    
    for ns in sys.argv[1:]:
        with oc.project(ns):
            jobs = oc.selector("jobs")
            print("Jobs in {}: \n{}".format(ns, "\n".join(oc.selector("jobs").names())))
    
            now = datetime.datetime.now(datetime.UTC)
            for job in jobs.objects():
                created = datetime.datetime.fromisoformat(
                    job.model.metadata.creationTimestamp,
                )
    
                age = now - created
                print(f"Job {job.name()} age is {age}")
    

    Running that produces:

    $ python ocexample.py example-namespace
    Jobs in example-namespace: 
    daily-openshift-metrics-collector-28534320
    daily-openshift-metrics-collector-28535760
    daily-openshift-metrics-collector-28537200
    produce-report-28488300
    produce-report-28532940
    Job daily-openshift-metrics-collector-28534320 age is 2 days, 4:33:53.857197
    Job daily-openshift-metrics-collector-28535760 age is 1 day, 4:33:53.857197
    Job daily-openshift-metrics-collector-28537200 age is 4:33:53.857197
    Job produce-report-28488300 age is 34 days, 3:33:53.857197
    Job produce-report-28532940 age is 3 days, 3:33:53.857197
    

    Which matches up with what kubectl was telling us.