Kubernetes Tweet Operator
How do you write a Kubernetes Operator from scratch? To answer this question as simply as possible I wanted to find the bare minimum needed to write the blueprint for a custom resource, generate the client code and write a reconciler.
But I wanted it to do something external to the Kubernetes cluster, not just mock about with Pods or Deployments, because in my mind that is the real power of custom controllers: they can do just about anything. There are Kubernetes controllers to create cloud resources (EC2 instances, S3 buckets, you name it), managing stateful applications and even ordering pizza.
So here is the Tweet Operator: a Kubernetes custom controller that manages the lifecycle of the custom resource Tweet: each resource has a text field, and when created, the operator posts the tweet. It then monitors the details of the tweet and posts back retweet and like counts to the status field of the Kubernetes custom resource.
The first hurdle was to actually get access to the Twitter API: they have recently changed it, so in order to post things to Twitter you need to first enable what they call Extended access. This is far from obvious, and by the looks of it will probably change again in the future.
I followed a short YouTube video to set up a basic Twitter bot using a couple of existing Twitter Go libraries. The code is here.