Skip to content

Commit 7a5987b

Browse files
committed
chore: Get resource from cache instead of APIServer
Currently in ACK, at the beginning of the Reconciler for each event, we get the resource from the k8s APIServer. This is an unecessary call, as the latest object that triggered the reconciler is already stored in the informers cache. With this change, we will be retrieving the resource from the cache instead of the APIServer.
1 parent c28a9f4 commit 7a5987b

File tree

9 files changed

+386
-100
lines changed

9 files changed

+386
-100
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ mocks: install-mockery ## Build mocks
3535
@echo -n "building mocks for sigs.k8s.io/controller-runtime/pkg/client ... "
3636
@bin/mockery --quiet --name="(Object|Client|Status|Reader|SubResourceWriter)" --case=underscore --output=mocks/controller-runtime/pkg/client --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/client"
3737
@echo "ok."
38+
@echo -n "building mocks for sigs.k8s.io/controller-runtime/pkg/cache ... "
39+
@bin/mockery --quiet --name="(Cache)" --case=underscore --output=mocks/controller-runtime/pkg/cache --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/cache"
40+
@echo "ok."
3841

3942
help: ## Show this help.
4043
@grep -F -h "##" $(MAKEFILE_LIST) | grep -F -v grep | sed -e 's/\\$$//' \

mocks/controller-runtime/pkg/cache/cache.go

Lines changed: 231 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/controller-runtime/pkg/cache/new_cache_func.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/adoption_reconciler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/apimachinery/pkg/runtime/schema"
2727
"k8s.io/apimachinery/pkg/types"
2828
ctrlrt "sigs.k8s.io/controller-runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/cache"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
3031
k8sctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3132
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -57,7 +58,7 @@ type adoptionReconciler struct {
5758
// of an upstream controller-runtime.Manager
5859
func (r *adoptionReconciler) BindControllerManager(mgr ctrlrt.Manager) error {
5960
r.kc = mgr.GetClient()
60-
r.apiReader = mgr.GetAPIReader()
61+
r.ackResourceCache = mgr.GetCache()
6162
return ctrlrt.NewControllerManagedBy(
6263
mgr,
6364
).For(
@@ -251,7 +252,7 @@ func (r *adoptionReconciler) Sync(
251252

252253
// Only create the described resource if it does not already exist
253254
// in k8s cluster.
254-
if err := r.apiReader.Get(ctx, types.NamespacedName{
255+
if err := r.ackResourceCache.Get(ctx, types.NamespacedName{
255256
Namespace: described.MetaObject().GetNamespace(),
256257
Name: described.MetaObject().GetName(),
257258
}, described.RuntimeObject()); err != nil {
@@ -306,15 +307,15 @@ func (r *adoptionReconciler) getAdoptedResource(
306307
req ctrlrt.Request,
307308
) (*ackv1alpha1.AdoptedResource, error) {
308309
ro := &ackv1alpha1.AdoptedResource{}
309-
// Here we use k8s APIReader to read the k8s object by making the
310+
// Here we use k8s ACKResourceCache to read the k8s object by making the
310311
// direct call to k8s apiserver instead of using k8sClient.
311312
// The reason is that k8sClient uses a cache and sometimes k8sClient can
312313
// return stale copy of object.
313314
// It is okay to make direct call to k8s apiserver because we are only
314315
// making single read call for complete reconciler loop.
315316
// See following issue for more details:
316317
// https://github.com/aws-controllers-k8s/community/issues/894
317-
if err := r.apiReader.Get(ctx, req.NamespacedName, ro); err != nil {
318+
if err := r.ackResourceCache.Get(ctx, req.NamespacedName, ro); err != nil {
318319
return nil, err
319320
}
320321
return ro, nil
@@ -615,7 +616,7 @@ func NewAdoptionReconcilerWithClient(
615616
metrics *ackmetrics.Metrics,
616617
cache ackrtcache.Caches,
617618
kc client.Client,
618-
apiReader client.Reader,
619+
ackResourceCache cache.Cache,
619620
) acktypes.AdoptedResourceReconciler {
620621
return &adoptionReconciler{
621622
reconciler: reconciler{
@@ -625,7 +626,7 @@ func NewAdoptionReconcilerWithClient(
625626
metrics: metrics,
626627
cache: cache,
627628
kc: kc,
628-
apiReader: apiReader,
629+
ackResourceCache: ackResourceCache,
629630
},
630631
}
631632
}

0 commit comments

Comments
 (0)