@@ -25,16 +25,20 @@ import (
25
25
"fmt"
26
26
"io"
27
27
"io/fs"
28
+ appsv1 "k8s.io/api/apps/v1"
29
+ corev1 "k8s.io/api/core/v1"
30
+ "k8s.io/apimachinery/pkg/labels"
28
31
"log"
29
32
"os"
30
33
"path"
34
+ "sigs.k8s.io/controller-runtime/pkg/cache"
31
35
"strconv"
32
36
"strings"
33
37
"time"
34
38
35
39
"github.com/go-logr/logr"
36
40
37
- "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
41
+ fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
38
42
"github.com/FoundationDB/fdb-kubernetes-operator/controllers"
39
43
"github.com/FoundationDB/fdb-kubernetes-operator/fdbclient"
40
44
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
@@ -160,6 +164,38 @@ func StartManager(
160
164
fdbclient .DefaultCLITimeout = time .Duration (operatorOpts .CliTimeout ) * time .Second
161
165
fdbclient .MaxCliTimeout = time .Duration (operatorOpts .MaxCliTimeout ) * time .Second
162
166
167
+ // Define the cache options for the client cache used by the operator. If no label selector is defined, the
168
+ // default cache configuration will be used.
169
+ cacheOptions := cache.Options {}
170
+ // Only if a label selector is defined we have to update the cache options.
171
+ if operatorOpts .LabelSelector != "" {
172
+ // Parse the label selector, if the label selector is not parsable panic.
173
+ labelSelector , parseErr := labels .Parse (operatorOpts .LabelSelector )
174
+ if parseErr != nil {
175
+ log .Fatalf ("could not parse label selector: %s, got error: %s" , operatorOpts .LabelSelector , parseErr )
176
+ }
177
+
178
+ selector := cache.ObjectSelector {
179
+ Label : labelSelector ,
180
+ }
181
+
182
+ // Set the label selector for all resources that the operator manages, this should reduce the resources that
183
+ // are cached by the operator if a label selector is provided.s
184
+ cacheOptions .SelectorsByObject = map [client.Object ]cache.ObjectSelector {
185
+ & fdbv1beta2.FoundationDBCluster {}: selector ,
186
+ & corev1.Pod {}: selector ,
187
+ & corev1.PersistentVolumeClaim {}: selector ,
188
+ & corev1.ConfigMap {}: selector ,
189
+ & corev1.Service {}: selector ,
190
+ & appsv1.Deployment {}: selector ,
191
+ }
192
+
193
+ // Make sure we set the label selector for any additional watched objects.
194
+ for _ , object := range watchedObjects {
195
+ cacheOptions .SelectorsByObject [object ] = selector
196
+ }
197
+ }
198
+
163
199
options := ctrl.Options {
164
200
Scheme : scheme ,
165
201
MetricsBindAddress : operatorOpts .MetricsAddr ,
@@ -169,11 +205,13 @@ func StartManager(
169
205
RenewDeadline : & operatorOpts .RenewDeadline ,
170
206
RetryPeriod : & operatorOpts .RetryPeriod ,
171
207
Port : 9443 ,
208
+ NewCache : cache .BuilderWithOptions (cacheOptions ),
172
209
}
173
210
174
211
if operatorOpts .WatchNamespace != "" {
175
212
options .Namespace = operatorOpts .WatchNamespace
176
213
setupLog .Info ("Operator starting in single namespace mode" , "namespace" , options .Namespace )
214
+ cacheOptions .Namespace = operatorOpts .WatchNamespace
177
215
} else {
178
216
setupLog .Info ("Operator starting in Global mode" )
179
217
}
@@ -288,8 +326,8 @@ func moveFDBBinaries(log logr.Logger) error {
288
326
}
289
327
290
328
for _ , binEntry := range binDir {
291
- if binEntry .IsDir () && v1beta2 .VersionRegex .Match ([]byte (binEntry .Name ())) {
292
- version , err := v1beta2 .ParseFdbVersion (binEntry .Name ())
329
+ if binEntry .IsDir () && fdbv1beta2 .VersionRegex .Match ([]byte (binEntry .Name ())) {
330
+ version , err := fdbv1beta2 .ParseFdbVersion (binEntry .Name ())
293
331
if err != nil {
294
332
return err
295
333
}
0 commit comments