<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Lenard</title>
    <description>The latest articles on DEV Community by Chris Lenard (@chrislenard).</description>
    <link>https://dev.to/chrislenard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F487603%2Fe66f0d46-7021-4399-905e-d5a1efc90dea.jpeg</url>
      <title>DEV Community: Chris Lenard</title>
      <link>https://dev.to/chrislenard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chrislenard"/>
    <language>en</language>
    <item>
      <title>Debug and Test Operator SDK in VS Code</title>
      <dc:creator>Chris Lenard</dc:creator>
      <pubDate>Sun, 11 Oct 2020 21:24:13 +0000</pubDate>
      <link>https://dev.to/chrislenard/debug-and-test-operator-sdk-in-vs-code-1gib</link>
      <guid>https://dev.to/chrislenard/debug-and-test-operator-sdk-in-vs-code-1gib</guid>
      <description>&lt;p&gt;If you built a kubernetes operator with Operator-SDK and you need to use environment variables, you will find it difficult to debug and test in VS Code. There are many avenues to debug and test:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;make run&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;make test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;VS Code debugger 
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qKGHEDNd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e7o4sxizlv0ii4gww86b.jpg" alt="vscode debugger icon"&gt; &lt;/li&gt;
&lt;li&gt;VS Code CodeLens 
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NbWUoltW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kupcbo6sg7ahr5crx7cf.jpg" alt="vscode CodeLens UI"&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;go run&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;go test&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;.env&lt;/code&gt; File
&lt;/h2&gt;

&lt;p&gt;First, we need to centralize all environment variables into a &lt;code&gt;.env&lt;/code&gt; file in the project root.&lt;/p&gt;

&lt;p&gt;Here is an example of how the &lt;code&gt;.env&lt;/code&gt; file should look.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FOO_BAR=barfoo
DEBUG_LEVEL=info
WATCH_NAMESPACE=""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only caveat is your file must NOT contain any # characters. Makefile uses # for comments and will not play friendly.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;godotenv&lt;/code&gt; Package
&lt;/h2&gt;

&lt;p&gt;Next, you will add &lt;a href="https://github.com/joho/godotenv"&gt;the &lt;code&gt;godotenv&lt;/code&gt; package&lt;/a&gt;, which reads &lt;code&gt;.env&lt;/code&gt; and creates environment variables.&lt;/p&gt;

&lt;p&gt;Now, in &lt;code&gt;main.go&lt;/code&gt; add to the beginning of the &lt;code&gt;main&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Set env vars, if available.&lt;/span&gt;
&lt;span class="n"&gt;godotenv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"./.env"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;Using the same approach, you can make a method in a unit test file like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;godotenv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"../../.env"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call &lt;code&gt;before()&lt;/code&gt; at the beginning of any test that requires environment variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  VS Code
&lt;/h2&gt;

&lt;p&gt;These changes are necessary to make VS Code CodeLens work. Add this property to &lt;code&gt;.vscode/settings.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"go.testEnvFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/.env"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the &lt;code&gt;envFile&lt;/code&gt; property to &lt;code&gt;.vscode/launch.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"FooBar"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"mode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"auto"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/main.go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"envFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/.env"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it. Now, you can debug and test any way you want. Enjoy!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>operator</category>
      <category>testing</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
