如何创建带有状态字段的 Kubernetes 对象?

huangapple go评论80阅读模式
英文:

How to create Kubernetes objects with Status fields?

问题

我正在使用 Kubernetes Operator 在集群中创建自定义资源,CR 的 Status 字段已经填充,但是当对象被创建时,Status 字段为空。

这是我创建 CR 的方式:

reconcile.Create(ctx, &object)

这是我尝试使用 Kubernetes Operator 实现的目标:

如何创建带有状态字段的 Kubernetes 对象?

英文:

I am using the kubernetes operator to create a custom resource in the cluster, the CR has the Status field populated, but when the object gets created the Status field is empty.

This is how I am creating the CR:

reconcile.Create(ctx, &object)

This is what I am trying to accomplish with k8s operator:

如何创建带有状态字段的 Kubernetes 对象?

答案1

得分: 2

Kubernetes API和资源的架构遵循以下模式。

  1. 客户端可以通过指定“期望状态”(这是资源的spec:部分)来创建资源。这是发送给API服务器的“创建”请求。

  2. 控制器订阅/监视资源的更改,在“协调循环”中执行操作,它们可能会更新资源的状态(这是资源的status:部分)。

有关控制器如何实现并更新状态的示例,请参见Kubebuilder书籍:实现控制器-更新状态

示例中的客户端是一个“控制器运行时客户端”:

"sigs.k8s.io/controller-runtime/pkg/client"

以下是更新status子资源的示例代码:

if err := r.Status().Update(ctx, &cronJob); err != nil {
    log.Error(err, "无法更新CronJob状态")
    return ctrl.Result{}, err
}
英文:

The architecture of Kubernetes API and resources follows a pattern.

  1. Clients may create resources, by specifying a desired state (This is the spec: part of a resource). This is a "create" request sent to the API Server.

  2. Controllers, subscribe/watch to changes of resources, while doing actions in a reconciliation loop, they might update the Status of the resource (this is the status: part of the resource).

For an example of how a controller is implemented and updates the status, see the Kubebuilder book: Implementing a Controller - Update the Status.

The client in the example is a "controller runtime client":

"sigs.k8s.io/controller-runtime/pkg/client"

Example code, where the reconciler updates the status sub-resource:

if err := r.Status().Update(ctx, &cronJob); err != nil {
    log.Error(err, "unable to update CronJob status")
    return ctrl.Result{}, err
}

huangapple
  • 本文由 发表于 2022年9月2日 03:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/73574615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定