alex-kharlamov / KubeResearch

MIT License
1 stars 0 forks source link

[ls] speedup for selected namespace(filter on server side) #11

Open github-actions[bot] opened 8 months ago

github-actions[bot] commented 8 months ago

https://github.com/alex-kharlamov/KubeResearch/blob/204eb434137234444445b3d0b847e04946ec7bb9/kubr/backends/volcano.py#L266


        print(resp)

    def list_jobs(self, namespace: str = 'All', show_all: bool = False, head: int = None):
        # TODO [ls] show used resources
        # TODO [ls] speedup for selected namespace(filter on server side)
        # TODO [ls] show events for pending jobs
        jobs_stat = self.crd_client.list_cluster_custom_object(group='batch.volcano.sh',
                                                               version='v1alpha1',
                                                               plural='jobs')
        jobs = jobs_stat['items']

        extracted_jobs = defaultdict(list)
        for job in jobs:
            # TODO convert server time to local to fix humanize.naturaldelta timezone handling
            job_state = {'Name': job['metadata']['name'],
                         'Namespace': job['metadata']['namespace'],
                         'State': job['status']['state']['phase'],
                         'Age': datetime.strptime(job['status']['state']['lastTransitionTime'],
                                                                   '%Y-%m-%dT%H:%M:%SZ')
                         }
            if namespace != 'All' and job_state['Namespace'] != namespace:
                continue

            if job_state['State'] in ['Pending', 'Running', 'Failed', 'Completed']:
                extracted_jobs[job_state['State']].append(job_state)
            else:
                extracted_jobs['extra'].append(job_state)

        for state in ['Pending', 'Running', 'Completed', 'Failed', 'extra']:
            extracted_jobs[state].sort(key=lambda x: x['Age'], reverse=True)
            for job in extracted_jobs[state]:
                job['Age'] = humanize.naturaldelta(datetime.utcnow() - job['Age'])
            if head:
                extracted_jobs[state] = extracted_jobs[state][:head]

            # TODO [ls] add pretty formatting that will show only first 10 jobs in completed and failed states are shown
            if not show_all and state in ['Completed', 'Failed']:
                extracted_jobs[state] = extracted_jobs[state][:5]

            extracted_jobs[state] = tabulate(extracted_jobs[state], headers='keys', tablefmt='grid')

        # TODO pretty handling of empty list in running jobs
        result = join_tables_horizontally(extracted_jobs['Running'], extracted_jobs['Pending'])
        result += '\n\n'
        result += join_tables_horizontally(extracted_jobs['Completed'], extracted_jobs['Failed'])

        if show_all:
            result += '\n\n'
            # TODO pretty handling of empty list in all jobs
            result += extracted_jobs['extra']

        return result