willthames / kubernetes-validate

Other
45 stars 5 forks source link

Add support for validating objects with a to_dict method #23

Closed jacobtomlinson closed 10 months ago

jacobtomlinson commented 10 months ago

Thanks for the useful library!

Some other libraries such as kubernetes, kubernetes-asyncio, kr8s and lightkube all have objects that can represent Kubernetes resources, and all of those objects have a to_dict() method. This PR adds support for those objects to be passed directly into kubernetes_validate.validate().

import kubernetes_validate
from kr8s.objects import Pod

pod = Pod({
        "apiVersion": "v1",
        "kind": "Pod",
        "metadata": {
            "name": "my-pod",
        },
        "spec": {
            "containers": [{"name": "pause", "image": "gcr.io/google_containers/pause",}]
        },
    })

kubernetes_validate.validate(pod, "1.28", strict=True)
willthames commented 10 months ago

Hi @jacobtomlinson - I'm definitely happy with the idea of this PR, and once the test suite passes I'll merge it.

I don't know enough mypy to be able to offer suggestions for fixes, unfortunately!

jacobtomlinson commented 10 months ago

Thanks @willthames. I think I've made mypy happy now.

jacobtomlinson commented 10 months ago

Using Any felt like cheating here so on second thoughts I made a protocol instead, I've not done that before so that was a fun learning experience. mypy is happy for me locally so hopefully CI will be happy now.

willthames commented 10 months ago

ok, now the updates are failing flake8 tests, but that should be very simple to fix up.

jacobtomlinson commented 10 months ago

Gah so sorry. Pushed another commit.

willthames commented 10 months ago

Thanks @jacobtomlinson , I'll sort a new 1.28 release shortly

willthames commented 10 months ago

1.28.1 is now released

jacobtomlinson commented 10 months ago

Awesome thanks @willthames!

jacobtomlinson commented 10 months ago

I added an example of using kubernetes-validate to the kr8s documentation too.