kr8s-org / kr8s

A batteries-included Python client library for Kubernetes that feels familiar for folks who already know how to use kubectl
https://kr8s.org
BSD 3-Clause "New" or "Revised" License
839 stars 45 forks source link

Support getting unknown types #432

Closed jacobtomlinson closed 4 months ago

jacobtomlinson commented 4 months ago

Closes #199

The goal of this PR is to make kr8s.get(...) as flexible as kubectl get ....

For example if I have Istio installed I can list Gateway objects in the following ways:

kubectl get gw
kubectl get gateway
kubectl get gateways
kubectl get gateway.networking.istio.io
kubectl get gateways.networking.istio.io
kubectl get gateway.v1.networking.istio.io
kubectl get gateways.v1.networking.istio.io

With this PR I can now do the following in kr8s.

import kr8s

kr8s.get("gw")
kr8s.get("gateway")
kr8s.get("gateways")
kr8s.get("gateway.networking.istio.io")
kr8s.get("gateways.networking.istio.io")
kr8s.get("gateway.v1.networking.istio.io")
kr8s.get("gateways.v1.networking.istio.io")

Previously I would've had to use Gateway = kr8s.objects.new_class("gateway", "networking.istio.io/v1") to register the class before I could get it, but after this PR we look this up automatically if no class can be found but the resource exists on the server.

To reinstate the old behaviour we can set the allow_unknown_type=False.

import kr8s

kr8s.get("gw", allow_unknown_type=False)
# KeyError: 'No object registered for gateway.networking.istio.io. See https://docs.kr8s.org/en/stable/object.html#extending-the-objects-api for more information on how to register a new object.'

You can of course still generate the class manually if you want to use things like the default class methods, or you want a type to work with later.

import kr8s
from kr8s.objects import new_class

Gateway = new_class("gateway", "networking.istio.io/v1")
Gateway.list()
codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 83.75000% with 13 lines in your changes missing coverage. Please review.

Project coverage is 95.00%. Comparing base (87063fc) to head (691d306). Report is 121 commits behind head on main.

Files with missing lines Patch % Lines
kr8s/_api.py 75.00% 7 Missing :warning:
kr8s/tests/test_api.py 76.00% 6 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #432 +/- ## ========================================== + Coverage 94.61% 95.00% +0.38% ========================================== Files 29 30 +1 Lines 3141 3740 +599 ========================================== + Hits 2972 3553 +581 - Misses 169 187 +18 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.