从k8s事件处理程序中获取对象的注释。

huangapple go评论104阅读模式
英文:

Get annotations from object in k8s event handler

问题

我正在基于sample-controller构建一个小型的k8s控制器。

我正在使用以下事件处理程序监听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)
}

这部分代码运行良好;我的事件正常触发,并且enqueueServiceAccount()函数被调用。

这是我第一次接触Golang,我无法弄清楚如何从obj中获取对象的Kubernetes注解。

我使用go-spew转储了该对象,并确认它具有ObjectMeta。我只是不确定如何将其转换为可以访问ObjectMeta的某个对象 - 从那里获取注解应该很容易(在这种情况下,该对象没有任何注解,它是一个<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"}}

我如何访问该对象的注解?

英文:

I'm building a little k8s controller based on the sample-controller.

I'm listening for ServiceAccount events with the following event handler:

...
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.

This is my first foray into Golang and I can't figure out how to get the object's Kubernetes annotations from the obj.

I dumped the object with go-spew and can confirm it's got an 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.

(*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?

答案1

得分: 6

你可以使用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)
	}
}

但通常人们倾向于使用controller-runtime。

英文:

You can use a 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.

答案2

得分: 1

SampleController不是最容易处理的代码。他们有关于如何将对象转换为已知资源类型的示例。他们还有关于如何从列表中查找资源的示例。

除非你有特定的需求,我建议你也考虑使用kubebuilder,并遵循《kubebuilder书籍》中直观的解释来创建控制器。

英文:

SampleController is not the most easy code to deal with. 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.

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.

huangapple
  • 本文由 发表于 2021年5月28日 01:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/67726840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定