K8s Operator 使用事件过滤器监听秘密更改

Alb*_*rto 6 go kubernetes kubernetes-secrets kubebuilder

几个月前我们创建了一个控制器,它使用 kubebuilder 运行得很好。

\n

几周前,我们将 \xe2\x80\x9clistener\xe2\x80\x9d 添加到秘密中,当秘密更改时(秘密属性)\n应该调用协调,问题是它有时有效有时无效, (您更改秘密应用它并且协调不会发生\xe2\x80\x99t),我们正在为完全相同的秘密文件执行此操作

\n

我们尝试了几天来找到根本原因,但没有成功,(我们将k8s.io/client-go v0.23.4和 更改为v0.22.3,现在 v0.22.1这才起作用。\n知道问题可能是什么吗?任何提示都会有帮助。或者我们有任何其他方法可以做到这一点可以尝试一下。

\n
func (r *vtsReconciler) SetupWithManager(mgr ctrl.Manager) error {\n    manager := ctrl.NewControllerManagedBy(mgr).\n        For(&vts.str).\n        WithEventFilter(predicate.Or(predicate.AnnotationChangedPredicate{}))\n    manager = manager.Watches(&source.Kind{Type: &v1.Secret{}}, handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {\n        return r.SecretRequests.SecretFinder(a.GetName())\n    }))\n    return manager.Complete(r)\n}\n\n\n\nfunc (secm *SecretMapper) SecretFinder(name string) []reconcile.Request {\n    v := cli.ObjectKey{Name: name}\n    return secm.SecMap[v.String()]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Blo*_*je5 4

最有可能的问题是,该问题WithEventFIlter适用于控制器所监视的所有对象。CRD 的生成会自动递增,但这并不适用于所有资源类型。

来自 GenerationChangedPredicate 文档:

// Caveats:
//
// * The assumption that the Generation is incremented only on writing to the spec does not hold for all APIs.
// E.g For Deployment objects the Generation is also incremented on writes to the metadata.annotations field.
// For object types other than CustomResources be sure to verify which fields will trigger a Generation increment when they are written to.
Run Code Online (Sandbox Code Playgroud)

您可以通过创建秘密/更新秘密来检查这一点,您将看到没有生成集(至少在我的本地 k3d 集群上没有)。

它很可能适用于创建,因为最初控制器会将现有资源与集群同步。

要解决它,您可以使用:

// Caveats:
//
// * The assumption that the Generation is incremented only on writing to the spec does not hold for all APIs.
// E.g For Deployment objects the Generation is also incremented on writes to the metadata.annotations field.
// For object types other than CustomResources be sure to verify which fields will trigger a Generation increment when they are written to.
Run Code Online (Sandbox Code Playgroud)

它应该仅将谓词应用于您的自定义资源。