Support returning existing ApiObjects in object_factory
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 for x in possible_object_types if inspect.isclass(x) and api_version == getattr(x, 'version', None) and kind == getattr(x, '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()
Deleting a branch is permanent. It CANNOT be undone. Continue?