Support returning existing ApiObjects in object_factory #7

Open
opened 3 months ago by ioben · 0 comments
ioben commented 3 months ago

It seems like object_factory was originally designed only to be used to generate ApiObjects for CRDs, but it's also very useful for having easy programic access to the already defined types. I had to write this helper method to allow it to properly return the correct ApiObject if one already exists, and was curious if there was interest in integrating it upstream? If I were to integrate it upstream, I imagine we could avoid the use of inspect by just registering each class manually.

import sys
import inspect

from pykube.objects import object_factory

def _object_factory(cluster_api, api_version, kind):
    possible_object_types = inspect.getmembers(sys.modules[pykube.objects.__name__])
    api_object_type = next((
        x[1] for x in possible_object_types
        if inspect.isclass(x[1])
            and api_version == getattr(x[1], 'version', None)
            and kind == getattr(x[1], 'kind', None)), None)

    if api_object_type:
        return api_object_type

    return object_factory(cluster_api, api_version, kind)

Specifically I'm using the above while I loop through a set of yaml documents to easily create resources from a set of files. Not sure if there's a better way to do it?

def with_manifest_objects(cluster_api, path, wait=True):
    with open(path, 'r') as fh:
        objects = []
        for document in yaml.safe_load_all(fh):
            api_object_type = _object_factory(cluster_api, document['apiVersion'], document['kind'])
            api_object = api_object_type(cluster_api, document)
            yield api_object
            objects.append(api_object)

        if not wait:
            return

        for obj in objects:
            if not hasattr(obj, 'ready'): continue
            while not obj.ready:
                time.sleep(1)
                obj.reload()
It seems like object_factory was originally designed only to be used to generate ApiObjects for CRDs, but it's also very useful for having easy programic access to the already defined types. I had to write this helper method to allow it to properly return the correct ApiObject if one already exists, and was curious if there was interest in integrating it upstream? If I were to integrate it upstream, I imagine we could avoid the use of inspect by just registering each class manually. ```python import sys import inspect from pykube.objects import object_factory def _object_factory(cluster_api, api_version, kind): possible_object_types = inspect.getmembers(sys.modules[pykube.objects.__name__]) api_object_type = next(( x[1] for x in possible_object_types if inspect.isclass(x[1]) and api_version == getattr(x[1], 'version', None) and kind == getattr(x[1], 'kind', None)), None) if api_object_type: return api_object_type return object_factory(cluster_api, api_version, kind) ``` Specifically I'm using the above while I loop through a set of yaml documents to easily create resources from a set of files. Not sure if there's a better way to do it? ```python def with_manifest_objects(cluster_api, path, wait=True): with open(path, 'r') as fh: objects = [] for document in yaml.safe_load_all(fh): api_object_type = _object_factory(cluster_api, document['apiVersion'], document['kind']) api_object = api_object_type(cluster_api, document) yield api_object objects.append(api_object) if not wait: return for obj in objects: if not hasattr(obj, 'ready'): continue while not obj.ready: time.sleep(1) obj.reload() ```
Sign in to join this conversation.
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.