[英]Operator SDK - Update CreationTimestamp
我目前正在使用 Golang 和 Operator SDK 編寫 Kube.netes Operator。
為了知道資源的創建是否超時,我檢查了當前資源的CreationTimestamp
屬性。 成功Update
后,我想更新該資源的CreationTimestamp
,但是當我這樣做時,沒有任何反應並且CreationTimestamp
保持不變......
我的Reconcile
循環看起來像這樣:
func (r *MyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
myObject := &v1alpha1.My{}
err := r.Get(ctx, req.NamespacedName, myObject)
if err != nil {
// do something
}
//if marked to be deleted
//do something
//...
//if marked to be updated
myObject.SetCreationTimestamp(v1.Now())
err = r.Update(ctx, configMap) //updates all fields that I changed except for CreationTimestamp...
if err != nil {
println("ERR:", err.Error()) //doesnt get thrown
}
println(myObject.CreationTimestamp) //still the old timestamp instead of v1.Now()
//...
}
或者有沒有其他方法可以跟蹤上次協調資源的時間?
我認為沒有辦法實現這一點,但我找到了解決方法。
在您的CRD
中,字段metadata.annotations
可以將信息存儲在不會被覆蓋的map[string]string
中。
因此,我創建了一個名為CreationTimestamp
的字段,並使用myObject.ObjectMeta.Annotations["creationTimestamp"]
在我的 go 代碼中檢索它。
我可以像這樣更新值: myObject.ObjectMeta.Annotations["creationTimestamp"] = "newvalue"
然后執行Update
以保存更改
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.