go - 获取在 kubebuilder 中触发 Controller 的事件类型

标签 go kubernetes kubernetes-custom-resources kubebuilder

我刚刚开始使用 kubebuilder 和 Golang 来使用自定义资源扩展我们的 Kubernetes 集群。我很乐意根据实际调用它的事件在协调器函数中做不同的事情。

资源是否已创建?更新了吗?被删除了吗?

这些事件中的每一个都会触发 Controller ,但是,我似乎无法找到查看这些事件中哪些事件实际发生的可能性。我可以通过编写这样的协调器来解决这个问题:

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")
    }
}

但是,这感觉有些隐晦而且有点古怪。

是否有一种干净的(可能是 native 的)方法来获取协调器调用的事件类型?

最佳答案

您将无法做到这一点,因为该系统被设计为基于级别,并且它不是由单个事件更改触发,而是由从 apiserver 获取的实际集群状态触发。

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

并符合 #44 :

Request contains the information necessary to reconcile a Kubernetes object. This includes the information to uniquely identify the object - its Name and Namespace. It does NOT contain information about any specific Event or the object contents itself.

关于go - 获取在 kubebuilder 中触发 Controller 的事件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67472710/

相关文章:

google-app-engine - 如何在 Go App Engine Urlfetch 包中使用 Cookie

kubernetes - 如何从外部网络访问 Kubernetes Dashboard

go - Kubernetes 运算符(operator)使用 yaml 模板创建 Deployment

Kubernetes:自定义资源的 RBAC 授权失败

pointers - 如何使用反射获取字符串指针的变量名?

go - 有没有办法根据内容动态解码 json?

http - 在 HTTP 路由器中将未知的 URL 路径设置为错误

kubernetes - 如何在Kubernetes中查看外部指标数据?

kubernetes - 如何在事件探针/就绪探针中的kubernetes中忽略HTTP请求健康检查?

Kubernetes CRDs - 引用现有的验证规范