Skip to content

Commit 3c6f266

Browse files
authored
read API resources only once (#290)
1 parent 76f5253 commit 3c6f266

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Diff for: pkg/controller/generic_reconciler.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strconv"
1010
"strings"
11+
"sync"
1112

1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -27,7 +28,8 @@ import (
2728
)
2829

2930
var (
30-
_ manager.Runnable = &GenericReconciler{}
31+
_ manager.Runnable = &GenericReconciler{}
32+
once sync.Once
3133
)
3234

3335
// GenericReconciler watches a defined object
@@ -41,6 +43,7 @@ type GenericReconciler struct {
4143
logger logr.Logger
4244
cmWatcher *configmap.Watcher
4345
validationEngine validations.Interface
46+
apiResources []metav1.APIResource
4447
}
4548

4649
// NewGenericReconciler returns a GenericReconciler struct
@@ -156,12 +159,16 @@ func (gr *GenericReconciler) LookForConfigUpdates(ctx context.Context) {
156159
}
157160

158161
func (gr *GenericReconciler) reconcileEverything(ctx context.Context) error {
159-
apiResources, err := reconcileResourceList(gr.discovery, gr.client.Scheme())
160-
if err != nil {
161-
return fmt.Errorf("retrieving resources to reconcile: %w", err)
162-
}
162+
once.Do(func() {
163+
apiResources, err := reconcileResourceList(gr.discovery, gr.client.Scheme())
164+
if err != nil {
165+
gr.logger.Error(err, "retrieving API resources to reconcile")
166+
return
167+
}
168+
gr.apiResources = apiResources
169+
})
163170

164-
for i, resource := range apiResources {
171+
for i, resource := range gr.apiResources {
165172
gr.logger.Info("apiResource", "no", i+1, "Group", resource.Group,
166173
"Version", resource.Version,
167174
"Kind", resource.Kind)
@@ -173,7 +180,7 @@ func (gr *GenericReconciler) reconcileEverything(ctx context.Context) error {
173180
return fmt.Errorf("getting watched namespaces: %w", err)
174181
}
175182

176-
gvkResources := gr.getNamespacedResourcesGVK(apiResources)
183+
gvkResources := gr.getNamespacedResourcesGVK(gr.apiResources)
177184
errNR := gr.processNamespacedResources(ctx, gvkResources, namespaces)
178185
if errNR != nil {
179186
return fmt.Errorf("processing namespace scoped resources: %w", errNR)
@@ -209,8 +216,8 @@ func (gr *GenericReconciler) groupAppObjects(ctx context.Context,
209216
Limit: gr.listLimit,
210217
Namespace: namespace,
211218
}
219+
list.SetGroupVersionKind(gvk)
212220
for {
213-
list.SetGroupVersionKind(gvk)
214221

215222
if err := gr.client.List(ctx, &list, listOptions); err != nil {
216223
return nil, fmt.Errorf("listing %s: %w", gvk.String(), err)
@@ -285,12 +292,12 @@ func (gr *GenericReconciler) processNamespacedResources(
285292
}
286293
for label, objects := range relatedObjects {
287294
gr.logger.Info("reconcileNamespaceResources",
288-
"Reconciling group of", len(objects), "objects with app label", label,
295+
"Reconciling group of", len(objects), "objects with labels", label,
289296
"in the namespace", ns.name)
290297
err := gr.reconcileGroupOfObjects(ctx, objects, ns.name)
291298
if err != nil {
292299
return fmt.Errorf(
293-
"reconciling related objects with 'app' label value '%s': %w", label, err,
300+
"reconciling related objects with labels '%s': %w", label, err,
294301
)
295302
}
296303
}

0 commit comments

Comments
 (0)