5.3.2.4.3. 协调循环


每个控制器都有一个协调器对象,它带有实现了协调循环的 Reconcile() 方法。协调循环通过 Request 参数传递,该参数是从缓存中查找主资源对象 Memcached 的命名空间和名称键:

import (
	ctrl "sigs.k8s.io/controller-runtime"

	cachev1 "github.com/example-inc/memcached-operator/api/v1"
	...
)

func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  // Lookup the Memcached instance for this reconcile request
  memcached := &cachev1.Memcached{}
  err := r.Get(ctx, req.NamespacedName, memcached)
  ...
}
Copy to Clipboard Toggle word wrap

根据返回值、结果和错误,请求可能会重新排序,协调循环可能会再次触发:

// Reconcile successful - don't requeue
return ctrl.Result{}, nil
// Reconcile failed due to error - requeue
return ctrl.Result{}, err
// Requeue for any reason other than an error
return ctrl.Result{Requeue: true}, nil
Copy to Clipboard Toggle word wrap

您可以将 Result.RequeueAfter 设置为在宽限期后重新排序请求:

import "time"

// Reconcile for any reason other than an error after 5 seconds
return ctrl.Result{RequeueAfter: time.Second*5}, nil
Copy to Clipboard Toggle word wrap
注意

您可以返回带有 RequeueAfter 设置的 Result 来定期协调一个 CR。

有关协调器、客户端并与资源事件交互的更多信息,请参阅 Controller Runtime Client API 文档

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat