<p>I want to read &amp; parse Volumesnapshot object (or any K8s object) in go. I tried  to take reference from <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">https://github.com/kubernetes/client-go</a> .</p>
<p>I am trying to add custom annotations in VolumeSnapshot CRD metadata as</p>
<pre><code>apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
  name: new-snapshot-test
  **annotation:**
        **test:Tushar**
spec:
  volumeSnapshotClassName: csi-hostpath-snapclass
  source:
    persistentVolumeClaimName: pvc-test
</code></pre>
<p>I also have a custom CSI driver. I need my CSI driver to look for that volumneSnapshot in K8s api server and parse this custom annotation and then process accordingly.</p>

Scorpio
<p>You can follow any of the examples in the Github repo <a href="https://github.com/kubernetes/client-go/tree/master/examples" rel="nofollow noreferrer">here</a>. For example using the dynamic/unstructured way (code snippet based on <a href="https://github.com/kubernetes/client-go/blob/master/examples/dynamic-create-update-delete-deployment/main.go" rel="nofollow noreferrer">this</a>):</p>
<pre class="lang-golang prettyprint-override"><code>func main() {
    var kubeconfig *string
    if home := homedir.HomeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    namespace := &quot;default&quot;

    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err)
    }
    client, err := dynamic.NewForConfig(config)
    if err != nil {
        panic(err)
    }

    volumesnapshotRes := schema.GroupVersionResource{Group: &quot;snapshot.storage.k8s.io&quot;, Version: &quot;v1beta1&quot;, Resource: &quot;volumesnapshots&quot;}

    // List VolumeSnapshots
    prompt()
    fmt.Printf(&quot;Listing volume snapshots in namespace %q:\n&quot;, apiv1.NamespaceDefault)
    list, err := client.Resource(volumesnapshotRes).Namespace(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        panic(err)
    }
    for _, d := range list.Items {
    ...
        // do whatever with the items (print, etc)
    }
    // Finish up
    ...
</code></pre>
