如何强制对自定义控制器中的 SharedIndexInformer 进行完全重新同步

How to force a full resync on a SharedIndexInformer in a custom controller

我正在 client-go 的帮助下用 Go 为 Kubernetes 编写自定义控制器。 它基于 sample-controller,目前运行良好。

SharedIndexInformer 可以选择定期重新同步所有对象。 (Parameter resyncPeriod is set to 30 seconds in the sample-controller.)

有没有办法立即强制重新同步?

似乎处理周期性重新同步的代码似乎调用了 store.Resync()。 我试过打电话给 fooInformer.Informer().GetStore().Resync()。调用成功,但重新同步没有发生。我错过了什么?


我正在使用 client-go v0.17.2 并且服务器是 EKS v1.14.9-eks-c0eccc

调用 fooInformer.Informer().GetStore().Resync() 时,您正在使用 Resync function/method 中定义的 Store 类型:client-go/tools/cache/store.go

我们可以看到以下内容:

在商店类型定义中:

// Resync is meaningless in the terms appearing here but has
// meaning in some implementations that have non-trivial
// additional behavior (e.g., DeltaFIFO).
Resync() error

在下面的重新同步定义中:

// Resync is meaningless for one of these
func (c *cache) Resync() error {
return nil
}

除非您确实有其他 class 来执行重新同步,否则这实际上什么都不做。

这就是为什么

The call succeeds, but the resync is not happening.

希望对您有所帮助!

这是不可能的。

执行定期重新同步的 cache.Storek8s.io/client-go/tools/cache/controller.go 中的 newInformer 中实例化为 cache.DeltaFIFO 队列:

    // This will hold incoming changes. Note how we pass clientState in as a
    // KeyLister, that way resync operations will result in the correct set
    // of update/delete deltas.
    fifo := NewDeltaFIFOWithOptions(DeltaFIFOOptions{
        KnownObjects:          clientState,
        EmitDeltaTypeReplaced: true,
    })

这是由 cache.New() 作为未导出字段 cache.controller{}.config.Queue 返回的,没有用于访问的导出函数 - 因此无法手动调用 Resync()

您可以通过列出线人存储中的每个对象,然后调用 ResourceEventHandlerFuncs 的 AddFunc(通常是 xxxController.OnAddXxx)来实现此目的。

for _, v := range xxxController.xxxInformer.GetStore().List() {
    xxxController.OnAddXxx(v)
}

或者,如果您想破解,可以使用 reflect

field := reflect.ValueOf(xxxController.xxxInformer).Elem().FieldByName("controller").Elem().Elem().FieldByName("reflector").Elem().FieldByName("store")
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface().(*cache.DeltaFIFO).Resync()