<p>I'm using the <a href="https://github.com/kubernetes-client/javascript" rel="nofollow noreferrer">Javascript Kubernetes Client</a> and I'm trying to read all resources from a custom resource definition. In particular I want to run <code>kubectl get prometheusrule</code> (prometheusrule is my CRD).</p>
<p>I couldn't find a wat to do this yet. I can read resources like this:</p>
<pre><code>const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

k8sApi.listNamespacedPod('default').then((res) =&gt; {
    res.body.items.forEach(pod =&gt; console.log(pod.metadata.name));
});
</code></pre>
<p>But it does not provide a method for reading CRDs.</p>
<p>I also tried</p>
<pre><code>const k8Client = k8s.KubernetesObjectApi.makeApiClient(kc);
k8Client.read({ kind: &quot;service&quot;}).then(res =&gt; console.log(res));
</code></pre>
<p>But this way I get the error <code>UnhandledPromiseRejectionWarning: Error: Unrecognized API version and kind: v1 service</code></p>
<p>Any idea how I can achieve this?</p>

Jonas
<p>You can use the <a href="https://github.com/kubernetes-client/javascript/blob/master/src/gen/api/customObjectsApi.ts#L1491" rel="nofollow noreferrer">listNamespacedCustomObject</a> function. This function has four required arguments as described below:</p>
<ul>
<li><strong>group</strong> - the custom resource's group name</li>
<li><strong>version</strong> - the custom resource's version</li>
<li><strong>namespace</strong> - the custom resource's namespace</li>
<li><strong>plural</strong> - the custom resource's plural name.</li>
</ul>
<hr />
<p>I've created a sample script that lists all <code>PrometheusRules</code> to illustrate how it works:</p>
<pre><code>$ cat list_rules.js
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig()
kc.loadFromDefault()

const k8sApi = kc.makeApiClient(k8s.CustomObjectsApi)

k8sApi.listNamespacedCustomObject('monitoring.coreos.com','v1','default', 'prometheusrules').then((res) =&gt; {
    res.body.items.forEach(rule =&gt; console.log(rule.metadata.name));
});
</code></pre>
<p>We can check if it works as expected:</p>
<pre><code>$ node list_rules.js
prometheus-kube-prometheus-alertmanager.rules
prometheus-kube-prometheus-etcd
prometheus-kube-prometheus-general.rules
prometheus-kube-prometheus-k8s.rules
...
</code></pre>
