<p>I'm building a little k8s controller based on the <a href="https://github.com/kubernetes/sample-controller" rel="nofollow noreferrer">sample-controller</a>.</p>
<p>I'm listening for ServiceAccount events with the following event handler:</p>
<pre><code>...
serviceAccountInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
    AddFunc:    controller.enqueueServiceAccount,
    DeleteFunc: controller.enqueueServiceAccount,
})
...

func (c *Controller) enqueueServiceAccount(obj interface{}) {
    var key string
    var err error

    if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
        utilruntime.HandleError(err)
        return
    }
    c.workqueue.Add(key)
}
</code></pre>
<p>This is working fine; my events are coming in and the <code>enqueueServiceAccount()</code> function is getting called.</p>
<p>This is my first foray into Golang and I can't figure out how to get the object's Kubernetes annotations from the <code>obj</code>.</p>
<p>I dumped the object with <code>go-spew</code> and can confirm it's got an <code>ObjectMeta</code>. I'm just not sure how I cast this into some object where I can access the <code>ObjectMeta</code> - and from there it should be easy to get the annotations (in this case this object does't have any, it's one of the <code>&lt;nil&gt;</code> values.</p>
<pre><code>(*v1.ServiceAccount)(0xc0002c1010)(&amp;ServiceAccount{ObjectMeta:{kube-proxy  kube-system /api/v1/namespaces/kube-system/serviceaccounts/kube-proxy d2013421-92c8-44ae-b6d8-202231ea557c 234 0 2021-04-29 18:40:20 +0100 BST &lt;nil&gt; &lt;nil&gt; map[eks.amazonaws.com/component:kube-proxy k8s-app:kube-proxy] map[kubectl.kubernetes.io/last-applied-configuration:{&quot;apiVersion&quot;:&quot;v1&quot;,&quot;kind&quot;:&quot;ServiceAccount&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;labels&quot;:{&quot;eks.amazonaws.com/component&quot;:&quot;kube-proxy&quot;,&quot;k8s-app&quot;:&quot;kube-proxy&quot;},&quot;name&quot;:&quot;kube-proxy&quot;,&quot;namespace&quot;:&quot;kube-system&quot;}}
</code></pre>
<p><strong>How can I access this object's annotations?</strong></p>

<p>You can use a <a href="https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/meta/interfaces.go#L44" rel="noreferrer">MetaAccessor</a>:</p>
<pre class="lang-golang prettyprint-override"><code>import (
   metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
   &quot;k8s.io/apimachinery/pkg/runtime&quot;
)

var metaAccessor = metav1.NewAccessor()

func (c *Controller) enqueueServiceAccount(obj interface{}) {
    if typed, ok := obj.(runtime.Object); ok {
        annotations, err := metaAccessor.Annotations(typed)
    }
}

</code></pre>
<p>But often people tend to use controller-runtime.</p>
