@@ -23,6 +23,7 @@ import (
2323 agentoctopuscomv1beta1 "github.com/octopusdeploy/octopus-permissions-controller/api/v1beta1"
2424 "github.com/octopusdeploy/octopus-permissions-controller/internal/metrics"
2525 "github.com/octopusdeploy/octopus-permissions-controller/internal/rules"
26+ "k8s.io/client-go/util/retry"
2627 ctrl "sigs.k8s.io/controller-runtime"
2728 "sigs.k8s.io/controller-runtime/pkg/client"
2829 logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -138,17 +139,25 @@ func updateStatusOnFailure[T WSAResource, S WSAStatus](
138139) {
139140 log := logf .FromContext (ctx )
140141
141- switch s := any (status ).(type ) {
142- case * agentoctopuscomv1beta1.WorkloadServiceAccountStatus :
143- s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
144- "False" , ReasonReconcileFailed , err .Error ())
145- case * agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus :
146- s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
147- "False" , ReasonReconcileFailed , err .Error ())
148- }
142+ retryErr := retry .RetryOnConflict (retry .DefaultRetry , func () error {
143+ if getErr := c .Get (ctx , client .ObjectKeyFromObject (resource ), resource ); getErr != nil {
144+ return getErr
145+ }
146+
147+ switch s := any (status ).(type ) {
148+ case * agentoctopuscomv1beta1.WorkloadServiceAccountStatus :
149+ s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
150+ "False" , ReasonReconcileFailed , err .Error ())
151+ case * agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus :
152+ s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
153+ "False" , ReasonReconcileFailed , err .Error ())
154+ }
155+
156+ return c .Status ().Update (ctx , resource )
157+ })
149158
150- if statusErr := c . Status (). Update ( ctx , resource ); statusErr != nil {
151- log .Error (statusErr , "failed to update status after reconciliation failure" )
159+ if retryErr != nil {
160+ log .Error (retryErr , "failed to update status after reconciliation failure" )
152161 }
153162}
154163
@@ -161,17 +170,25 @@ func updateStatusOnSuccess[T WSAResource, S WSAStatus](
161170) {
162171 log := logf .FromContext (ctx )
163172
164- switch s := any (status ).(type ) {
165- case * agentoctopuscomv1beta1.WorkloadServiceAccountStatus :
166- s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
167- "True" , ReasonReconcileSuccess , successMessage )
168- case * agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus :
169- s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
170- "True" , ReasonReconcileSuccess , successMessage )
171- }
173+ retryErr := retry .RetryOnConflict (retry .DefaultRetry , func () error {
174+ if getErr := c .Get (ctx , client .ObjectKeyFromObject (resource ), resource ); getErr != nil {
175+ return getErr
176+ }
177+
178+ switch s := any (status ).(type ) {
179+ case * agentoctopuscomv1beta1.WorkloadServiceAccountStatus :
180+ s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
181+ "True" , ReasonReconcileSuccess , successMessage )
182+ case * agentoctopuscomv1beta1.ClusterWorkloadServiceAccountStatus :
183+ s .Conditions = updateCondition (s .Conditions , ConditionTypeReady ,
184+ "True" , ReasonReconcileSuccess , successMessage )
185+ }
186+
187+ return c .Status ().Update (ctx , resource )
188+ })
172189
173- if err := c . Status (). Update ( ctx , resource ); err != nil {
174- log .Error (err , "failed to update status after successful reconciliation" )
190+ if retryErr != nil {
191+ log .Error (retryErr , "failed to update status after successful reconciliation" )
175192 }
176193}
177194
0 commit comments