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

Support getting unknown types #432

Merged
merged 4 commits into from
Jul 3, 2024

Conversation

jacobtomlinson
Copy link
Member

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()

Copy link

codecov bot commented Jul 3, 2024

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 ⚠️
kr8s/tests/test_api.py 76.00% 6 Missing ⚠️
Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request kr8s tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create new classes automatically in kr8s.get
1 participant