<p>I am writing a command line tool in Go which will perform an action based on the existence of a particular pod on a <code>k8s</code> cluster, in a specific namespace.</p>

<p>I could do via command line (shell) invocations within my <code>go</code> program something like </p>

<pre><code>kubectl get pods -n mynapespace l app=myapp
</code></pre>

<p>or in case I am not certain about the labels, something even less elegant as:</p>

<pre><code>kubectl get pods -n mynapespace | grep -i somepatternIamcertainabout
</code></pre>

<p>However, given that I am using the k8s native language (Go) I was wondering whether there might be a more Go native/specific way of making such an inquiry to the k8s api server, without resorting to shell invocations from within my cli tool.</p>

pkaramol
<blockquote>
  <p>However, given that I am using the k8s native language (Go) I was
  wondering whether there might be a more Go native/specific way of
  making such an inquiry to the k8s api server, without resorting to
  shell invocations from within my cli tool.</p>
</blockquote>

<p>If you want to talk with <strong>k8s cluster</strong> in your programs written in <strong>go</strong> without resorting to shell invocations, <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">client-go</a> library is the way to go. It contains everything you need to query your k8s api server in your go programs.</p>

<blockquote>
  <h3>What's included</h3>
  
  <ul>
  <li>The <code>kubernetes</code> package contains the clientset to
  access Kubernetes API. </li>
  <li>The <code>discovery</code> package is used to discover APIs
  supported by a Kubernetes API server.</li>
  <li>The <code>dynamic</code> package contains a
  dynamic client that can perform generic operations on arbitrary
  Kubernetes API objects.</li>
  <li>The <code>plugin/pkg/client/auth</code> packages contain
  optional authentication plugins for obtaining credentials from
  external sources. </li>
  <li>The <code>transport</code> package is used to set up auth and
  start a connection. </li>
  <li>The <code>tools/cache</code> package is useful for writing
  controllers.</li>
  </ul>
</blockquote>
