I am currently writing a Kubernetes Operator using Golang and the Operator SDK.
In order to know if the creation of a resource has timed out I check the CreationTimestamp
property of my current resource. After a successful Update
I want to update the CreationTimestamp
of that resource, but when I do that, nothing happens and the CreationTimestamp
stays the same...
My Reconcile
loop looks something like this:
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()
//...
}
Or is there any other way to track when the resource was reconciled the last time?
CodePudding user response:
I dont think there is a way to achieve this, but I found a workaround.
In your CRD
the field metadata.annotations
can store information in a map[string]string
that wont be overwritten.
So I created a field called CreationTimestamp
and retrieve it in my go code with myObject.ObjectMeta.Annotations["creationTimestamp"]
.
I can update the value like this: myObject.ObjectMeta.Annotations["creationTimestamp"] = "newvalue"
and then doing an Update
to save the changes