Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain references to dynamically generated classes when using kr8s.objects.new_class #313

Closed
jcmcken opened this issue Feb 17, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@jcmcken
Copy link

jcmcken commented Feb 17, 2024

Which project are you requesting an enhancement for?

kr8s

What do you need?

For more background, see here

Calling kr8s.objects.new_class is necessary for Kubernetes object types (e.g. CRDs or other native objects) that kr8s doesn't natively generate classes for. However, you need to assign the class, otherwise by the time you make use of the class, it will have been garbage-collected. A small improvement would be internally cache references to the new class so it doesn't get garbage-collected

@jcmcken jcmcken added the enhancement New feature or request label Feb 17, 2024
@jacobtomlinson
Copy link
Member

This issue is potentially worked around by #432 as you no longer need to call new_class unless you specifically want to do something with the class that it returns.

The original problem this issue was discussing was around garbage collection when not assigning the result of new_class to anything.

import kr8s

kr8s.objects.new_class("mutatingwebhookconfiguration", "admissionregistration.k8s.io/v1", namespaced=False, asyncio=False)

# The class created by `new_class` may be garbage collected by the time we get here and the next line will fail
mwcs = kr8s.get("mutatingwebhookconfigurations.admissionregistration.k8s.io/v1") 

As of #432 you don't need to call new_class in this example as the class will be created under the hood automatically.

import kr8s

# Just works
mwcs = kr8s.get("mutatingwebhookconfigurations.admissionregistration.k8s.io/v1") 

In cases where you explicitly want to call new_class I think it's fair to say you will assign it to something, either because you want to call classmethods on it, or because you want to subclass it.

So I'm going to close out this issue as not-planned as it doesn't feel necessary any more.

@jacobtomlinson jacobtomlinson closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants