from Hacker News

Show HN: Kr8s – a Python client library for Kubernetes

by jacobtomlinson on 9/13/23, 7:38 PM with 58 comments

Hey folks. I'm the author of kr8s.

I’ve been working on kr8s for a while now and one of my core goals is to build a Python library for Kubernetes that is the most simple, readable and produces the most maintainable code. It should enable folks to write clean code when working with the Kubernetes API.

If you're interested in how it compares with other libraries then check out [this post](https://jacobtomlinson.dev/posts/2023/comparison-of-kr8s-vs-...).

Happy to answer any questions you might have in the comments here .

  • by multani on 9/13/23, 9:48 PM

    This looks really cool!

    I wrote a few applications using both the official Kubernetes client and the asyncio variant from Tomasz. Both have the same problems to me (that you exposed on the comparison page):

    The documentation is really terrible (it's even non-existent for kubernetes-asyncio). For the official client, it's hidden deep inside the repository, across hundreds of files (592 at the moment); half the link cross doc don't work, there are rendering issues, etc. It's really a gigantic mess.

    The API is extremely verbose, directly the underlying Kubernetes API exposed to Python. It gets the job done and it's better than shell script, but the verbosity + the total lack of a decent documentation (cf. above) makes it hard to use and see what you can do with it. Most of the time, you have to fire the REPL and try a bunch of calls until it makes sense. I like that most of the responses are proper objects, but they display as dict in the REPL and of course, the responses format are not documented in the doc :)

    Last but not least: they both lack support for fancy authentication mechanism. We go to Kubernetes through a fancy proxy that require specific TLS settings + an external exec plugin to get the credentials. The official Kubernetes client should have the right support (for what we need at least) in the next major version but the asyncio variant not yet (I made a PR to fix this, but this requires a new version of aiohttp which is not released yet...) Both clients are very similar for the autogenerated code, but also subtly different for all the things around so you can't really expect 100% that what works on one will work on the other.

    I'm not sure how kr8s works with this fancy auth system, but at least for the first 2 points, it seems like a huge win, I'll give it a try!

  • by theptip on 9/13/23, 9:11 PM

    In the past I used PyKube (https://pykube.readthedocs.io/en/latest/readme.html), which did the job. One issue that pykube has is that there is no autocomplete on API object fields. Does kr8s wire this up? Seems you could use a TypedDict on newer versions of Python.

    Back in the old days you wouldn't get autocomplete on k8s YAML files either, but now the schema is well-known and so your IDE will complete all the fields (even CRDs). These days you get this with Pulumi, if I'm not mistaken.

  • by Nullabillity on 9/13/23, 9:52 PM

    Requesting objects without a specific version/apigroup is pretty dangerous, you're effectively opting out of K8s' primary backwards compatibility measure.
  • by snug on 9/13/23, 11:00 PM

    Looks great, but I'm bashing my head trying to figure out what the r means in kr8s?
  • by nyellin on 9/14/23, 7:01 AM

    To put one more option out there, we use Hikaru (https://pypi.org/project/hikaru/) in Robusta.dev (https://github.com/robusta-dev/robusta) and have been pretty happy with it. Example code below:

      with Pod().read(name='thename', namespace='the-namespace') as p:
          p.labels['new-label'] = 'value'
  • by cpressland on 9/13/23, 9:58 PM

    I have literally just spent the last few weeks writing pydantic models for Kubernetes for an internal testing tool. I’ll see how this compares, it looks outstanding.
  • by uneekname on 9/13/23, 10:00 PM

    This is awesome. Jacob, your work on Dask Kubernetes has been immensely helpful to me. The world of distributed Python grows stronger!
  • by willthames on 9/13/23, 10:49 PM

    The comparison doesn’t include the (perhaps confusingly named) openshift library. All the ansible kubernetes modules rely heavily on it because its support for dynamic client (where you just want to apply a manifest and don’t know in advance that it’s a Deployment and a Service and a ConfigMap) is first rate.
  • by techdragon on 9/17/23, 2:04 AM

    I used this in my KiwiPyCon conference talk yesterday! I used it with Kopf to write a basic little kubernetes operator in Python.

    Wish I had seen this thread before my talk, but the YouTube video will be online soon and I can link to it when they are uploaded.

  • by kungfufrog on 9/13/23, 8:23 PM

    This looks great, will be giving it a go today to replace some increasingly complex bash scripts!
  • by aknfxb3m on 9/13/23, 10:27 PM

    I stopped at the 3rd example. Kr8s and lightkube are the only solutions I'd dare to use.
  • by bovermyer on 9/14/23, 10:42 AM

    This is delightful. I'll be taking it for a spin today.
  • by dbragdon on 9/13/23, 8:28 PM

    Awesome library, but I can't view the documentation at work since the doc site uses http. Please consider using https.
  • by ledauphin on 9/13/23, 10:55 PM

    does this provide sane default behavior for refreshing expired auth tokens and retrying random client timeouts? the "official" SDK has driven me absolutely crazy trying to wrap all the different pieces with retries so that basic long-running operations won't randomly raise spurious exceptions...
  • by bjt12345 on 9/13/23, 10:15 PM

    How do you connect remotely? The examples don't easily show this.
  • by bdcravens on 9/14/23, 12:52 AM

    Seems to be a verbal namespace collision with Rust crates (assuming it's pronounced the same)
  • by codetrotter on 9/13/23, 11:25 PM

    How do you pronounce it?

    Krewbernetes?

    Krates?

  • by gustavus on 9/13/23, 9:44 PM

    The cycle of tooling

    Step 1: Look I've made this new great tool that uses a declarative language, you simply declare what you want your environment to look like and boom it will build it out. No more having to figure out a complicated logic flows that can fail or having code that dies on weird error cases, and you don't have to be a developer to use it.

    Step 2: The tool isn't powerful or flexible enough and the declarative code is getting to cumbersome so we've introduced some basic templating and logic flow to this tool to make it more flexible and reduce bloat.

    Step 3: The control flow isn't powerful enough to really meet our needs so we have moved it entirely to a full programming language.

    Step 4: Go back to step 1.