登录
首页 >  Golang >  Go问答

Operator SDK - 更新资源创建时间

来源:stackoverflow

时间:2024-02-20 18:18:23 320浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《Operator SDK - 更新资源创建时间》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我目前正在使用 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()

   //...
}

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


正确答案


我认为没有办法实现这一目标,但我找到了解决方法。

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

因此,我创建了一个名为 CreationTimestamp 的字段,并使用 myObject.ObjectMeta.Annotations["creationTimestamp"] 在我的 go 代码中检索它。

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

理论要掌握,实操不能落!以上关于《Operator SDK - 更新资源创建时间》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>