<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://nasermirzaei89.net/feed.xml" rel="self" type="application/atom+xml" /><link href="https://nasermirzaei89.net/" rel="alternate" type="text/html" /><updated>2025-12-26T22:12:10+00:00</updated><id>https://nasermirzaei89.net/feed.xml</id><title type="html">Naser Mirzaei</title><subtitle>My Personal Web Page, I Will Write Everything Here</subtitle><author><name>Naser Mirzaei</name></author><entry><title type="html">Why Software Engineers Must Think Beyond Code</title><link href="https://nasermirzaei89.net/2025/12/17/why-software-engineers-must-think-beyond-code/" rel="alternate" type="text/html" title="Why Software Engineers Must Think Beyond Code" /><published>2025-12-17T00:00:00+00:00</published><updated>2025-12-17T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2025/12/17/why-software-engineers-must-think-beyond-code</id><content type="html" xml:base="https://nasermirzaei89.net/2025/12/17/why-software-engineers-must-think-beyond-code/"><![CDATA[<p>One of the important things that, as a programmer or software engineer, we must know and do is that instead of jumping straight into software design, we first go for designing the system itself.</p>

<h2 id="start-with-the-manual-system">Start with the manual system</h2>

<p>Imagine you want to design a software for course registration at a university. One of the tasks of a software engineer is to design that automation and system without using any technology.
The database is replaced with a binder, the in-app form is replaced with a paper form, online payment and receipts are replaced with cash payment and paper receipts, and so on.</p>

<p>If this paper system cannot run cleanly, software will only automate the chaos.</p>

<h2 id="layer-technology-gradually">Layer technology gradually</h2>

<p>Once we can design and set up this system and make it work flawlessly, then we reach the stage where we can improve parts of it—or all of it—with technology to increase speed, efficiency, security, etc.</p>

<p>For instance, instead of cash, you can go one step further and use a POS machine, or bank payment to the university account with a payment receipt.
Or instead of a binder in the office, you can use Excel to make searching faster.</p>

<p>Ultimately, the ideal is for all these steps to become fully computerized and integrated with other parts of the university.</p>

<h2 id="why-this-matters-in-the-ai-era">Why This Matters in the AI Era</h2>

<p>If we cannot create a non-computerized version of software for every need, we are ultimately not software engineers but just coders.</p>

<p>In today’s world, where AI has come to handle part of our work and everything is becoming AI-powered, this skill becomes even more important.
Because as a software engineer, I need to be able to plan, so that I can then give smaller parts of that plan to AI to write for me.</p>

<p>This is where, the more skilled we are at this fundamental engineering ability, the better and more effectively we can use AI in our work, or even have AI-powered tools.</p>

<p>At the current level, AI may be able to write a function much better than me as a senior, but it cannot judge whether it makes sense for the overall program solution to use that function or not, which makes the role of a software engineer even more important.</p>

<p>In programming editor copilots, we also constantly see that it’s the software engineer who plans and converts user prompts into simpler tasks for the AI tool, so it can do more and bigger work.</p>

<h2 id="key-insight">Key Insight</h2>

<p>The general point is whenever we want to understand any technology or concept, like AI, blockchain, etc., we must first imagine and understand it in a completely non-computerized world.</p>]]></content><author><name>Naser Mirzaei</name></author><category term="System_Design" /><summary type="html"><![CDATA[Learn why software engineers should design systems before writing code, and how understanding non-digital processes helps leverage AI and technology effectively.]]></summary></entry><entry><title type="html">Dev Dependencies In Golang</title><link href="https://nasermirzaei89.net/2025/11/05/dev-dependencies-in-golang/" rel="alternate" type="text/html" title="Dev Dependencies In Golang" /><published>2025-11-05T00:00:00+00:00</published><updated>2025-11-05T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2025/11/05/dev-dependencies-in-golang</id><content type="html" xml:base="https://nasermirzaei89.net/2025/11/05/dev-dependencies-in-golang/"><![CDATA[<p>In <a href="https://go.dev/doc/go1.24">Go 1.24</a>, the Go team introduced new functionality for managing developer tooling dependencies via the <code class="language-plaintext highlighter-rouge">-tool</code> flag for <code class="language-plaintext highlighter-rouge">go get</code>.</p>

<p>Before this, if your project needed tools such as <a href="https://github.com/air-verse/air">github.com/air-verse/air</a> or <a href="https://golangci-lint.run/">golangci-lint</a> (tools written in Go), you had to install them directly on your machine and ensure their versions matched across all developers’ machines.</p>

<p>Now, you can add them to your <code class="language-plaintext highlighter-rouge">go.mod</code> file with a defined version.</p>

<p>For example, imagine you’re using golangci-lint. You can add it to your <code class="language-plaintext highlighter-rouge">go.mod</code> file with:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>go get <span class="nt">-tool</span> github.com/golangci/golangci-lint/v2/cmd/golangci-lint
</code></pre></div></div>

<p>It downloads latest version.
Or you can download a specific version by:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>go get <span class="nt">-tool</span> github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.4.0
</code></pre></div></div>

<p>Then run it with:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>go tool golangci-lint run
</code></pre></div></div>

<p>You no longer need to install golangci-lint globally or rely on a globally installed binary that might have a different version.
On every developer machine and in CI, it runs with the specified version.</p>

<p>If you check your <code class="language-plaintext highlighter-rouge">go.mod</code> file, you’ll see a line has been added:</p>

<pre><code class="language-gomod">
tool github.com/golangci/golangci-lint/v2/cmd/golangci-lint
</code></pre>

<p>It also appears as an indirect dependency:</p>

<pre><code class="language-gomod">require (
    ...
    github.com/golangci/golangci-lint/v2 v2.6.1 // indirect
    ...
)
</code></pre>

<p>In short, managing developer tools with go get -tool and go tool delivers:</p>

<ul>
  <li>Reproducible, pinned versions across machines and CI</li>
  <li>No global installs; tools are isolated per module</li>
  <li>Faster onboarding with fewer manual steps</li>
  <li>Controlled, reviewable upgrades via go.mod diffs</li>
  <li>Cleaner, hermetic CI pipelines</li>
</ul>

<p>Adopt it to keep tooling predictable and maintenance low-friction.</p>]]></content><author><name>Naser Mirzaei</name></author><category term="Golang" /><category term="Tooling" /><summary type="html"><![CDATA[Using the Go tool to run project tooling with pinned versions]]></summary></entry><entry><title type="html">Error Handling In Golang</title><link href="https://nasermirzaei89.net/2025/06/11/error-handling-in-golang/" rel="alternate" type="text/html" title="Error Handling In Golang" /><published>2025-06-11T00:00:00+00:00</published><updated>2025-06-11T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2025/06/11/error-handling-in-golang</id><content type="html" xml:base="https://nasermirzaei89.net/2025/06/11/error-handling-in-golang/"><![CDATA[<p>When we get an error in Golang, we MUST handle it immediately. There are different methods of handling errors. Go keeps things simple with its approach to error handling, making it very explicit and flexible.</p>

<h2 id="basics-of-error-handling-in-go">Basics of Error Handling in Go</h2>

<p>Go uses a built-in <code class="language-plaintext highlighter-rouge">error</code> type, which is just an interface. Functions often return an <code class="language-plaintext highlighter-rouge">error</code> value as their last return parameter. If the error is not <code class="language-plaintext highlighter-rouge">nil</code>, it means something went wrong, and you must handle it.</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">res</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">someFunction</span><span class="p">()</span>
<span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
  <span class="c">// handle the error</span>
<span class="p">}</span>
</code></pre></div></div>

<p>You either handle the error right there or return it up the chain.</p>

<p>Do not use <code class="language-plaintext highlighter-rouge">if err == nil</code> as your main conditional. It’s not idiomatic Go. Checking for <code class="language-plaintext highlighter-rouge">err != nil</code> clearly signals that you’re looking for the presence of an error, which matches Go’s error-first mindset. Reversing this logic hurts readability and can confuse others reading your code.</p>

<h2 id="custom-errors">Custom Errors</h2>

<p>Sometimes you want to act differently based on the type of error. You can define custom errors:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">var</span> <span class="n">ErrBookNotFound</span> <span class="o">=</span> <span class="n">errors</span><span class="o">.</span><span class="n">New</span><span class="p">(</span><span class="s">"book not found"</span><span class="p">)</span>
</code></pre></div></div>

<p>Or:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">type</span> <span class="n">BookNotFoundError</span> <span class="k">struct</span> <span class="p">{</span>
  <span class="n">ID</span> <span class="kt">string</span>
<span class="p">}</span>

<span class="k">func</span> <span class="p">(</span><span class="n">err</span> <span class="n">BookNotFoundError</span><span class="p">)</span> <span class="n">Error</span><span class="p">()</span> <span class="kt">string</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"book with id '%s' not found"</span><span class="p">,</span> <span class="n">err</span><span class="o">.</span><span class="n">ID</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Or wrap errors with more context:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"error on insert book: %w"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
</code></pre></div></div>

<p>This makes it easier to understand where and why an error occurred.</p>

<h2 id="best-practices">Best Practices</h2>

<ul>
  <li>Always check and handle errors.</li>
  <li>Return early if something fails.</li>
  <li>Use clear and specific error messages.</li>
  <li>Don’t use panic unless it’s truly unrecoverable.</li>
</ul>

<h2 id="error-wrapping-and-unwrapping">Error Wrapping and Unwrapping</h2>

<p>If you expect certain errors, you can check them using <code class="language-plaintext highlighter-rouge">errors.Is</code> or <code class="language-plaintext highlighter-rouge">errors.As</code>:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="n">errors</span><span class="o">.</span><span class="n">Is</span><span class="p">(</span><span class="n">err</span><span class="p">,</span> <span class="n">ErrBookNotFound</span><span class="p">)</span> <span class="p">{</span>
  <span class="c">// handle not found</span>
<span class="p">}</span>
</code></pre></div></div>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">var</span> <span class="n">bookNotFoundError</span> <span class="n">BookNotFoundError</span>
<span class="k">if</span> <span class="n">errors</span><span class="o">.</span><span class="n">As</span><span class="p">(</span><span class="n">err</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">bookNotFoundError</span><span class="p">)</span> <span class="p">{</span>
  <span class="c">// handle not found</span>
  <span class="c">// also you have access to bookNotFoundError.ID here</span>
<span class="p">}</span>
</code></pre></div></div>

<p>You can also unwrap errors to get the root cause:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">underlying</span> <span class="o">:=</span> <span class="n">errors</span><span class="o">.</span><span class="n">Unwrap</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
</code></pre></div></div>

<p>Use <code class="language-plaintext highlighter-rouge">errors.Unwrap</code> when you want to access the direct underlying error. This is useful if you want to log the root cause only, perform specific logic, or re-wrap errors in a custom chain. If you’re doing error comparisons or type assertions, prefer <code class="language-plaintext highlighter-rouge">errors.Is</code> and <code class="language-plaintext highlighter-rouge">errors.As</code>.</p>

<h2 id="use-cases-by-app-type">Use Cases by App Type</h2>

<h3 id="cli-applications">CLI Applications</h3>

<ul>
  <li>Print meaningful errors to stderr.</li>
  <li>Exit with appropriate status codes.</li>
</ul>

<h3 id="webapi-applications">Web/API Applications</h3>

<ul>
  <li>Map internal errors to proper HTTP status codes.</li>
  <li>Hide sensitive error details from clients.</li>
</ul>

<h3 id="background-jobs--workers">Background Jobs / Workers</h3>

<ul>
  <li>Retry logic if it’s a transient error.</li>
  <li>Log and monitor errors.</li>
</ul>

<h2 id="example-handling-based-on-error-type">Example: Handling Based on Error Type</h2>

<p>Sometimes, we know what errors to expect and handle them accordingly:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">func</span> <span class="n">upsertBook</span><span class="p">(</span><span class="n">book</span> <span class="n">Book</span><span class="p">)</span> <span class="kt">error</span> <span class="p">{</span>
  <span class="n">res</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">getBook</span><span class="p">(</span><span class="n">book</span><span class="o">.</span><span class="n">ID</span><span class="p">)</span>
  <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
    <span class="k">if</span> <span class="n">errors</span><span class="o">.</span><span class="n">Is</span><span class="p">(</span><span class="n">err</span><span class="p">,</span> <span class="n">ErrBookNotFound</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">err</span> <span class="o">=</span> <span class="n">insertBook</span><span class="p">(</span><span class="n">book</span><span class="p">)</span>
      <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"error on insert book: %w"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
      <span class="p">}</span>

      <span class="k">return</span> <span class="no">nil</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"error on get book: %w"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
  <span class="p">}</span>

  <span class="n">err</span> <span class="o">=</span> <span class="n">replaceBook</span><span class="p">(</span><span class="n">book</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span> <span class="n">book</span><span class="p">)</span>
  <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"error on replace book: %w"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
  <span class="p">}</span>

  <span class="k">return</span> <span class="no">nil</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Here, when <code class="language-plaintext highlighter-rouge">getBook</code> fails, we check if it’s a not-found error, so we insert instead. If not, we return the error with context.</p>

<h2 id="why-wrapping-errors-matters">Why Wrapping Errors Matters</h2>

<p>Even if you can’t handle an error, you’re still responsible for reporting it clearly to the caller. Wrapping it adds valuable context:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">return</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Errorf</span><span class="p">(</span><span class="s">"error on register new user: %w"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>
</code></pre></div></div>

<p>Compare these two logs:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context deadline exceeded
</code></pre></div></div>

<p>vs.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>error on register new user: error on insert user to database: error on run exec: context deadline exceeded
</code></pre></div></div>

<p>This second log acts like a breadcrumb trail, making it easier to trace where the error really happened.</p>

<h2 id="common-mistakes">Common Mistakes</h2>

<ul>
  <li>Ignoring errors (e.g., <code class="language-plaintext highlighter-rouge">_ = someFunc()</code>), unless you do it intentionally.</li>
  <li>Using panic for normal errors</li>
  <li>Losing original error details (not using <code class="language-plaintext highlighter-rouge">%w</code>)</li>
  <li>Writing <code class="language-plaintext highlighter-rouge">if err == nil</code> instead of <code class="language-plaintext highlighter-rouge">if err != nil</code></li>
</ul>

<h2 id="conclusion">Conclusion</h2>

<p>Error handling is not an afterthought in Go—it’s part of the design. Handle every error explicitly, wrap them with context, and stay consistent. That’s how you build robust, debuggable Go apps.</p>]]></content><author><name>Naser Mirzaei</name></author><category term="Golang" /><category term="Error" /><summary type="html"><![CDATA[Learn effective error handling in Golang with practical examples, best practices, and real-world use cases. Discover how to use custom errors, wrap and unwrap errors, and avoid common mistakes to write clean, maintainable Go code.]]></summary></entry><entry><title type="html">Schedule Your Time</title><link href="https://nasermirzaei89.net/2024/12/08/schedule-your-time/" rel="alternate" type="text/html" title="Schedule Your Time" /><published>2024-12-08T00:00:00+00:00</published><updated>2024-12-08T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2024/12/08/schedule-your-time</id><content type="html" xml:base="https://nasermirzaei89.net/2024/12/08/schedule-your-time/"><![CDATA[<p>I often feel like I don’t have enough time in the day.
I spend most of my day working, eating, sleeping, and still feel like there’s not enough time to do what I want.</p>

<p>So, at the beginning of December 2024, I started scheduling my time. Now, after one week of living on a schedule, I want
to share my experience and think about how I can improve it.</p>

<h2 id="what-is-it">What is it?</h2>

<p>First of all,
you can check how people like <a href="https://en.wikipedia.org/wiki/Elon_Musk">Elon Musk</a>
or <a href="https://en.wikipedia.org/wiki/Cal_Newport">Cal Newport</a> schedule their time.
This can really help you find and improve your own model.
However, doing exactly what they do can be difficult or may not work for you, depending on your goals and lifestyle.</p>

<p>So, I decided to schedule my life in a way that suits me.
I usually use my MacBook, so I created a <a href="https://sheets.new/">Google Sheet</a> and always keep it open.
I divided the day into 30-minute chunks, with the time as the first column and the days of the week as the table
headers.</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>Mon, Dec 2</th>
      <th>Tue, Dec 3</th>
      <th>Wed, Dec 4</th>
      <th>Thu, Dec 5</th>
      <th>Fri, Dec 6</th>
      <th>…</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>12:00 AM</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>Sleep</td>
      <td>…</td>
    </tr>
    <tr>
      <td>12:30 AM</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>…</td>
    </tr>
    <tr>
      <td>……..</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>…</td>
    </tr>
    <tr>
      <td>09:00 AM</td>
      <td>Breakfast</td>
      <td>Breakfast</td>
      <td>Breakfast</td>
      <td>Breakfast</td>
      <td>Breakfast</td>
      <td>…</td>
    </tr>
    <tr>
      <td>09:30 AM</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>…</td>
    </tr>
    <tr>
      <td>……..</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>…</td>
    </tr>
    <tr>
      <td>11:30 PM</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>…</td>
    </tr>
  </tbody>
</table>

<p>I scheduled all my fixed routines for the week, such as:</p>

<ul>
  <li>Sleep (every day)</li>
  <li>Daily meeting (every weekday)</li>
  <li>Meals (3 times a day)</li>
  <li>Shower (every other day, excluding weekends)</li>
  <li>Haircut (every 3 weeks)</li>
  <li>…</li>
</ul>

<p>Then, I added my tasks for the next day in the appropriate columns.
If a task had a fixed time, such as a work meeting, I scheduled it precisely.
If I planned to do something in the evening, I scheduled it around that time.</p>

<h2 id="what-did-i-do">What did I do?</h2>

<p>The next day, I checked my schedule and knew exactly what I needed to do and when I would do it.</p>

<p>I didn’t fill every hour of my day to stay super busy.
I just told myself, “Tomorrow, I will do these tasks.”
I always keep a sticky note app open on my MacBook to jot down thoughts, so I don’t need to ruminate on them.</p>

<p>After finishing one task, I looked at my sheet to see what to do next.
If a task took longer than expected, I either stopped or extended it, depending on its importance.</p>

<p>I try to finish breakfast or lunch within 30 minutes (including preparation), but I allow more time for dinner since I
spend it with my wife.
Sometimes, dinner can take up to three hours if we watch a movie or relax afterward.</p>

<p>Note: Currently, I don’t schedule short tasks like brushing my teeth or bathroom breaks.</p>

<h2 id="improvements">Improvements</h2>

<h3 id="categories">Categories</h3>

<p>After a few days, I decided to categorize my tasks, such as:</p>

<ul>
  <li>Side Project 1 (Code)</li>
  <li>Daily Job (Code)</li>
  <li>Side Project 2 (Meeting)</li>
  <li>Play (Video Game)</li>
  <li>Prepare (Dinner)</li>
</ul>

<p>I also combined tasks if I did them simultaneously:</p>

<ul>
  <li>Breakfast, Daily Job (Presentation)</li>
</ul>

<p>This method made it easier to write concise titles and reduced verbosity.</p>

<p>Additionally, I could create charts or gain insights into how much time I spent on projects or entertainment.</p>

<p>It also helped me see how time was distributed within a project — for example, what percentage was spent on research,
meetings, or coding.</p>

<h3 id="scheduling-the-whole-day">Scheduling the Whole Day</h3>

<p>At first, I scheduled my day from 9 AM to midnight.
But soon, I realized I needed to schedule the full 24 hours.
Some days, I wake up earlier or later, or I go to bed at different times.</p>

<p>This update also allowed me to track my sleep.
Last week, I went to bed at 12 AM, 1 AM, 1 AM, 12 AM, 2 AM, 3 AM, and 3 AM.</p>

<p>It also gave me the flexibility to extend tasks past midnight when necessary.</p>

<h3 id="decreasing-chunk-time">Decreasing Chunk Time</h3>

<p>I started with 30-minute chunks because they were easy to manage and less stressful.
However, this sometimes made scheduling difficult.</p>

<p>For example, if a task took 40 or 80 minutes, I faced a dilemma:
Should I stop when just a bit was left or extend by 30 minutes and waste the remaining 20?</p>

<p>Next week, I plan to try 15-minute chunks.
Honestly, I’m not sure yet 😅.
Maybe someday, I’ll use 5-minute chunks!</p>

<h3 id="more-hacks-and-improvements">More Hacks and Improvements</h3>

<ul>
  <li>I’m considering using the <a href="https://en.wikipedia.org/wiki/Pomodoro_Technique">Pomodoro Technique</a> to stay focused and
protect my eyes during long screen sessions.</li>
  <li>This might sound funny, but I’m thinking of using a short beep alarm between chunks. I’ve been checking the clock more
often lately 😂. It reminds me of a character from the <a href="https://www.imdb.com/title/tt4158110/">Mr. Robot</a> series.</li>
  <li>I tried this before, but I want to define long-term goals (yearly, quarterly, monthly, and weekly) to stay more
focused.</li>
</ul>

<h2 id="why-use-fixed-chunks">Why Use Fixed Chunks?</h2>

<p>I find it easier to manage fixed chunks compared to fluid scheduling.
Fixed chunks are simpler to measure — I can count cells and see that I spent 2.5 hours on a task.</p>

<p>Plus, I can easily update my Google Sheet from my MacBook or phone.</p>

<h2 id="benefits">Benefits</h2>

<ul>
  <li>I realized I can complete all my tasks if I schedule my day.</li>
  <li>I wasted less time.</li>
  <li>Time feels more valuable.</li>
  <li>I’m getting better at estimating how long tasks take.</li>
  <li>I procrastinate less.</li>
  <li>I’m more mindful and aware of how I spend my day.</li>
</ul>

<p>When I decided to try this method, I didn’t research what others were doing — I didn’t even
ask <a href="https://chatgpt.com">ChatGPT</a>!</p>

<p>If I research and improve this method, I’ll update this post.</p>

<p>Let me know your thoughts on this method and how you manage your time! ✌️</p>]]></content><author><name>Naser Mirzaei</name></author><category term="Schedule" /><category term="Time" /><summary type="html"><![CDATA[If you can't do all your daily tasks, schedule your day]]></summary></entry><entry><title type="html">Setting Up a Highly Available etcd Cluster for Kubernetes</title><link href="https://nasermirzaei89.net/2023/10/31/setting-up-a-highly-available-etcd-cluster/" rel="alternate" type="text/html" title="Setting Up a Highly Available etcd Cluster for Kubernetes" /><published>2023-10-31T00:00:00+00:00</published><updated>2023-10-31T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2023/10/31/setting-up-a-highly-available-etcd-cluster</id><content type="html" xml:base="https://nasermirzaei89.net/2023/10/31/setting-up-a-highly-available-etcd-cluster/"><![CDATA[<p>Before set up, we should know what is it and why we need it?
It helps us to understand how important it is (or it’s not).</p>

<h2 id="what-is-etcd">What is etcd</h2>

<p>According to https://etcd.io/docs/v3.5/faq/#what-is-etcd</p>

<blockquote>
  <p>etcd is a consistent distributed key-value store.
Mainly used as a separate coordination service, in distributed systems.
And designed to hold small amounts of data that can fit entirely in memory.</p>
</blockquote>

<h2 id="why-kubernetes-uses-etcd">Why Kubernetes uses etcd</h2>

<p>Kubernetes uses etcd as its distributed key-value store to maintain a reliable and consistent data store for managing
the cluster’s configuration, state, and metadata.
Etcd provides a highly available and strongly consistent data store,
which is essential for ensuring that Kubernetes components can communicate and coordinate effectively across different
nodes in the cluster.
It allows Kubernetes to store and retrieve critical information about the desired state of the system,
making it resilient and capable of handling failures while ensuring that the desired configurations are maintained.</p>

<h2 id="why-we-need-high-available">Why we need high available</h2>

<p>A highly available etcd cluster is crucial for Kubernetes because it serves as the authoritative data store for the
cluster’s configuration and state.
Kubernetes relies on etcd to store information about the desired state of applications and resources,
as well as the current state of the cluster.
If etcd were to become unavailable or suffer from data inconsistencies,
it could lead to cluster-wide outages, data loss, or unreliable resource management.
High availability in etcd ensures that even in the presence of hardware failures or network issues,
Kubernetes can continue to function, maintain cluster state,
and provide the desired services with minimal disruption,
making it a critical component for the reliability and resilience of a Kubernetes cluster.</p>

<h2 id="prerequisites">Prerequisites</h2>

<p>In this article, I used <a href="https://hetzner.cloud/?ref=VJt8VCfetCEE">Hetzner Cloud</a> as it’s a reliable,
not expensive cloud provider with acceptable modern technologies.</p>

<p>This link contains a refer id, and by using it, you will receive €20 credit by signing up,
and I’ll receive €10 if you spend at lease €10.</p>

<p>I use Hetzner, because it does not have its own Kubernetes service.
So, you can find articles and solutions for running your own cluster on it easier.</p>

<p>In contrast, with other providers, you may be constrained to utilize their proprietary managed Kubernetes service,
which can often be a sensible choice.</p>

<p>Also, you can use any service provider or your own infrastructure as long as the servers have access to each other and
the internet.</p>

<h3 id="hetzner-cli">Hetzner CLI</h3>

<p>For creating networks, subnets, and servers, I use <code class="language-plaintext highlighter-rouge">hcloud</code> to access my Hetzner account.</p>

<p>You can install it from: https://github.com/hetznercloud/cli</p>

<p>I used this command for macOS:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>brew <span class="nb">install </span>hcloud
</code></pre></div></div>

<p>After installing it, you need to obtain an API Token by creating a Project in your Hetzner Cloud,
and going to “Security” menu, then “API Tokens” tab. Generate a token with “Read &amp; Write” permission.</p>

<p>In your local terminal, enter:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>hcloud context create &lt;PROJECT_NAME&gt;
</code></pre></div></div>

<p>And enter the generated token.</p>

<p>It will store the context in <code class="language-plaintext highlighter-rouge">~/.config/hcloud/cli.toml</code> if it’s valid,
and will set it the default context to use.
You can switch between contexts by <code class="language-plaintext highlighter-rouge">hcloud context use &lt;CONTEXT&gt;</code>.</p>

<h3 id="openssl">OpenSSL</h3>

<p>I used <a href="https://www.openssl.org/">OpenSSL</a> to create Certificate Authorities and generate Certificates.
You can use other tools like <a href="https://github.com/cloudflare/cfssl">CFSSL</a>,
or <a href="https://github.com/OpenVPN/easy-rsa">EasyRSA</a>.
More info at https://kubernetes.io/docs/tasks/administer-cluster/certificates/</p>

<p>In macOS, enter:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>brew <span class="nb">install </span>openssl@3
</code></pre></div></div>

<h3 id="ssh-and-scp">SSH and SCP</h3>

<p>Also, I use <code class="language-plaintext highlighter-rouge">ssh</code> to connect to servers, and <code class="language-plaintext highlighter-rouge">scp</code> to copy files to them.</p>

<h3 id="provision-servers">Provision Servers</h3>

<p>Create a network:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">NETWORK_NAME</span><span class="o">=</span>network-1
hcloud network create <span class="nt">--name</span> <span class="k">${</span><span class="nv">NETWORK_NAME</span><span class="k">}</span> <span class="nt">--ip-range</span> 10.1.0.0/16
</code></pre></div></div>

<p>I used “US East” for location.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>hcloud network add-subnet <span class="k">${</span><span class="nv">NETWORK_NAME</span><span class="k">}</span> <span class="nt">--network-zone</span> us-east <span class="nt">--type</span> server <span class="nt">--ip-range</span> 10.1.1.0/24
</code></pre></div></div>

<p>It’s better add your/a SSH Key to the project.
So, it can be used for connecting to the servers.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>hcloud ssh-key create <span class="nt">--name</span> ssh-key-1 <span class="nt">--public-key-from-file</span> ~/.ssh/id_rsa.pub
</code></pre></div></div>

<p>Use your own ssh public key address instead of <code class="language-plaintext highlighter-rouge">id_rsa.pub</code>.</p>

<p>Minimum node counts for High Availability is 3.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">NODES</span><span class="o">=(</span>node-1 node-2 node-3<span class="o">)</span>

<span class="k">for </span>node <span class="k">in</span> <span class="k">${</span><span class="nv">NODES</span><span class="k">}</span><span class="p">;</span> <span class="k">do
  </span>hcloud server create <span class="se">\</span>
    <span class="nt">--name</span> <span class="k">${</span><span class="nv">node</span><span class="k">}</span> <span class="se">\</span>
    <span class="nt">--network</span> <span class="k">${</span><span class="nv">NETWORK_NAME</span><span class="k">}</span> <span class="se">\</span>
    <span class="nt">--image</span> ubuntu-22.04 <span class="se">\</span>
    <span class="nt">--type</span> cpx11 <span class="se">\</span>
    <span class="nt">--ssh-key</span> ssh-key-1 <span class="se">\</span>
    <span class="nt">--location</span> ash <span class="se">\</span>
    <span class="nt">--label</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="se">\</span>
    <span class="nt">--label</span> <span class="nv">cluster</span><span class="o">=</span>cluster-1
<span class="k">done</span>
</code></pre></div></div>

<p>I used the lightest server type (cpx11) for testing purposes.
Also, I used labels for selecting nodes in lists later.</p>

<p>You can use any name you wish for the servers.</p>

<h2 id="provisioning-a-ca-and-generating-tls-certificates">Provisioning a CA and Generating TLS Certificates</h2>

<h3 id="create-the-ca-certificate-and-key">Create the CA Certificate and Key</h3>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>openssl genrsa <span class="nt">-out</span> etcd-ca.key 2048
openssl req <span class="nt">-x509</span> <span class="nt">-new</span> <span class="nt">-nodes</span> <span class="nt">-key</span> etcd-ca.key <span class="nt">-subj</span> <span class="s2">"/CN=etcd-ca"</span> <span class="nt">-days</span> 5475 <span class="nt">-out</span> etcd-ca.crt
</code></pre></div></div>

<p>5475 Days is 15 years.</p>

<h3 id="generate-and-sign-etcd-member-certificates">Generate and Sign etcd Member Certificates</h3>

<p>Create a template file for config:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cat</span> <span class="o">&lt;&lt;</span><span class="no">EOF</span><span class="sh"> | tee etcd-server.conf.tpl
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn

[ dn ]
CN = &lt;NODE_NAME&gt;

[ req_ext ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = &lt;NODE_NAME&gt;
DNS.2 = localhost
IP.1 = 127.0.0.1
IP.2 = &lt;NODE_INTERNAL_IP&gt;

[ v3_ext ]
subjectAltName=@alt_names
</span><span class="no">EOF
</span></code></pre></div></div>

<p>We need 2 certificates for each etcd node.
Server certificate for authenticating client requests,
and Peer certificate to communicate with other nodes.</p>

<p>Although it’s not mandatory,
using separate certificates for different purposes adds an extra layer of security
and minimizes the risk of compromise in a distributed system like etcd.</p>

<p>For each member (Server Cert):</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="k">for </span>NODE <span class="k">in</span> <span class="si">$(</span>hcloud server list <span class="nt">--selector</span> <span class="nv">cluster</span><span class="o">=</span>cluster-1 <span class="nt">--selector</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="nt">-o</span> noheader <span class="nt">-o</span> <span class="nv">columns</span><span class="o">=</span>name<span class="si">)</span><span class="p">;</span> <span class="k">do
  </span>openssl genrsa <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.key 2048

  <span class="nv">INTERNAL_IP</span><span class="o">=</span><span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{ (index .PrivateNet 0).IP}}'</span><span class="si">)</span>

  <span class="nb">cat </span>etcd-server.conf.tpl | <span class="nb">sed</span> <span class="nt">-e</span> <span class="s2">"s/</span><span class="se">\&lt;</span><span class="s2">NODE_NAME&gt;/</span><span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="s2">/"</span> <span class="nt">-e</span> <span class="s2">"s/</span><span class="se">\&lt;</span><span class="s2">NODE_INTERNAL_IP&gt;/</span><span class="k">${</span><span class="nv">INTERNAL_IP</span><span class="k">}</span><span class="s2">/"</span> <span class="o">&gt;</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.conf 

  openssl req <span class="nt">-new</span> <span class="nt">-key</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.key <span class="se">\</span>
    <span class="nt">-config</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.conf <span class="se">\</span>
    <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.csr
  openssl x509 <span class="nt">-req</span> <span class="nt">-in</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.csr <span class="nt">-CA</span> etcd-ca.crt <span class="nt">-CAkey</span> etcd-ca.key <span class="se">\</span>
    <span class="nt">-sha256</span> <span class="nt">-CAcreateserial</span> <span class="nt">-days</span> 730 <span class="nt">-extensions</span> v3_ext <span class="nt">-extfile</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.conf <span class="se">\</span>
    <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.crt
<span class="k">done</span>

</code></pre></div></div>

<p>For each member (Peer Cert):</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="k">for </span>NODE <span class="k">in</span> <span class="si">$(</span>hcloud server list <span class="nt">--selector</span> <span class="nv">cluster</span><span class="o">=</span>cluster-1 <span class="nt">--selector</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="nt">-o</span> noheader <span class="nt">-o</span> <span class="nv">columns</span><span class="o">=</span>name<span class="si">)</span><span class="p">;</span> <span class="k">do
  </span>openssl genrsa <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.key 2048

  <span class="nv">EXTERNAL_IP</span><span class="o">=</span><span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>
  <span class="nv">INTERNAL_IP</span><span class="o">=</span><span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{ (index .PrivateNet 0).IP}}'</span><span class="si">)</span>

  <span class="nb">cat </span>etcd-server.conf.tpl | <span class="nb">sed</span> <span class="nt">-e</span> <span class="s2">"s/</span><span class="se">\&lt;</span><span class="s2">NODE_NAME&gt;/</span><span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="s2">/"</span> <span class="nt">-e</span> <span class="s2">"s/</span><span class="se">\&lt;</span><span class="s2">NODE_INTERNAL_IP&gt;/</span><span class="k">${</span><span class="nv">INTERNAL_IP</span><span class="k">}</span><span class="s2">/"</span> <span class="nt">-e</span> <span class="s2">"s/</span><span class="se">\&lt;</span><span class="s2">NODE_EXTERNAL_IP&gt;/</span><span class="k">${</span><span class="nv">EXTERNAL_IP</span><span class="k">}</span><span class="s2">/"</span> <span class="o">&gt;</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.conf 

  openssl req <span class="nt">-new</span> <span class="nt">-key</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.key <span class="se">\</span>
    <span class="nt">-config</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.conf <span class="se">\</span>
    <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.csr
  openssl x509 <span class="nt">-req</span> <span class="nt">-in</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.csr <span class="nt">-CA</span> etcd-ca.crt <span class="nt">-CAkey</span> etcd-ca.key <span class="se">\</span>
    <span class="nt">-sha256</span> <span class="nt">-CAcreateserial</span> <span class="nt">-days</span> 730 <span class="nt">-extensions</span> v3_ext <span class="nt">-extfile</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.conf <span class="se">\</span>
    <span class="nt">-out</span> <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.crt
<span class="k">done</span>

</code></pre></div></div>

<h3 id="copy-certificates-to-servers">Copy Certificates to servers</h3>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="k">for </span>NODE <span class="k">in</span> <span class="si">$(</span>hcloud server list <span class="nt">--selector</span> <span class="nv">cluster</span><span class="o">=</span>cluster-1 <span class="nt">--selector</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="nt">-o</span> noheader <span class="nt">-o</span> <span class="nv">columns</span><span class="o">=</span>name<span class="si">)</span><span class="p">;</span> <span class="k">do
  </span>scp etcd-ca.crt root@<span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>:~/
  scp <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.key root@<span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>:~/etcd-server.key
  scp <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-server</span>.crt root@<span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>:~/etcd-server.crt
  scp <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.key root@<span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>:~/etcd-peer.key
  scp <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="nt">-etcd-peer</span>.crt root@<span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>:~/etcd-peer.crt
<span class="k">done</span>

</code></pre></div></div>

<h2 id="install">Install</h2>

<p>Now, we can go to the servers to continue the work on them.</p>

<h3 id="ssh-to-node">SSH to Node</h3>

<p>On all servers:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
ssh root@<span class="si">$(</span>hcloud server describe &lt;NODE_NAME&gt; <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{.PublicNet.IPv4.IP}}'</span><span class="si">)</span>

</code></pre></div></div>

<p>Replace <code class="language-plaintext highlighter-rouge">&lt;NODE_NAME&gt;</code> with real node names.</p>

<h3 id="install-etcd">Install etcd</h3>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>apt update
apt <span class="nb">install</span> <span class="nt">-y</span> etcd
</code></pre></div></div>

<p>In this article etcd 3.5.10 is released.
Future releases may have a different configuration.</p>

<h2 id="configure">Configure</h2>

<p>Now etcd is installed and running in single mode on each node.
So, we need to update the configurations.</p>

<p>I create a subdirectory for <code class="language-plaintext highlighter-rouge">etcd</code> in its favorite parent directory.
Move and rename all certificates there.
Then, change the directory owner to the use <code class="language-plaintext highlighter-rouge">etcd</code>, which has been created on installing etcd.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">mkdir</span> <span class="nt">-p</span> /etc/etcd
<span class="nb">mv</span> ~/etcd-ca.crt /etc/etcd/ca.crt
<span class="nb">mv</span> ~/etcd-server.key /etc/etcd/server.key
<span class="nb">mv</span> ~/etcd-server.crt /etc/etcd/server.crt
<span class="nb">mv</span> ~/etcd-peer.key /etc/etcd/peer.key
<span class="nb">mv</span> ~/etcd-peer.crt /etc/etcd/peer.crt
<span class="nb">chown</span> <span class="nt">-R</span> etcd /etc/etcd
</code></pre></div></div>

<p>Now we need to update etcd config values to make them a cluster.</p>

<p>For some values we need to provide them on our own machine which the <code class="language-plaintext highlighter-rouge">hcloud</code> cli is configured.</p>

<p>I wrote this script to provide value of <code class="language-plaintext highlighter-rouge">ETCD_INITIAL_CLUSTER</code> variable:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="nv">ARRAY</span><span class="o">=()</span>
<span class="k">for </span>NODE <span class="k">in</span> <span class="si">$(</span>hcloud server list <span class="nt">--selector</span> <span class="nv">cluster</span><span class="o">=</span>cluster1 <span class="nt">--selector</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="nt">-o</span> noheader <span class="nt">-o</span> <span class="nv">columns</span><span class="o">=</span>name<span class="si">)</span><span class="p">;</span> <span class="k">do
  </span>ARRAY+<span class="o">=(</span><span class="s2">"</span><span class="k">${</span><span class="nv">NODE</span><span class="k">}</span><span class="s2">=https://</span><span class="si">$(</span>hcloud server describe <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span> <span class="nt">-o</span> <span class="nv">format</span><span class="o">=</span><span class="s1">'{{ (index .PrivateNet 0).IP}}'</span><span class="si">)</span><span class="s2">:2380"</span><span class="o">)</span>
<span class="k">done

</span><span class="nv">ETCD_INITIAL_CLUSTER</span><span class="o">=</span><span class="si">$(</span><span class="nb">printf</span> <span class="s2">",%s"</span> <span class="s2">"</span><span class="k">${</span><span class="nv">ARRAY</span><span class="p">[@]</span><span class="k">}</span><span class="s2">"</span><span class="si">)</span>
<span class="nv">ETCD_INITIAL_CLUSTER</span><span class="o">=</span><span class="k">${</span><span class="nv">ETCD_INITIAL_CLUSTER</span>:1<span class="k">}</span>
<span class="nb">echo</span> <span class="nv">$ETCD_INITIAL_CLUSTER</span>

</code></pre></div></div>

<p>Run this on your own machine and use it when editing the etcd config file.</p>

<p>Also, you need each server internal ip.
Make sure to use the correct value on each server.</p>

<p>Then, we need to uncomment and update these values in the <code class="language-plaintext highlighter-rouge">/etc/default/etcd</code> file.</p>

<ul>
  <li>ETCD_NAME: use hostname value for each server.</li>
  <li>ETCD_INITIAL_CLUSTER: $ETCD_INITIAL_CLUSTER</li>
  <li>ETCD_INITIAL_ADVERTISE_PEER_URLS: <code class="language-plaintext highlighter-rouge">https://${INTERNAL_IP}:2380</code></li>
  <li>ETCD_LISTEN_PEER_URLS: <code class="language-plaintext highlighter-rouge">https://${INTERNAL_IP}:2380</code></li>
  <li>ETCD_LISTEN_CLIENT_URLS: <code class="language-plaintext highlighter-rouge">https://${INTERNAL_IP}:2379,https://127.0.0.1:2379</code></li>
  <li>ETCD_ADVERTISE_CLIENT_URLS: <code class="language-plaintext highlighter-rouge">https://${INTERNAL_IP}:2379</code></li>
  <li>ETCD_CLIENT_CERT_AUTH: <code class="language-plaintext highlighter-rouge">true</code></li>
  <li>ETCD_PEER_CLIENT_CERT_AUTH: <code class="language-plaintext highlighter-rouge">true</code></li>
  <li>ETCD_TRUSTED_CA_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/ca.crt</code></li>
  <li>ETCD_CERT_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/server.crt</code></li>
  <li>ETCD_KEY_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/server.key</code></li>
  <li>ETCD_PEER_TRUSTED_CA_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/ca.crt</code></li>
  <li>ETCD_PEER_CERT_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/peer.crt</code></li>
  <li>ETCD_PEER_KEY_FILE: <code class="language-plaintext highlighter-rouge">/etc/etcd/peer.key</code></li>
</ul>

<p>Use real values in the above variables.</p>

<p>Now restart etcd service on each server:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>systemctl restart etcd.service
</code></pre></div></div>

<p>If it didn’t restart successfully, check the etcd log with:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>journalctl <span class="nt">-xeu</span> etcd.service
</code></pre></div></div>

<h2 id="verify">Verify</h2>

<p>To verify if it works enter this command on each server:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">ETCDCTL_API</span><span class="o">=</span>3 etcdctl member list <span class="se">\</span>
  <span class="nt">--endpoints</span><span class="o">=</span>https://127.0.0.1:2379 <span class="se">\</span>
  <span class="nt">--cacert</span><span class="o">=</span>/etc/etcd/ca.crt <span class="se">\</span>
  <span class="nt">--cert</span><span class="o">=</span>/etc/etcd/server.crt <span class="se">\</span>
  <span class="nt">--key</span><span class="o">=</span>/etc/etcd/server.key
</code></pre></div></div>

<p>Output should be like:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>11a85cd56d9530bf, started, node-1, https://10.1.1.1:2380, https://10.1.1.1:2379
6e6ca551d2049908, started, node-2, https://10.1.1.2:2380, https://10.1.1.2:2379
cba426eb7a695eef, started, node-3, https://10.1.1.3:2380, https://10.1.1.3:2379
</code></pre></div></div>

<p>You can store a value in a node:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">ETCDCTL_API</span><span class="o">=</span>3 etcdctl <span class="nb">set </span>foo bar <span class="se">\</span>
  <span class="nt">--endpoints</span><span class="o">=</span>https://127.0.0.1:2379 <span class="se">\</span>
  <span class="nt">--cacert</span><span class="o">=</span>/etc/etcd/ca.crt <span class="se">\</span>
  <span class="nt">--cert</span><span class="o">=</span>/etc/etcd/server.crt <span class="se">\</span>
  <span class="nt">--key</span><span class="o">=</span>/etc/etcd/server.key
</code></pre></div></div>

<p>and check it on other nodes:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">ETCDCTL_API</span><span class="o">=</span>3 etcdctl get foo <span class="se">\</span>
  <span class="nt">--endpoints</span><span class="o">=</span>https://127.0.0.1:2379 <span class="se">\</span>
  <span class="nt">--cacert</span><span class="o">=</span>/etc/etcd/ca.crt <span class="se">\</span>
  <span class="nt">--cert</span><span class="o">=</span>/etc/etcd/server.crt <span class="se">\</span>
  <span class="nt">--key</span><span class="o">=</span>/etc/etcd/server.key
</code></pre></div></div>

<p>It should show <code class="language-plaintext highlighter-rouge">bar</code> as value.</p>

<h2 id="further-considerations">Further considerations</h2>

<p>You now have a fully operational etcd cluster, which you can integrate into your Kubernetes environment.</p>

<p>When updating, to ensure its continued reliability, remember to consult changelogs to avoid disruptions or data loss.</p>

<p>Additionally, please be aware that server and peer certificates have a two-year validity period,
so it’s essential to proactively renew and replace them before they expire.</p>

<h2 id="cleaning-up">Cleaning up</h2>

<p>for removing servers and network you can run:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">for </span>NODE <span class="k">in</span> <span class="si">$(</span>hcloud server list <span class="nt">--selector</span> <span class="nv">cluster</span><span class="o">=</span>cluster-1 <span class="nt">--selector</span> <span class="nv">etcd</span><span class="o">=</span><span class="nb">true</span> <span class="nt">-o</span> noheader <span class="nt">-o</span> <span class="nv">columns</span><span class="o">=</span>name<span class="si">)</span><span class="p">;</span> <span class="k">do
  </span>hcloud server delete <span class="k">${</span><span class="nv">NODE</span><span class="k">}</span>
<span class="k">done

</span><span class="nv">NETWORK_NAME</span><span class="o">=</span>network-1
hcloud network delete <span class="k">${</span><span class="nv">NETWORK_NAME</span><span class="k">}</span>

hcloud ssh-key delete ssh-key-1
</code></pre></div></div>]]></content><author><name>Naser Mirzaei</name></author><category term="etcd" /><category term="kubernetes" /><summary type="html"><![CDATA[Before set up, we should know what is it and why we need it? It helps us to understand how important it is (or it’s not).]]></summary></entry><entry><title type="html">Multi-job</title><link href="https://nasermirzaei89.net/2021/12/22/multi-job/" rel="alternate" type="text/html" title="Multi-job" /><published>2021-12-22T00:00:00+00:00</published><updated>2021-12-22T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2021/12/22/multi-job</id><content type="html" xml:base="https://nasermirzaei89.net/2021/12/22/multi-job/"><![CDATA[<p>As a Software Developer or, to be more general a Software Engineer,
I experienced different work situations, teams, and projects.
Each one had some good and bad points, in my opinion.
Work is an important part of our life as a coder, developer, software engineer, or what else you name us.
Most of us work for more than common work hours, and I think it’s not just some hard workers that work hard;
Our work is as the same as our hobby!</p>

<p>After (or even before) the coronavirus pandemic, most employees have the chance to work from home.
So, working hours became almost meaningless.
Also, bad economic conditions put pressure on the people.
Then…</p>

<p>We started to work for multiple companies simultaneously! 😶</p>

<p>These days I see more and more people that work in multiple companies, Or even some freelancers that overwork!</p>

<p>I was (and still am) one of them.
First I fell into the trap of a decision (a long story), then I was tempted by more income 🤑</p>

<p>Most of us say to ourselves that we can work and earn more, why not do that?</p>

<p>But after some months, I realized it was not worth it.</p>

<p>We know the number 1 reason for multi-job is earning, and other reasons are rare.
But will we really achieve this goal by this action?</p>

<p>Quick Answer: “Yes” in short term, “No” in long term.</p>

<p>What will occur if you overwork in more than one job?</p>

<ul>
  <li>Yes, you earn more than usual!</li>
  <li>You have job safety (what does it mean for a developer!!!)</li>
  <li>You get more experience because you work in 2 different environments.</li>
  <li>You have less free time to live, spend with family, or have fun.</li>
</ul>

<p>Why do you want to earn more money?
You can’t afford your expenses?
You want to earn now and spend in old ages?</p>

<p>You have less time to read and learn new skills or technologies to improve yourself in work because you always work and can’t put more time and energy to explore new things.</p>

<p>You can’t be devoted to a project if you love it.
Most of us have the chance to find the project that we are working on amazing and invest more time and energy in it,
and enjoy it!
Keep in mind work plays an important role in our life, so it’s better to work on what you love, not less than it.</p>

<p>You don’t (can’t) put enough energy and focus on the project, so you are less efficient than your degree,
so the employer has to hire more people or even produce products with a lower quality.
Then, it’s not a Win-Win game.
every action like this leads to Lose-Lose in long term.</p>

<blockquote>
  <p>Human is enough idiot to think they are smart!</p>
</blockquote>

<p>But what if we stick to one job and choose another solution?</p>

<p>What if we make ourselves more worthy for the company and get promotions and earn more?</p>

<p>Maybe some of you say that who knows our value!</p>

<p>I can tell you just change your current situation.
Find your paradise or make your current location a new paradise.
We did it once, Believe in me!</p>

<p>If you work just in one job, you have more time.
So, you have more chances to start a side project.
Side projects are awesome!
you learn a lot from them, and they grow you faster and make you deserve to have some labels like: senior, etc.
Everyone should be paid for what they deserve.</p>

<p>No conclusion 😁</p>

<p>I hope reading this helps you to decide better on choosing the way.
Also, always it depends.
Maybe you have a different experience and disagree with me.
For example, I have a little freelancing experience and can’t say my opinion about them.
By the way, it’s my experience and my decision 🙂</p>]]></content><author><name>Naser Mirzaei</name></author><summary type="html"><![CDATA[Working in multiple jobs or not?]]></summary></entry><entry><title type="html">Why Stack Overflow reputation is bullshit! and why it’s not!</title><link href="https://nasermirzaei89.net/2021/04/02/why-stackoverflow-reputation-is-bullshit-and-why-its-not/" rel="alternate" type="text/html" title="Why Stack Overflow reputation is bullshit! and why it’s not!" /><published>2021-04-02T00:00:00+00:00</published><updated>2021-04-02T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2021/04/02/why-stackoverflow-reputation-is-bullshit-and-why-its-not</id><content type="html" xml:base="https://nasermirzaei89.net/2021/04/02/why-stackoverflow-reputation-is-bullshit-and-why-its-not/"><![CDATA[<p>When you start to work with some services like Stack Overflow or all other Stack Exchange products you find that there is a mechanism for users to get access to different abilities and features in the service, and it is the reputation service.
You can upvote or downvote others if you earned an amount of votes, and maybe some other restrictions.</p>

<p>This is an excellent mechanism to control spam.
But the problem is when some companies or people evaluate and score you based on your Stack Overflow reputation. Just compare the number of PHP developers with Rustaceans (Rust developers) in the world! So when somebody asks/answers a question in Stackoverflow about PHP, more people will upvote (sometimes downvote) their post. But could you say they are more wise or professional than a person who has activities about Rust in Stackoverflow, because they have many more reputations than the latter person?</p>

<p>Maybe you say, you should not compare two people in different communities or technologies with each other.
Of course Yes! But still we have two more problems:
First one is, nobody checks your reputation related to which technology that you use.
The second one (the more critical one) is that if you ask or answer a question that is more advanced than common problems, you will earn less reputation than asking or answering a common problem. Because there are fewer people that face that problem.</p>

<p>We have this problem in other services like GitHub for stars. Most times if you write a JS library it would earn more stars than a Golang library, because JS has more developers and also JS developers are more addicted to use libraries for trivial works. 😁</p>

<p>Also, some services like HackerRank have scoring mechanisms for developers based on the code in their repositories, and it leads to this problem if you use them for reviewing a developer’s abilities. Of course, I know most times these are gamification tricks to tempt them to make their profiles more complete or be more active on their services.</p>

<p>It would be good to mention “Goodhart’s Law” which is somehow related to this article. When these metrics become measurements, developers try to focus on improving their rank in these services.</p>

<blockquote>
  <p>There are some services like <a href="https://gimhub.com">gimhub.com</a> that you can buy GitHub Stars!!!</p>
</blockquote>

<p>It’s always hard to measure a developer’s abilities until you work with them on a project. So we should be careful when deciding based on these funny parameters.</p>]]></content><author><name>Naser Mirzaei</name></author><category term="StackOverflow" /><category term="Reputation" /><category term="GitHub" /><category term="Goodhart_Law" /><summary type="html"><![CDATA[We can misuse some metrics like Stackoverflow reputation or GitHub Stars!]]></summary></entry><entry><title type="html">Working Remotely</title><link href="https://nasermirzaei89.net/2020/04/17/working-remotely/" rel="alternate" type="text/html" title="Working Remotely" /><published>2020-04-17T00:00:00+00:00</published><updated>2020-04-17T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2020/04/17/working-remotely</id><content type="html" xml:base="https://nasermirzaei89.net/2020/04/17/working-remotely/"><![CDATA[<h2 id="what-is-it">What Is It</h2>

<p>Remote work has many other names, such as Telework, Working from Home (WFH), and so on.</p>

<p>I can define this terminology in one sentence:</p>

<blockquote>
  <p>When you don’t have to go to your office to do your job, you are working remotely.</p>
</blockquote>

<p><em>THAT’S IT</em></p>

<p>More description would be redundancy.</p>

<h2 id="benefits">Benefits</h2>

<p>After knowing what remote work is, you are going to find what its benefits are and how can it help us.</p>

<h3 id="for-company">For Company</h3>

<p>Most companies, as I have experienced, are against working remotely,
because they think that they need to see their employees when they are working.
Maybe they can’t trust their employees.
They believe in synergy in working on-site.
However, the problem is that you can be present whole the day in the office, but work less than an hour!</p>

<p>If the employer can measure your efficiency well, they can measure it remotely too.
In all Tech companies (and non-Tech ones) there are task management tools for managing projects and tasks.
It shows 3 situations:</p>

<ol>
  <li>What to do?</li>
  <li>What every employee is doing?</li>
  <li>What is done?</li>
</ol>

<p>As an employer, boss, team lead, or manager
you can see what’s going on by observing the board on the screen instead of watching employees and saying
“Yay, they are in their seats, and it seems they’re working!”</p>

<p>Also, there are some economic goals in remote work.
Imagine a company that is fully remote.
There are successful examples of it.
I remember two of them at this time: <a href="https://automattic.com/">Automattic</a> and <a href="https://buffer.com/">Buffer</a>.
Automattic is one of the first fully remote companies, I guess. OK, keep imagining!</p>

<p>There is no office or big office for working at. So there is no cost for it,
and you don’t have to have some on-site perks.</p>

<p>Also, you can hire from everywhere, not only the city your office is in it.
So, you can hire from where employees are cheaper, especially if you are in a high-cost city,
and you have more candidates to hire.</p>

<p>Another benefit of remote work is that you can prevent (or decrease) office relationships (because there is no office).
Office relationship (or Office romance) is a situation in which two members of a business in an office become romantically linked with one another.
You can search for the dangers of this situation for the company and employees.
Some companies have a rule: if you marry your colleague, one of you has to leave the company.</p>

<h3 id="for-employee">For Employee</h3>

<p>If you find the benefits of working remotely, you never want to work on-site.</p>

<p>How much time do you lose in traffic jam? 2 hours on average! Does the company compensate for that?</p>

<p>How do you feel after that? You would smell like exhaust fumes.</p>

<p>You wouldn’t eat breakfast because you are in a hurry to get to the office on time?</p>

<p>You don’t have to move to the office every day in remote work.</p>

<p>Would you always like to order expensive and low-quality foods for lunch? Or eat the food of the night before?</p>

<p>You can eat or cook a fresh meal like your weekends when you work remotely.</p>

<p>You have an open office and when somebody gets a cold, everyone is at risk of getting infected?</p>

<p>The office is hot for you? Is it too cold because the air conditioner is always turned on?</p>

<p>Tired of noises in the office? Do you hate putting on headphones all the day?</p>

<p>You are safe at home. You can choose the temperature as you like, Turn your music player on,
and start your work after a delicious breakfast.</p>

<p>Is this city too crowded? or have high costs? Work everywhere you want. Location doesn’t matter.
You can earn like an engineer in the capital, and spend like the lord of a village.</p>

<p>You don’t have to leave your parents and travel to a city for better opportunities.</p>

<p>Also, imagine you can travel to Bali Islands and keep working and earning.</p>

<h3 id="for-society-and-the-nature">For Society and the Nature</h3>

<p>The most important benefit for the public is decreasing the traffic jams and air pollution.
All of us almost know how many people commute every day.
Isn’t it a good way to decrease it?</p>

<h3 id="last-words">Last Words</h3>

<p>Before the COVID-19 outbreak, there were many companies and people that found the benefits of remote work,
but these days, many people have to stay at home. Therefore, working remotely is bolder than before,
and maybe it’s the start of a revolution in the way people work.</p>

<p>As we experienced in our current company (that I tried many times to tell them benefits of remote work),
our colleagues that stayed at home and worked remotely, they benefit from  a higher level of job satisfaction.</p>

<p>I hope more companies, including our company, find that working remotely is a good choice for them and their employees,
and brings many benefits and improves the standards for their work family and society.</p>]]></content><author><name>Naser Mirzaei</name></author><category term="remote" /><category term="work" /><category term="telecommuting" /><summary type="html"><![CDATA[Working remotely has many benefits for companies, employees, and even earth planet.]]></summary></entry><entry><title type="html">Deadlocks In Golang</title><link href="https://nasermirzaei89.net/2020/03/07/deadlocks-in-golang/" rel="alternate" type="text/html" title="Deadlocks In Golang" /><published>2020-03-07T00:00:00+00:00</published><updated>2020-03-07T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2020/03/07/deadlocks-in-golang</id><content type="html" xml:base="https://nasermirzaei89.net/2020/03/07/deadlocks-in-golang/"><![CDATA[<h2 id="problem">Problem</h2>

<p>Run this golang code:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">package</span> <span class="n">main</span>

<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">bool</span><span class="p">)</span>
	<span class="n">ch</span> <span class="o">&lt;-</span> <span class="no">true</span>
<span class="p">}</span>

</code></pre></div></div>

<p>You can run it here: <a href="https://play.golang.org/p/Eqa5-D2bC0i">https://play.golang.org/p/Eqa5-D2bC0i</a></p>

<p>The last line waits forever for someone to read the channel. So, the program will be stuck and never ends.</p>

<p>It’s a <strong>Deadlock</strong>.</p>

<p>Fortunately, Golang detects deadlocks in runtime, and you will get this:</p>

<blockquote>
  <p>The First time I faced deadlock was when I was working on a <a href="https://github.com/golang/go/wiki/WebAssembly">WebAssembly</a> program.</p>
</blockquote>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
	/tmp/sandbox459053840/prog.go:5 +0x60

Program exited: status 2.
</code></pre></div></div>

<p>A deadlock occurs when a goroutine is waiting for a channel forever.</p>

<p>There are some more examples of deadlock</p>

<h2 id="example-i">Example I</h2>

<p>When a goroutine writes to a channel that will never be read. Like the above code in the first of this article.</p>

<h2 id="example-ii">Example II</h2>

<p>When a goroutine reads from a channel that no one will write to it:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">package</span> <span class="n">main</span>

<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">bool</span><span class="p">)</span>
	<span class="o">&lt;-</span> <span class="n">ch</span>
<span class="p">}</span>
</code></pre></div></div>

<h2 id="example-iii">Example III</h2>

<p>When a group goroutines wait for each other to fill channels,
but they can’t fill them because they are waiting for a channel to fill:</p>

<div class="language-go highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">package</span> <span class="n">main</span>

<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="n">ch1</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">int</span><span class="p">)</span>
	<span class="n">ch2</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">int</span><span class="p">)</span>
	<span class="n">done</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">bool</span><span class="p">)</span>

	<span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span>
		<span class="o">&lt;-</span><span class="n">ch1</span>
		<span class="n">ch2</span> <span class="o">&lt;-</span> <span class="m">1</span>
	<span class="p">}()</span>

	<span class="k">go</span> <span class="k">func</span><span class="p">()</span> <span class="p">{</span>
		<span class="o">&lt;-</span><span class="n">ch2</span>
		<span class="n">ch1</span> <span class="o">&lt;-</span> <span class="m">1</span>
		<span class="n">done</span> <span class="o">&lt;-</span> <span class="no">true</span>
	<span class="p">}()</span>

	<span class="o">&lt;-</span><span class="n">done</span>
<span class="p">}</span>
</code></pre></div></div>

<p><img src="https://nasermirzaei89.net/assets/gridlock.svg" alt="Gridlock on a network of two-way streets. The red cars are those causing the gridlock by stopping in the middle of the intersection." /></p>

<h2 id="sync-package">Sync Package</h2>

<p>Deadlock also occurs in <a href="https://golang.org/pkg/sync/">sync</a> package when a goroutine is waiting for a lock forever.</p>

<h2 id="last-words">Last words</h2>

<p>Source codes with deadlock occurrence compile and build successfully,
but panics in runtime.</p>

<p>So, care when you use channels (and sync package),
and write good tests to trap deadlocks before deployment.</p>

<p>✨ <em><a href="https://en.wikipedia.org/wiki/Gridlock">Gridlock</a> image is from <a href="https://wikipedia.org/">Wikipedia</a></em></p>]]></content><author><name>Naser Mirzaei</name></author><category term="Deadlock" /><category term="Golang" /><category term="Goroutine" /><summary type="html"><![CDATA[A deadlock happens when a goroutine is reading from or writing a channel that never can be used]]></summary></entry><entry><title type="html">Write A Great Ci For Gitlab</title><link href="https://nasermirzaei89.net/2019/03/09/write-a-great-ci-for-gitlab/" rel="alternate" type="text/html" title="Write A Great Ci For Gitlab" /><published>2019-03-09T00:00:00+00:00</published><updated>2019-03-09T00:00:00+00:00</updated><id>https://nasermirzaei89.net/2019/03/09/write-a-great-ci-for-gitlab</id><content type="html" xml:base="https://nasermirzaei89.net/2019/03/09/write-a-great-ci-for-gitlab/"><![CDATA[<h2 id="problem">Problem</h2>
<p>When you develop an application or service, you need to test it in a production-like environment, or store versions to revert to previous versions on fails.</p>

<p>These steps are a bit hard and annoying if you do all of them manually. So you need a full-time person to do them even at midnight.</p>

<h2 id="solution">Solution</h2>
<p><a href="https://gitlab.com">GitLab</a> (and <a href="https://github.com">GitHub</a>) has a great tool for automating Deployment named <a href="https://about.gitlab.com/product/continuous-integration/">Gitlab-CI</a>. I don’t want to introduce them. You can read their documentation and about sections if you want!</p>

<p>So, let’s do these steps to have a complete ci/cd process:</p>

<h3 id="step-0">Step 0</h3>

<p>First, you should use <a href="https://git-scm.com">Git</a> for version control and <a href="https://www.docker.com/">Docker</a> for containerize your builds, and so forth.
If you haven’t used them yet, you’re not a developer! Shame on you 😒</p>

<p>I assume I have a simple web server in <a href="https://golang.org">Golang</a> and want to deploy it on a <a href="https://kubernetes.io">Kubernetes</a> cluster in both staging and production environment.</p>

<h3 id="step-1-using-a-workflow">Step 1: Using a Workflow</h3>
<p>There are many flows for coding and deployment such as <a href="https://datasift.github.io/gitflow/IntroducingGitFlow.html">Git Flow</a> or <a href="https://docs.gitlab.com/ee/workflow/gitlab_flow.html">GitLab FLow</a>. You can read about them and use either if you want.</p>

<p>I often use this flow or a simpler one:</p>

<ol>
  <li>Develop a feature or fix a bug in a branch (custom name or create a branch from a gitlab issue)</li>
  <li>Send a merge request to master and merge it if all CI steps passed.</li>
  <li>Deploy the latest commit of master to the staging environment</li>
  <li>Tag a commit on master and deploy it to production if I want to release a new version</li>
</ol>

<p>In this flow, I always test new features on staging before release. If I find a small bug in production, I will fix it and release a new version. Or if I find a big bug, I will deploy an older version, and release a new version when I fix the bug. I really suggest staging to prevent testing in production.</p>

<blockquote>
  <p>Remember that staging is an environment to test the current project, not projects that use it. For example, the staging environment of rest is not to test the android application that uses this rest!</p>
</blockquote>

<h3 id="step-2-write-gitlab-ci-file">Step 2: Write GitLab CI File</h3>

<p>As I said before, you should use Docker to build and containerize your application. I wrote a <a href="https://nasermirzaei89.net/2018/12/25/dockerize-go-application/">Blog Post</a> before for dockerizing a go application.</p>

<p>Add a <code class="language-plaintext highlighter-rouge">.gitlab-ci.yml</code> file to your project:</p>
<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">touch</span> .gitlab-ci.yml
</code></pre></div></div>

<p>You should have 3 stages in your CI:</p>

<ul>
  <li>Build</li>
  <li>Test</li>
  <li>Deploy</li>
</ul>

<p>Add them to your CI file:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">stages</span><span class="pi">:</span>
  <span class="pi">-</span> <span class="s">Build</span>
  <span class="pi">-</span> <span class="s">Test</span>
  <span class="pi">-</span> <span class="s">Deploy</span>
</code></pre></div></div>

<p>You can split your steps into 3 sections:</p>

<ul>
  <li>Development</li>
  <li>Staging</li>
  <li>Production</li>
</ul>

<h4 id="lint">Lint</h4>

<p>I always check my code is pretty linted only in development:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Development</span>

<span class="na">Lint</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golangci/golangci-lint:v1.21.0</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make lint</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>As you see, this step will run on every branch except master.
I know that alpine images are smaller than default, but I didn’t use alpine because I need <code class="language-plaintext highlighter-rouge">Makefile</code> support.</p>

<blockquote>
  <p>You can add a pre-commit hook to check lint step on your local repo before gitlab!</p>
</blockquote>

<h4 id="build">Build</h4>

<p>After checking lint you should check that your code will build or not:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Build</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golang:1.13</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make build</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>I always use a version of Golang that is installed on my development pc.</p>

<p>As you see, this step is also excluded from the master branch. because I only check whether my code builds or not, and leave the binary alone after every build.</p>

<h4 id="test">Test</h4>

<p>Like lint step, we also should check our new code will pass all tests or not:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Test</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golang:1.13</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make test</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>You can split this step to multiple steps if you have separate tests:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Unit Test</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">_MyUnitTesterImage_</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make unit-test</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>

<span class="na">Acceptance Test</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">_MyAcceptanceTesterImage_</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make acceptance-test</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="c1"># Other tests...</span>
</code></pre></div></div>

<p>After these steps, your code may merge to master. You can prevent code to merge before passing ci in your project settings page.</p>

<p>Now your code merged to master it’s time to run CI jobs on this branch.</p>

<h4 id="build-image-latest">Build Image Latest</h4>

<p>in this step you should build an image for the staging environment:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Staging</span>

<span class="na">Build Image Latest</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">docker</span>
  <span class="na">services</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker:dind</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY</span>
    <span class="pi">-</span> <span class="s">docker build --tag $CI_REGISTRY_IMAGE:latest .</span>
    <span class="pi">-</span> <span class="s">docker push $CI_REGISTRY_IMAGE:latest</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>In this step we use Docker for building an image, so we must use a docker image.</p>

<p>At the first script, we assume to use our gitlab registry to push our image to it. So, we must log in to this registry before build and push. <code class="language-plaintext highlighter-rouge">$CI_JOB_TOKEN</code> is a token to login in your gitlab registry and <code class="language-plaintext highlighter-rouge">$CI_REGISTRY</code> is the address of the registry.</p>

<p>After the login, we build our image with a tag name that contains <code class="language-plaintext highlighter-rouge">$CI_REGISTRY_IMAGE</code> and docker tag <code class="language-plaintext highlighter-rouge">latest</code>. for example, this will be: <code class="language-plaintext highlighter-rouge">registry.nasermirzaei89.net/myproject/api:latest</code></p>

<p>At the end, we push the image to the registry. Now you can check your registry to see your new image created with <code class="language-plaintext highlighter-rouge">latest</code> tag.</p>

<h4 id="deploy-staging">Deploy Staging</h4>

<p>I always deploy my code automatically to staging:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Deploy Staging</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Deploy</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">registry.nasermirzaei89.net/gitlab-ci/kubectl:latest</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">cat kubernetes.tpl.yml | sed "s//myproject-staging/g; s//latest/g; s//api.staging.myproject.nasermirzaei89.net/g" | kubectl apply -f -</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>Again, I used a pre-made image for a step. As you see, I deploy my code to a kubernetes cluster by kubectl.
I built this image with the config of my cluster.
So, it connects to my cluster without requesting authorization info.
Also, I used a template file for deploying on kubernetes and replaced variables with <code class="language-plaintext highlighter-rouge">sed</code> command.</p>

<p>Now it’s time to release!</p>

<h4 id="release-a-tag">Release a Tag</h4>

<p>When you want to add a tag to a commit for a version, you should create an image from this tag automatically:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Build Image Tag</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">docker</span>
  <span class="na">services</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker:dind</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY</span>
    <span class="pi">-</span> <span class="s">docker build --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG .</span>
    <span class="pi">-</span> <span class="s">docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">$CI_COMMIT_TAG</code> is the name of the tag, eg: <code class="language-plaintext highlighter-rouge">v1.2.0</code>.
So, it doesn’t mean that this step only runs on <code class="language-plaintext highlighter-rouge">tags</code></p>

<p>after passing this build stage on a tag, see the registry section on your gitlab to make sure the image has been built with the expected tag.</p>

<p>Be careful you said to run on master or except master in last steps so many of them will run on <code class="language-plaintext highlighter-rouge">tags</code>. You need to exclude them from tags to prevent running them on tagging a version.
Add this to all last steps.</p>
<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<h4 id="deploy-production">Deploy Production</h4>

<p>After building an image with a specific tag, we want to deploy it on production:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">Deploy Production</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Deploy</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">registry.nasermirzaei89.net/gitlab-ci/kubectl:latest</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">cat kubernetes.tpl.yml | sed "s//myproject/g; s//$CI_COMMIT_TAG/g; s//api.myproject.nasermirzaei89.net/g" | kubectl apply -f -</span>
  <span class="na">when</span><span class="pi">:</span> <span class="s">manual</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>You can see I used <code class="language-plaintext highlighter-rouge">$CI_COMMIT_TAG</code> instead of <code class="language-plaintext highlighter-rouge">latest</code> in my variable replacement.</p>

<p>Also, this step is <code class="language-plaintext highlighter-rouge">manual</code> and you must play it manually. So, when you want to revert to an earlier version, you can play <code class="language-plaintext highlighter-rouge">Deploy Production</code> job on that version in pipelines, and that version will deploy to production.</p>

<p>Your <code class="language-plaintext highlighter-rouge">.gitlab-ci.yml</code> file at a glance:</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">stages</span><span class="pi">:</span>
  <span class="pi">-</span> <span class="s">Build</span>
  <span class="pi">-</span> <span class="s">Test</span>
  <span class="pi">-</span> <span class="s">Deploy</span>

<span class="c1"># Development</span>

<span class="na">Lint</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golangci/golangci-lint:v1.21.0</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make lint</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="na">Build</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golang:1.13</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make build</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="na">Test</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Test</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">golang:1.13</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">make test</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="c1"># Staging</span>

<span class="na">Build Image Latest</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">docker</span>
  <span class="na">services</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker:dind</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY</span>
    <span class="pi">-</span> <span class="s">docker build --tag $CI_REGISTRY_IMAGE:latest .</span>
    <span class="pi">-</span> <span class="s">docker push $CI_REGISTRY_IMAGE:latest</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="na">Deploy Staging</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Deploy</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">registry.nasermirzaei89.net/gitlab-ci/kubectl:latest</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">cat kubernetes.tpl.yml | sed "s//myproject-staging/g; s//latest/g; s//api.staging.myproject.nasermirzaei89.net/g" | kubectl apply -f -</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">master</span>
  <span class="na">except</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="na">Build Image Tag</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">build</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">docker</span>
  <span class="na">services</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker:dind</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY</span>
    <span class="pi">-</span> <span class="s">docker build --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG .</span>
    <span class="pi">-</span> <span class="s">docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>

<span class="na">Deploy Production</span><span class="pi">:</span>
  <span class="na">stage</span><span class="pi">:</span> <span class="s">Deploy</span>
  <span class="na">image</span><span class="pi">:</span> <span class="s">registry.nasermirzaei89.net/gitlab-ci/kubectl:latest</span>
  <span class="na">script</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">cat kubernetes.tpl.yml | sed "s//myproject/g; s//$CI_COMMIT_TAG/g; s//api.myproject.nasermirzaei89.net/g" | kubectl apply -f -</span>
  <span class="na">when</span><span class="pi">:</span> <span class="s">manual</span>
  <span class="na">only</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">tags</span>
</code></pre></div></div>

<p>It’s my GitLab CI file in most projects, but it may be simpler or more complex in some projects.
Also, you might have tags for your runners. So, you must add tags to select a runner in your steps.</p>

<p>Love automation and make development enjoyable!</p>]]></content><author><name>Naser Mirzaei</name></author><category term="GitLab" /><category term="Continuous_Integration" /><category term="Continuous_Delivery" /><category term="Gitlab_CI" /><category term="Deployment" /><summary type="html"><![CDATA[My Gitlab CI file after 3 years using of gitlab]]></summary></entry></feed>