<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Kenan Jasim</title><link>https://kenan.sh/</link><description>Recent content on Kenan Jasim</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sun, 04 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://kenan.sh/index.xml" rel="self" type="application/rss+xml"/><item><title>What should a unit test actually test?</title><link>https://kenan.sh/posts/what-should-a-unit-test-test/</link><pubDate>Sun, 04 Oct 2026 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/what-should-a-unit-test-test/</guid><description>&lt;p&gt;For a long time I treated the three rules of TDD as law. Don&amp;rsquo;t write production code until you have a failing test. Don&amp;rsquo;t write more of a test than is needed to fail. Don&amp;rsquo;t write more code than is needed to pass. My old notes from 2021 even set a target of 90 to 100% unit test coverage, and I was proud of hitting it.&lt;/p&gt;
&lt;p&gt;The problem is that on real projects, the tests slowly became the thing slowing us down. A small refactor would break dozens of tests even though nothing the user could see had changed. It took me a while to work out that TDD itself wasn&amp;rsquo;t the problem. I had misunderstood what I was supposed to be testing.&lt;/p&gt;
&lt;h2 class="heading" id="how-i-used-to-write-tests"&gt;
 How I used to write tests
 &lt;a href="#how-i-used-to-write-tests"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The way I was taught, every class or function got its own test file. Every public method got at least one test. Anything the code depended on was mocked, so each test exercised exactly one piece of code in isolation.&lt;/p&gt;
&lt;p&gt;It felt rigorous. The coverage numbers looked great, and every function had a test sitting next to it that described what it did. I don&amp;rsquo;t think this was a stupid way to work. It is what most books and courses teach, and for small codebases it mostly holds up.&lt;/p&gt;
&lt;h2 class="heading" id="where-it-fell-apart"&gt;
 Where it fell apart
 &lt;a href="#where-it-fell-apart"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The cracks only showed once a codebase had been around for a year or two.&lt;/p&gt;
&lt;p&gt;We regularly had more test code than production code. That isn&amp;rsquo;t a problem in itself, but a lot of it was mock setup that described &lt;em&gt;how&lt;/em&gt; a function did its job rather than &lt;em&gt;what&lt;/em&gt; it did. When we changed the how, the mocks had to be rewritten, even though the result was the same.&lt;/p&gt;
&lt;p&gt;Refactoring started to feel expensive. And when refactoring is expensive, people stop doing it, which is the opposite of what TDD is meant to encourage. The tests were supposed to give us the confidence to change the design. Instead they locked it in place.&lt;/p&gt;
&lt;p&gt;Eventually we had red tests nobody could explain. Was a test failing because the feature was broken, or because someone had renamed a private helper? After enough of those, people stopped trusting the suite and started re-running it until it passed.&lt;/p&gt;
&lt;p&gt;The lesson I took from all of this is simple. A test that breaks when the behaviour &lt;em&gt;hasn&amp;rsquo;t&lt;/em&gt; changed is testing the wrong thing.&lt;/p&gt;
&lt;h2 class="heading" id="the-misunderstanding"&gt;
 The misunderstanding
 &lt;a href="#the-misunderstanding"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Ian Cooper&amp;rsquo;s talk &lt;a href="https://www.youtube.com/watch?v=EZ05e7EMOLM"&gt;TDD, Where Did It All Go Wrong&lt;/a&gt; changed how I think about this. His main point is that the &amp;ldquo;unit&amp;rdquo; in unit test was never meant to be a class or a function. The unit of isolation is the &lt;em&gt;test&lt;/em&gt;. Tests should be isolated from each other, so they can run in any order and in parallel. They don&amp;rsquo;t need to isolate every function from every other function.&lt;/p&gt;
&lt;p&gt;Once you see it that way, the right thing to test is behaviour through the public API. The internals of a package are implementation details, and you should be free to rewrite them without touching a single test.&lt;/p&gt;
&lt;p&gt;This is also what makes the refactor step of red, green, refactor work. You get to green however you can, then clean up the design while the tests stay exactly as they are. If the refactor makes you edit the tests, you aren&amp;rsquo;t really refactoring. You&amp;rsquo;re changing two things at once and hoping they still agree.&lt;/p&gt;
&lt;h2 class="heading" id="what-this-looks-like-in-go"&gt;
 What this looks like in Go
 &lt;a href="#what-this-looks-like-in-go"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Go makes this easier than most languages, because a test file can declare itself as an external package. If you write &lt;code&gt;package cache_test&lt;/code&gt; instead of &lt;code&gt;package cache&lt;/code&gt;, your tests can only see what is exported. The compiler stops you reaching into the internals.&lt;/p&gt;
&lt;p&gt;Take a small generic cache with &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;Get&lt;/code&gt;, where items expire after a TTL. The test I would have written a few years ago looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;TestSet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;testing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;New&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Minute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="c1"&gt;// Reaching into the internal map&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;		&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;expected 1 item, got %d&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;		&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;expected value 1, got %d&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It passes, and it&amp;rsquo;s useless. Rename &lt;code&gt;items&lt;/code&gt;, swap the map for something else, or change how entries are stored, and it breaks. Worse, it doesn&amp;rsquo;t test what &lt;code&gt;Set&lt;/code&gt; is actually for. Nobody calls &lt;code&gt;Set&lt;/code&gt; to put something in a map. They call it so that a later &lt;code&gt;Get&lt;/code&gt; returns the value. The only meaningful way to test &lt;code&gt;Set&lt;/code&gt; is through &lt;code&gt;Get&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The version I would write now only uses the public API:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cache_test&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;TestGetExpiresAfterTTL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;testing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;New&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Minute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Time&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;got&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;got&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;		&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Get(a) = %d, %v; want 1, true&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;got&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="c1"&gt;// Move the clock past the TTL&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Minute&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;		&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;expected item to have expired&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;	&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There is still something being faked here, the clock, but that is the point. Time is a real boundary of the system, like the network or the disk. Those are the places where a fake earns its keep. Faking things &lt;em&gt;inside&lt;/em&gt; your own package is where tests start to rot.&lt;/p&gt;
&lt;p&gt;This test also catches a real bug. An older cache I wrote only removed expired items on a background ticker, so &lt;code&gt;Get&lt;/code&gt; happily returned stale values until the next tick. The first test would never have noticed. A behaviour test like the second one would have failed straight away.&lt;/p&gt;
&lt;h2 class="heading" id="so-is-tdd-dead"&gt;
 So is TDD dead?
 &lt;a href="#so-is-tdd-dead"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;No. When David Heinemeier Hansson &lt;a href="https://dhh.dk/2014/tdd-is-dead-long-live-testing.html"&gt;declared TDD dead&lt;/a&gt; back in 2014, he was reacting to the same pain I have described above. But the answer isn&amp;rsquo;t to stop writing tests first. It&amp;rsquo;s to stop coupling them to the implementation.&lt;/p&gt;
&lt;p&gt;I still often write the test first, because it forces me to think about how the code will be used before I think about how it works. What has changed is that coverage is no longer the goal. If you test every behaviour that matters through the public API, high coverage tends to follow. If you chase the number directly, you end up with tests like the first example.&lt;/p&gt;
&lt;h2 class="heading" id="takeaways"&gt;
 Takeaways
 &lt;a href="#takeaways"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Test behaviour, not implementation.&lt;/li&gt;
&lt;li&gt;The test is the unit of isolation, not the class or function.&lt;/li&gt;
&lt;li&gt;If a refactor breaks a test without changing behaviour, the test was wrong.&lt;/li&gt;
&lt;li&gt;Only fake things at the edges of your system, such as time, network and disk.&lt;/li&gt;
&lt;li&gt;In Go, use external &lt;code&gt;_test&lt;/code&gt; packages so the compiler keeps you honest.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Good tests aren&amp;rsquo;t the ones that give you the highest coverage. They&amp;rsquo;re the ones the team still trusts six months from now, when someone needs to change the code underneath them.&lt;/p&gt;</description></item><item><title>Satellite handover in non-terrestrial networks</title><link>https://kenan.sh/posts/ntn-conditional-handover/</link><pubDate>Mon, 25 May 2026 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/ntn-conditional-handover/</guid><description>&lt;p&gt;I have been working on non-terrestrial networks, and satellite handover is one of the parts I have spent a while trying to get my head around. This is roughly what I have understood so far, written up mostly to organise my own thinking, so it is more a set of notes than anything authoritative.&lt;/p&gt;
&lt;p&gt;Phones hand over between towers all the time. You drive down the motorway and your phone moves from one tower to the next without you noticing. Doing the same thing with a satellite turns out to be much harder, to the point where there are whole sections of the 3GPP spec about it, and the reason is that the tower is moving, and moving quickly. A satellite in low Earth orbit travels across the sky at roughly 7.5 km per second. By the time the network notices your signal weakening, decides to move you, and sends the command, the satellite it was reacting to has already moved on. Normal handover assumes the network has time to react, and with satellites that assumption does not hold.&lt;/p&gt;
&lt;h2 class="heading" id="why-the-usual-approach-breaks-down"&gt;
 Why the usual approach breaks down
 &lt;a href="#why-the-usual-approach-breaks-down"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Normally your phone reports signal measurements, the network decides that a neighbouring cell is better, and it commands the handover in real time. On the ground this works fine. In orbit it runs into a few problems: the signal difference across a cell is often too gradual to trigger cleanly, a large number of phones may need to move at once, and the latency means a &amp;ldquo;hand over now&amp;rdquo; command can arrive too late to be useful. So the fix is not to make the real-time decision faster. It is to stop making the decision in real time at all.&lt;/p&gt;
&lt;h2 class="heading" id="conditional-handover-decide-early-trigger-later"&gt;
 Conditional handover: decide early, trigger later
 &lt;a href="#conditional-handover-decide-early-trigger-later"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Conditional Handover (CHO) was added in 3GPP Release 16. Instead of commanding the handover when it is needed, the network configures the candidate target cells in advance and attaches a condition to each one. It hands that package to the phone, and the phone watches for the condition and carries out the handover itself when it is met. No real-time command is needed.&lt;/p&gt;
&lt;p&gt;It is a bit like leaving instructions in advance rather than directing each step: &amp;ldquo;when you get to the roundabout, take the third exit.&amp;rdquo; You do not need to be on the phone at the roundabout.&lt;/p&gt;
&lt;p&gt;For satellites, Release 17 added two conditions that matter here, one based on distance and one based on time. Which one you use depends on how the beam is pointed at the ground.&lt;/p&gt;
&lt;h2 class="heading" id="two-kinds-of-cell"&gt;
 Two kinds of cell
 &lt;a href="#two-kinds-of-cell"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;An &lt;strong&gt;Earth Fixed Cell&lt;/strong&gt; keeps its coverage on the same area of ground by steering the beam as the satellite moves. The connection is more stable from the user&amp;rsquo;s point of view, but it needs active beam steering to achieve.&lt;/p&gt;
&lt;p&gt;An &lt;strong&gt;Earth Moving Cell&lt;/strong&gt; does not do this. The beam points straight down and sweeps across the ground as the satellite passes. The hardware is simpler, but the coverage area is always moving, so handovers happen more often.&lt;/p&gt;
&lt;h2 class="heading" id="d1-the-distance-trigger"&gt;
 D1: the distance trigger
 &lt;a href="#d1-the-distance-trigger"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;D1 (&lt;code&gt;condEventD1&lt;/code&gt;) is used with fixed cells. The cell is anchored to the ground and you move across it, so the natural trigger is distance. It fires when you have moved far enough past your serving cell&amp;rsquo;s reference point &lt;em&gt;and&lt;/em&gt; are close enough to the neighbour&amp;rsquo;s. For fixed cells those reference points are the same for every phone in the cell, so the network can configure it once for everyone.&lt;/p&gt;
&lt;h2 class="heading" id="t1-the-time-trigger"&gt;
 T1: the time trigger
 &lt;a href="#t1-the-time-trigger"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;T1 (&lt;code&gt;condEventT1&lt;/code&gt;) is the one I find most interesting, because it leans fully into deciding early. The network knows where its satellites will be, so it can work out the window during which you should switch and simply tell you. The phone watches its own clock and switches when the time falls in that window. There is no real-time round trip that might arrive late.&lt;/p&gt;
&lt;h2 class="heading" id="sib19-the-data-that-makes-it-work"&gt;
 SIB19: the data that makes it work
 &lt;a href="#sib19-the-data-that-makes-it-work"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;None of this works unless the phone knows where the satellites are and when things will happen. That is what SIB19 provides, a satellite-specific broadcast added in Release 17. It carries the ephemeris (the satellite&amp;rsquo;s position and path), the epoch time, the time at which the serving cell stops covering you (which feeds T1), and the cell&amp;rsquo;s reference location (which feeds D1). Without SIB19 the phone has nothing to check its conditions against.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The thing that stuck with me while reading about this is that the underlying idea is more general than satellites. The usual response to &amp;ldquo;the network cannot react fast enough&amp;rdquo; would be to try to react faster. Conditional handover does the opposite. It does the expensive work early, while there is still time, and then gives the phone a simple rule, a distance or a clock time, to check on its own.&lt;/p&gt;
&lt;p&gt;If you want the actual detail, it is in 3GPP TS 38.331, though it is not light reading.&lt;/p&gt;</description></item><item><title>Post-quantum cryptography, and what's actually shipping</title><link>https://kenan.sh/posts/post-quantum-crypto-tls/</link><pubDate>Mon, 30 Mar 2026 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/post-quantum-crypto-tls/</guid><description>&lt;p&gt;A few years ago I worked at a company building post-quantum cryptography, and it is an area I have kept half an eye on since. Going back through my notes recently, the part I find most interesting is not the theory so much as how much of it has actually shipped, and how much has not. This is really me writing that up to make sense of it, rather than any kind of authoritative guide.&lt;/p&gt;
&lt;h2 class="heading" id="the-threat-harvest-now-decrypt-later"&gt;
 The threat: harvest now, decrypt later
 &lt;a href="#the-threat-harvest-now-decrypt-later"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A sufficiently large quantum computer would break the maths behind most of the encryption in use today. The awkward part is that it does not need to exist yet to be a problem. Someone can record your encrypted traffic now and decrypt it once the hardware exists. So anything that needs to stay secret for a long time, like health records or state secrets, is arguably already at risk. That is why this is treated as urgent rather than a problem for 2035.&lt;/p&gt;
&lt;h2 class="heading" id="what-quantum-actually-breaks"&gt;
 What quantum actually breaks
 &lt;a href="#what-quantum-actually-breaks"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;There are really two jobs involved. Key exchange (RSA, elliptic curves) establishes a shared secret between two parties who have not met. Signatures (from the certificate authorities) prove you are talking to who you think you are. Both rely on problems that are hard for classical computers but tractable for a quantum computer running Shor&amp;rsquo;s algorithm, so both need replacing.&lt;/p&gt;
&lt;h2 class="heading" id="what-replaced-them"&gt;
 What replaced them
 &lt;a href="#what-replaced-them"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;NIST ran an open competition from 2016 to choose the replacements, with entrants trying to break each other&amp;rsquo;s submissions in public. Most of the winners are lattice-based, built on grid problems that stay hard even for quantum machines. If you read older write-ups, including my notes, the names can be confusing, because the finalists were standardised in 2024 under different names:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Kyber became ML-KEM (key exchange)&lt;/li&gt;
&lt;li&gt;Dilithium became ML-DSA (signatures)&lt;/li&gt;
&lt;li&gt;SPHINCS+ became SLH-DSA (signatures)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;They are the same algorithms with new names, so it is mostly a matter of translating as you read.&lt;/p&gt;
&lt;h2 class="heading" id="what-is-actually-shipping-hybrids"&gt;
 What is actually shipping: hybrids
 &lt;a href="#what-is-actually-shipping-hybrids"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The thing protecting your browser at the moment is not pure post-quantum. It is a hybrid called X25519MLKEM768, which combines the older X25519 elliptic-curve exchange with the new ML-KEM-768. You run both and combine the secrets, so you are safe as long as at least one of them holds. If the new lattice maths turns out to have a flaw nobody has found yet, the classical half still covers you. That caution is why hybrids were preferred over going straight to post-quantum, and they are now fairly widespread. A 2026 survey of tens of thousands of domains found that roughly half already support it.&lt;/p&gt;
&lt;h2 class="heading" id="the-part-that-has-been-left-alone-signatures"&gt;
 The part that has been left alone: signatures
 &lt;a href="#the-part-that-has-been-left-alone-signatures"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The number that stood out to me from the same survey was the share of those domains using post-quantum &lt;em&gt;certificates&lt;/em&gt;, which was zero.&lt;/p&gt;
&lt;p&gt;So the key exchange has been made quantum-resistant, which protects recorded traffic, but the signatures, the part that proves who you are talking to, are still fully classical. A future quantum attacker could not read an old session, but could forge a certificate and impersonate a server in real time. The reason is fairly practical rather than dramatic: post-quantum signatures are large and awkward. Some do not fit in a single network packet, some need fiddly floating-point maths, and some are simply slow. So the easier half was done first.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When I try to make sense of what all this means in practice, a few things stand out:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hybrid key exchange is the part that is ready today, and turning it on where a stack supports it costs very little.&lt;/li&gt;
&lt;li&gt;Long-lived secrets are the most exposed to harvest-now-decrypt-later, so they seem like the obvious thing to worry about first.&lt;/li&gt;
&lt;li&gt;The signature side is the part still largely unaddressed, and it is worth remembering that quantum-proofing the key exchange is not the same as being safe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the migration looks to be about half done. The encryption side has largely been handled, and the authentication side mostly has not.&lt;/p&gt;</description></item><item><title>What actually makes a developer "senior"?</title><link>https://kenan.sh/posts/senior-developer/</link><pubDate>Mon, 26 Jan 2026 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/senior-developer/</guid><description>&lt;p&gt;For a long time I assumed that being a &amp;ldquo;senior&amp;rdquo; developer mostly meant writing better code. By better I think I meant cleverer, or denser, the sort of thing that makes someone stop and work out how it does what it does. The longer I have worked on teams, the less I believe this.&lt;/p&gt;
&lt;p&gt;The senior code I appreciate most now is usually the boring code, the code I can read when I am tired and still understand. That is roughly the shift I want to talk about: seniority is less about the code itself, and more about keeping the codebase, and the people working in it, out of trouble later on. Most of the things below come back to that.&lt;/p&gt;
&lt;h2 class="heading" id="write-code-that-is-easy-to-read"&gt;
 Write code that is easy to read
 &lt;a href="#write-code-that-is-easy-to-read"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Code that other people can read without you sitting next to them is usually worth more than code that is clever. Machines will run almost anything, but people have to maintain it, so it makes sense to write for the people. When I get the urge to write a clever one-liner, it is usually worth stopping and asking whether the next person will actually thank me for it.&lt;/p&gt;
&lt;h2 class="heading" id="dont-say-something-is-done-until-it-is"&gt;
 Don&amp;rsquo;t say something is done until it is
 &lt;a href="#dont-say-something-is-done-until-it-is"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;&amp;ldquo;Basically done&amp;rdquo; is easy to say when the code compiles, the happy path works, and you are tired of looking at it. The problem is that someone will build on top of it, and the last ten percent you skipped ends up buried under their work. I would rather tell someone something is not finished yet than hand over something that looks finished but is not.&lt;/p&gt;
&lt;h2 class="heading" id="be-careful-about-turning-refactoring-into-a-ticket"&gt;
 Be careful about turning refactoring into a ticket
 &lt;a href="#be-careful-about-turning-refactoring-into-a-ticket"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;This is the one I feel most strongly about. When some code is a mess, the obvious &amp;ldquo;responsible&amp;rdquo; thing to do is raise a ticket to refactor it. In practice I think this often backfires. Once a refactor is a separate ticket with an estimate on it, it becomes one of the first things dropped when a sprint gets tight, because it does not ship anything a user can see.&lt;/p&gt;
&lt;p&gt;I have had more success folding that work into a feature that does ship. If I am already in the payments code for a feature, I will clean it up while I am there. The refactor travels with something visible, so it tends to actually get done.&lt;/p&gt;
&lt;h2 class="heading" id="be-clear-about-scope"&gt;
 Be clear about scope
 &lt;a href="#be-clear-about-scope"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You agree to do something, and then partway through it grows. &amp;ldquo;While you are in there, could you also&amp;hellip;&amp;rdquo;. Before long the thing you committed to is bigger than what you agreed to, and you can end up being held to a deadline you never really signed up for. Part of keeping the quality up, I think, is being honest about this when it happens, and saying plainly that the extra work is extra, rather than letting it quietly fold into the original estimate.&lt;/p&gt;
&lt;h2 class="heading" id="write-down-your-patterns-and-talk-before-adding-new-ones"&gt;
 Write down your patterns, and talk before adding new ones
 &lt;a href="#write-down-your-patterns-and-talk-before-adding-new-ones"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;It helps to write down the patterns your team uses. Not a big document, just enough: what the pattern is, why it exists, and where it is used. I also try not to quietly drop a new abstraction into a shared codebase and hope people pick it up on their own. It is usually better to talk it through with the team first, otherwise you have added something nobody really agreed to.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When I think about what makes someone senior, it is usually these things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They write code that is easy to read, even when they could write something cleverer.&lt;/li&gt;
&lt;li&gt;They are honest about when something is and is not finished.&lt;/li&gt;
&lt;li&gt;They keep structural work like refactoring attached to work that ships.&lt;/li&gt;
&lt;li&gt;They are clear about what they have agreed to do.&lt;/li&gt;
&lt;li&gt;They write their patterns down, and talk about new ones before adding them.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;None of this is really about years. I have worked with people a few years in who already think this way, and people much further along who do not. For me the change is when you stop asking &amp;ldquo;how do I write code that works&amp;rdquo; and start asking &amp;ldquo;how do I write code the team will still be glad of in six months&amp;rdquo;.&lt;/p&gt;</description></item><item><title>Does SOLID even apply to Go?</title><link>https://kenan.sh/posts/idiomatic-go-solid-design/</link><pubDate>Mon, 08 Dec 2025 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/idiomatic-go-solid-design/</guid><description>&lt;p&gt;Something I have wondered about for a while is whether SOLID actually means much in Go. The principles were written with class-based languages like Java and C++ in mind, and Go has neither classes nor inheritance, so it is a fair question whether they still apply or whether I am just repeating a Java-era idea out of habit.&lt;/p&gt;
&lt;p&gt;The view I have ended up with is that most of SOLID does still apply, but in Go it mostly reduces to one feature, the interface. If you understand small, implicitly satisfied interfaces, you have understood most of what people mean when they talk about idiomatic Go design.&lt;/p&gt;
&lt;h2 class="heading" id="the-main-rule-accept-interfaces-return-structs"&gt;
 The main rule: accept interfaces, return structs
 &lt;a href="#the-main-rule-accept-interfaces-return-structs"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;If there is one thing to take from this, it is this one.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Save accepts an interface, so it writes to anything with a Write&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// method: a file, a socket, or a bytes.Buffer in a test.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Writer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// NewStore returns a concrete *Store, not an interface. The caller&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// decides what to depend on, not you.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;NewStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;Store&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Save&lt;/code&gt; asks for the smallest thing it needs, an &lt;code&gt;io.Writer&lt;/code&gt;, so it behaves the same in production and in a test without knowing the difference. &lt;code&gt;NewStore&lt;/code&gt; returns the concrete type. If you return an interface here, you have decided for the caller what they are allowed to see, which is often not what they want. It is usually better to return the struct and let them narrow it down on their side if they need to.&lt;/p&gt;
&lt;h2 class="heading" id="solid-without-the-classes"&gt;
 SOLID without the classes
 &lt;a href="#solid-without-the-classes"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Taken one at a time, with the class machinery removed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single Responsibility&lt;/strong&gt; is really about package cohesion. Name packages for what they do (&lt;code&gt;net/http&lt;/code&gt;, &lt;code&gt;os/exec&lt;/code&gt;) rather than &lt;code&gt;util&lt;/code&gt; or &lt;code&gt;common&lt;/code&gt;, which tend to turn into dumping grounds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Open/Closed&lt;/strong&gt; happens through embedding rather than inheritance, which I will come to below.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Liskov Substitution&lt;/strong&gt; is basically interfaces. Two types are interchangeable if the caller cannot tell them apart, and in Go a type satisfies an interface implicitly by having the right methods, with no &lt;code&gt;implements&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interface Segregation&lt;/strong&gt; means depending only on the behaviour you use. If you only need to read, accept an &lt;code&gt;io.Reader&lt;/code&gt; rather than a larger &lt;code&gt;File&lt;/code&gt; type with fifteen methods.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dependency Inversion&lt;/strong&gt; means your logic depends on an interface, and the concrete implementation sits behind it. The aim is an import graph that is wide and flat rather than tall and narrow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Four of the five come down to using a small interface. That is not really a coincidence. Go put the useful part of SOLID into the language and left out most of the ceremony around it.&lt;/p&gt;
&lt;h2 class="heading" id="composition-instead-of-inheritance"&gt;
 Composition instead of inheritance
 &lt;a href="#composition-instead-of-inheritance"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The one that tends to catch people coming from Java is Open/Closed. There is no &lt;code&gt;extends&lt;/code&gt;, so you embed instead:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;prefix&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// embedded&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;addr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Because &lt;code&gt;Logger&lt;/code&gt; is embedded, &lt;code&gt;Server&lt;/code&gt; gets its &lt;code&gt;Log&lt;/code&gt; method promoted, so &lt;code&gt;srv.Log(&amp;quot;started&amp;quot;)&lt;/code&gt; works without writing a forwarding method or touching &lt;code&gt;Logger&lt;/code&gt;&amp;rsquo;s code. You have extended the behaviour without changing the original, which is what Open/Closed is asking for, done through composition.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When I am thinking about a design in Go, I keep coming back to a few things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Accept interfaces and return structs.&lt;/li&gt;
&lt;li&gt;Keep interfaces small, ideally a single method.&lt;/li&gt;
&lt;li&gt;Name packages for what they do, and avoid &lt;code&gt;util&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compose with embedding rather than reaching for inheritance that is not there.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I would not say SOLID is wrong in Go so much as mostly redundant. The language already pushes you towards small interfaces, and small interfaces are most of the point.&lt;/p&gt;</description></item><item><title>Which concurrency tool should you reach for in Go?</title><link>https://kenan.sh/posts/go-concurrency/</link><pubDate>Mon, 27 Oct 2025 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/go-concurrency/</guid><description>&lt;p&gt;A while ago I added a goroutine to a piece of code to make it faster, and I started getting a different result each time I ran it. Not on every run, maybe one run in five, which made it harder to track down. It was a race condition, and it is a reasonable illustration of what makes concurrency awkward: the difficult part is not doing several things at once, it is doing several things at once and still being sure of the result.&lt;/p&gt;
&lt;p&gt;Go gives you a few tools for this, and most of the work is picking the right one. This is roughly the order I reach for them.&lt;/p&gt;
&lt;h2 class="heading" id="concurrency-is-not-the-same-as-parallelism"&gt;
 Concurrency is not the same as parallelism
 &lt;a href="#concurrency-is-not-the-same-as-parallelism"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;It is worth separating the two. Parallelism is running things at the same time. Concurrency is structuring a program as independent pieces that &lt;em&gt;could&lt;/em&gt; run at the same time. One is about execution and the other is about structure. If you get the structure right, so that the independent pieces do not share memory, then giving the program more cores tends to speed it up without changing the result.&lt;/p&gt;
&lt;p&gt;The idea underneath this is Communicating Sequential Processes: write each piece as ordinary sequential code, and have the pieces communicate through channels rather than sharing state. If nothing is shared, there is nothing to fight over.&lt;/p&gt;
&lt;h2 class="heading" id="channels-are-usually-the-default"&gt;
 Channels are usually the default
 &lt;a href="#channels-are-usually-the-default"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A channel passes a value from one goroutine to another. One side puts a value in, the other takes it out.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;jobs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;chan&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;go&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;jobs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// signal &amp;#34;no more values&amp;#34;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;range&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;jobs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;got&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The send &lt;code&gt;jobs &amp;lt;- i&lt;/code&gt; blocks until something is ready to receive, and the &lt;code&gt;range&lt;/code&gt; blocks until there is something to take. They meet, hand over one value, and carry on. Neither goroutine touches the other&amp;rsquo;s memory, so there is nothing to corrupt. The &lt;code&gt;close&lt;/code&gt; is how the &lt;code&gt;range&lt;/code&gt; loop knows when to stop. One thing to watch is that sending on a closed channel panics, so you should only ever close a channel from the sending side.&lt;/p&gt;
&lt;h2 class="heading" id="select-for-more-than-one-channel"&gt;
 select for more than one channel
 &lt;a href="#select-for-more-than-one-channel"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When you have more than one channel, &lt;code&gt;select&lt;/code&gt; handles whichever is ready. The part I find most useful is that it lets you avoid waiting forever:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;select&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="nx"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;got&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;case&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;After&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;gave up waiting&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If a job arrives within two seconds you handle it, and if it does not, &lt;code&gt;time.After&lt;/code&gt; fires and you move on instead of blocking. I have used that pattern to deal with a lot of &amp;ldquo;why is this stuck&amp;rdquo; situations.&lt;/p&gt;
&lt;h2 class="heading" id="when-channels-do-not-fit-mutexes"&gt;
 When channels do not fit: mutexes
 &lt;a href="#when-channels-do-not-fit-mutexes"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Channels work well until you have genuinely shared state, like a cache or a counter that a lot of goroutines all need to update. You can model that with channels, but you tend to end up with one goroutine &amp;ldquo;owning&amp;rdquo; the state while everything else queues to talk to it, which is really just a lock with extra steps. In that case it is simpler to use an actual lock:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;mu&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Mutex&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Inc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mu&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;defer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mu&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Lock&lt;/code&gt; takes ownership and makes everything else wait, and the deferred &lt;code&gt;Unlock&lt;/code&gt; always releases it, including if the function panics. It is worth remembering that a mutex is effectively a queue, so the longer you hold it, the longer everything behind it waits.&lt;/p&gt;
&lt;h2 class="heading" id="atomics-when-even-a-mutex-is-too-much"&gt;
 Atomics, when even a mutex is too much
 &lt;a href="#atomics-when-even-a-mutex-is-too-much"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;For a single integer that is updated often enough that a mutex becomes a problem, there is &lt;code&gt;sync/atomic&lt;/code&gt;. These operations map to single CPU instructions, so there is no lock involved:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int64&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddInt64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// safe increment, no lock&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadInt64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// safe read&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I would treat this as a last resort rather than a default. It only works on integer types, and while you can build more complicated things out of &lt;code&gt;atomic.CompareAndSwap&lt;/code&gt;, it gets difficult quickly. If you are reaching for atomics, it is worth measuring first, because a lot of &amp;ldquo;lock-free&amp;rdquo; code has a mutex underneath it anyway.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When I am deciding which of these to use, I roughly work down a list:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reach for channels first, and use &lt;code&gt;select&lt;/code&gt; to coordinate them, so that you avoid shared state where you can.&lt;/li&gt;
&lt;li&gt;When you do have genuinely shared state, such as a cache, a registry or a hot counter, use a mutex.&lt;/li&gt;
&lt;li&gt;Only drop to atomics for a single hot integer, and only once you have measured a reason to.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Most of the time you do not need to go very far down that list.&lt;/p&gt;</description></item><item><title>How should you structure a Go package?</title><link>https://kenan.sh/posts/go-package-layout/</link><pubDate>Mon, 15 Sep 2025 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/go-package-layout/</guid><description>&lt;p&gt;Every Go project I have worked on that grew past a few thousand lines eventually ran into the same question: where does this file actually belong? If you get it wrong early it tends to make the rest of the codebase harder to work in. The layout below is the one that has held up best for me, and it is based on Ben Johnson&amp;rsquo;s standard package layout.&lt;/p&gt;
&lt;h2 class="heading" id="layouts-that-tend-not-to-work"&gt;
 Layouts that tend not to work
 &lt;a href="#layouts-that-tend-not-to-work"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A few common approaches that I have seen cause problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;One large package.&lt;/strong&gt; This avoids circular imports, but it becomes hard to navigate as it grows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grouping by function&lt;/strong&gt; (all the handlers in one place, all the controllers in another). You end up with repetitive names like &lt;code&gt;controller.UserController&lt;/code&gt;, and the layers often start importing each other.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grouping by module.&lt;/strong&gt; This tends to run into the same problems in a slightly different form.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 class="heading" id="the-root-package-holds-the-domain-types"&gt;
 The root package holds the domain types
 &lt;a href="#the-root-package-holds-the-domain-types"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Put your domain types, meaning the plain data and the interfaces that describe how they are used, in the root package, and try to keep that package from depending on any of your other packages.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// package myapp: the root. Pure domain, no dependencies on your own code.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;myapp&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;interface&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;User&lt;/code&gt; here is just data. &lt;code&gt;UserService&lt;/code&gt; describes what you can do with a user without saying anything about &lt;em&gt;how&lt;/em&gt; it is done. There is no database or HTTP in here. The point is to keep the centre of the application free of that detail, so that everything else depends inward on it.&lt;/p&gt;
&lt;h2 class="heading" id="sub-packages-grouped-by-their-dependency"&gt;
 Sub-packages grouped by their dependency
 &lt;a href="#sub-packages-grouped-by-their-dependency"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Each sub-package acts as an adapter between the clean domain and one external thing, and it implements the domain&amp;rsquo;s interfaces:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// package postgres: implements myapp.UserService against a real database.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;postgres&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DB&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;myapp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// query the db, build and return a *myapp.User&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;All of the Postgres-specific code lives in one place. If you later want to swap it for SQLite, or an in-memory version for tests, you write another package that satisfies the same &lt;code&gt;myapp.UserService&lt;/code&gt; interface, and the domain does not change. That separation is most of the reason to do this, and it is what makes the code straightforward to test.&lt;/p&gt;
&lt;h2 class="heading" id="a-shared-mock-package"&gt;
 A shared mock package
 &lt;a href="#a-shared-mock-package"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Because everything communicates through the domain interfaces, you can keep a &lt;code&gt;mock&lt;/code&gt; package of fake implementations. Tests use those instead of spinning up a real database, which keeps them fast and focused on the logic rather than the infrastructure.&lt;/p&gt;
&lt;h2 class="heading" id="main-wires-everything-together"&gt;
 main wires everything together
 &lt;a href="#main-wires-everything-together"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; package is the only place that knows which real adapters to use. It imports the concrete packages, connects them to the domain, and starts the application. It is the one place allowed to know about everything, and keeping that knowledge in a single place is part of why the rest stays clean.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The short version:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Domain types go in the root package, and it depends on none of your other packages.&lt;/li&gt;
&lt;li&gt;Group sub-packages by the dependency they wrap, with each one implementing a domain interface.&lt;/li&gt;
&lt;li&gt;Keep a shared &lt;code&gt;mock&lt;/code&gt; package for tests.&lt;/li&gt;
&lt;li&gt;Let &lt;code&gt;main&lt;/code&gt; be the only place that wires the real implementations together.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I would also avoid a &lt;code&gt;util&lt;/code&gt; package. If it is hard to describe what a package is for in a short phrase, that is usually a sign it is doing too many things.&lt;/p&gt;</description></item><item><title>When to use named returns in Go?</title><link>https://kenan.sh/posts/named-returns-golang/</link><pubDate>Mon, 11 Aug 2025 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/named-returns-golang/</guid><description>&lt;p&gt;Recently while reviewing a PR addressing some linting issues, a colleague of mine pointed out that we had enabled the &lt;code&gt;nonamedreturns&lt;/code&gt; linter rule in our project. This rule is part of the &lt;code&gt;golangci-lint&lt;/code&gt; tool and it checks for named return values in functions.&lt;/p&gt;
&lt;p&gt;This prompted a discussion about when, if ever, it is appropriate to use named return values in Go.&lt;/p&gt;
&lt;h2 class="heading" id="what-are-named-returns"&gt;
 What are named returns?
 &lt;a href="#what-are-named-returns"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;In Golang, when defining a function, you can &amp;ldquo;name&amp;rdquo; the return values. This means that you can specify the names of the return variables in the function signature. For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// This is a &amp;#34;naked return&amp;#34; - it automatically returns &amp;#39;sum&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example, &lt;code&gt;sum&lt;/code&gt; is a named return value. The function &lt;code&gt;add&lt;/code&gt; returns the sum of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, and the return value is named &lt;code&gt;sum&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You may also notice that the &lt;code&gt;return&lt;/code&gt; statement does not specify a value. This is because the named return value &lt;code&gt;sum&lt;/code&gt; is automatically returned when the function exits.&lt;/p&gt;
&lt;p&gt;There are plenty of examples in the Go standard library where named returns are used, for example the &amp;lsquo;context&amp;rsquo; package &lt;a href="https://pkg.go.dev/context#WithCancel"&gt;&lt;code&gt;WithCancel&lt;/code&gt;&lt;/a&gt; function uses named returns:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;WithCancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cancel&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;CancelFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, even within the same package, you will find functions that do not use named returns, such as the &lt;a href="https://pkg.go.dev/context#WithDeadline"&gt;&lt;code&gt;WithDeadline&lt;/code&gt;&lt;/a&gt; function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;WithDeadline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;CancelFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Ultimately, I came to this conclusion when reviewing the code.&lt;/p&gt;
&lt;h2 class="heading" id="prefer-small-functions-with-clear-return-types"&gt;
 Prefer small functions with clear return types
 &lt;a href="#prefer-small-functions-with-clear-return-types"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The ideal function should be small and have a clear purpose. If the function is small enough, the return types should be self-explanatory, and named returns may not be necessary.&lt;/p&gt;
&lt;p&gt;For example, consider this function which does something and returns an error:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Do something&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// or an error if something went wrong&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This function is straightforward, and the return type is clear. There is no need for named returns here.&lt;/p&gt;
&lt;p&gt;If the function is responsible for doing some action, and returns a single and an error, the function name or some function docstring should indicate what it is returning:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// getSomething retrieves something and returns it along with an error if it fails.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;getSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Get something and return it&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;something&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// or an error if something went wrong&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="where-multiple-returns-are-needed-use-structs"&gt;
 Where multiple returns are needed, use structs
 &lt;a href="#where-multiple-returns-are-needed-use-structs"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When a function has multiple return values, especially if they are not self-explanatory, it is often better to use a struct to encapsulate the return values. This makes the code more readable and maintainable.&lt;/p&gt;
&lt;p&gt;For example, instead of returning multiple values like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;getSomeItems&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Get some items and return them&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;item1&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;item2&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// or an error if something went wrong&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can define a struct to encapsulate the return values:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;getSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Get something and return it&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;item1&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;item2&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// or an error if something went wrong&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This approach makes it clear what each return value represents, and it allows for easier expansion in the future if more fields need to be added.&lt;/p&gt;
&lt;h2 class="heading" id="when-to-use-named-returns"&gt;
 When to use named returns
 &lt;a href="#when-to-use-named-returns"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Where structs are not appropriate, due to adiditonal complexity or where the function is small enough, named returns can be used. However, it is important to avoid &amp;ldquo;naked returns&amp;rdquo; (using &lt;code&gt;return&lt;/code&gt; without specifying the return values).&lt;/p&gt;
&lt;p&gt;For example, if you have a function that retrieves a value and a boolean indicating whether it exists, you might write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// getAndExists retrieves a value and indicates whether it exists.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;getAndExists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Retrieve the value and set ok to true if it exists&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;some value&amp;#34;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Dont do naked returns, explicitly return what you want&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This way, the named return values are clear, and the function is still small and easy to understand.&lt;/p&gt;
&lt;h2 class="heading" id="conclusion"&gt;
 Conclusion
 &lt;a href="#conclusion"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;In conclusion, when deciding when I should use named returns in Go, I consider the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the function is small and the return types are self-explanatory, I prefer not to use named returns.&lt;/li&gt;
&lt;li&gt;If the function has multiple return values that are not self-explanatory, I prefer to use a struct to encapsulate the return values.&lt;/li&gt;
&lt;li&gt;If named returns are used, I avoid naked returns and explicitly return the named values.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Generally, we as software engineers should try to make sure that our code is simple, easy to read and understand, and maintainable. Named returns can be a useful tool in achieving this, however naked returns can often make the code less clear and harder to maintain.&lt;/p&gt;
&lt;p&gt;Therefore, I prefer to use named returns sparingly and only when they add clarity to the code.&lt;/p&gt;</description></item><item><title>Certified Kubernetes Administrator Notes</title><link>https://kenan.sh/posts/cka-notes/</link><pubDate>Tue, 16 Jan 2024 00:00:00 +0000</pubDate><guid>https://kenan.sh/posts/cka-notes/</guid><description>&lt;h1 class="heading" id="section-1-core-concepts"&gt;
 Section 1: Core Concepts
 &lt;a href="#section-1-core-concepts"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="cluster-components"&gt;
 Cluster Components
 &lt;a href="#cluster-components"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Kubernetes allows you to think of any number of servers as a single virtual machine. It abstracts the complexities of the infrastructure and how it connects. It hosts your apps in containers. Consists of nodes which host containers. Worker nodes will host the apps as containers and master nodes will manage plan and schedule nodes (control plane)&lt;/p&gt;
&lt;h2 class="heading" id="control-plane-components"&gt;
 Control Plane Components
 &lt;a href="#control-plane-components"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="etcd"&gt;
 etcd
 &lt;a href="#etcd"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Distributed key-value database which kubernetes uses to store it’s objects. Cluster forms with an elected leader and the followers Before any value is written to the cluster, they agree on that value then persist it, this is known as &lt;strong&gt;consensus&lt;/strong&gt;. They coordinate using the RAFT protocol&lt;/p&gt;
&lt;h3 class="heading" id="api-server"&gt;
 API Server
 &lt;a href="#api-server"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;The Kubernetes API exposes an API endpoint which allows users and cluster components to communicate with each other. It lets you query and manipulate the state of API objects in etcd, ensuring data is valid when retrieved or placed in the database. The API handles authentication through RBAC.&lt;/p&gt;
&lt;h3 class="heading" id="controller-manager"&gt;
 Controller Manager
 &lt;a href="#controller-manager"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;The controller manager contains all the control loops Kubernetes needs to function. A controller is a control loop that watches the shared state of the cluster through the apiserver and makes changes attempting to move the current state towards the desired state. For example you have the as the endpoint controller, service account controller, replication controller, node controller&lt;/p&gt;
&lt;h3 class="heading" id="scheduler"&gt;
 Scheduler
 &lt;a href="#scheduler"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;A scheduler watches for newly created pds which are not yet scheduled to a node. It then becomes responsible
for finding the best node to schedule that pod on. The default scheduler filters nodes according to scheduling requirements. The scheduler is then left with feasible nodes to schedule the pods onto. The scheduler then assigns a score to the feasible nodes based on scheduling policies and picks the one with the highest ranking&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Policies&lt;/strong&gt; allow you to configure predicates for filtering and priorities for scoring&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Profiles&lt;/strong&gt; allow you to configure plugins that implement different stages such as: QueueSort, Filter, Score, Bind, Reserve, Permit&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 class="heading" id="node-components"&gt;
 Node Components
 &lt;a href="#node-components"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="kubelet"&gt;
 kubelet
 &lt;a href="#kubelet"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Primary node agent which runs on each node. It will register the node with the API server and is responsible for taking in PodSpecs (through the API server, static pod, http endpoint or http requests) and ensuring that the containers defined in those PodSpecs are healthy and running by communicating with the container runtime.&lt;/p&gt;
&lt;h3 class="heading" id="kube-proxy"&gt;
 Kube Proxy
 &lt;a href="#kube-proxy"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Network proxy which implements part of the services concept. They maintain network roles on the nodes which allow network communication to and from Pods. It utilises the OS packet filtering layer if there is one (iptables) or it forwards the packets themself.&lt;/p&gt;
&lt;h3 class="heading" id="container-runtime"&gt;
 Container Runtime
 &lt;a href="#container-runtime"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Software which is responsible for running containers. This can be containerd, &lt;code&gt;CRI-O&lt;/code&gt; or &lt;code&gt;Docker&lt;/code&gt;.&lt;/p&gt;
&lt;h2 class="heading" id="pods"&gt;
 Pods
 &lt;a href="#pods"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Pods are the smallest addressable units for kubernetes. It is a group of one or more containers which shares storage and network resources and specs on how the containers should be run.&lt;/p&gt;
&lt;p&gt;To create pods via the kubernetes client, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl run &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt; --image&lt;span class="o"&gt;={&lt;/span&gt;image&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# run directly&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl run &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt; --image&lt;span class="o"&gt;={&lt;/span&gt;image&lt;span class="o"&gt;}&lt;/span&gt; -o&lt;span class="o"&gt;=&lt;/span&gt;yaml --dry-run&lt;span class="o"&gt;=&lt;/span&gt;client &lt;span class="c1"&gt;# Get YAML definition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can edit a Pod’s image using the &lt;code&gt;kubectl edit&lt;/code&gt; command. Pods can take time to delete. To delete a pod quickly then run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl delete pod &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt; --grace-period&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt; --force
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="replica-sets"&gt;
 Replica Sets
 &lt;a href="#replica-sets"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A ReplicaSet’s purpose is to maintain a stable set of replica Pods. It’s often used to guarantee the availability of a specified number of identical pods. They have a selector which defines which pods it will acquire. ReplicaSets create and delete Pods as needed to reach the specified number of replicas. They create these pods using the specified Pod specification.&lt;/p&gt;
&lt;p&gt;Replica Sets are linked to Pods via the &lt;code&gt;metadata.OwnerReferences&lt;/code&gt; field. This field specifies what resource the Pod is owned by. It will acquire any pods that match the selector and whose OwnerReferences are empty.&lt;/p&gt;
&lt;p&gt;Any change that is made to a ReplicaSet only takes effect after the pods have been deleted.&lt;/p&gt;
&lt;h2 class="heading" id="deployments"&gt;
 Deployments
 &lt;a href="#deployments"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A Kubernetes Deployment provides declarative updates for Pods and ReplicaSets. The Deployment Controller changes the actual state to the desired state at a controlled rate.&lt;/p&gt;
&lt;h3 class="heading" id="create-a-deployment"&gt;
 Create a Deployment
 &lt;a href="#create-a-deployment"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;To create deployments via the kubernetes client, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create deployment --image&lt;span class="o"&gt;=&lt;/span&gt;nginx --replicas&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; nginx
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="namespaces"&gt;
 Namespaces
 &lt;a href="#namespaces"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A Namespace provides a mechanism for isolating groups of resources in a single cluster. They allow you to separate resources between teams/projects
To create a namespace via the kubernetes client, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create namespace &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To access a resource from another namespace use: &lt;code&gt;[resource-name].[namespace].[type].cluster.local&lt;/code&gt;&lt;/p&gt;
&lt;h2 class="heading" id="services"&gt;
 Services
 &lt;a href="#services"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them. The set of Pods targeted by a Service is usually determined by a selector.&lt;/p&gt;
&lt;h3 class="heading" id="cluster-ip-service"&gt;
 Cluster IP Service
 &lt;a href="#cluster-ip-service"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;A ClusterIP Service assigns an IP address the serviceCIDR, an address range that is different from nodes and Pods. Exists only for load balancing purposes and for the routing rules that are written to disk by iptables. Packets match the rule for the service and are rewritten to go where they need to.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl expose &lt;span class="o"&gt;[&lt;/span&gt;resource&lt;span class="o"&gt;]&lt;/span&gt; --port&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;80&lt;/span&gt; --target-port&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;8080&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="nodeport-service"&gt;
 NodePort Service
 &lt;a href="#nodeport-service"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Similar to the ClusterIP except it has an additional port in the range of of 30000 and 32767 that used to reach the Service from outside the cluster. The port is exposed from all nodes.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl expose &lt;span class="o"&gt;[&lt;/span&gt;resource&lt;span class="o"&gt;]&lt;/span&gt; --type&lt;span class="o"&gt;=&lt;/span&gt;NodePort --port&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;80&lt;/span&gt; &lt;span class="c1"&gt;#Random Nodeport&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="loadbalancer-service"&gt;
 LoadBalancer Service
 &lt;a href="#loadbalancer-service"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Builds on top of the node port service, has an IP and a port like Cluster IP and an external port like NodePort but when you request a service of type LoadBalancer a real load balancer is automatically attached to the port. Every cloud provider attaches its own load balancer.&lt;/p&gt;
&lt;h3 class="heading" id="service-discovery"&gt;
 Service Discovery
 &lt;a href="#service-discovery"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;When you create a service an entry is added to the DNS entries in the cluster. There’s a component in the control plane of your Kubernetes cluster whose only job is to watch for new and old Service and add their entry to the DNS.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Entries follow &lt;code&gt;name-of-the-service.the-namespace.svc.cluster.local&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;If the service is in the same namespace you can drop most of that and just use the name. If not you could use &lt;code&gt;name-of-the-service.the-namespace&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 class="heading" id="section-2-scheduling"&gt;
 Section 2: Scheduling
 &lt;a href="#section-2-scheduling"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="manual-scheduling"&gt;
 Manual Scheduling
 &lt;a href="#manual-scheduling"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Setting the &lt;code&gt;spec.nodeName&lt;/code&gt; will bind the Pod to the selected Node. If this can&amp;rsquo;t happen then the scheduler will look for the best node to schedule a pod in.&lt;/p&gt;
&lt;h2 class="heading" id="labels-and-selectors"&gt;
 Labels and Selectors
 &lt;a href="#labels-and-selectors"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You can decorate pods with key value pairs, these can be used for a multitude of reasons. They can be used for filtering pod output in the kubectl command with the &lt;code&gt;--selector&lt;/code&gt; flag. Adding a selector to the services, deployments and Replica sets will allow it to point to a logical set of pods with the same label.&lt;/p&gt;
&lt;h2 class="heading" id="taints-and-tolerations"&gt;
 Taints and Tolerations
 &lt;a href="#taints-and-tolerations"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Taints allow a node to repel a set of pods. Tollerations applied to pods allow (but not require) them to schedule on that node. They ensure that pods cannot be scheduled on inappropriate nodes.
The Taint effect decides what happens to pods which do not tolerate the taint:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NoSchedule&lt;/code&gt; means that pods should not be scheduled on the node&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PreferNoSchedule&lt;/code&gt; means the scheduler should try to avoid scheduling on that node, but can if it has to&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NoExecute&lt;/code&gt; means new pods won’t be scheduled and existing pods should be evicted if they do not tolerate the taint.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To taint a node run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl taint nodes &amp;lt;node name&amp;gt; &lt;span class="nv"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;value1: taint-effect
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl taint nodes node1 &lt;span class="nv"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;value1:NoSchedule &lt;span class="c1"&gt;# Apply a taint&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl taint nodes node1 &lt;span class="nv"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;value1:NoSchedule- &lt;span class="c1"&gt;# Remove a taint&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A Tolleration can be added to a pod by placing the key, value and taint effect in the &lt;code&gt;spec.tolerations&lt;/code&gt; section of the Pod yaml file.&lt;/p&gt;
&lt;h2 class="heading" id="affinity-and-anti-affinity"&gt;
 Affinity and Anti-Affinity
 &lt;a href="#affinity-and-anti-affinity"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Affinities are a more capable version of node selectors. They allow more complex operations such as Exists, NotIn and NotExists and allows you to use anti-affinities which define where a pod should not be scheduled.&lt;/p&gt;
&lt;p&gt;There are two types of affinity available:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;requiredDuringSchedulingIgnoredDuringExecution&lt;/code&gt; - The affinity must be adhered to or the pod can&amp;rsquo;t be scheduled&lt;/li&gt;
&lt;li&gt;&lt;code&gt;preferedDuringSchedulingIgnoredDuringExecution&lt;/code&gt; - The affinity is preferred but the pod may be scheduled elsewhere if it can&amp;rsquo;t be met.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For &lt;strong&gt;nodes&lt;/strong&gt; you must first label the node. This can be done imperatively using the &lt;code&gt;kubectl label node&lt;/code&gt; command. From there you are able to alter the spec.affinity field of a Pod template.&lt;/p&gt;
&lt;p&gt;Like nodes, &lt;strong&gt;Pods&lt;/strong&gt; also have affinities, they work in the same ways as node affinities and are used to tell the scheduler to schedule pods based on other Pods’ labels.&lt;/p&gt;
&lt;p&gt;Pods can have anti-affinities, which makes sure a Pod is not scheduled where other pods match the labels written. This can be used for instance to only have one pod of the same type run on each node&lt;/p&gt;
&lt;h2 class="heading" id="resource-limits"&gt;
 Resource Limits
 &lt;a href="#resource-limits"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The Scheduler takes into account the resources of a pod before scheduling. If no node can take a pod then the pod is pending with a relevant error. You can limit these. If you go over CPU it will try to throttle, if over RAM then it will be terminated. CPU goes as low and 1m. 1 CPU is 1 vCPU.&lt;/p&gt;
&lt;p&gt;Kubernetes assumes that each pod has a resource request of 0.5cpu, 256 mB of RAM. These can be changed in the definition file. If the kubelet has to eject pods, it assumes that any pod without requests has gone over their quota and ejects them first&lt;/p&gt;
&lt;h2 class="heading" id="daemonsets"&gt;
 DaemonSets
 &lt;a href="#daemonsets"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Similar to ReplicaSets but runs one copy of a pod on each node. Even when a new node is added or deleted. Used for monitoring solutions and log viewers. &lt;code&gt;kube-proxy&lt;/code&gt; is put as a daemonset.&lt;/p&gt;
&lt;h2 class="heading" id="static-pods"&gt;
 Static Pods
 &lt;a href="#static-pods"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The kubelet of each node is able to function on its own. You can configure it to read Pod definition files from a static file path, by default this is &lt;code&gt;/etc/kubernetes/manifests&lt;/code&gt; (this can be changed in kubelet service file).&lt;/p&gt;
&lt;p&gt;Each Node’s kubelet will check this directory periodically and make any missing Pods, modify existing Pods, delete Pods whose manifests are no longer there and ensure all existing manifest pods are still running.&lt;/p&gt;
&lt;p&gt;The Kubernetes API considers these pods part of the cluster and will create a mirror object so it can still be seen by the user&lt;/p&gt;
&lt;h2 class="heading" id="multiple-schedulers"&gt;
 Multiple Schedulers
 &lt;a href="#multiple-schedulers"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Kubernetes is highly extensible. You are able to deploy your own scheduler as either the default scheduler or alongside the default scheduler. You have to create a &lt;code&gt;ConfigMap&lt;/code&gt; which contains a config file as specified to to differentiate it from the &lt;code&gt;kube-scheduler&lt;/code&gt; which takes the name &lt;code&gt;default-scheduler&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;--leader-elect&lt;/code&gt; flag is used to choose a leader to lead scheduling activities, this is used when there are multiple copies of the scheduler running on multiple nodes (high availability). You should set this as false on the custom scheduler or set lock object will set the name to differentiate it during election&lt;/p&gt;
&lt;h1 class="heading" id="section-3-logging-and-monitoring"&gt;
 Section 3: Logging and Monitoring
 &lt;a href="#section-3-logging-and-monitoring"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="monitor-cluster-components"&gt;
 Monitor Cluster Components
 &lt;a href="#monitor-cluster-components"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;It is often necessary to know Node, Resource and Pod metrics. Kubernetes doesn’t natively support this and so you need to deploy one yourself. The metric server (replacing heapster) can be deployed to provide metric data to the API. One metric server per cluster. It receives from each Pod and puts it in memory. The kubelet contains a container Advisor (cAdvisor) this will take the performance metrics from the container and expose it via the API&lt;/p&gt;
&lt;p&gt;To deploy the metric server, clone from GitHub and deploy using &lt;code&gt;kubectl apply -f&lt;/code&gt; then you can get the info by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl top &lt;span class="o"&gt;[&lt;/span&gt;resource&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl top nodes &lt;span class="c1"&gt;# Get node metrics&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl top pod nginx &lt;span class="c1"&gt;# Get specific pod metrics&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="application-logs"&gt;
 Application Logs
 &lt;a href="#application-logs"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You can collect the logs of a container simply. You can either go through the container runtime, for example &lt;code&gt;crictl logs&lt;/code&gt; for docker containers. Or you could go through the kubernetes api and run &lt;code&gt;kubectl logs&lt;/code&gt;. If the pod contains more than one container then you must specify the container name as well as the pod name&lt;/p&gt;
&lt;h1 class="heading" id="section-4-application-lifecycle-monitoring"&gt;
 Section 4: Application Lifecycle Monitoring
 &lt;a href="#section-4-application-lifecycle-monitoring"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="rolling-updates-and-rollbacks"&gt;
 Rolling Updates and Rollbacks
 &lt;a href="#rolling-updates-and-rollbacks"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You are able to perform rolling upgrades and rollbacks on Deployments, ReplicaSets and Daemonsets. This allows a simple mechanism to change images of a deployment without causing any downtime.&lt;/p&gt;
&lt;p&gt;The replica set of the initial image gets set, incrementally, to 0 while the other replica set spawns its pods. The pods are created and health checked while the old ones are being deleted. It ensures that only a certain number of Pods are down while they are being updated. By default, it ensures that at least 75% up and that only a certain number of Pods are created above the desired number of Pods. By default 125%&lt;/p&gt;
&lt;p&gt;You can change the image with&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl &lt;span class="nb"&gt;set&lt;/span&gt; image deployment/nginx-deployment &lt;span class="nv"&gt;nginx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nginx:1.16.1 &lt;span class="c1"&gt;#Perform Update&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl rollout status deployment/nginx-deployment &lt;span class="c1"&gt;# See Rollout Status&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Deployments can also have the &lt;code&gt;spec.strategy&lt;/code&gt; type Recreate which, unlike RollingUpdate, will bring all the old pods down before starting new ones.&lt;/p&gt;
&lt;h3 class="heading" id="rollback"&gt;
 Rollback
 &lt;a href="#rollback"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;You are able to rollback any rolling updates which have been done. By default, all of the Deployment&amp;rsquo;s rollout history is kept in the system so that you can rollback anytime you want&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl rollout &lt;span class="nb"&gt;history&lt;/span&gt; deployment/nginx-deployment &lt;span class="c1"&gt;# Get rollout history&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl rollout &lt;span class="nb"&gt;history&lt;/span&gt; deployment/nginx-deployment --revision&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;# Specific revision&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl rollout undo deployment/nginx-deployment &lt;span class="c1"&gt;# Rollback to previous&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl rollout undo deployment/nginx-deployment --to-revision&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;# Specific rollback&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="commands-and-arguments"&gt;
 Commands and Arguments
 &lt;a href="#commands-and-arguments"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You are able to define commands and arguments to pass into a kubernetes pod. The spec.containers.command field is written as a list starting with the entrypoint (binary to run) and any arguments passed to it. You can split the arguments into their own separate field spec.containers.args or keep them placed in the command list.&lt;/p&gt;
&lt;p&gt;The command field in kubernetes overwrites the command which may be specified in the container image. To run a command in shell, you can use:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-yaml" data-lang="yaml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/bin/sh&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;-c&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;while true; do echo hello; sleep 10;done&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="environment-variables"&gt;
 Environment Variables
 &lt;a href="#environment-variables"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You are able to pass environment variables into a kubernetes pod. These environment variables can be read by the individual containers. You can pass environment variables from:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;name-value pairs&lt;/strong&gt; - These come in the from of name value pairs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ConfigMaps&lt;/strong&gt; - Values from config maps can be passed as environment variables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secrets&lt;/strong&gt; - Decoded values from secrets can also be passed into pods via environment variables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can use the whole secret or configMap in the environment by using envFrom in place of env and referring to the secret or ConfigMap.&lt;/p&gt;
&lt;h2 class="heading" id="configmaps"&gt;
 ConfigMaps
 &lt;a href="#configmaps"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;ConfigMaps are used to store non-confidential data in key-value pairs. Pods are able to use config maps in environment variables, command line args or in the form of configuration files.
You can create a config map using kubectl:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create configmap &amp;lt;name&amp;gt; --from-literal&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;key&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;value&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create configmap &amp;lt;name&amp;gt; --from-file&lt;span class="o"&gt;=&lt;/span&gt;filename
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create configmap &amp;lt;name&amp;gt; --from-env-file&lt;span class="o"&gt;=&lt;/span&gt;filename
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="secrets"&gt;
 Secrets
 &lt;a href="#secrets"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Secrets are used to store sensitive data such as passwords, tokens or keys. Secrets much like ConfigMaps can be used as files, environment variables or are used by kubelet when pulling images from private registries.&lt;/p&gt;
&lt;p&gt;A secret is only sent to a node if a pod on that node requires it. The kubelet stores the secret into a tmpfs so that the secret is not written to disk storage. Once the Pod that depends on the secret is deleted, kubelet will delete its local copy of the secret data as well.&lt;/p&gt;
&lt;p&gt;You can create a secret using kubectl:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s1"&gt;&amp;#39;value&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; base64 &lt;span class="o"&gt;(&lt;/span&gt;--decode &lt;span class="k"&gt;for&lt;/span&gt; decoding&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create secret generic &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="c1"&gt;#SAME AS CONFIG MAP]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Kubectl create secret &lt;span class="o"&gt;[&lt;/span&gt;type&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;options&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="multi-container-pods"&gt;
 Multi Container Pods
 &lt;a href="#multi-container-pods"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Pods are designed to support multiple containers which form a cohesive unit. The containers in a Pod are automatically co-located and co-scheduled on the same physical or virtual machine in the cluster.&lt;/p&gt;
&lt;p&gt;The containers can share resources and dependencies, communicate with one another, and coordinate when and how they are terminated.&lt;/p&gt;
&lt;h2 class="heading" id="init-containers"&gt;
 Init Containers
 &lt;a href="#init-containers"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;At times you may want to run a process that runs to completion in a container. For example a process that pulls code or binary from a repository that will be used by the main application.Or a process that waits for an external service or database to be up before the actual application starts.&lt;/p&gt;
&lt;p&gt;You can configure multiple initContainers, in that case each init container is run one at a time in sequential order.&lt;/p&gt;
&lt;p&gt;If any of the initContainers fail to complete, Kubernetes restarts the Pod repeatedly until the Init Container succeeds.&lt;/p&gt;
&lt;h1 class="heading" id="section-5-cluster-maintenance"&gt;
 Section 5: Cluster Maintenance
 &lt;a href="#section-5-cluster-maintenance"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="os-upgrades"&gt;
 OS Upgrades
 &lt;a href="#os-upgrades"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When upgrading the operating system of a node, the node will be inaccessible for a while. Because of this the pods running on that node must be scheduled on other nodes in the cluster.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can remove the pods from a node so that the workloads will be evenly distributed across the other remaining nodes: &lt;code&gt;kubectl drain &amp;lt;node name&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Then the node will become unschedulable. Once the update has finished and the node is back online, the node can be marked as schedulable by running &lt;code&gt;kubectl uncordon &amp;lt;node name&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;To simply make the node unschedulable, without evicting pods, run &lt;code&gt;kubectl cordon &amp;lt;node name&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can only delete a pod which is managed by a ReplicaSet. Any pod which has been scheduled outside of this and is forcefully drained will be lost forever.&lt;/p&gt;
&lt;h2 class="heading" id="cluster-upgrade-process"&gt;
 Cluster Upgrade Process
 &lt;a href="#cluster-upgrade-process"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Components can be at any version the same or lower than the release version of the API. &lt;code&gt;kubectl&lt;/code&gt; can be one above the same or below.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;kubeadm&lt;/code&gt; binary has a way of upgrading, you can run &lt;code&gt;kubeadm upgrade&lt;/code&gt; plan to plan the upgrade versions and &lt;code&gt;kubeadm upgrade&lt;/code&gt; apply to apply that upgrade. You can also upgrade each component on its own&lt;/p&gt;
&lt;p&gt;You should upgrade master first then worker node. Use apt to update &lt;code&gt;kubeadm&lt;/code&gt; first upgrade the node then upgrade &lt;code&gt;kubelet&lt;/code&gt; to the right version&lt;/p&gt;
&lt;h2 class="heading" id="backup-and-restore-methods"&gt;
 Backup and Restore Methods
 &lt;a href="#backup-and-restore-methods"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Backing up the resources, etcd database and persistent volumes is an important task. It is essential that, if something goes wrong, the cluster can be salvaged. A primitive way to upgrade is to collect all the resources into one YAML file and store this somewhere&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl get all --all-namespaces -o yaml &amp;gt; all-services.yaml
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="etcd-backup"&gt;
 etcd Backup
 &lt;a href="#etcd-backup"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;The etcd database is separately able to be backed up. A snapshot of the etcd database can be taken and stored for when it is needed. When it comes time to restore the ectd database, a change to the etcd pod volume mount can be made to point to the data-dir provided to the ectd backup command.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;ETCDCTL_API&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; etcdctl --endpoints&lt;span class="o"&gt;=&lt;/span&gt;https://&lt;span class="o"&gt;[&lt;/span&gt;127.0.0.1&lt;span class="o"&gt;]&lt;/span&gt;:2379 &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;--cacert&lt;span class="o"&gt;=&lt;/span&gt;/etc/kubernetes/pki/etcd/ca.crt &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;--cert&lt;span class="o"&gt;=&lt;/span&gt;/etc/kubernetes/pki/etcd/server.crt &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;--key&lt;span class="o"&gt;=&lt;/span&gt;/etc/kubernetes/pki/etcd/server.key snapshot save /opt/snapshot-pre-boot.db 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You must pass the endpoint, cacert, cert and key flags into the ectd command so that it is able to properly communicate with the etcd database. You can then restore from this snapshot using the &lt;code&gt;snapshot restore&lt;/code&gt; command and setting the &lt;code&gt;data-dir&lt;/code&gt; flag to specify the directory to store the etcd data&lt;/p&gt;
&lt;h1 class="heading" id="section-6-security"&gt;
 Section 6: Security
 &lt;a href="#section-6-security"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="security-primitives"&gt;
 Security Primitives
 &lt;a href="#security-primitives"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Kubernetes uses users, certificates, external authentication with LDAP and service accounts for access to the API server. Once gaining access to the API server, what a user can do is determined by their role.&lt;/p&gt;
&lt;p&gt;All Kubernetes components are secured by TLS certificates. Network policies are used to secure communication between hosts.&lt;/p&gt;
&lt;h3 class="heading" id="certificate-details"&gt;
 Certificate Details
 &lt;a href="#certificate-details"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;It may be important to view the details of a certificate which has been generated either by the user or by the cluster provisioning tool. You are able to decode a certificate by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;openssl x509 -in /etc/kubernetes/pki/apiserver.crt -text -noout
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="certificate-api"&gt;
 Certificate API
 &lt;a href="#certificate-api"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="users"&gt;
 Users
 &lt;a href="#users"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes has two classes of users: normal users and service accounts. Kubernetes does not have objects which represent users and assumes there is some cluster independent way of storing users (keys, keystores or plain text).&lt;/p&gt;
&lt;p&gt;Any user which presents a valid certificate signed by the CA is considered authenticated. Kubernetes determines the username from the CN field (CN=ted).&lt;/p&gt;
&lt;h3 class="heading" id="certificates"&gt;
 Certificates
 &lt;a href="#certificates"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes heavily uses certificate authentication. Most components use their own certificate pairs for authentication.&lt;/p&gt;
&lt;h3 class="heading" id="client-generation"&gt;
 Client Generation
 &lt;a href="#client-generation"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;To generate client certificate and keys, you must create a certificate signing request. Adding group user &lt;code&gt;SYSTEM:MASTERS&lt;/code&gt; for admin privileges.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;openssl genrsa -out admin.key &lt;span class="m"&gt;2048&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;openssl req -new -key admin.key -subj &lt;span class="s2"&gt;&amp;#34;/CN=ted /O=system:masters&amp;#34;&lt;/span&gt; -out admin.csr
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="certificate-api-1"&gt;
 Certificate API
 &lt;a href="#certificate-api-1"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes allows you to sign the Certificate signing requests created above to add the users to the cluster. You need to first create a CertificateSigningRequest object. NOTE: the csr must be base64 encoded.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cat file.csr &lt;span class="p"&gt;|&lt;/span&gt; base64 -w &lt;span class="m"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;apiVersion: certificates.k8s.io/v1beta1 kind: CertificateSigningRequest
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;metadata:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; name: jane
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;spec:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; groups:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - system:authenticated
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; usages:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - digital signature
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - key encipherment
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - server auth
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; request:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;{&lt;/span&gt;BASE &lt;span class="m"&gt;64&lt;/span&gt; ENCODING GOES HERE&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can approve the request by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl certificate approve &lt;span class="o"&gt;{&lt;/span&gt;csr&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You then can collect the certificate by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl get csr &lt;span class="o"&gt;{&lt;/span&gt;name&lt;span class="o"&gt;}&lt;/span&gt; -o yaml
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;then decoding the certificate with &lt;code&gt;echo {certificate} | base64 —decode&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The signing is handled by the &lt;code&gt;kube-controller-manager&lt;/code&gt;.&lt;/p&gt;
&lt;h2 class="heading" id="kubeconfig"&gt;
 Kubeconfig
 &lt;a href="#kubeconfig"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;You are able to specify the server, client-key and certificate and ca as flags. Instead you are able to move these configurations into a file called a kubeconfig. The default file can be found at &lt;code&gt;~/.kube/config&lt;/code&gt; or you can specify with the kubeconfig flag&lt;/p&gt;
&lt;p&gt;The KubeConfig contains 3 sections:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Clusters&lt;/strong&gt; - Information regarding the cluster you wish to access such as name, certificate authority and api server URL&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Users&lt;/strong&gt; - The user which can access the cluster, this often contains the name, client cert and key&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contexts&lt;/strong&gt; - Combination of user and certificate. You can switch contexts using kubectl so you don&amp;rsquo;t have to keep multiple KubeConfigs. You can specify a default context by adding a current context field.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl config view --kubeconfig&lt;span class="o"&gt;={&lt;/span&gt;Specify&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# View a kubeconfig&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl config use-context &lt;span class="o"&gt;{&lt;/span&gt;context&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# Switch Context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="role-based-access-control"&gt;
 Role Based Access Control
 &lt;a href="#role-based-access-control"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Role-based access control (RBAC) is a method of regulating access to computer or network resources based on the roles of individual users within your organisation.&lt;/p&gt;
&lt;p&gt;The RBAC API declares four kinds of Kubernetes objects: Role, ClusterRole, RoleBinding and ClusterRoleBinding.&lt;/p&gt;
&lt;h3 class="heading" id="roles"&gt;
 Roles
 &lt;a href="#roles"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Roles are namespaced objects which represent a set of additive permissions. You can create a role by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create role pod-reader --verb&lt;span class="o"&gt;=&lt;/span&gt;get --verb&lt;span class="o"&gt;=&lt;/span&gt;list --verb&lt;span class="o"&gt;=&lt;/span&gt;watch --resource&lt;span class="o"&gt;=&lt;/span&gt;pods
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="role-binding"&gt;
 Role Binding
 &lt;a href="#role-binding"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;A role binding grants the permissions from a role to a user or set of users. It holds a list of users, groups, or service accounts, and a reference to the role being granted. You can create a role binding by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create rolebinding admin --role&lt;span class="o"&gt;=&lt;/span&gt;admin --user&lt;span class="o"&gt;=&lt;/span&gt;user1 --user&lt;span class="o"&gt;=&lt;/span&gt;user2 --group&lt;span class="o"&gt;=&lt;/span&gt;group1 --service-account&lt;span class="o"&gt;=&lt;/span&gt;svact
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="cluster-roles"&gt;
 Cluster Roles
 &lt;a href="#cluster-roles"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Cluster roles are roles which apply on the cluster level rather than on a namespace level. You can create a cluster role by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create clusterrole pod-reader --verb&lt;span class="o"&gt;=&lt;/span&gt;get,list,watch --resource&lt;span class="o"&gt;=&lt;/span&gt;pods
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="cluster-role-binding"&gt;
 Cluster Role Binding
 &lt;a href="#cluster-role-binding"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Cluster role bindings are the same as role bindings, but they bind cluster role permissions to a user or set of users:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create clusterrolebinding cluster-admin --clusterrole&lt;span class="o"&gt;=&lt;/span&gt;cluster-admin --user&lt;span class="o"&gt;=&lt;/span&gt;user1 --user&lt;span class="o"&gt;=&lt;/span&gt;user2 --group&lt;span class="o"&gt;=&lt;/span&gt;group1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="check-access"&gt;
 Check Access
 &lt;a href="#check-access"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;To check if you have permissions to do something then you can run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl auth can-i &amp;lt;command&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As an admin you can impersonate a user you can use the same command with the &lt;code&gt;--as &amp;lt;user&amp;gt;&lt;/code&gt; flag.&lt;/p&gt;
&lt;h3 class="heading" id="service-accounts"&gt;
 Service Accounts
 &lt;a href="#service-accounts"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;A service account provides an identity for pods to authenticate with the API server. Assigning roles to service accounts allow you to manage the access pods have to the API.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create serviceaccount NAME
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 class="heading" id="image-security"&gt;
 Image Security
 &lt;a href="#image-security"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;All images are by default pulled from DockerHub. You can provide the name of the repository in the same way as pulling from docker (username/image:version). For Kubernetes if the name of the user is the same as the app you can just provide the name, for example just nginx rather than nginx/nginx. You can also specify another registry in the form registry.io/path/to/app&lt;/p&gt;
&lt;h3 class="heading" id="private-repo"&gt;
 Private Repo
 &lt;a href="#private-repo"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;To use an image from a private repository. You have to provide the full path to the private repo. You pass the credentials into a kubernetes secret object of type docker-registry. In the pod definition file, you must specify the secret name in the &lt;code&gt;spec.imagePullSecrets&lt;/code&gt;.&lt;/p&gt;
&lt;h2 class="heading" id="security-contexts"&gt;
 Security Contexts
 &lt;a href="#security-contexts"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;A security context defines privilege and access control settings for a Pod or Container. You are able to specify security in the container level and pod level. If both are specified then it will take the container security. The security context settings include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Linux Capabilities&lt;/li&gt;
&lt;li&gt;UserID and GroupID&lt;/li&gt;
&lt;li&gt;Privilege Escalation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 class="heading" id="network-policy"&gt;
 Network Policy
 &lt;a href="#network-policy"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;NetworkPolicies are an application-centric construct which allow you to specify specify a set of ingress and egress rules defining how a pod is allowed to communicate with various network &amp;ldquo;entities&amp;rdquo;. The entities that a Pod can communicate with are identified through a combination of the following 3 identifiers: Pods, Namespaces and IP blocks&lt;/p&gt;
&lt;p&gt;There are two sorts of isolation for a pod: isolation for egress, and isolation for ingress. By default, a pod is non-isolated for egress and ingress. They are enforced by the type of network solution that is used, flannel does not support it but Kube-router, Calico, Romana, Wave-net do. No error, it just ignores them if they fail.&lt;/p&gt;
&lt;h1 class="heading" id="section-7-storage"&gt;
 Section 7: Storage
 &lt;a href="#section-7-storage"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="pre-requisites"&gt;
 Pre-Requisites
 &lt;a href="#pre-requisites"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="docker-storage"&gt;
 Docker Storage
 &lt;a href="#docker-storage"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Docker stores all its data in &lt;code&gt;var/lib/docker&lt;/code&gt;. Files in the image layer are read only and the files in the container layer are read and write. If you want to edit a file from the image layer it is copied to the container layer. This is called copy on write&lt;/p&gt;
&lt;p&gt;You can mount a folder from the host to a docker container and point a folder in that container to it or a volume from the var/lib/docker/volumes folder. Docker’s default &lt;strong&gt;volume driver&lt;/strong&gt; plugin is local and it helps to mount a volume to the docker host. Can specify the driver (such as AUFS, ZFS) using &lt;code&gt;--volume-driver&lt;/code&gt;&lt;/p&gt;
&lt;h3 class="heading" id="container-storage-interface"&gt;
 Container Storage Interface
 &lt;a href="#container-storage-interface"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Universal standard for storage, defines a set of remote procedure calls which can be called by the orchestrator. When a volume is needed it should run a create volume call and pass all the information that it needed (specified by the driver).&lt;/p&gt;
&lt;h2 class="heading" id="persistent-volumes"&gt;
 Persistent Volumes
 &lt;a href="#persistent-volumes"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Normally when a pod is created, any files which change within it are deleted when the pod is deleted. You are able to attach a volume to help persist the data, this could be a hostPath pointing to a directory or file on the host machine. This can be difficult as that folder has to exist on the node where the pod is running.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Persistent volumes&lt;/strong&gt; provide a large pool of data and allows users to make claims on part of the data that they want for their pods. You can use any storage solution (same as normal volumes) including cloud storage.&lt;/p&gt;
&lt;p&gt;Kubernetes binds a persistent volume to a &lt;strong&gt;PersistentVolumeClaim&lt;/strong&gt; based on sufficient capacity, such as access mode, volume mode and storage class.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can specify a label for the volume and selectors for the claims.&lt;/li&gt;
&lt;li&gt;The claim must have the same access modes
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ReadWriteOnce&lt;/strong&gt; means the volume can be mounted as read-write by a single node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ReadOnlyMany&lt;/strong&gt; means the volume can be mounted as read-only by many nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ReadWriteMany&lt;/strong&gt; means the volume can be mounted as read-write by many nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ReadWriteOncePod&lt;/strong&gt; means the volume can be mounted as read-write by a single Pod.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;You can get a big volume being used up by a smaller one as it is one to one mapping so no other pod can use it.&lt;/li&gt;
&lt;li&gt;When you delete a PVC the volume is either &lt;strong&gt;deleted&lt;/strong&gt;, &lt;strong&gt;recycled&lt;/strong&gt; (data deleted before being made available again) or &lt;strong&gt;retained&lt;/strong&gt; (default). You can set it by setting the persistentVolumeReclaimPolicy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can then use the persistent volume inside of a pod, mounting it in a similar fashion to a normal Volume.&lt;/p&gt;
&lt;h2 class="heading" id="storage-classes"&gt;
 Storage Classes
 &lt;a href="#storage-classes"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Allows dynamic provisioning in a cloud for storage. These classes can be used in volumes and persistent volumes instead of HostPath for instance. Binding can be immediate(default) or can be &lt;code&gt;WaitForFirstConsumer&lt;/code&gt; (This will delay the binding and provisioning of a PersistentVolume until a Pod using the PersistentVolumeClaim is created.)&lt;/p&gt;
&lt;h1 class="heading" id="section-8-networking"&gt;
 Section 8: Networking
 &lt;a href="#section-8-networking"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="networking-pre-requisites"&gt;
 Networking Pre-Requisites
 &lt;a href="#networking-pre-requisites"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="switching"&gt;
 Switching
 &lt;a href="#switching"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Switching is the act of sending packets between interfaces on a network switch. The Linux software equivalent of a switch is a bridge. Packets are not by default sent between interfaces in Linux, you can change this by enabling IP forwarding&lt;/p&gt;
&lt;h3 class="heading" id="routing"&gt;
 Routing
 &lt;a href="#routing"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Routing refers to how network packets are routed so they can reach their destination. For example if the packet is destined to a machine on the local network, then the packet can be easily sent to the local machine. However if the IP is unrecognised, the IP should be sent to the default gateway (often a router). The router consults the routing table to forward the packet to the relevant machine&lt;/p&gt;
&lt;h3 class="heading" id="domain-name-system"&gt;
 Domain Name System
 &lt;a href="#domain-name-system"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Domain Name System (DNS) allows domain names (&lt;a href="https://www.google.com"&gt;www.google.com&lt;/a&gt;) to be resolved into IP addresses (8.8.8.8). The DNS server will resolve the domain name to the correct IP of google.&lt;/p&gt;
&lt;p&gt;On Linux you are able to do basic DNS resolution using the &lt;code&gt;/etc/hosts&lt;/code&gt; file. It uses name resolution to substitute the name specified with the relevant IP address. This does cause issues as this is only local to the machine. It does not copy over to other machines on the network&lt;/p&gt;
&lt;p&gt;A DNS server effectively stores a hosts file on a server, and every time you wish to resolve a name your machine will check the server and get the IP in return. In Linux you can set the DNS server in the &lt;code&gt;/etc/resolv.conf&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;You can add multiple DNS Nameservers in the resolv file and this will allow a local server which contains resolutions in the local network to be queried first then a public DNS server such as google to be queried next which contains the public internet. The etc hosts file takes precedence, change this in &lt;code&gt;/etc/switch.conf&lt;/code&gt;&lt;/p&gt;
&lt;h4 class="heading" id="domain-names"&gt;
 Domain Names
 &lt;a href="#domain-names"&gt;#&lt;/a&gt;
&lt;/h4&gt;
&lt;p&gt;Domain names are separated by dots. The top level is the right most part of the name such as com and org are specifying the intent of the website. The middle name is the name of the site. subdomains such as www and maps and blogs help further divide stuff. You can have caches in DNS.&lt;/p&gt;
&lt;h4 class="heading" id="dns-records"&gt;
 DNS Records
 &lt;a href="#dns-records"&gt;#&lt;/a&gt;
&lt;/h4&gt;
&lt;p&gt;There are multiple record types in a DNS server. These define what is being stored by the DNS server.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A name records map names to IPv4 IP addresses&lt;/li&gt;
&lt;li&gt;AAAA records map names to IPv6 addresses&lt;/li&gt;
&lt;li&gt;CNAME records maps one name to another&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 class="heading" id="namespaces-1"&gt;
 Namespaces
 &lt;a href="#namespaces-1"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Used to implement network isolation generally in container runtimes, The network namespace isolates the arp table, routing and interfaces in the container from the hosts. Containers can see its own processes which it has created but no processes created by or on the host. This is the process namespace.&lt;/p&gt;
&lt;h4 class="heading" id="useful-commands"&gt;
 Useful Commands
 &lt;a href="#useful-commands"&gt;#&lt;/a&gt;
&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip netns add &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# Create Network Namespace&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip netns &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;name&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;command&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# Execute in the namespace&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip link add br0 &lt;span class="nb"&gt;type&lt;/span&gt; bridge &lt;span class="c1"&gt;# Create bridge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip link add veth-1 &lt;span class="nb"&gt;type&lt;/span&gt; veth peer name veth-2 &lt;span class="c1"&gt;# Create veth pair&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip link &lt;span class="nb"&gt;set&lt;/span&gt; veth-1 netns ns1 &lt;span class="c1"&gt;# Assign veth pair end to namespace&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip link &lt;span class="nb"&gt;set&lt;/span&gt; veth-2 master br0 &lt;span class="c1"&gt;# Assign other end to bridge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip a add &lt;span class="c1"&gt;# Add IP address&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ip link &lt;span class="nb"&gt;set&lt;/span&gt; veth-1 up &lt;span class="c1"&gt;# Bring Veth Pair Up&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;iptables -t nat -A POSTROUTING -s &lt;span class="o"&gt;[&lt;/span&gt;IP OF VETH NETWORK&lt;span class="o"&gt;]&lt;/span&gt; -j MASQURADE &lt;span class="c1"&gt;# Enable access to outside world (must add default route to container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;iptables -t nat -A PREROUTING --dport &lt;span class="o"&gt;[&lt;/span&gt;port on host&lt;span class="o"&gt;]&lt;/span&gt; --to-destination &lt;span class="o"&gt;[&lt;/span&gt;port to access&lt;span class="o"&gt;]&lt;/span&gt; -j DNAT &lt;span class="c1"&gt;# Allow public access&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 class="heading" id="docker-networking"&gt;
 Docker Networking
 &lt;a href="#docker-networking"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Docker networking is slightly different from other container networking. When you run a docker container you can specify one of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--network none&lt;/code&gt; - no interfaces, no access to outside or other network&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--network host&lt;/code&gt; - bound to the host networking&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--network bridge&lt;/code&gt; - internal private network (172.17.0.1/24) bound to a bridge (like the network namespaces)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Docker creates a bridge network in docker and a docker0 interface. They have a similar network namespace technique as before. IP is assigned as 172.16.0.1/24. docker inspect allows you to find the namespace which was created. It creates an veth pair between the host and the container mastered by the docker0 bridge&lt;/p&gt;
&lt;p&gt;You can map ports on docker using the &lt;code&gt;-p&lt;/code&gt; flag which adds an iptables rule to the host linking from the containers open port to the host port which was specified.&lt;/p&gt;
&lt;h3 class="heading" id="container-network-interfaces"&gt;
 Container Network Interfaces
 &lt;a href="#container-network-interfaces"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;rkt, mesos, docker and other container runtimes are all solving the same issue in very similar ways. CNI is a standard that describes how plugins should be developed and how CRIs should invoke them.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It aims to provide a plugin orientated networking solution for containers.&lt;/li&gt;
&lt;li&gt;It consists of specifications and libraries for writing networking plugins for Linux containers.&lt;/li&gt;
&lt;li&gt;It only deals with network connectivity and garbage collection of resources when containers are deleted.
There are two central definitions, namely a container and a network.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Container&lt;/strong&gt; - Synonymous with a network namespace, single addressable unit&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network&lt;/strong&gt; - Uniquely addressable group of entities that can communicate with one and another.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;CRIs are responsible for: creating the network namespace, identifying the network the container must attach to, invoking the CNI plugin with the ADD to add a container and DEL to delete the container and provide a JSON format of the network configuration&lt;/p&gt;
&lt;p&gt;The network plugin should support command line arguments (ADD,DEL,CHECK), support parameters, container id, network ns etc, Must manage IP address assignment to PODs. Must return results in specific format. CNI comes with default plugins: bridge, vlan, ipvlan, macvlan, windows, dhcp and host-local plugins&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Calico&lt;/strong&gt; - Network plugin for CNI, which manages a flat layer 3 network, assigning each workload a fully relatable IP address. For any overlay requirements it can implement IP to IP tunnelling or can use flannel.&lt;/p&gt;
&lt;h4 class="heading" id="docker"&gt;
 Docker
 &lt;a href="#docker"&gt;#&lt;/a&gt;
&lt;/h4&gt;
&lt;p&gt;Docker does not support CNI plugins, instead opting for their own slightly different CNM standards. To use CNI plugins on a docker container you need to create the container which the none network type and invoke the CNI plugin directly&lt;/p&gt;
&lt;h3 class="heading" id="cluster-networking"&gt;
 Cluster Networking
 &lt;a href="#cluster-networking"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;A kubernetes network should be a flat network that spans several nodes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Containers can talk to any other container in the network&lt;/strong&gt;, and there’s no translation of addresses in the process — i.e. no NAT is involved&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nodes in the cluster can talk to any other container in the network and vice-versa&lt;/strong&gt;. Even in this case, there’s no translation of addresses — i.e. no NAT&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A container’s IP address is always the same&lt;/strong&gt;, independently if seen from another container or itself.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Each node has separate IPs and MAC addresses&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 class="heading" id="cni-in-kubernetes"&gt;
 CNI In Kubernetes
 &lt;a href="#cni-in-kubernetes"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;h3 class="heading" id="pod-networking"&gt;
 Pod Networking
 &lt;a href="#pod-networking"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes has no built-in solution for pod networking. Kubernetes however expects any network plugin ensure that each pod should:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;have a unique IP address,&lt;/li&gt;
&lt;li&gt;be able to communicate with every other pod on the same node,&lt;/li&gt;
&lt;li&gt;be able to communicate with every other pod on other nodes without NAT.
This can be achieved manually:&lt;/li&gt;
&lt;li&gt;Create a bridge on each host (add IP address and bring up interface)&lt;/li&gt;
&lt;li&gt;Attach the network namespaces of containers to a bridge on each host (via veth pairs).&lt;/li&gt;
&lt;li&gt;Bring the interfaces up and add addresses.&lt;/li&gt;
&lt;li&gt;Add routing to the hosts to ensure multi nodes can communicate. or add to a router.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is effectively a CNI ADD command. Kubelet is configured to look at the cni-config-path (&lt;code&gt;/etc/cni/net.d/&lt;/code&gt; by default) when creating pods, which has been configured as a flag, it will find a config file here telling it which CNI plugin to run (if multiple, use the first in alphabetical order). Kubelet will look for the binary in the cni-bin-dir flag (&lt;code&gt;/opt/cni/bin&lt;/code&gt; by default) and run it with the add command providing the container id and namespace as arguments.&lt;/p&gt;
&lt;h3 class="heading" id="ip-address-management-ipam"&gt;
 IP Address Management (IPAM)
 &lt;a href="#ip-address-management-ipam"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;It is the responsibility of the CNI plugin to assign the IP addresses to the pods. Kubernetes doesn&amp;rsquo;t care how it is done as long as Pods have IPs and that they are unique.&lt;/p&gt;
&lt;p&gt;CNI comes with two built-in plugins (host local or dhcp) to help with this. The type which is used is defined in the cni config file.&lt;/p&gt;
&lt;h3 class="heading" id="weaveworks-plugin"&gt;
 Weaveworks Plugin
 &lt;a href="#weaveworks-plugin"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Once you have multiple kubernetes nodes, the manual addition of bridges and veth pairs simply will not scale. WeaveWorks is one of many CNI plugins which can be used to solve the networking issue.&lt;/p&gt;
&lt;p&gt;Weaveworks deploys an agent on each node, The agents communicate with each other and store a topology of the cluster so they know the IPs and amount of nodes, pods etc. WeaveWorks creates their own bridge and assigns their own IPs to them, it makes sure that the pods get the right route and can communicate with other pods.&lt;/p&gt;
&lt;p&gt;Weave intercepts packets going to a different node and it encapsulates the packet before sending it to the other node, the agent on the other node can then collect the packet, decapsulate it and route it to the right place.&lt;/p&gt;
&lt;p&gt;Weave can be deployed as pods or as a service. If you already have a cluster setup then you can use a simple kubectl command and it will deploy all the necessary components including the agents which are deployed as a Daemonset.&lt;/p&gt;
&lt;p&gt;## Service Networking
Pods very rarely communicate with each-other, often applications communicate with each other with the use of services. Services are not bound to a single node, they are available on every node meaning that a pod on any node can successfully communicate with a service.&lt;/p&gt;
&lt;p&gt;Pods are able to access services either via the IP address of the service or the name of the service. Services are a virtual object, the IP is not bound to any interface and only used as a reference for the rules.&lt;/p&gt;
&lt;p&gt;Each node runs an application called kube-proxy, which watches for changes from the kube-api server. When a new service is created it is responsible for assigning an ip from a predefined range (defined in the api server, shouldnt overlap with pod range) and is responsible for setting the rules which route the service’s IP:port to the pod ip.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;kube-proxy&lt;/code&gt; can use userspace (listens and routes itself), &lt;code&gt;iptables&lt;/code&gt; or &lt;code&gt;ipvs&lt;/code&gt; rules and this is set by the &lt;code&gt;--proxy-mode&lt;/code&gt; flag (iptables by default). To view the rules created by the kube-proxy run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;iptables -L -t nat &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;name-of-service&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;## DNS and CoreDNS&lt;/p&gt;
&lt;h3 class="heading" id="dns-in-kubernetes"&gt;
 DNS in Kubernetes
 &lt;a href="#dns-in-kubernetes"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes deploys a built-in DNS cluster if using &lt;code&gt;kubeadm&lt;/code&gt;. When a service is created, the DNS server will map the name of the service to the ip address of the service, meaning any pod in the cluster can now access the service by name (if the same namespace) or by name.namespace (if in a different namespace).&lt;/p&gt;
&lt;p&gt;Services are grouped together as sub domains of the namespace then grouped together further in a subdomain defining their type (svc) and then further grouped together into a root domain for the whole cluster (by &lt;code&gt;default cluster.local&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Records for pods are not created by default, this can be enabled in the DNS config file. The name of the dns record is the IP of the pod but with the dots replaced with dashes. The name follows the service convention, with type set to pod rather than svc.&lt;/p&gt;
&lt;h3 class="heading" id="coredns"&gt;
 CoreDNS
 &lt;a href="#coredns"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Post 1.12 a CoreDNS server is deployed as a Deployment (2 replicas) on the Kubernetes cluster. Kubernetes defines a config file for coredns in a config map mounted to the &lt;code&gt;/etc/coredns/Corefile&lt;/code&gt; file of the pod. This file defines plugins for the DNS server. The kubernetes plugin allows it to work with the kubernetes cluster.&lt;/p&gt;
&lt;p&gt;CoreDNS is exposed using a service, the IP of which is written to the pod’s &lt;code&gt;/etc/resolv.conf&lt;/code&gt; file by the kubelet, the file also has a search entry allowing for services to be looked up by just their names/name.namespace.&lt;/p&gt;
&lt;h2 class="heading" id="ingres-networking"&gt;
 Ingres Networking
 &lt;a href="#ingres-networking"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Normally when you deploy an application into kubernetes you tend to deploy the deployment and some services like ClusterIP or Nodeport. You can even use load balancers in cloud environments.&lt;/p&gt;
&lt;p&gt;This is difficult to scale because of the proxy servers needed to ensure that all routes still work. That is where ingress comes in.&lt;/p&gt;
&lt;p&gt;Ingress deploys a supported controller and resources, A controler effectively acts as an intelligent proxy server. routing traffic to services. You can create a controller with nginx for example. The Ingress resources can then be used.&lt;/p&gt;
&lt;p&gt;The default backend is where things get routed by default. You can set the http path to do for example /api. or you can use the hosts which allows different domains or subdomains. If you don&amp;rsquo;t specify a host it assumes *&lt;/p&gt;
&lt;p&gt;Ingress needs to be in the same namespace as the service, can create multiple ingress resources spanning namespaces:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl create ingress &amp;lt;ingress-name&amp;gt; --rule&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;host/path=service:port&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 class="heading" id="section-9-kubernetes-cluster-installation-and-design"&gt;
 Section 9: Kubernetes Cluster Installation and Design
 &lt;a href="#section-9-kubernetes-cluster-installation-and-design"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="design-a-kubernetes-cluster"&gt;
 Design a Kubernetes Cluster
 &lt;a href="#design-a-kubernetes-cluster"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;There are many different tools to deploy Kubernetes clusters for multiple use cases, for example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Minikube or Kubeadm for testing/learning purposes&lt;/li&gt;
&lt;li&gt;Kubeadm can be used for development purposes.&lt;/li&gt;
&lt;li&gt;For production you need Highly available with multiple master nodes with multiple worker nodes also. This can be done using Kubeadm or cloud providers offerings.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For on prem, the kubeadm tool is very useful, in addition cloud providers have relevant tools should you need to use a public cloud. You can use physical or virtual machines. A minimum of 4 nodes for production is preferred. The best practice is to have a dedicated master node. Etcd can be separated from the master node also and placed on its own server for better redundancy.&lt;/p&gt;
&lt;h3 class="heading" id="choosing-infrastructure"&gt;
 Choosing Infrastructure
 &lt;a href="#choosing-infrastructure"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Kubernetes can be deployed in many ways. You can go from binaries or you can use a tool. Kubeadm is only supported on linux. Minikube (provisions VMs) can be used for a pain free experience while kubeadm(requires VMs provisions) allows more versatility&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Turnkey Tools&lt;/strong&gt; - Provision VMs yourself and use a tool to deploy Kubernetes (e.g OpenShift, Cloud Foundry Container Runtime and Vagrant)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hosted Tools&lt;/strong&gt; - Kubernetes as a service (e.g Google Container Engine (GKE), EKS (Amazon))&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 class="heading" id="highly-available-kubernetes-cluster"&gt;
 Highly Available Kubernetes Cluster
 &lt;a href="#highly-available-kubernetes-cluster"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;When you lose a master the workers can run on their own but you have no access to the cluster through the api server etc.&lt;/p&gt;
&lt;p&gt;High availability provides redundancies for all the components in your cluster, by duplicating components across nodes, so that if one node goes down the cluster wont go down (master, workers etc)&lt;/p&gt;
&lt;p&gt;The api-server works on one request at a time, so multiple api server instances can be working at the same time. Should use a load balancer for the master nodes so you can point the kubectl to the IP of the load balancer and have the traffic balanced across the nodes.&lt;/p&gt;
&lt;p&gt;The controller manager and scheduler, run in parallel so they may duplicate actions. They run in an active standby method. They elect a leader, on startup, they try to gain a lock by updating their info in an endpoint file. The first one to do it gets the lease. This is checked as defined by flags.&lt;/p&gt;
&lt;h3 class="heading" id="etcd-in-high-availability"&gt;
 ETCD in High Availability
 &lt;a href="#etcd-in-high-availability"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Etcd can be stacked in the master (Easier to manage but if one node goes down then loses a member). The etcd server can be separated from the master nodes (additional server). API server needs to point to both the instances.&lt;/p&gt;
&lt;p&gt;When data is written or read, etcd makes sure that the data is consistent on all instances. ectd does not process the writes on all nodes. They elect a leader and the elected leader is responsible for processing the writes and ensures that the data is distributed to other nodes.&lt;/p&gt;
&lt;p&gt;A write is complete if it can be written on the majority of nodes. If a node is offline when write is done then as long as the etcd cluster achieves quorum (N/2 + 1) then the write can be complete. If a node comes back online then the write is propagated to it.&lt;/p&gt;
&lt;p&gt;ectd uses RAFT protocol. RAFT uses a random timer to initiate requests from ectd members to become a leader. Once a node is the leader they periodically send info saying that they are still assuming the role. If a node goes down another node assumes the role of the leader.&lt;/p&gt;
&lt;p&gt;Even numbers of etcd members means that the cluster is more likely to fail if there is network segmentation so odd numbers of etcd members have better chances to stay alive. 5 over 6 etc&lt;/p&gt;
&lt;h2 class="heading" id="instal-using-kubeadm"&gt;
 Instal using Kubeadm
 &lt;a href="#instal-using-kubeadm"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Kubeadm is a tool provided by kubernetes to help set up a kubernetes cluster following the best practices.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must first provision the machines needed for the cluster you are building (single node, 1 master 2 workers etc)&lt;/li&gt;
&lt;li&gt;Install a Container Runtime Interface (docker, containerd etc)&lt;/li&gt;
&lt;li&gt;Install the kubeadm, kubelet and kubectl binaries on all nodes.&lt;/li&gt;
&lt;li&gt;Initialise the master node &lt;code&gt;kubeadm init &amp;lt;args&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Ensure network prerequisites are met (cni plugin)&lt;/li&gt;
&lt;li&gt;Join worker nodes using the join command specified by the kubeadm tool&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 class="heading" id="section-10-troubleshooting"&gt;
 Section 10: Troubleshooting
 &lt;a href="#section-10-troubleshooting"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;h2 class="heading" id="application-failure"&gt;
 Application Failure
 &lt;a href="#application-failure"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Check the application is accessible on the IP of the application, check service and endpoints which it has grabbed. Check if the pod is running and if it has crashed and restarted. Check the logs of the pod to see why it is failing. Check any dependant services such as databases or apis.&lt;/p&gt;
&lt;h2 class="heading" id="control-plane-falure"&gt;
 Control Plane Falure
 &lt;a href="#control-plane-falure"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Check status of the nodes and pods on the cluster. You should also check the controlplane pods or services on the master nodes. Check the logs of the control plane components&lt;/p&gt;
&lt;h2 class="heading" id="worker-node-failure"&gt;
 Worker Node Failure
 &lt;a href="#worker-node-failure"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Check the status of the nodes of the pods. Check the description if the node is failed. If the statuses are unknown then it may have crashed. You need to check the status of the nodes. Check CPU, memory and disk space. Check kubelet and kubelet certs.&lt;/p&gt;
&lt;h2 class="heading" id="troubleshoot-network"&gt;
 Troubleshoot Network
 &lt;a href="#troubleshoot-network"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Check the CNI binary directory and the network plugin that is being used. You can also check if DNS resolves by deploying a test pod and execing into it. Check kube proxy and its logs if service rules are not being applied&lt;/p&gt;
&lt;h1 class="heading" id="section-11-jsonpath-queries"&gt;
 Section 11: JSONPATH Queries
 &lt;a href="#section-11-jsonpath-queries"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;p&gt;JSONpath is useful where there are 100s of nodes and 1000s of pods you need to query and you will be using kubectl for these objects. JSON path is supported to help with filtering the queries.&lt;/p&gt;
&lt;p&gt;You must first get the path to the field by running kubectl with the output set as json. Then you can form the query &lt;code&gt;-o=jsonpath '{.items[0].spec.containers[0].image}’&lt;/code&gt; (You can use wildcard instead of the number in a list to list all the objects). You can also insert a new line with &lt;code&gt;{&amp;quot;\n&amp;quot;}&lt;/code&gt; and a tab with &lt;code&gt;{&amp;quot;\t&amp;quot;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;You can use loops and ranges:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-JSON" data-lang="JSON"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;range&lt;/span&gt; &lt;span class="err"&gt;.items[*]&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="err"&gt;.metadata.name&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="nt"&gt;&amp;#34;\t&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="err"&gt;.status.capacity.cpu&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="nt"&gt;&amp;#34;\n&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;}{&lt;/span&gt;&lt;span class="err"&gt;end&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can use the &lt;code&gt;--sort-by&lt;/code&gt; flag in kubectl and pass in the jsonpath and this will sort by the item specified. You can also add custom columns to kubectl by using the json path of the fields you want:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;kubectl get nodes -o&lt;span class="o"&gt;=&lt;/span&gt;custom-columns&lt;span class="o"&gt;=&lt;/span&gt;NODE:.metadata.name, CPU:.status.capacity.cpu
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>About</title><link>https://kenan.sh/about/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kenan.sh/about/</guid><description>&lt;div class="about-hero"&gt;





&lt;img class="avatar avatar-about" src="https://kenan.sh/images/selfie_hu_b5618d4bd8afca77.jpg" data-alt-src="https://kenan.sh/images/90s_hu_7271eac57a798b5d.jpg" title="psst — click me" alt="Kenan Jasim" width="110" height="110" /&gt;


&lt;h1 class="heading" id="hi-im-kenan-"&gt;
 Hi, I&amp;rsquo;m Kenan &lt;span class="wave"&gt;👋&lt;/span&gt;
 &lt;a href="#hi-im-kenan-"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;/div&gt;
&lt;blockquote class="about-quote"&gt;
Tenderness and kindness are not signs of weakness and despair, but manifestations of strength and resolution.
&lt;cite&gt;— Kahlil Gibran&lt;/cite&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&amp;rsquo;m a software engineer living in London. This is where I write about whatever I&amp;rsquo;m into at the
moment. For my work and CV, head to &lt;a href="https://kenanjasim.com/"&gt;kenanjasim.com&lt;/a&gt;.&lt;/p&gt;
&lt;h2 class="heading" id="quick-facts"&gt;
 Quick facts
 &lt;a href="#quick-facts"&gt;#&lt;/a&gt;
&lt;/h2&gt;

&lt;div class="facts"&gt;
&lt;ul&gt;
&lt;li&gt;📍 &lt;strong&gt;London&lt;/strong&gt;, UK-born&lt;/li&gt;
&lt;li&gt;🌍 &lt;strong&gt;Lebanese &amp;amp; Iraqi&lt;/strong&gt; roots&lt;/li&gt;
&lt;li&gt;🗣️ &lt;strong&gt;English &amp;amp; Arabic&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;💚 &lt;strong&gt;Type 1 diabetic&lt;/strong&gt; since I was one&lt;/li&gt;
&lt;li&gt;🎓 &lt;strong&gt;Electronic engineering&lt;/strong&gt; at King&amp;rsquo;s&lt;/li&gt;
&lt;li&gt;🍲 &lt;strong&gt;Volunteer&lt;/strong&gt; at &lt;a href="https://www.sufra-nwlondon.org.uk/"&gt;Sufra&lt;/a&gt;&amp;rsquo;s community kitchen&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;

&lt;h3 class="heading" id="things-im-into"&gt;
 Things I&amp;rsquo;m into
 &lt;a href="#things-im-into"&gt;#&lt;/a&gt;
&lt;/h3&gt;

&lt;div class="facts facts-lately"&gt;
&lt;ul&gt;
&lt;li&gt;🏃 &lt;strong&gt;Running:&lt;/strong&gt; working up to my first continuous 5k, &lt;a href="https://www.strava.com/athletes/97755215"&gt;on Strava&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;🎧 &lt;strong&gt;Music:&lt;/strong&gt; pop, hip-hop and Arabic music. Fairuz, Kazdoura, Olivia Rodrigo, Little Simz, &lt;em&gt;brat&lt;/em&gt;, &lt;em&gt;Madvillainy&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;📚 &lt;strong&gt;Reading:&lt;/strong&gt; mostly politics and history. &lt;em&gt;Between Two Rivers&lt;/em&gt;, &lt;em&gt;Pity the Nation&lt;/em&gt;, &lt;em&gt;The Will to Change&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;⚽ &lt;strong&gt;Football:&lt;/strong&gt; Manchester United fan, for my sins. Still convinced it was better before the 2010s&lt;/li&gt;
&lt;li&gt;🎱 &lt;strong&gt;Snooker&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;📷 &lt;strong&gt;Photography:&lt;/strong&gt; photos coming soon&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;

&lt;h2 class="heading" id="places-ive-been"&gt;
 Places I&amp;rsquo;ve been
 &lt;a href="#places-ive-been"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;I love collecting places more than things. Everywhere shaded is somewhere I&amp;rsquo;ve set foot.&lt;/p&gt;






&lt;link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsvectormap@1.6.0/dist/jsvectormap.min.css"&gt;




&lt;div class="travel-layout"&gt;
 &lt;div id="travel-map" class="travel-map" role="img" aria-label="World map with countries I've visited highlighted"&gt;&lt;/div&gt;

 &lt;ul class="travel-list" aria-label="Countries I've visited"&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇩🇪&lt;/span&gt; Germany&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇬🇷&lt;/span&gt; Greece (Santorini)&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇱🇧&lt;/span&gt; Lebanon&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇳🇴&lt;/span&gt; Norway&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇸🇪&lt;/span&gt; Sweden&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇬🇧&lt;/span&gt; United Kingdom&lt;/li&gt;
 
 
 &lt;li&gt;&lt;span class="travel-flag" aria-hidden="true"&gt;🇺🇸&lt;/span&gt; United States&lt;/li&gt;
 
 &lt;/ul&gt;
&lt;/div&gt;

&lt;script src="https://cdn.jsdelivr.net/npm/jsvectormap@1.6.0/dist/jsvectormap.min.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/jsvectormap@1.6.0/dist/maps/world.js"&gt;&lt;/script&gt;
&lt;script&gt;
(function () {
 var visited = ["DE","GR","LB","NO","SE","GB","US"];
 var instance = null;

 function cssVar(name, fallback) {
 var v = getComputedStyle(document.body).getPropertyValue(name).trim();
 return v || fallback;
 }

 function render() {
 if (typeof jsVectorMap === 'undefined') return;
 var el = document.getElementById('travel-map');
 if (!el) return;
 if (instance) { instance.destroy(); instance = null; }
 el.innerHTML = '';
 var accent = cssVar('--accent', '#157A60');
 instance = new jsVectorMap({
 selector: '#travel-map',
 map: 'world',
 zoomButtons: true,
 zoomOnScroll: false,
 backgroundColor: 'transparent',
 
 
 regionsSelectable: false,
 selectedRegions: visited.map(function (code) { return code.toUpperCase(); }),
 regionStyle: {
 initial: {
 fill: cssVar('--map-base', '#d6d0c4'),
 stroke: cssVar('--background', '#f6f1e7'),
 strokeWidth: 0.4
 },
 selected: { fill: accent },
 hover: { fillOpacity: 0.75 }
 }
 });
 }

 if (document.readyState === 'complete') {
 render();
 } else {
 window.addEventListener('load', render);
 }
 window.addEventListener('themechanged', render);
})();
&lt;/script&gt;

&lt;h2 class="heading" id="say-hi"&gt;
 Say hi
 &lt;a href="#say-hi"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;If you want to say hello (or tell me I&amp;rsquo;m wrong about football), I&amp;rsquo;m easy to find:&lt;/p&gt;


&lt;div class="social-icons"&gt;
 &lt;a href="https://bsky.app/profile/kenan.sh" target="_blank"
 rel="noopener noreferrer me"
 title="Bluesky"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 320" fill="currentColor"&gt;
 &lt;path
 d="M180 141.964C163.699 110.262 119.308 51.1817 78.0347 22.044C38.4971 -5.86834 23.414 -1.03207 13.526 3.43594C2.08093 8.60755 0 26.1785 0 36.5164C0 46.8542 5.66748 121.272 9.36416 133.694C21.5786 174.738 65.0603 188.607 105.104 184.156C107.151 183.852 109.227 183.572 111.329 183.312C109.267 183.642 107.19 183.924 105.104 184.156C46.4204 192.847 -5.69621 214.233 62.6582 290.33C137.848 368.18 165.705 273.637 180 225.702C194.295 273.637 210.76 364.771 295.995 290.33C360 225.702 313.58 192.85 254.896 184.158C252.81 183.926 250.733 183.645 248.671 183.315C250.773 183.574 252.849 183.855 254.896 184.158C294.94 188.61 338.421 174.74 350.636 133.697C354.333 121.275 360 46.8568 360 36.519C360 26.1811 357.919 8.61012 346.474 3.43851C336.586 -1.02949 321.503 -5.86576 281.965 22.0466C240.692 51.1843 196.301 110.262 180 141.964Z" /&gt;
&lt;/svg&gt;
 &lt;/a&gt;
 &lt;a href="https://www.linkedin.com/in/kenanjasim/" target="_blank"
 rel="noopener noreferrer me"
 title="Linkedin"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
 stroke-linecap="round" stroke-linejoin="round"&gt;
 &lt;path d="M16 8a6 6 0 0 1 6 6v7h-4v-7a2 2 0 0 0-2-2 2 2 0 0 0-2 2v7h-4v-7a6 6 0 0 1 6-6z"&gt;&lt;/path&gt;
 &lt;rect x="2" y="9" width="4" height="12"&gt;&lt;/rect&gt;
 &lt;circle cx="4" cy="4" r="2"&gt;&lt;/circle&gt;
&lt;/svg&gt;
 &lt;/a&gt;
 &lt;a href="https://github.com/kenanjasim/" target="_blank"
 rel="noopener noreferrer me"
 title="Github"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
 stroke-linecap="round" stroke-linejoin="round"&gt;
 &lt;path
 d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"&gt;
 &lt;/path&gt;
&lt;/svg&gt;
 &lt;/a&gt;
 &lt;a href="https://www.strava.com/athletes/97755215" target="_blank"
 rel="noopener noreferrer me"
 title="Strava"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" stroke-width="2"&gt;
 &lt;path
 d="M15.387 17.944l-2.089-4.116h-3.065L15.387 24l5.15-10.172h-3.066m-7.008-5.599l2.836 5.598h4.172L10.463 0l-7 13.828h4.169" /&gt;
&lt;/svg&gt;
&lt;/svg&gt;
 &lt;/a&gt;
 &lt;a href="mailto:me@kenan.sh" target="_blank"
 rel="noopener noreferrer me"
 title="Email"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 21" fill="none" stroke="currentColor" stroke-width="2"
 stroke-linecap="round" stroke-linejoin="round"&gt;
 &lt;path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"&gt;&lt;/path&gt;
 &lt;polyline points="22,6 12,13 2,6"&gt;&lt;/polyline&gt;
&lt;/svg&gt;
 &lt;/a&gt;
 &lt;a href="https://kenan.sh/index.xml" target="_blank"
 rel="noopener noreferrer me"
 title="Rss"&gt;
 &lt;svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
 stroke-linecap="round" stroke-linejoin="round"&gt;
 &lt;path d="M4 11a9 9 0 0 1 9 9" /&gt;
 &lt;path d="M4 4a16 16 0 0 1 16 16" /&gt;
 &lt;circle cx="5" cy="19" r="1" /&gt;
&lt;/svg&gt;
 &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Thanks for stopping by 💚&lt;/p&gt;</description></item></channel></rss>