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

Create an instance of CustomResourceDefinition from a dataclass #456

Open
Tracked by #315
jacobtomlinson opened this issue Jul 31, 2024 · 0 comments
Open
Tracked by #315
Labels
enhancement New feature or request kr8s

Comments

@jacobtomlinson
Copy link
Member

jacobtomlinson commented Jul 31, 2024

We could add a classmethod to the CustomResourceDefinition object classes which takes a dataclass and returns an instance of the CRD. This classmethod could follow a similar interface to new_class to set things like the name, version and the group.

Here's an example of what this could look like using the kr8s sync API.

from dataclasses import dataclass, field
from apischema import schema
from kr8s.objects import CustomResourceDefinition


@dataclass
class MyCustomResourceSpecV1:
    foo: str
    bar: str
    baz: list[str] = field(
        default_factory=list,
        metadata=schema(
            description="List of baz",
            unique=False,
        ),
    )


my_crd = CustomResourceDefinition.from_dataclass(
    "MyCustomResource.example.com", namespaced=True, v1=MyCustomResourceSpecV1
)
my_crd.create()

The from_dataclass classmethod would need to behave similarly to kubecrd.KubeResourceBase.crd_schema_dict().

The CustomResourceDefinition contains some metadata about the resource such as the name, group, etc and whether it is versioned. It then contains a list of specification versions. The implementation in kubecrd assumes only one version but it would be nice to support multiple. These could be passed as **kwargs as v1 is in the example above, or as a versions= kwarg with a list of dicts to allow configuration of which is served and stored.

This could even be CustomResourceDefinitions implementation of #142 and use the name CustomResourceDefinition.gen(...) instead of CustomResourceDefinition.from_dataclass(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request kr8s
Projects
None yet
Development

No branches or pull requests

1 participant