<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>analogous-dev</id>
    <title>Analogous Dev Blog</title>
    <updated>2025-02-22T13:09:51.108Z</updated>
    <generator>https://github.com/nuxt-community/feed-module</generator>
    <link rel="alternate" href="https://www.analogous.dev"/>
    <subtitle>The analogous developer. Technology topics for newcomers and experts alike.</subtitle>
    <logo>analogous-dev.png</logo>
    <icon>favicon.ico</icon>
    <rights>Cole Arendt 2022</rights>
    <entry>
        <title type="html"><![CDATA[Using the Kubernetes Python Client with AWS]]></title>
        <id>using-the-kubernetes-python-client-with-aws</id>
        <link href="/blog/using-the-kubernetes-python-client-with-aws"/>
        <updated>2021-01-27T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[
As someone who normally just uses `kubectl` and `helm` to talk to my Kubernetes
clusters, the idea of scripting modifications to my Kubernetes cluster was
exciting!! I cracked open the
`kubernetes-python` client and
started playing. TL;DR; We use the `boto3`, `eks-token`, and `kubernetes` python packages to
talk to an EKS  ...]]></summary>
        <content type="html"><![CDATA[
As someone who normally just uses `kubectl` and `helm` to talk to my Kubernetes
clusters, the idea of scripting modifications to my Kubernetes cluster was
exciting!! I cracked open the
[`kubernetes-python`](https://github.com/kubernetes-client/python) client and
started playing.

## TL;DR;

We use the `boto3`, `eks-token`, and `kubernetes` python packages to
talk to an EKS cluster without depending on `kubeconfig`.

## Why

For those of us interactively building and maintaining kubernetes resources, 
`helm` or `kubectl` become our bread and butter. They provide very nice CLI
interfaces and have all the bells and whistles one could ask for!

Moreover, when it comes to AWS (Amazon Web Services) and their EKS (Elastic
Kubernetes Service) clusters, they handle authentication smoothly and easily via
a handy CLI command and the `kubeconfig` file (usually stored at `~/.kube/config`:

```
aws eks update-kubeconfig --alias mycluster
```

(Tip: Don't forget the `--alias` flag!)

My assume-role settings and environment variables integrate nicely from my shell: everything is dandy!

However, in this case, I want to build an _app_ / software program that _itself_
accesses, modifies, and maintains resources on my Kubernetes cluster. Of course,
one can script a solution around these CLIs, but that is not very portable (requires
the CLIs installed) and is prone to failure because `kubeconfig` is user-defined, 
user-maintained, and hinges entirely on `contexts` which are arbitrary text strings.

So I headed for the `kubernetes-python` API client which simplifies making API requests
against a Kubernetes cluster. With any luck, I will finish the day with a Python program
that creates my Kubernetes resources.

**NOTE**: Before we get started, it is worth noting that I am presuming you have
a way to authenticate to AWS. In my case, I have already provided environment
variables, an instance profile, etc. that gives this program access to the AWS
API as the necessary IAM role.

## The Story is Auth

As with many of my stories, this story will become one largely consisting of
authentication. Unraveling the "magic" of the `kubeconfig` file and AWS's IAM
authentication was not an easy task - as I soon learned, the `kubernetes-python`
API client _also_ depends heavily on `kubeconfig` by default.

Most of [the
examples](https://github.com/kubernetes-client/python/tree/master/examples) look
something like this, and if you want to use `kubeconfig`, this works very well.

```python
from kubernetes import config

config.load_kube_config()
core_v1 = core_v1_api.CoreV1Api()
```

(Although there is also a cool example of [in-cluster config](https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py))

However, for portability, I want to bypass `kubeconfig` but will be running outside of a cluster. So let's see how this
class works.

```python
config.kube_config.Configuration
```

After a bit of flailing, fighting, and digging, I learned that you can
initialize an API connection with the API endpoint and a bearer token.
Basically, you initialize a configuration object directly and then use that to
initialize an API client.

<!-- url: https://github.com/colearendt/example-python-kubernetes/raw/master/breakout/1_api_client.py -->
```python
import kubernetes


def k8s_api_client(endpoint: str, token: str, cafile: str) -> kubernetes.client.CoreV1Api:
    kconfig = kubernetes.config.kube_config.Configuration(
        host=endpoint,
        api_key={'authorization': 'Bearer ' + token}
    )
    kconfig.ssl_ca_cert = cafile
    kclient = kubernetes.client.ApiClient(configuration=kconfig)
    return kubernetes.client.CoreV1Api(api_client=kclient)
```
<!-- url: end -->

There are other useful options in `kconfig`, like `kconfig.proxy` and
`kconfig.verify_ssl`. Have a look at the class for more details!

### Authenticate to AWS / EKS

Now we need to figure out how to get a bearer token to talk to EKS. I cannot use
the magic in `kubeconfig`, which led me to the [AWS CLI's `aws eks get-token`
command](https://docs.aws.amazon.com/cli/latest/reference/eks/get-token.html). I
chose to opt for a pure python solution in the
[`eks-token`](https://pypi.org/project/eks-token/) package. You can evaluate the
source for yourself [here](https://github.com/peak-ai/eks-token) and [more
specifically,
here](https://github.com/peak-ai/eks-token/blob/master/eks_token/logics.py).


With this module, we can acquire an EKS token easily:

<!-- url: https://github.com/colearendt/example-python-kubernetes/raw/master/breakout/2_token.py -->
```python
import eks_token

cluster_name = 'my-eks-cluster'
my_token = eks_token.get_token(cluster_name)
```
<!-- url: end -->

**NOTE:** It is worth checking whether the `boto3` or `aws` libraries provide
access to this functionality directly from Python as things improve!

### Now TLS

Unfortunately, the `kubernetes-python` client does not allow for inlining
the TLS CA Certificate. As a result, we have to write it to a temp file
(which by inspection is exactly what the `kubeconfig` approach is doing)

<!-- url: https://github.com/colearendt/example-python-kubernetes/raw/master/breakout/3_cafile.py -->
```python

   
import boto3
import tempfile
import base64


def _write_cafile(data: str) -> tempfile.NamedTemporaryFile:
    # protect yourself from automatic deletion
    cafile = tempfile.NamedTemporaryFile(delete=False)
    cadata_b64 = data
    cadata = base64.b64decode(cadata_b64)
    cafile.write(cadata)
    cafile.flush()
    return cafile


bclient = boto3.client('eks')
cluster_data = bclient.describe_cluster(name=cluster_name)['cluster']
my_cafile = _write_cafile(cluster_data['certificateAuthority']['data'])
```

### Put it all together

Armed with a bearer token and transport layer security (TLS), we now have the
tools we need to succeed!!

<!-- url: https://github.com/colearendt/example-python-kubernetes/raw/master/breakout/4_main.py -->
```python
api_client = k8s_api_client(
    endpoint=cluster_data['endpoint'],
    token=my_token['status']['token'],
    cafile=my_cafile.name
)

api_client.list_namespace()
```

## Go wild

The first time `list_namespace()` returned data, I was ecstatic.
Authentication successful! Now the world is your oyster.

All that's left is perusing the [API client
docs](https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md)
and refreshing AWS creds every so often!

For example, creating a configmap:

<!-- url: https://github.com/colearendt/example-python-kubernetes/raw/master/breakout/5_configmap.py -->
```python
my_configmap = kubernetes.client.V1ConfigMap(
    api_version='v1',
    metadata={'name': 'my-configmap'},
    kind='ConfigMap',
    data={'my_file.txt': 'mycontent'}
)

api_client.create_namespaced_config_map(namespace='default', body=my_configmap)

# NOTE: to update a configmap, you need to
# use k8s_client.replace_namespaced_config_map
#
# If it already exists, create will give a 409 conflict
```

Have fun!!

[The source for this article is available here (along with a few extra
examples)](https://github.com/colearendt/example-python-kubernetes)

## Outtakes

Yes, there were some outtakes:

- Standing up [`mitmproxy`](https://mitmproxy.org/) in a docker container and
  setting `kconfig.proxy='http://localhost:8080` and `kconfig.verify_ssl=False`
  before initializing `api_client` so I could see the requests being made to the
  Kubernetes cluster and verify whether authentication was being sent
- Flailing on temp files (today, I learned that python disposes of temp files
  rather quickly by default)
- Trolling the internet and finding an unfortunate absence of docs on this
  topic. "Is this obvious to everyone else? Am I doing something wrong?" Classic
  questions of a lonely explorer.
- Accidentally installing the `aws` package into my `pyenv` and breaking my
  terminal's ability to assume roles when in certain directories.

Programming can be hard! It is so nice to know we are not alone!
]]></content>
        <category label="DevOps"/>
        <category label="Python"/>
        <category label="Programming"/>
        <category label="HowTo"/>
        <category label="AWS"/>
        <category label="Kubernetes"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Using sssd in a Playground Without TLS]]></title>
        <id>sssd-without-tls</id>
        <link href="/blog/sssd-without-tls"/>
        <updated>2022-10-01T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[
`sssd` has established itself as the most common way to provision system
accounts via LDAP or Active Directory on linux servers across all linux
distributions. However, working with it can be tricky! TL;DR; We show an example of using `sssd` to contact an LDAP server that is
listening  ...]]></summary>
        <content type="html"><![CDATA[
[`sssd`](https://sssd.io/) has established itself as the most common way to provision system
accounts via LDAP or Active Directory on linux servers across all linux
distributions. However, working with it can be tricky!

## TL;DR;

We show an example of using `sssd` to contact an LDAP server that is
listening on port 389 (in plaintext / no TLS). This is _NOT_ a good
idea in any production environment. However, it can be important
and helpful in playgrounds, learning, or other experiments. The magic
configuration is `ldap_auth_disable_tls_never_use_in_production = true`.

## Why

It is quite straightforward to stand up an LDAP server listening in plaintext. My
favorite mechanism is using the [`openldap`
container](https://github.com/osixia/docker-openldap)
, [although there are other options](https://github.com/nitnelave/lldap).

```bash
docker run -it --rm -p 389:389 osixia/openldap:latest
```

However, if you have a toy linux container running `sssd`, this is unfortunately not an obvious option! Why, you ask?
This is all just a dev playground!? Right. Well the `sssd` maintainers want to be very careful about not creating
security vulnerabilities or letting their users get hacked. This means you have to work hard to open yourself up to this
type of vulnerability in your playground.

Specifically, we will use the `ldap_auth_disable_tls_never_use_in_production` setting.

> NOTE: Do not use this setting in any "real" environment with "real" users, passwords, sensitive data, etc.

## Give it a Shot

### Create Users

First, we need to create and populate our LDAP server. Let's go ahead and do that. It is easiest if we create a file
with users first. For a more advanced LDIF file, check
out [the repository associated with this post](https://github.com/colearendt/container-playground):

_users.ldif_
```ldif
version: 1

## Entry 1: dc=angl,dc=dev
#dn: dc=angl,dc=dev
#dc: angl
#o: Angl Dev
#objectclass: top
#objectclass: dcObject
#objectclass: organization
#
## Entry 2: cn=admin,dc=angl,dc=dev
#dn: cn=admin,dc=angl,dc=dev
#cn: admin
#description: LDAP administrator
#objectclass: simpleSecurityObject
#objectclass: organizationalRole
#userpassword: {SSHA}+FquX8RcwTtBPo7mu2pgSvjaQYX9HpCL
#
#
# Entry 3: cn=engineering_group,dc=angl,dc=dev
dn: cn=engineering_group,dc=angl,dc=dev
cn: engineering_group
gidnumber: 500
memberuid: joe
memberuid: julie
objectclass: posixGroup
objectclass: top

# Entry 4: dc=engineering,dc=angl,dc=dev
dn: dc=engineering,dc=angl,dc=dev
dc: engineering
description: The Engineering Department
o: Engineering
objectclass: dcObject
objectclass: organization
objectclass: top


# Entry 5: cn=joe,dc=engineering,dc=angl,dc=dev
dn: cn=joe,dc=engineering,dc=angl,dc=dev
cn: joe
gidnumber: 500
givenname: Joe
homedirectory: /home/joe
loginshell: /bin/sh
mail: joe@angl.dev
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
sn: Golly
uid: joe
uidnumber: 1000
userpassword: {MD5}j/MkifkvM0FmlL6P3C1MIg==

# Entry 9: cn=julie,dc=engineering,dc=angl,dc=dev
dn: cn=julie,dc=engineering,dc=angl,dc=dev
cn: julie
gidnumber: 500
givenname: Julie
homedirectory: /home/julie
loginshell: /bin/sh
mail: julie@angl.dev
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
sn: Jolly
uid: julie
uidnumber: 1001
userpassword: {MD5}FvEvXoN54ivpleUF6/wbhA==
```

You will notice that the first two entries are commented out. They are included to represent a _complete_ LDIF file.
However, the `osixia/docker-openldap` container help us by provisioning these automatically.

Further, you will notice that passwords are included. This makes things easier for our playground, but is _definitely_ a
bad idea in real life / production applications.

### Create LDAP Server

Now let's create the server itself!

```bash
docker network create playground-network
docker run \
  -d --name openldap --rm \
  -p 389:389 \
  --network playground-network \
  -v $(pwd)/users.ldif:/container/service/slapd/assets/config/bootstrap/ldif/50-bootstrap.ldif \
  -e LDAP_TLS=false \
  -e LDAP_DOMAIN="angl.dev" \
  -e LDAP_ADMIN_PASSWORD="admin" \
  osixia/openldap:1.5.0 \
  --copy-service --loglevel debug
```

And check that it is working

```bash
docker exec -it openldap ldapsearch -D cn=admin,dc=angl,dc=dev -b dc=angl,dc=dev -w admin cn
docker exec -it openldap ldapsearch -D cn=admin,dc=angl,dc=dev -b dc=angl,dc=dev -w admin cn=julie \*
```

If you look carefully, you will notice that:

1. We created a persistent network for our containers to share
2. We provisioned users from our `ldif` file
3. We disabled TLS on the service
4. We bumped up the logging verbosity for debugging purposes

These are all useful tidbits to dig into if you are not familiar!

### Configure sssd Server

It is possible to run `sssd` in a fairly vanilla `ubuntu:jammy` container.

```bash
docker run -it --name sssd --rm --network playground-network ubuntu:jammy bash

apt update && apt install -y sssd ldap-utils vim
```

Then you need to create your `sssd.conf` file. Notice our magic
option `ldap_auth_disable_tls_never_use_in_production=true`. This will be the magic that makes things work for us!
```bash
cat << EOF > /etc/sssd/sssd.conf
[sssd]
config_file_version = 2
services = nss, pam
domains = LDAP

[nss]
filter_users = root,named,avahi,haldaemon,dbus,radiusd,news,nscd
filter_groups =

[pam]

[domain/LDAP]
id_provider = ldap
auth_provider = ldap
chpass_provider = ldap
sudo_provider = ldap
enumerate = true
# ignore_group_members = true
cache_credentials = false
ldap_schema = rfc2307
ldap_uri = ldap://openldap:389
ldap_search_base = dc=angl,dc=dev
ldap_user_search_base = dc=angl,dc=dev
ldap_user_object_class = posixAccount
ldap_user_name = uid

ldap_group_search_base = dc=angl,dc=dev
ldap_group_object_class = posixGroup
ldap_group_name = cn
ldap_id_use_start_tls = false
ldap_tls_reqcert = never
ldap_tls_cacert = /etc/ssl/certs/ca-certificates.crt
ldap_default_bind_dn = cn=admin,dc=angl,dc=dev
ldap_default_authtok = admin
access_provider = ldap
ldap_access_filter = (objectClass=posixAccount)
min_id = 1
max_id = 0
ldap_user_uuid = entryUUID
ldap_user_shell = loginShell
ldap_user_home_directory = homeDirectory
ldap_user_uid_number = uidNumber
ldap_user_gid_number = gidNumber
ldap_group_gid_number = gidNumber
ldap_group_uuid = entryUUID
ldap_group_member = memberUid
ldap_auth_disable_tls_never_use_in_production = true
use_fully_qualified_names = false
ldap_access_order = filter
debug_level=6
EOF
chmod 600 /etc/sssd/sssd.conf
```

Now let's start the `sssd` service
```bash
sssd -i
# should see some log messages that suggest things are happening!
```

### Be sure it works!

Now let's make sure that this works by starting another shell in our `jammy` container.

```bash
docker exec -it sssd bash

id joe
# uid=1000(joe) gid=500(engineering_group) groups=500(engineering_group)
id julie
# uid=1001(julie) gid=500(engineering_group) groups=500(engineering_group)
```

## Using `docker-compose`

For playground environments like this, `docker-compose` makes this setup much easier to architect and reuse. You can
use [my example compose setup](https://github.com/colearendt/container-playground) if you prefer.

```bash
cd compose/
docker network create playground-network
NETWORK=playground-network docker-compose -f ldap.yml -f sssd.yml -f network.yml up -d
docker exec -it compose_sssd_1 bash

sssd -i >/tmp/sssd.log 2>&1 &
id joe
```

## Review

Well done! You have successfully started your own `sssd` container. Although this is very much a toy, it is a
great "jumping off point" to learn and understand how `sssd` works in more detail!

Any time you need a toy LDAP server for `sssd`, just remember: `ldap_auth_disable_tls_never_use_in_production = true`.
]]></content>
        <category label="Docker"/>
        <category label="Container"/>
        <category label="LDAP"/>
        <category label="sssd"/>
        <category label="DevOps"/>
        <category label="SysAdmin"/>
        <category label="HowTo"/>
        <category label="openldap"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Helm Intro and Helm Cheatsheet]]></title>
        <id>helm-cheatsheet</id>
        <link href="/blog/helm-cheatsheet"/>
        <updated>2022-10-10T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[
Below is an introduction to Helm! If you want to skip to the
cheatsheet, you can download it
here. What is Helm According to its own docs, Helm is "the" package
manager for Kubernetes. What does this mean? It's a way of keeping track of all your Kubernetes  ...]]></summary>
        <content type="html"><![CDATA[
Below is an introduction to Helm! If you want to [skip to the
cheatsheet](#cheat-sheet), you can [download it
here](https://www.analogous.dev/download/cheatsheet/helm.pdf).

## What is Helm

According to [its own docs](https://helm.sh/docs/), Helm is "the" package
manager for Kubernetes. What does this mean?

It's a way of keeping track of all your Kubernetes stuff!

Helm as I describe it is a mechanism for packaging and parameterizing standard
Kubernetes YAML files. It uses [Go
Templating](https://blog.gopheracademy.com/advent-2017/using-go-templates/) for
most of this mechanism, and adds a layer of version / metadata tracking as
well. All of this packaged up into tarballs used by a client-side-only (as of
`helm` v3) CLI.

So basically: Helm = YAML + Go Templating + Versioning + Tar balls.

## Why use it?

Why use it? There are lots of alternatives out there, and many purported "Helm replacements,"
but Helm has yet to give up its throne, and I have not found anything better
for my own use cases... yet. So what are Helm's strengths?

I will do my best not to wax poetic. I am biased and a big fan of Helm. As a layer of
abstraction between an application and Kubernetes, I think it is a fantastic asset.

In particular, I think this is because:

- No runtime dependency
- Client-side only utility
- Data stored server side for collaboration
- output represents native Kubernetes objects (i.e. interoperable with other tools)
- `helm template` gives rapid feedback on iterating and testing
- plain text file output / diffs is very easy to parse

As a system administrator, it is nice because it offers:

- Version pinning for reproducibility
- Everything is open source tarballs, so dependencies are easy to track and introspect
- application vendors will ideally maintain their own chart and good NEWS files

## When to use it?

So that's _what_ it is, and _why_ it is desirable. But _when_ is it useful?

I find that helm particularly shines in a handful of situations:

- Managing an array of applications deployed on Kubernetes
- Packaging your own application for use by customers
- Encoding complex knowledge about "how to run an application" (to an extent,
  then you get to
  [operators](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/))
- To set up easy "roll-back" policies for applications that support the
  behavior

Occasionally a wrapper like [ArgoCD](https://argoproj.github.io/cd/), [Flux](https://fluxcd.io/)
, [helmfile](https://github.com/helmfile/helmfile), or [pulumi](https://www.pulumi.com/docs/get-started/kubernetes/)
will be useful to manage your helm deployments too, so
that you don't have to keep track of a bunch of CLI commands.

## When not to use it?

Helm can definitely be overkill in some "hello world" or very simple deployment
situations. Unfortunately, it also **does not have a great answer for
[CRDs](https://helm.sh/docs/topics/charts/#custom-resource-definitions-crds)
yet**. Moreover, it is **only useful for Kubernetes**, so if you are unfamiliar
with Kubernetes, it will have limited utility for you.

The other case where it may not be useful is in some **internal applications**.
Maintaining a helm chart for an application can end up being a sizable amount
of work, and they do not allow arbitrary inputs, so if you miss some key (i.e.
"imagePullSecrets,") you can end up spending a lot of time key-chasing across
your charts. I have heard of folks using [Kustomize](https://kustomize.io/) in
such a situation, although another option is to use a meta chart (one chart for
many apps) or Functions-as-a-Service (FaaS) framework like
[Serverless](https://www.serverless.com/),
[OpenFaas](https://www.openfaas.com/), [Knative](https://knative.dev/docs/),
etc.

Also, helm charts do have a **complexity ceiling**. Go Templating provides
lots of flexibility, but being DRY is hard, and there are many parts of the
process that are not optimal from a software development point of view. As
charts become more complex, an
[operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
becomes increasingly beneficial as a mechanism to provide better software
semantics to the application management process. However, the learning curve
for operators can also be a bit steep.

Finally, helm charts unfortunately **do not have hard-and-fast standards about how values are used** across the
ecosystem. As a result, you will often encounter wild variations in chart quality, value naming, and value behavior.

## Hello World

Let's get started on a hello world example! First, you need to [install
kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl), [install
helm](https://helm.sh/docs/intro/install/), and have a kubernetes cluster
available.  Once those things are taken care of, a hello world example of a
helm deployment is pretty straightforward!

For this example, we will use my [generic
chart](https://github.com/colearendt/helm/tree/main/charts/generic), useful for
deploying simple services with standard configuration or helm needs.

We are also going to use [this hello-world container](https://hub.docker.com/r/paulbouwer/hello-kubernetes).

First, add the repository that houses our example chart:
```
helm repo add colearendt https://colearendt.github.io/helm/
```

You can look at the values available for the chart:
```
helm show values colearendt/generic

# I like to pipe it to a pager for search and such
helm show values colearendt/generic | less
```

Then create a YAML file called _my-values.yaml_ to hold values:

_my-values.yaml_
```
image:
  repository: paulbouwer/hello-kubernetes
  tag: "1.10"
pod:
  port: 8080
```

Then template the output:
```
helm template hello-world colearendt/generic -f my-values.yaml
```

And install it into the Kubernetes cluster!

```
helm upgrade --install hello-world colearendt/generic -f my-values.yaml
```

Then you should be able to see the app deployed:
```
helm list
kubectl get pods
```

And view the service in your web browser at http://localhost:8080:
```
kubectl port-forward svc/hello-world-generic 8080:80
```

### Clean Up

If you want to clean up after yourself:

```bash
# delete the helm release
helm delete hello-world

# delete the repository reference
helm repo remove colearendt
```

Unfortunately, I have not taken much time to dive into troubleshooting here! If you are hitting issues,
please [shoot me an email](mailto:info@analogous.dev) - I would love to have feedback on what to improve! Maybe
someday I will take the time to set up comments 😅

## Best Practices

So now you have a "Hello World" deployment under your belt. However, it also helps to keep in mind some best practices
as you keep improving. Below is a handful of helm chart conventions that may be unfamiliar if you are new to the
community:

- Make sure to pin helm chart versions with the `--version` flag
- Maintain a `NEWS.md` file (or read the `NEWS.md` file) to keep track of
  changes between versions
- Keep an eye out for "upgrading directions" in the `README.md` or elsewhere
- Use `helm show values` to see the default values (and comment strings
  associated). Ideally these are presented or discussed in a `README` as well.
- Avoid [`sub-charts`](https://helm.sh/docs/chart_template_guide/subcharts_and_globals/) if you can. It is tempting as a
  DRY software principle, but turns out to be a pretty advanced topic with lots of tricky edge cases. In particular,
  namespaces can be painful.

## Cheat Sheet

I took the time to arrange a "cheat sheet" of my favorite helm commands and the
contexts in which they are useful. It was inspired by [RStudio's array of excellent
cheat sheets for the R community](https://www.rstudio.com/resources/cheatsheets/).

A hit-list of some of the most useful commands:

- `helm show values chartrepo/chartname`
- `helm template releasename chartrepo/chartname`
- `helm upgrade --install releasename chartrepo/chartname`
- `helm repo add https://repourl`
- `helm repo list`
- `helm search repo`
- `helm info`
- `helm list`

And the cheat-sheet itself can be downloaded [here](https://www.analogous.dev/download/cheatsheet/helm.pdf).

<div style="position: relative; text-align: center">
  <div>
    <iframe src="https://dl.angl.dev/cheatsheet/helm.pdf" style="height: 230px; width: auto">
    </iframe>
  </div>
  <a href="https://analogous.dev/download/cheatsheet/helm.pdf" target="_blank" style="position:absolute; top:0; 
      left:0; display:inline-block; width:100%; height:100%; z-index:5;">
  </a>
</div>

## What's Next?

The [helm project](https://helm.sh/) is an open source project. There is much
that could be improved, and many applications that need helm charts or need
improved helm charts. You can make a difference! If you are interested in
learning more, check out the [helm tag](/?tag=Helm) on this blog to see other writing on
the topic, and start poking around on [ArtifactHub](https://artifacthub.io/),
where lots of charts are centralized for easier searching!
]]></content>
        <category label="HowTo"/>
        <category label="Helm"/>
        <category label="DevOps"/>
        <category label="Tech"/>
        <category label="Kubernetes"/>
        <category label="Container"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Websockets and Cloudflare Workers]]></title>
        <id>websockets-and-cloudflare-workers</id>
        <link href="/blog/websockets-and-cloudflare-workers"/>
        <updated>2025-02-22T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[
Websockets and Workers! A match made in heaven! Or not. Stateful, persistent communication? With ephemeral, floaty web workers that have almost no state? How in the world can those interoperate!? Leave it to the talented folks at Cloudflare to build a fantastic integration. This is  ...]]></summary>
        <content type="html"><![CDATA[
Websockets and Workers! A match made in heaven!

Or not.

Stateful, persistent communication? With ephemeral, floaty web workers that have almost no state?

How in the world can those interoperate!?

Leave it to the talented folks at Cloudflare to build a fantastic integration. This is way easier than you might expect!

## Getting Started

To get set up, you want to create a standard boilerplate web worker. I love starting with the `Application Starter`:

```
wrangler init
# give it a name...
# choose "Application Starter"
# then "API starter (OpenAPI compliant)"
```

(If you don't have `wrangler` installed yet, then you need to go take care of that!)

Now we're set up! Let's get this thing rolling!

## Get it working locally

As with most workers (and other projects) - the first step is to get things working locally. Thankfully, wrangler has a pretty killer setup for this. So let's get things started right out of the gate.

Navigate to the directory you just created and:
```
wrangler dev
```

### Add websocket

Now we're going to add a simple websocket server to the `endpoints` directory

_endpoints/websocket.ts_
```
import { OpenAPIRoute } from "chanfana";
import { Context } from "hono";

export class WebSocket extends OpenAPIRoute {
	async handle(c: Context) {
		// optional - if you want to be strict
		const upgradeHeader = c.req.header("Upgrade");
		if (!upgradeHeader || upgradeHeader !== "websocket") {
			return new Response("Expected Upgrade: websocket", { status: 426 });
		}

		const webSocketPair = new WebSocketPair();
		const [client, server] = Object.values(webSocketPair);

		server.accept();
		server.addEventListener("message", (event) => {
			console.log(event);
			server.send("Hello from the other side!");
		});

		server.addEventListener("close", () => {
			console.log("WebSocket closed");
		});

		return new Response(null, {
			status: 101,
			webSocket: client,
		});
	}
}
```

And integrate into the `index.ts` file:

_index.ts_

```
import { WebSocket } from "./endpoints/websocket";

...

openapi.all("/api/ws", WebSocket);
```

### Test it!

Now how do you test websockets!? Well there are some cool tools out there on the web. 

But I just wanted to test locally, so I made a little python script to test with:

_wstest.py_
```
import argparse
from websocket import create_connection

def main():
    parser = argparse.ArgumentParser(description='WebSocket test client')
    parser.add_argument('--domain', '-d', nargs='?', default='localhost:8787', help='The hostname to connect to (e.g. localhost:8787)')
    parser.add_argument('--tls', '-t', action='store_true', help='Use TLS')
    parser.add_argument('--path', '-p', nargs='?', default='/api/ws', help='The path to connect to (e.g. /api/ws)')
    args = parser.parse_args()

    if args.tls:
        protocol = "wss"
    else:
        protocol = "ws"
    
    # ensure that args.domain does not have a protocol prefix
    # parse domain as a url
    domain = args.domain.split('://')[1]
    path = args.path

    # ensure that there is only one slash between domain and path
    if path.startswith('/'):
        path = path[1:]
    if domain.endswith('/'):
        domain = domain[:-1]

    ws_url = f"{protocol}://{domain}/{path}"
    ws = create_connection(ws_url)
    print(f"--> Connected to {ws_url}")
    print("--> Sending 'Hello, World'...")
    ws.send("Hello, World")
    result = ws.recv()
    print(f"<-- Received '{result}'")

    try:
        while True:
            msg = input('--> ')
            ws.send(msg)
            result = ws.recv()
            print(f"<-- Received '{result}'")
    except KeyboardInterrupt:
        print("\nClosing connection...")
    finally:
        ws.close()

if __name__ == '__main__':
    main()
```

You will need one little library:
```
pip install websocket-client
```

And then you should be good to test!

```
# use the port that your websocket worker is running on from `wrangler dev`
python3 ./wstest.py -d localhost:59462
```

## Deploy to Cloudflare

This should be pretty straightforward! Deploy your worker!

```
wrangler deploy
```

Then redirect the websocket test script and see what happens! (Make sure to include the TLS flag!):

```
python3 ./wstest.py -d my-deployed-domain.workers.dev -t
```

## More debugging

There are lots of snags you can hit with websockets, especially if you start using the browser.

- Beware of CORS
- Be mindful of `ws://` vs. `wss://` (for TLS connections) protocol issues
- Be careful of client code, which needs to handle reconnections and can cause problems disconnecting unintentionally
- Firewalls, enterprise security restrictions, and reverse proxies can cause all sorts of weird issues with websockets!
- It is probably worth using socket.js or having some fallback mechanisms handled... but more on that another time.

## Successful Websocketing!

Congratulations! You have hopefully deployed a websocket server successfully to Cloudflare! 

I would love to hear how it went, if you ran into issues, how this article can be improved, or what you ended up building! [Let me know!](mailto:info@analogous.dev)

Thanks!
]]></content>
        <category label="HowTo"/>
        <category label="Websockets"/>
        <category label="DevOps"/>
        <category label="Tech"/>
        <category label="Cloudflare"/>
        <category label="Web Worker"/>
    </entry>
</feed>