简体   繁体   English

获取在 kubebuilder 中触发 controller 的事件类型

[英]Getting the type of event, that triggered a controller in kubebuilder

I'm just getting started with kubebuilder and Golang to extend our Kubernetes-cluster with a custom resource.我刚刚开始使用 kubebuilder 和 Golang 来使用自定义资源扩展我们的 Kubernetes 集群。 I would love to do different things in the reconciler-function based on the event, that actually called it.我很想根据实际调用它的事件在协调器功能中做不同的事情。

Was the resource created?资源是否创建? Was it updated?更新了吗? Was it deleted?被删除了吗?

Each of those events triggers the controller, however, I can't seem to find a possibility to see, which of those events actually happened.这些事件中的每一个都会触发 controller,但是,我似乎无法找到可能看到哪些事件实际发生了。 I can work around this issue by writing a reconciler like this:我可以通过编写这样的协调器来解决这个问题:

func (r *ServiceDescriptorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    service := &batchv1.ServiceDescriptor{}
    if err := r.Get(context.TODO(), req.NamespacedName, service); err != nil && errors.IsNotFound(err) {
        fmt.Println("Resource was not found -> must have been deleted")
    else {
        fmt.Println("No errors found -> Resource must have been created or updated")
    }
}

However, this feels oddly implicit and kinda hacky.然而,这感觉奇怪的隐含和有点hacky。

Is there a clean (possibly native) way of getting the event-type of the reconciler-call?是否有一种干净的(可能是本机的)方法来获取协调器调用的事件类型?

You won't be able to to that because this system was designed as level-based and it's not being triggered by individual events changes but rather by the actual cluster state that is being fetch from the apiserver.您将无法做到这一点,因为该系统被设计为基于级别的,并且它不是由单个事件更改触发,而是由从 apiserver 获取的实际集群 state 触发。

Looking at reconcile.go you will notice in line #84 has this comment about it:查看reconcile.go你会注意到在第 84行有这样的评论:

Reconciliation is level-based, meaning action isn't driven off changes in individual Events , but instead is driven by actual cluster state read from the apiserver or a local cache.协调是基于级别的,这意味着操作不是由单个事件的更改驱动,而是由从 apiserver 或本地缓存读取的实际集群 state 驱动。 For example if responding to a Pod Delete Event, the Request won't contain that a Pod was deleted,instead the reconcile function observes this when reading the cluster state and seeing the Pod as missing.例如,如果响应 Pod 删除事件,则请求不会包含 Pod 已删除,而是协调 function 在读取集群 state 并看到 Pod 丢失时观察到这一点。

And in line #44 :第 44行:

Request contains the information necessary to reconcile a Kubernetes object.请求包含协调 Kubernetes object 所需的信息。 This includes the information to uniquely identify the object - its Name and Namespace.这包括唯一标识 object 的信息 - 它的名称和命名空间。 It does NOT contain information about any specific Event or the object contents itself .它不包含有关任何特定事件或 object 内容本身的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM