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

Improve flexibility when getting resources by name #198

Merged

Conversation

jacobtomlinson
Copy link
Member

@jacobtomlinson jacobtomlinson commented Nov 1, 2023

Closes #196

This PR makes many general improvements to kr8s._objects.get_class to enable looking up classes from various strings. Previously this had only been implemented inkubectl-ng so this PR pushes that down into kr8s and also adds more flexibility.

The outcome is that when using kr8s.get(resource) the resource now supports the formats KIND[.GROUP][/VERSION] or KIND[.VERSION][.GROUP] where KIND can be singular, plural, titlecase or a short name.

Examples

import kr8s

pods = kr8s.get("pods")  # Only this worked previously
pods = kr8s.get("pod")  # Singular
pods = kr8s.get("Pod")  # Title
pods = kr8s.get("po")  # Short name
pods = kr8s.get("pods/v1")  # Explicit version

For resources that have an API group such as roles.rbac.authorization.k8s.io/v1 we can also select that in various ways.

import kr8s

roles = kr8s.get("roles")  # Only this worked previously
roles = kr8s.get("role")  # Singular
roles = kr8s.get("Role")  # Title
roles = kr8s.get("roles.rbac.authorization.k8s.io")  # Full group name
roles = kr8s.get("roles.rbac.authorization.k8s.io/v1")  # Full group name with explicit version
roles = kr8s.get("roles.v1.rbac.authorization.k8s.io")  # Full group name with explicit version alternative

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Merging #198 (f939436) into main (f94a526) will increase coverage by 0.04%.
The diff coverage is 97.82%.

@@            Coverage Diff             @@
##             main     #198      +/-   ##
==========================================
+ Coverage   94.96%   95.01%   +0.04%     
==========================================
  Files          25       25              
  Lines        2405     2448      +43     
==========================================
+ Hits         2284     2326      +42     
- Misses        121      122       +1     
Files Coverage Δ
kr8s/_api.py 90.68% <100.00%> (+0.23%) ⬆️
kr8s/tests/test_api.py 100.00% <100.00%> (ø)
kr8s/tests/test_objects.py 98.78% <100.00%> (+0.01%) ⬆️
kr8s/_objects.py 92.12% <95.45%> (+0.08%) ⬆️

@jacobtomlinson jacobtomlinson merged commit fb313e9 into kr8s-org:main Nov 1, 2023
12 checks passed
@jacobtomlinson jacobtomlinson deleted the kind-group-version-lookup branch November 1, 2023 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support operating on specific API versions
1 participant