在golang中的自定义Kubernetes控制器中更新状态

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

Update status in a custom Kubernetes controller in golang

问题

我正在构建一个Go Kubernetes操作员。我已经使用kubebuilder创建了它。
我想将一些内部细节存储在CRD状态中。我尝试过:

  • 更新整个资源:
if err = r.Client.Update(ctx, upCRD); err != nil {
    return ctrl.Result{}, client.IgnoreNotFound(err)
}
  • 只更新状态:
if err = r.Status().Update(ctx, upCRD); err != nil {
    return reconcile.Result{}, client.IgnoreNotFound(err)
}

状态结构定义如下:

type HAAuditStatus struct {
    ChaosStrategyCron  cron.EntryID       `json:"chaosStrategyCron,omitempty"`
    TestStatus         TestStatus         `json:"testStatus,omitempty"`
    MetricStatus       MetricStatus       `json:"metricStatus,omitempty"`
    RoundRobinStrategy RoundRobinStrategy `json:"roundRobinStrategy,omitempty"`
    FixedStrategy      FixedStrategy      `json:"fixedStrategy,omitempty"`
    NextChaosDateTime  int64              `json:"nextChaosDateTime,omitempty"`
    Created            bool               `json:"created,default=false"`
}

没有引发错误,并且修改的specs字段实际上已经持久化,但是status字段的值在下一次调和步骤中仍然保持默认值。
我查看了GitHub或StackOverflow上的其他问题,但是没有任何建议解决我的问题,我无法弄清楚问题出在哪里。有关更全面的信息,您可以参考操作员所在的repo

非常欢迎任何建议 在golang中的自定义Kubernetes控制器中更新状态

英文:

I am building a Go Kubernetes operator. I have used kubebuilder to create it.
I want to store some internal details in the CRD status. I have tried :

  • To update the whole resource :
if err = r.Client.Update(ctx, upCRD); err != nil {
    return ctrl.Result{}, client.IgnoreNotFound(err)
}
  • And to update only the status :
if err = r.Status().Update(ctx, upCRD); err != nil {
	return reconcile.Result{}, client.IgnoreNotFound(err)
}

The status struct is defined as follows :

type HAAuditStatus struct {
	ChaosStrategyCron  cron.EntryID       `json:"chaosStrategyCron,omitempty"`
	TestStatus         TestStatus         `json:"testStatus,omitempty"`
	MetricStatus       MetricStatus       `json:"metricStatus,omitempty"`
	RoundRobinStrategy RoundRobinStrategy `json:"roundRobinStrategy,omitempty"`
	FixedStrategy      FixedStrategy      `json:"fixedStrategy,omitempty"`
	NextChaosDateTime  int64              `json:"nextChaosDateTime,omitempty"`
	Created            bool               `json:"created,default=false"`
}

No error is raised and the specs fields modified are actually persisted but not the status field whose values remain the default at the next reconciling step.
I have looked at the other issues on GitHub or StackOverflow but any suggestion made solved my issue and I can't figure out what is the problem. For a a bigger picture, you can refer to the repo where the operator is located.

Any suggestion is very welcomed 在golang中的自定义Kubernetes控制器中更新状态

答案1

得分: 1

我可能找到了状态未更新的原因。

在更新状态之前,我还在更新规格字段(以向用户提供有关创建资源的反馈)。

问题是规格更新触发了新的协调,而在此更新之后的指令(其中包括状态更新)未执行。

我意识到使用规格来向用户提供反馈并不合适,事件更适合此目的。

英文:

I might have found the reason why the status were not updated.

Before updating the status, I was also updating the spec fields (to give some feedback to the user on created resources).

The issue is caused by the fact that the specs updates trigger a new reconcilation, and the instruction after this update (among them the status update) were not execute.

I realized that using specs to give feedback to the user is not suitable and the events were more appropriate for this purpose.

huangapple
  • 本文由 发表于 2023年2月5日 01:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75347120.html
匿名

发表评论

匿名网友

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

确定