1

我目前正在使用 Golang 和 Operator SDK 编写一个 Kubernetes 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()

   //...
}

或者有没有其他方法可以跟踪上次协调资源的时间?

4

1 回答 1

0

我不认为有办法实现这一点,但我找到了一种解决方法。

在您CRD的字段metadata.annotations中可以存储map[string]string不会被覆盖的信息。

所以我创建了一个名为的字段CreationTimestamp,并在我的 go 代码中使用myObject.ObjectMeta.Annotations["creationTimestamp"].

我可以像这样更新值:myObject.ObjectMeta.Annotations["creationTimestamp"] = "newvalue"然后执行Update保存更改

于 2022-02-22T23:07:12.053 回答