[英]Get annotations from object in k8s event handler
I'm building a little k8s controller based on the sample-controller .我正在基于sample-controller构建一个小 k8s controller 。
I'm listening for ServiceAccount events with the following event handler:我正在使用以下事件处理程序监听 ServiceAccount 事件:
...
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)
}
This is working fine;这工作正常; my events are coming in and the enqueueServiceAccount()
function is getting called.我的事件即将到来,并且enqueueServiceAccount()
function 被调用。
This is my first foray into Golang and I can't figure out how to get the object's Kubernetes annotations from the obj
.这是我第一次涉足 Golang,我不知道如何从obj
获取对象的 Kubernetes 注释。
I dumped the object with go-spew
and can confirm it's got an ObjectMeta
.我用go-spew
spew 转储了 object 并且可以确认它有一个ObjectMeta
。 I'm just not sure how I cast this into some object where I can access the ObjectMeta
- 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 <nil>
values.我只是不确定如何将其转换为一些 object ,在那里我可以访问ObjectMeta
- 从那里应该很容易获得注释(在这种情况下,这个 object 没有任何注释,它是<nil>
之一价值观。
(*v1.ServiceAccount)(0xc0002c1010)(&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 <nil> <nil> map[eks.amazonaws.com/component:kube-proxy k8s-app:kube-proxy] map[kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"labels":{"eks.amazonaws.com/component":"kube-proxy","k8s-app":"kube-proxy"},"name":"kube-proxy","namespace":"kube-system"}}
How can I access this object's annotations?如何访问该对象的注释?
You can use a MetaAccessor :您可以使用MetaAccessor :
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var metaAccessor = metav1.NewAccessor()
func (c *Controller) enqueueServiceAccount(obj interface{}) {
if typed, ok := obj.(runtime.Object); ok {
annotations, err := metaAccessor.Annotations(typed)
}
}
But often people tend to use controller-runtime.但通常人们倾向于使用控制器运行时。
SampleController is not the most easy code to deal with. SampleController 不是最容易处理的代码。 They have example on how they cast objects to a known resource type.他们有关于如何将对象转换为已知资源类型的示例。 And they also have example on how they lookup the resource from a lister .他们还有如何从 lister 中查找资源的示例。
Unless you have specific needs, I would recommend to also consider using kubebuilder and follow the kubebuilder book that has intuitive explanations of making controllers.除非您有特殊需求,否则我建议您也考虑使用 kubebuilder 并遵循kubebuilder 书籍,该书对制作控制器有直观的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.