Alternative implementation of the sample controller

30 views
Skip to first unread message

Rafael Ávila de Espíndola

unread,
Nov 2, 2020, 11:12:05 AM11/2/20
to kuberne...@googlegroups.com
Hi,

Not sure if the sample controller qualifies as "dev", sorry if this is
the wrong list.

I have been learning Kubernetes. To make sure I understood how it works,
I have re-implemented the sample controller. The code, if anyone is
interested, is at

https://github.com/espindola/sample-controller-from-scratch

Some of the differences are expected from being implemented today: It
uses the v1 API instead of v1beta and therefore has the schema that v1
requires.

It also includes a fix for
https://github.com/kubernetes/sample-controller/issues/75 (assuming that
it is actually an issue).

Other differences are a personal preference. As a user I am not very
found of large yaml files and as a developer I try to avoid auto
generated code. The sample controller then registers its own CRD and
uses reflection to handle the various types it watches instead of auto
generating code for that.

The tests are also completely local by mocking the http transport. That
is no replacement for integration testing, but I think provide a really
good first line of defense while development and are much faster than a
integration tests could ever be:

$ go test -count=100 ./pkg/controller
ok github.com/espindola/sample-controller/pkg/controller 0.245s

And still have very good coverage:

$ go test ./pkg/controller -coverprofile cover.out -coverpkg ./pkg/controller,./pkg/kubeapi
ok github.com/espindola/sample-controller/pkg/controller 0.012s
coverage: 97.3% of statements in ./pkg/controller, ./pkg/kubeapi

And a shameless plug:

I have used wireshark to help me understand the protocol. That is only
possible if the tls keys are saved somewhere. I wrote

https://github.com/kubernetes/kubernetes/pull/95414

In order to do it. Do I have to do something else on that pull request?
I am honestly lost on the process.

Cheers,
Rafael

Ben Swartzlander

unread,
Nov 3, 2020, 1:05:42 PM11/3/20
to kuberne...@googlegroups.com
On 11/2/20 10:40 AM, Rafael Ávila de Espíndola wrote:
> Hi,
>
> Not sure if the sample controller qualifies as "dev", sorry if this is
> the wrong list.
>
> I have been learning Kubernetes. To make sure I understood how it works,
> I have re-implemented the sample controller. The code, if anyone is
> interested, is at
>
> https://github.com/espindola/sample-controller-from-scratch
>
> Some of the differences are expected from being implemented today: It
> uses the v1 API instead of v1beta and therefore has the schema that v1
> requires.

Thanks for posting this illustrative example! I love understanding the
low-level bits and this is a great way to see precisely what's going on.
Too many developers rely on the crutch that is auto-generated code. Your
code is very concise and to the point.

My only worry is that while an example like this is great for learning,
production controllers should probably continue to be built the more
traditional way, with generated code and maximal code re-use, because
scaling a sample like this up would force you to reinvent a lot of
things and inevitably create a lot of new bugs along the way.

Great work though!
-Ben

Rafael Ávila de Espíndola

unread,
Nov 4, 2020, 8:15:44 PM11/4/20
to Ben Swartzlander, kuberne...@googlegroups.com
Ben Swartzlander <b...@swartzlander.org> writes:

> On 11/2/20 10:40 AM, Rafael Ávila de Espíndola wrote:
>> Hi,
>>
>> Not sure if the sample controller qualifies as "dev", sorry if this is
>> the wrong list.
>>
>> I have been learning Kubernetes. To make sure I understood how it works,
>> I have re-implemented the sample controller. The code, if anyone is
>> interested, is at
>>
>> https://github.com/espindola/sample-controller-from-scratch
>>
>> Some of the differences are expected from being implemented today: It
>> uses the v1 API instead of v1beta and therefore has the schema that v1
>> requires.
>
> Thanks for posting this illustrative example! I love understanding the
> low-level bits and this is a great way to see precisely what's going on.
> Too many developers rely on the crutch that is auto-generated code. Your
> code is very concise and to the point.
>
> My only worry is that while an example like this is great for learning,
> production controllers should probably continue to be built the more
> traditional way, with generated code and maximal code re-use, because
> scaling a sample like this up would force you to reinvent a lot of
> things and inevitably create a lot of new bugs along the way.
>
> Great work though!

Thanks.

Yes, I expect most developers involved in kubernetes like the way
kubernetes does things :-)

I personally just find it hard not knowing what the underlying layers
are doing, so this was a good learning exercise. Hopefully others find
it instructive.

Cheers,
Rafael
Reply all
Reply to author
Forward
0 new messages