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
800 stars 43 forks source link

get calls are causing many requests #251

Closed benedikt-bartscher closed 9 months ago

benedikt-bartscher commented 9 months ago

Which project are you reporting a bug for?

kr8s

What happened?

Every get call with kr8s seems to fetch all crd endpoints once. Even if i use the Secret object directly. Is this intended behaviour?

>>> import logging
>>> from kr8s.objects import Secret
>>> logging.basicConfig(level=logging.INFO)
>>> Secret.get("regcred")
INFO:httpx:HTTP Request: GET https://8.8.8.8/api/ "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/api/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/ "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/acid.zalan.do/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/acme.cert-manager.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/admissionregistration.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/agent.k8s.elastic.co/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/apiextensions.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/apiregistration.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/apm.k8s.elastic.co/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/apps/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/authentication.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/authorization.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/autoscaling/v2 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/autoscaling.k8s.elastic.co/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/batch/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/beat.k8s.elastic.co/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/cert-manager.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/certificates.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/cilium.io/v2 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/clusterctl.cluster.x-k8s.io/v1alpha3 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/coordination.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/discovery.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/elasticsearch.k8s.elastic.co/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/enterprisesearch.k8s.elastic.co/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/events.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/external-secrets.io/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/flowcontrol.apiserver.k8s.io/v1beta3 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/flux.kluctl.io/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/generators.external-secrets.io/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/gitops.kluctl.io/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/grafana.integreatly.org/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/helm.toolkit.fluxcd.io/v2beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/kibana.k8s.elastic.co/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/kustomize.toolkit.fluxcd.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/logstash.k8s.elastic.co/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/maps.k8s.elastic.co/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/mariadb.mmontes.io/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/metrics.k8s.io/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/minio.min.io/v2 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/monitoring.coreos.com/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/networking.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/node.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/notification.toolkit.fluxcd.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/policy/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/rbac.authorization.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/redhatcop.redhat.io/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/scheduling.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/secrets.hashicorp.com/v1beta1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/source.toolkit.fluxcd.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/stackconfigpolicy.k8s.elastic.co/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/storage.k8s.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/sts.min.io/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/vault.banzaicloud.com/v1alpha1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/apis/velero.io/v1 "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://8.8.8.8/api/v1/namespaces/pgadmin/secrets "HTTP/1.1 200 OK"
<Secret regcred>

Anything else?

No response

jacobtomlinson commented 9 months ago

This many calls is definitely not intended. We look up all of the API resources so that we can expand shortnames in calls like kr8s.get("po"). I think there are two separate things here we might want to fix:

jacobtomlinson commented 9 months ago

I've just merged #252 which fixes this specific problem.

>>> import logging
>>> from kr8s.objects import Pod
>>> logging.basicConfig(level=logging.INFO)
>>> Pod.get("test-pod")
INFO:httpx:HTTP Request: GET https://127.0.0.1:43613/api/v1/namespaces/foo/pods "HTTP/1.1 200 OK"
<Pod test-pod>

I've also raised #253 to track caching these requests better in situations where we do actually want to call them.