Skip to content

Commit

Permalink
fix cache early read problem
Browse files Browse the repository at this point in the history
  • Loading branch information
enesonus committed May 1, 2024
1 parent d40300d commit 4b9161e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 11 additions & 5 deletions internal/controller/apiextensions/claim/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (fn DefaultsSelectorFn) SelectDefaults(ctx context.Context, cm resource.Com
// for each XR kind they can bind to. Each controller must watch its subset of
// claims and any XRs they bind to.
type Reconciler struct {
client client.Client
client client.Client
apiReader client.Reader

gvkClaim schema.GroupVersionKind
gvkXR schema.GroupVersionKind
Expand Down Expand Up @@ -302,8 +303,10 @@ func WithPollInterval(after time.Duration) ReconcilerOption {
// configure their composite resources.
func NewReconciler(m manager.Manager, of resource.CompositeClaimKind, with resource.CompositeKind, o ...ReconcilerOption) *Reconciler {
c := unstructured.NewClient(m.GetClient())
ar := m.GetAPIReader()
r := &Reconciler{
client: c,
apiReader: ar,
gvkClaim: schema.GroupVersionKind(of),
gvkXR: schema.GroupVersionKind(with),
managedFields: &NopManagedFieldsUpgrader{},
Expand All @@ -330,10 +333,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco

cm := claim.New(claim.WithGroupVersionKind(r.gvkClaim))
if err := r.client.Get(ctx, req.NamespacedName, cm); err != nil {
// There's no need to requeue if we no longer exist. Otherwise we'll be
// requeued implicitly because we return an error.
log.Debug(errGetClaim, "error", err)
return reconcile.Result{}, errors.Wrap(resource.IgnoreNotFound(err), errGetClaim)
// If the unstructured cache does not have the claim, hit the API Server
if err := r.apiReader.Get(ctx, req.NamespacedName, cm); err != nil {
// There's no need to requeue if we no longer exist. Otherwise we'll be
// requeued implicitly because we return an error.
log.Debug(errGetClaim, "error", err)
return reconcile.Result{}, errors.Wrap(resource.IgnoreNotFound(err), errGetClaim)
}
}

record := r.record.WithAnnotations("external-name", meta.GetExternalName(cm))
Expand Down
14 changes: 10 additions & 4 deletions internal/controller/apiextensions/composite/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,11 @@ func (fn KindObserverFunc) WatchComposedResources(kind ...schema.GroupVersionKin
// NewReconciler returns a new Reconciler of composite resources.
func NewReconciler(mgr manager.Manager, of resource.CompositeKind, opts ...ReconcilerOption) *Reconciler {
kube := unstructured.NewClient(mgr.GetClient())
ar := mgr.GetAPIReader()

r := &Reconciler{
client: kube,
client: kube,
apiReader: ar,

gvk: schema.GroupVersionKind(of),

Expand Down Expand Up @@ -449,7 +451,8 @@ func NewReconciler(mgr manager.Manager, of resource.CompositeKind, opts ...Recon

// A Reconciler reconciles composite resources.
type Reconciler struct {
client client.Client
client client.Client
apiReader client.Reader

gvk schema.GroupVersionKind

Expand Down Expand Up @@ -477,8 +480,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco

xr := composite.New(composite.WithGroupVersionKind(r.gvk))
if err := r.client.Get(ctx, req.NamespacedName, xr); err != nil {
log.Debug(errGet, "error", err)
return reconcile.Result{}, errors.Wrap(resource.IgnoreNotFound(err), errGet)
// If the cache does not have the composite, hit the API Server
if err := r.apiReader.Get(ctx, req.NamespacedName, xr); err != nil {
log.Debug(errGet, "error", err)
return reconcile.Result{}, errors.Wrap(resource.IgnoreNotFound(err), errGet)
}
}

log = log.WithValues(
Expand Down

0 comments on commit 4b9161e

Please sign in to comment.