Skip to content

Commit 1f4afe5

Browse files
authored
Fix reconcile loop when cache definitions are invalid (#164)
The reconcile set the status two times with each reconcile: First to `WaitingForCacheSync` and then to `*Error`. This PR changes the logic to set `WaitingForCacheSync` only after it is known if there were any errors.
1 parent 5d745ef commit 1f4afe5

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

controllers/managedresource_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"strings"
9+
"sync/atomic"
910

1011
"github.com/DmitriyVTitov/size"
1112
"github.com/google/go-jsonnet"
@@ -73,6 +74,10 @@ type ManagedResourceReconciler struct {
7374

7475
configHash string
7576
configGeneration int64
77+
78+
// started is set to true after the first reconcile has been started.
79+
// It's a bit of a hack to detect that the caches are synced as it's much easier than starting to track the cache sync state.
80+
started atomic.Bool
7681
}
7782

7883
type instanceCache struct {
@@ -170,6 +175,8 @@ func newEspejoteError(err error, t EspejoteErrorType) EspejoteError {
170175
}
171176

172177
func (r *ManagedResourceReconciler) Reconcile(ctx context.Context, req Request) (ctrl.Result, error) {
178+
r.started.CompareAndSwap(false, true)
179+
173180
// Since we are not using the default builder we need to add the request to the context ourselves
174181
ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("request", req))
175182

controllers/managedresource_controller_manager.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ func (r *ManagedResourceControllerManager) Reconcile(ctx context.Context, req re
132132
r.recordErr(ctx, fmt.Errorf("failed to ensure instance controller: %w", err), string(DependencyConfigurationError), managedResource, req),
133133
)
134134
}
135+
if !ic.reconciler.started.Load() && managedResource.Status.Status != string(WaitingForCacheSync) {
136+
managedResource.Status.Status = string(WaitingForCacheSync)
137+
if err := r.Status().Update(ctx, &managedResource); err != nil {
138+
return ctrl.Result{}, fmt.Errorf("failed to update status of managed resource %q: %w", req.NamespacedName, err)
139+
}
140+
}
135141
if err := ic.StartErr(); err != nil {
136142
return ctrl.Result{}, multierr.Combine(
137143
fmt.Errorf("failed to start instance controller for managed resource %q: %w", req.NamespacedName, err),
@@ -156,14 +162,13 @@ func (r *ManagedResourceControllerManager) recordErr(ctx context.Context, err er
156162
}
157163

158164
r.Recorder.Eventf(&managedResource, nil, "Warning", errType, "Reconcile", "%s: %s", errType, err.Error())
159-
var statusUpdateErr error
160165
if managedResource.Status.Status != errType {
161166
managedResource.Status.Status = errType
162167
if err := r.Status().Update(ctx, &managedResource); err != nil {
163-
statusUpdateErr = fmt.Errorf("failed to update status of managed resource %q: %w", req.NamespacedName, err)
168+
return fmt.Errorf("failed to update status of managed resource %q: %w", req.NamespacedName, err)
164169
}
165170
}
166-
return statusUpdateErr
171+
return nil
167172
}
168173

169174
func (r *ManagedResourceControllerManager) ensureInstanceControllerFor(ctx context.Context, mr espejotev1alpha1.ManagedResource) (*resourceController, error) {
@@ -199,13 +204,6 @@ func (r *ManagedResourceControllerManager) ensureInstanceControllerFor(ctx conte
199204
delete(r.controllers, mrKey)
200205
}
201206

202-
if mr.Status.Status != string(WaitingForCacheSync) {
203-
mr.Status.Status = string(WaitingForCacheSync)
204-
if err := r.Status().Update(ctx, &mr); err != nil {
205-
return nil, fmt.Errorf("failed to update status of managed resource %q: %w", mrKey, err)
206-
}
207-
}
208-
209207
instanceCtrlCtx, instanceCtrlCancel := context.WithCancel(r.ControllerLifetimeCtx)
210208
reconciler := &ManagedResourceReconciler{
211209
For: mrKey,

0 commit comments

Comments
 (0)