<p>I want to execute equivalent of</p>

<p><code>kubectl get all -l app=myapp -n mynamespace</code></p>

<p>or</p>

<p><code>kubectl label all -l version=1.2.0,app=myapp track=stable --overwrite</code></p>

<p>using client-go</p>

<p>I looked at <a href="https://github.com/kubernetes/client-go/blob/master/dynamic" rel="noreferrer">dynamic</a> package, but it seems like it needs <code>GroupVersionResource</code>, which is different for, say, Service objects and Deployment objects. Also when I pass <code>schema.GroupVersionResource{Group: "apps", Version: "v1"}</code> it doesn't find anything, when I pass <code>schema.GroupVersionResource{Version: "v1"}</code> it finds only namespace object and also doesn't looks for labels, though I provided label options:</p>

<pre><code>resource := schema.GroupVersionResource{Version: "v1"}
listOptions := metav1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", AppName), FieldSelector: ""}
res, listErr := dynamicClient.Resource(resource).Namespace("myapps").List(listOptions)
</code></pre>

<p>I also looked at runtime package, but didn't find anything useful. I took a look at how <code>kubectl</code> implement this, bit haven't figured it out yet, too many levels of abstractions.</p>

Kseniia Churiumova
<p>You can't list &quot;all objects&quot; with one call.</p>
<p>Unfortunately the way Kubernetes API is architected is via API groups, which have multiple APIs under them.</p>
<p>So you need to:</p>
<ol>
<li>Query all API groups (<code>apiGroup</code>)</li>
<li>Visit each API group to see what APIs (<code>kind</code>) it exposes.</li>
<li>Actually query that <code>kind</code> to get all the objects (here you may actually filter the list query with the label).</li>
</ol>
<p>Fortunately, <code>kubectl api-versions</code> and <code>kubectl api-resources</code> commands do these.</p>
<p>So to learn how kubectl finds all &quot;kinds&quot; of API resources, run:</p>
<pre><code>kubectl api-resources -v=6
</code></pre>
<p>and you'll see kubectl making calls like:</p>
<ul>
<li><code>GET https://IP/api</code></li>
<li><code>GET https://IP/apis</code></li>
<li>then it visits every api group:
<ul>
<li><code>GET https://IP/apis/metrics.k8s.io/v1beta1</code></li>
<li><code>GET https://IP/apis/storage.k8s.io/v1</code></li>
<li>...</li>
</ul>
</li>
</ul>
<p>So if you're trying to clone this behavior with client-go, you should use the same API calls, <strike>or better just write a script just shells out to <code>kubectl api-resources -o=json</code> and script around it.</strike></p>
<p>If you aren't required to use client-go, there's a <a href="https://github.com/corneliusweig/ketall" rel="nofollow noreferrer">kubectl plugin called <code>get-all</code></a> which exists to do this task.</p>
