K8s plugins: Kubectl your Tesla

Savithru Lokanath
3 min readApr 1, 2020
Fig1. Kubectl plugins

Plugins are a native way of extending the kubectlclient functionality in Kubernetes. kubectl CLI client written in Golang to interact with the Kubernetes control plane. Its simple to use, modular & extensible.

This means that a Kubernetes developer can build custom plugins to add new features to thekubectl client's functionality. Plugins extend kubectl with new sub-commands, allowing for new and custom features not included in the main distribution of kubectl.

Since the functionality of the client can be extended to pretty much anything that exposes a programmatic interface (APIs), I thought why not write a plugin to control a Tesla Model 3 while also learning how easy the process is! Fun right…?

Prerequisites, let’s gather them first.

  • We need to choose a scripting language to write the plugin. You can write a plugin in any language that allows you to write command-line utilities. There’s a multitude of options; Bash, Python, Go, etc.
  • Next, do we know if Tesla exposes an API to control its vehicles? I know that Tesla has a robust set of vehicle APIs that are used by applications such as the phone app to talk to the vehicles. Reverse engineering this interaction should give us the actual REST API calls, but we’ll save that for some another day. Instead, we will use an already published API doc by a Tesla fanboy.

I’ll pick Python as my language choice. It's simple & has a robust set of libraries to play with.

There’s no installation or pre-loading of the plugin that is required. Plugin executables receive the inherited environment from the kubectl binary.

A plugin determines which command path it wishes to implement based on its name. For example, a plugin wanting to provide a new command kubectl foo, would simply be named kubectl-foo, and live somewhere in your PATH.

An example (Bash implementation), as listed on the Kubernetes website is shown below.

### FILE (savvy)$ sudo cat <<EOF>> /usr/local/bin/kubectl-foo
#!/bin/bash

if [[ "$1" == "version" ]]
then
echo "1.0.0"
exit 0
fi

echo "I am a plugin named kubectl-foo"
EOF
### TEST(savvy)$ chmod +x /usr/local/bin/kubectl-foo(savvy)$ kubectl foo
I am a plugin named kubectl-foo
(savvy)$ kubectl foo version
1.0.0

Easy right? Time to start coding…

We’ll call the plugin Tesla (You can call it whatever you want). Following the plugin naming convention, we’ll name the plugin file kubectl-tesla. I’m also going to use the requestslibrary to make REST calls & click, a Python package to build CLI interfaces. It’s very similar to argparse, but takes away some of the complexity.

Following the same approach as above,

(savvy)$ pip install click requests(savvy)$ sudo cat <<EOF>> /usr/local/bin/kubectl-mycar#!/usr/bin/env pythonimport os, requests, clickurl="{0}/api/1/vehicles/{1}/command/".format(os.getenv("MYCARURL"), os.getenv("MYCARID"))
headers={"Authorization": "Bearer {0}".format(os.getenv("MYCARTOKEN"))}
@click.command()
@click.option("--honk", help="Honks the horn twice.", is_flag=True)
def execute(**kwargs):
""" KUBECTL PLUGIN TO INTERACT WITH TESLA """
for key, value in kwargs.items():
if key == "honk":
honk(value)
def honk(flag):
""" FUNCTION TO POST HONK REQUEST TO TESLA API SERVER """
if flag:
response = requests.post(url + "/honk_horn",headers=headers)
if response.status_code == 200:
click.echo("Honking twice...")
if __name__ == "__main__":
execute()
EOF

Let’s export the secrets (carID & clientToken) & make the script an executable. Then we can run,kubectl tesla --help to display the help screen.

(savvy)$ export MYCARURL=https://owner-api.teslamotors.com
(savvy)$
export MYCARID=<YOUR-CAR-ID>
(savvy)$
export MYCARTOKEN=<API-TOKEN>
(savvy)$ chmod +x /usr/local/bin/kubectl-tesla(savvy)$ kubectl tesla --help
Usage: kubectl tesla [OPTIONS]
KUBECTL PLUGIN TO INTERACT WITH TESLAOptions:
--honk Honks the horn twice.
--help Show this message and exit.

And finally, let’s honk…

(savvy)$ kubectl tesla --honk
Honking twice...

I extended the script to control some more car features & below is a fun video demoing the same.

(savvy)$ kubectl tesla --help
Usage: kubectl tesla [OPTIONS]
Options:
--honk Honks the horn twice.
--flash Flash the headlights once.
--sentry [on|off] Activate/Deactivate sentry mode.
--doors [lock|unlock] Lock/Unlock doors.
--climate [on|off] Activate/Deactivate climate control.
--warmers [on|off] Activate/Deactivate seat warmers.
--help Show this message and exit.
Fig2. Video demonstrating kubectl tesla plugin

That’s about it!!! With this feature built into the Kubernetes client, I’m pretty sure it opens up a lot of creative minds & we get to see some exciting plugins getting built.

Stay safe.

— Savi

--

--