hashicorp / hcp-terraform-operator

Kubernetes Operator allows managing HCP Terraform resources via Kubernetes Custom Resources.
https://developer.hashicorp.com/terraform/cloud-docs
Mozilla Public License 2.0
122 stars 29 forks source link

🐛 Workspace cannot be updated by TFO to set the workspaceID when a finalizer is added by another controller #440

Closed kGarciaSemarchy closed 1 month ago

kGarciaSemarchy commented 2 months ago

Operator Version, Kind and Kubernetes Version

YAML Manifest File

Output Log

Kubectl Outputs

Question

Hi, In order to automate a process, I have CompositeController (from Metacontroller) which inspect workspaces and call sync and finalize hooks. This controller add a finalizer on the workspace resource. It seems that the resource is updated in the same time that an update from the terraform-cloud-operator to set status with workspaceID because TFO throw an error:

2024-07-18T09:38:33Z    ERROR   Workspace Controller    {"workspace": {"name":"tenant-3bf542be-infra","namespace":"test-ci-tenants"}, "msg": "update status with workspace ID", "error": "Operation cannot be fulfilled on workspaces.app.terraform.io \"tenant-3bf542be-infra\": the object has been modified; please apply your changes to the latest version and try again"} 
2024-07-18T09:38:33Z    ERROR   Workspace Controller    {"workspace": {"name":"tenant-3bf542be-infra","namespace":"test-ci-tenants"}, "msg": "reconcile workspace", "error": "Operation cannot be fulfilled on workspaces.app.terraform.io \"tenant-3bf542be-infra\": the object has been modified; please apply your changes to the latest version and try again"}

It seems that there are no retry about the update on TFO. Is it a bug on TFO implementation or something is wrong on my side ?

References

https://metacontroller.github.io/metacontroller/api/compositecontroller.html

Community Note

arybolovlev commented 2 months ago

Hi @kGarciaSemarchy,

Could you please provide us with more details here?

Thank you.

kGarciaSemarchy commented 2 months ago

Hi @arybolovlev,

Sorry, my question was hidden. Comment updated. Don't hesitate if I can help.

Thank you

arybolovlev commented 2 months ago

That happens because the workspace object gets modified between the time when the workspace controller reads it from Kubernetes API and finishes reconciliation. Since you mention finalizers, I guess that only metadata.resourceVersion gets updated.

Does that happen only for newly created workspaces or do those that exist also fail to update? I can assume that only new workspace objects are affected.

Thanks!

kGarciaSemarchy commented 2 months ago

You right, it's the case only for the newly created workspaces.

arybolovlev commented 2 months ago

Thank you, @kGarciaSemarchy. I was able to reproduce this issue. It seems like we can use the Patch() method instead of the Update() method when updating the status. However, it will take some time to implement this since there is another side effect of having another finalizer attached to the object that we need to take into account.

We expect version 2.6.0 to be released at the end of July and then we can release 2.6.1 which will include a fix for this issue.

kGarciaSemarchy commented 2 months ago

Thanks for the feedback and your time. We will wait the release 2.6.1 to test again !

arybolovlev commented 1 month ago

Hi @kGarciaSemarchy,

I plan to cut a 2.6.1 release next week with a fix for this issue. Please keep an eye on notifications. I will let you know if this changes.

Thanks.