<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hampus Carlsson on Medium]]></title>
        <description><![CDATA[Stories by Hampus Carlsson on Medium]]></description>
        <link>https://medium.com/@hampusc?source=rss-78c3260bef2c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ObQSVVzWluuXj4E6.jpg</url>
            <title>Stories by Hampus Carlsson on Medium</title>
            <link>https://medium.com/@hampusc?source=rss-78c3260bef2c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 03:32:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hampusc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How a gift turned into an open source contribution]]></title>
            <link>https://medium.com/@hampusc/how-a-gift-turned-into-an-open-source-contribution-94699923204a?source=rss-78c3260bef2c------2</link>
            <guid isPermaLink="false">https://medium.com/p/94699923204a</guid>
            <category><![CDATA[gifts]]></category>
            <category><![CDATA[bird-net]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[birds]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Hampus Carlsson]]></dc:creator>
            <pubDate>Fri, 16 Aug 2024 07:24:39 GMT</pubDate>
            <atom:updated>2024-08-16T07:24:39.174Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ir-xO8O8LIkmrdoLFQ6AqA.jpeg" /><figcaption>My grandparents with BirdNet-Go on their iPad</figcaption></figure><p>This story began during the holiday season when I was brainstorming gift ideas for my family. My grandmother, who has always enjoyed feeding and watching birds from her window, is especially challenging to shop for. A few years back, I read about BirdNET, a neural network trained to identify birds by their songs and calls. The idea of using BirdNET to enhance her birdwatching immediately caught my attention. I thought this would be the perfect gift — not for Christmas, but as a springtime birthday present.</p><p>I had an unused Raspberry Pi 2 Zero W lying around from a previous project, so I decided to repurpose it. My initial plan was to install <a href="https://github.com/mcguirepr89/BirdNET-Pi">BirdNET-P</a>i, but I quickly ran into several issues. The installation process was cumbersome due to the lack of a Docker image, and the Raspberry Pi 2 Zero W struggled with the processing and SD card write demands. Additionally, the project appeared to be abandoned, so I decided to explore other options.</p><p>While searching for alternatives, I came across a project called <a href="https://github.com/tphakala/birdnet-go">BirdNET-Go</a>, which at the time was a CLI-only tool. I followed its development closely, and when it released a “realtime” mode with a web interface in February, I decided to give it a try. It was a great improvement — far less resource-intensive and with a much more modern feel compared to BirdNET-Pi.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xGhfrwkMYRT7RdQmNfNy-w.png" /><figcaption>Daily summary view in BirdNET-Go, showing a table of the daily sightings</figcaption></figure><p>Unfortunately, at the time, only ARM binaries were available, making it challenging to experiment with on my personal computer. Although the compilation process was manageable with the provided instructions, I saw an opportunity to simplify it for others by contributing a Dockerfile to the project. I quickly followed up this initial contribution with additional improvements, such as CI workflows for automatically building and releasing the Docker image, improved documentation, Dev Container configurations, and various bug fixes. The Docker image has since become the preferred deployment method, and the project is steadily gaining in popularity.</p><p>In May, I finally set up the Raspberry Pi at my grandparents’ house. Unfortunately, my timing was a bit off, as they had just taken down their bird feeding station for storage until fall. I mounted the microphone in a window near the forest instead, and it was still able to detect birds without any issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*twAP7YIwBBDEm47qCyelXg.jpeg" /><figcaption>Mic mounted through window</figcaption></figure><p>The result was an instant success! My grandmother loved it, and even my grandfather, who tends to be skeptical about new technology, became an avid birdwatcher, frequently checking their iPad for bird sightings. My aunt, who is really into birdwatching, has funnily enough become quite envious of their setup and now wants one of her own.</p><p>One aspect I felt was missing from the experience was the ability to see images of the detected birds. To address this, I made another contribution to the project, adding code to fetch bird images from Wikipedia and display them on the dashboard. After a thorough review process involving licensing and attribution considerations, the feature was finally merged into the project in June. I then remotely updated the installation at my grandparents’ home to include this new functionality. It didn’t take long — within an hour, my grandfather called, thrilled about the bird images now appearing on the dashboard.</p><p>In summary, what started as a simple gift turned into an unexpected journey of contributing to an open-source project focused on birds — a topic that isn’t even one of my interests. What made it all the more rewarding was seeing the happiness it brought to my grandparents and hopefully others too. I hope to continue contributing to this project in the near future. One of the things I’m currently working on is <a href="https://github.com/isZumpo/birdnet-go-charts">turning my personal Kubernetes BirdNET-Go setup into a Helm chart</a>, making it easier for others to set up their own instances. More on this soon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*suVil82co3R5vWwCa2Flag.png" /><figcaption>Most recent view in BirdNET-Go allowing for playback of detected birds</figcaption></figure><p>BirdNet-Go: <a href="https://github.com/tphakala/birdnet-go">https://github.com/tphakala/birdnet-go</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94699923204a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to attach USB devices to Kubernetes pods using Akri]]></title>
            <link>https://medium.com/@hampusc/how-to-attach-usb-devices-to-kubernetes-pods-using-akri-19fb70d41f1e?source=rss-78c3260bef2c------2</link>
            <guid isPermaLink="false">https://medium.com/p/19fb70d41f1e</guid>
            <category><![CDATA[devices]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[usb]]></category>
            <category><![CDATA[akri]]></category>
            <dc:creator><![CDATA[Hampus Carlsson]]></dc:creator>
            <pubDate>Tue, 18 Jun 2024 16:22:25 GMT</pubDate>
            <atom:updated>2024-08-14T14:06:05.643Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ah2LcU1mOCjB1UVzt_aE9w.jpeg" /></figure><p>A while back, I decided to migrate my 3D printer’s <a href="https://github.com/OctoPrint/OctoPrint">OctoPrint</a> server — a web interface for controlling 3D printers — from running directly in Docker to inside Kubernetes. Previously, it was set up in Docker by mounting the required USB device with the --device /dev/ttyUSB* argument. This setup worked well, but I wanted to leverage Kubernetes&#39; orchestration capabilities for better resource management and scalability</p><p>At first glance, the Kubernetes documentation provided little guidance on translating Docker’s --device arguments into a Kubernetes pod specification. However, after some research, I found a potential “hacky” solution. Let’s take a quick look at this initial approach and its quirks before moving on to Akri, which I believe is the best method yet to manage USB devices in Kubernetes.</p><h3>Hostpath volume mounts of devices</h3><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: example<br>spec:<br>  containers:<br>  - name: example-container<br>    securityContext:<br>      privileged: true<br>    volumeMounts:<br>    - name: hostpath-volume<br>      mountPath: /dev/ttyUSB1<br>  volumes:<br>  - name: hostpath-volume<br>    hostPath:<br>      path: /dev/ttyUSB1</pre><p>One somewhat direct translation of the --device argument in Docker is to use hostPath volumes in the pod specification. Typically, hostPath mounts are used to access the file system on the node where the pod is running, but it can also be employed to access devices. For instance, executing ls /dev | grep ttyUSB inside the pod would list the connected USB devices. However, simply making the device visible is not enough; it will not be functional unless the pod is also running as privileged.</p><p>Running a pod in privileged mode gives it elevated permissions, allowing it to access host devices and other sensitive system areas. This increases the risk of security vulnerabilities, as privileged pods can potentially interfere with the host system. Additionally, this approach is inflexible because the /dev/ttyUSB* path may change at any time, requiring manual updates to the configuration.</p><h3>Enter the world of Akri</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*rY-_ecQGvykVW5npgRB3WQ.png" /></figure><p>To overcome these security and flexibility issues, I turned to Akri, an open-source project that that simplifies the management of edge devices in Kubernetes. Akri allows USB devices to be discovered and exposed to the Kubernetes API, enabling the scheduling of pods that require these resources in a more secure and flexible manner.</p><p>Akri finds USB devices by utilizing udev queries, meaning that it queries each node for USB devices using the <a href="https://en.wikipedia.org/wiki/Udev">udev</a> subsystem. This method allows Akri to quickly detect changes in hardware configuration. For example, if a USB device is unplugged from one node and plugged into another, Kubernetes will be promptly informed, enabling it to reschedule pods as needed.</p><h3>Installation</h3><p>Now that Akri has been introduced, let’s dive into the installation process of it. The most straightforward approach is to use the provided <a href="https://github.com/project-akri/akri/blob/main/deployment/helm/Chart.yaml">Helm chart</a>. I personally use <a href="https://argo-cd.readthedocs.io/en/stable/">Argo CD</a> to manage installations, which allows me to keep configurations in a Git repository. Trust me, having everything in a Git repository is invaluable — when something eventually goes wrong, you can easily spin up a new cluster and start over, much like quick saves in a game!</p><p>Here’s how you can set up Akri with Argo CD. Create the following two files for the Argo CD application utilizing Akri as a subchart:</p><pre>.<br>├── Chart.yaml<br>└── values.yaml</pre><p><strong>Chart.yaml</strong></p><pre>apiVersion: v2<br>name: akri-example<br>version: 0.1.0<br>dependencies:<br>- name: akri<br>  version: 0.12.20<br>  repository: https://project-akri.github.io/akri/</pre><p><strong>Values.yaml</strong></p><pre>akri:<br>  kubernetesDistro: k3s # Specify Kubernetes distro here: k3s|microk8s|k8s<br>  udev:<br>    discovery:<br>      enabled: true</pre><p>The <em>udev discovery enabled</em> part is crucial because it deploys a DaemonSet on every Kubernetes node. A DaemonSet ensures that a copy of a pod runs on all or some nodes, which in this case, allows each node to look for attached USB devices and expose them to the Kubernetes scheduler.</p><p>That is it! Now Akri should be installed and ready for use.</p><h3>Determine udev rule for USB device detection</h3><p>Akri uses <a href="https://en.wikipedia.org/wiki/Udev">udev</a> to query for devices. Therefore, one must specify the exact udev rules for detecting desired devices. This process can be a bit tricky, but is manageable. Let’s go through an example; follow along on your terminal.</p><blockquote><em>lsusb</em></blockquote><pre>hampus@laptopserver:~$ lsusb<br>Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub<br>Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub<br>Bus 001 Device 003: ID 0bda:58c6 Realtek Semiconductor Corp. <br>Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub<br>Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub<br>Bus 004 Device 002: ID 174c:55aa ASMedia Technology Inc. Name: ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge<br>Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub<br>Bus 003 Device 032: ID 054c:0155 Sony Corp. Eyetoy Video Device<br>Bus 003 Device 031: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter<br>Bus 003 Device 030: ID 1a40:0101 Terminus Technology Inc. Hub<br>Bus 003 Device 029: ID 1a40:0201 Terminus Technology Inc. FE 2.1 7-port Hub<br>Bus 003 Device 028: ID 10c4:ea60 Silicon Labs CP210x UART Bridge<br>Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub</pre><p>The <strong>lsusb</strong> command lists all USB devices connected to my computer. By comparing the output before and after plugging in the 3D printer, I identified the device as:</p><p><strong>Bus 003 Device 031: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter</strong></p><p>Next, we need to find the specific fields to base our udev query on using the udevadm info command. This command provides detailed information about the device, which helps in creating precise udev rules. First, take note of the bus and device ID from the lsusb output.</p><p>Bus <strong>003</strong> Device <strong>031</strong>: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter</p><p>Combine them into /dev/bus/usb/&lt;bus&gt;/&lt;device&gt;, which in this example is: /dev/bus/usb/003/031.</p><pre>udevadm info --attribute-walk --path=$(udevadm info --query=path /dev/bus/usb/003/031)</pre><pre>hampus@laptopserver:~$ udevadm info --attribute-walk --path=$(udevadm info --query=path /dev/bus/usb/003/031)Udevadm info starts with the device specified by the devpath and then<br>walks up the chain of parent devices. It prints for every device<br>found, all possible attributes in the udev rules key format.<br>A rule to match, can be composed by the attributes of the device<br>and the attributes from one single parent device.<br><br>looking at device &#39;/devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.4/3-3.4.1&#39;:<br>    KERNEL==&quot;3-3.4.1&quot;<br>    SUBSYSTEM==&quot;usb&quot;<br>    DRIVER==&quot;usb&quot;<br>    ATTR{tx_lanes}==&quot;1&quot;<br>    ATTR{bDeviceClass}==&quot;ff&quot;<br>    ATTR{bmAttributes}==&quot;80&quot;<br>    ATTR{bMaxPacketSize0}==&quot;8&quot;<br>    ATTR{configuration}==&quot;&quot;<br>    ATTR{authorized}==&quot;1&quot;<br>    ATTR{rx_lanes}==&quot;1&quot;<br>    ATTR{bDeviceProtocol}==&quot;00&quot;<br>    ATTR{quirks}==&quot;0x0&quot;<br>    ATTR{speed}==&quot;12&quot;<br>    ATTR{avoid_reset_quirk}==&quot;0&quot;<br>    ATTR{maxchild}==&quot;0&quot;<br>    ATTR{urbnum}==&quot;454&quot;<br>    ATTR{product}==&quot;USB2.0-Serial&quot;<br>    ATTR{devpath}==&quot;3.4.1&quot;<br>    ATTR{idProduct}==&quot;7523&quot;<br>    ATTR{bMaxPower}==&quot;96mA&quot;<br>    ATTR{version}==&quot; 1.10&quot;<br>    ATTR{bcdDevice}==&quot;0254&quot;<br>    ATTR{ltm_capable}==&quot;no&quot;<br>    ATTR{removable}==&quot;unknown&quot;<br>    ATTR{busnum}==&quot;3&quot;<br>    ATTR{bDeviceSubClass}==&quot;00&quot;<br>    ATTR{bNumConfigurations}==&quot;1&quot;<br>    ATTR{bConfigurationValue}==&quot;1&quot;<br>    ATTR{bNumInterfaces}==&quot; 1&quot;<br>    ATTR{devnum}==&quot;31&quot;<br>    ATTR{idVendor}==&quot;1a86&quot;<br><br>... lots of more text for parent devices, ignore these and scroll to the top</pre><p>You can select many attributes depending on how precise you want your query to be. I often choose ATTR{idVendor} and ATTR{idProduct} since these are usually sufficient.</p><p><strong>Important note:</strong> Transforming ATTR into ATTRS can be convenient as it broadens the selection, ensuring all required devices are detected. Meaning that the above would be turned into ATTRS{idVendor} and ATTRS{idProduct}</p><h3>Add udev rules to Akri configuration</h3><p>Now, let’s add our first Akri Configuration using the udev rules we determined. This configuration file specifies the discovery rules for Akri to identify and manage the USB devices. If following along using helm/ArgoCD, this is the folder structure I have been using:</p><pre>.<br>├── Chart.yaml<br>├── templates<br>│   └── 3dprinter.yaml<br>└── values.yaml</pre><p>Merge the found udev properties into a comma seperated list: ATTRS{idVendor}==&quot;1a86&quot;, ATTRS{idProduct}==&quot;7523&quot; and put it into the Configuration object below. Feel free to change the name of the object to your liking.</p><p><strong>3dprinter.yaml</strong></p><pre>apiVersion: akri.sh/v0<br>kind: Configuration<br>metadata:<br>  name: akri-3dprinter<br>spec:<br>  capacity: 1<br>  discoveryHandler:<br>    discoveryDetails: |<br>      groupRecursive: true # Recommended unless using very exact udev rules<br>      udevRules:<br>      - ATTRS{idVendor}==&quot;1a86&quot;, ATTRS{idProduct}==&quot;7523&quot;<br>    name: udev</pre><p>After applying this change to your Kubernetes cluster, one new Kubernetes resource of the type Instance should be automatically created if everything is properly configured.</p><pre>apiVersion: akri.sh/v0<br>kind: Instance<br>metadata:<br>  name: akri-3dprinter-583e0a<br>spec:<br>  brokerProperties:<br>    UDEV_DEVNODE_0: /dev/bus/usb/003/009<br>    UDEV_DEVNODE_1: /dev/ttyUSB1<br>    UDEV_DEVPATH: /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.4/3-3.4.1<br>  configurationName: akri-3dprinter<br>  deviceUsage:<br>    akri-3dprinter-583e0a-0: C:0:laptopserver<br>  nodes:<br>  - laptopserver<br>  shared: false</pre><p>By looking at the brokerProperties fields, notice how Akri detects a few different paths/devices that are connected to the udev query. All of these will be visible inside the pods using this resource. How cool is that?</p><h3>Using Akri USB device inside Kubernetes pod</h3><p>Alright, now the usb device is being detected by Akri. Now let’s use it inside a pod. Akri resources are exposed to pods through the <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/device-plugins/">device plugin framework,</a> meaning that they can be access using the resources field of the pod specification. Take the name of your Configuration object and put it into the limits and request fields such as this:</p><pre>resources:<br>    limits:<br>      akri.sh/akri-3dprinter: &quot;1&quot;<br>    requests:<br>      akri.sh/akri-3dprinter: &quot;1&quot;</pre><p><strong>Full pod example</strong></p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: example<br>spec:<br>  containers:<br>  - name: example-container<br>    resources:<br>      limits:<br>        akri.sh/akri-3dprinter: &quot;1&quot;<br>      requests:<br>        akri.sh/akri-3dprinter: &quot;1&quot;</pre><p>If using a deployment, then I do recommend using the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#recreate-deployment">recreate stragegy</a> since only one pod at a time can make use of a single akri device.</p><h3>Conclusion</h3><p>In conclusion, migrating OctoPrint to run inside Kubernetes using Akri for USB device management offers significant advantages in terms of security, flexibility, and maintainability. Akri’s dynamic discovery and management of USB devices align well with Kubernetes’ resource management principles, making it a superior solution compared to using privileged pods with hostPath volumes.</p><p>Since starting to use Akri, I have used it for many more things than Octoprint. For example, I am using it for zigbee, cameras, and even microphones. Leave a comment and let me know what you will be using Akri for.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19fb70d41f1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>