<ul>
<li>client-go v0.19.2</li>
<li>golang 1.13</li>
</ul>
<p>I'm building a tool to create k8s resources from json(just like kubectl create -f).</p>
<p>I found that dynamic client can do such things,but when i use it with code bellow,i found it is hard to find schema.GroupVersionResource for given resource's json.Am i missing something or the only way to get resource is through restmapper?</p>
<pre class="lang-golang prettyprint-override"><code>container := &amp;unstructured.Unstructured{}
if err := container.UnmarshalJSON([]byte(jsonstring); err != nil {
    return err
}

_, err := k8sclient.Dynamic.Resource(?).Create(ctx, container, metav1.CreateOptions{})
if err != nil {
    return err
}
</code></pre>
<p>I know a work around is to write some code like bellow, but i'm sure it's not the best practice and there are too many of them besides crds.</p>
<pre class="lang-golang prettyprint-override"><code>var kindResourceMap = map[string]schema.GroupVersionResource{
    &quot;Deployment&quot;: {
        Group:    &quot;apps&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;deployments&quot;,
    },
    &quot;ConfigMap&quot;: {
        Group:    &quot;apps&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;configmaps&quot;,
    },
    &quot;Job&quot;: {
        Group:    &quot;batch&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;jobs&quot;,
    },
    &quot;Secret&quot;: {
        Group:    &quot;api&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;secrets&quot;,
    },
    &quot;Service&quot;: {
        Group:    &quot;api&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;services&quot;,
    },
    &quot;StatefulSet&quot;: {
        Group:    &quot;apps&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;statefulsets&quot;,
    },
    &quot;PersistentVolume&quot;: {
        Group:    &quot;api&quot;,
        Version:  &quot;v1&quot;,
        Resource: &quot;persistentvolumes&quot;,
    },
    &quot;CustomResourceDefinition&quot;: {
        Group:    &quot;apiextensions.k8s.io&quot;,
        Version:  &quot;v1beta1&quot;,
        Resource: &quot;customresourcedefinitions&quot;,
    },
}
</code></pre>

Jonyhy96
<p>You can use <code>restmapper</code> that directly queries the definitions from <code>metav1</code> using the <code>discovery</code> client.</p>
<pre class="lang-golang prettyprint-override"><code>import (
    &quot;k8s.io/client-go/rest&quot;
    &quot;k8s.io/client-go/discovery&quot;
    &quot;k8s.io/client-go/restmapper&quot;
    &quot;k8s.io/apimachinery/pkg/runtime/schema&quot;
)

 ...

c := discovery.NewDiscoveryClientForConfigOrDie(&amp;rest.Config{})

groupResources, err := restmapper.GetAPIGroupResources(c)
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)

mapping, err := mapper.RESTMapping(schema.ParseGroupKind(&quot;apps.Deployment&quot;))
fmt.Println(mapping.Resource)
</code></pre>
<p>This is cooked in <code>sigs.k8s.io/controller-runtime/pkg/client</code></p>
<pre class="lang-golang prettyprint-override"><code>mapping, err := c.RESTMapper().RESTMapping(schema.ParseGroupKind(&quot;apps.Deployment&quot;))
fmt.Println(mapping.Resource)
</code></pre>
<p>Look here for how it's done:
<a href="https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/apiutil/dynamicrestmapper.go#L77" rel="nofollow noreferrer">https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/apiutil/dynamicrestmapper.go#L77</a></p>
