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 APIObject based class from a CustomResourceDefinition #457

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 can use the CustomResourceDefinition objects to create/update/delete Custom Resource Definitions. But if we want to create the Custom Resources themselves we need to call new_class() with the name of the CRD and some options to generate a class that we can use to create resources.

It would be nice if CustomResourceDefinition had a utility method to do this for us automatically.

For example if we start by making a new CRD.

from kr8s.objects import CustomResourceDefinition

# Define a custom resource definition for `shirts.stable.example.com`
shirts_crd = CustomResourceDefinition(
    {
        "metadata": {"name": "shirts.stable.example.com"},
        "spec": {
            "group": "stable.example.com",
            "scope": "Namespaced",
            "names": {"plural": "shirts", "singular": "shirt", "kind": "Shirt"},
            "versions": [
                {
                    "name": "v1",
                    "served": True,
                    "storage": True,
                    "schema": {
                        "openAPIV3Schema": {
                            "type": "object",
                            "properties": {
                                "spec": {
                                    "type": "object",
                                    "properties": {
                                        "color": {"type": "string"},
                                        "size": {"type": "string"},
                                    },
                                }
                            },
                        }
                    },
                }
            ],
        },
    }
)
# Create the CRD
shirts_crd.create()

Now we have a CRD for shirts.stable.example.com/v1 we might want to make a new Shirt resource. Currently we would need to use new_class() and pass a lot of the same information that we passed to the CRD.

from kr8s.objects import new_class

Shirt = new_class("shirts.stable.example.com/v1", namespaced=True)  # If Shirt was scalable this would be even more complex

Instead we could add a method to CustomResourceDefinition to do this for us.

# Create an object class for `shirts.stable.example.com/v1`
Shirt = shirts_crd.as_class(version="v1")  # The version could be optional and just use the first as a default

Alternatively we could update new_class() to accept a CRD.

from kr8s.objects import new_class

Shirt = new_class(shirts_crd)  # Would need to think about how to handle which schema version is used

Then we can finally define and create a Shirt.

# Define a shirt
my_shirt = Shirt(
    {
        "metadata": {"name": "myshirt"},
        "spec": {
            "color": "red",
            "size": "XL",
        },
    }
)
my_shirt.create()
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