@@ -31,11 +31,14 @@ import (
31
31
prometheusOperator "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
32
32
"github.com/spf13/cast"
33
33
appsv1 "k8s.io/api/apps/v1"
34
+ batchv1 "k8s.io/api/batch/v1"
34
35
corev1 "k8s.io/api/core/v1"
36
+ rbacv1 "k8s.io/api/rbac/v1"
35
37
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
36
38
"k8s.io/apimachinery/pkg/fields"
37
39
"k8s.io/apimachinery/pkg/labels"
38
40
"k8s.io/apimachinery/pkg/runtime"
41
+ "k8s.io/apimachinery/pkg/selection"
39
42
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
40
43
_ "k8s.io/client-go/plugin/pkg/client/auth"
41
44
"k8s.io/klog/v2"
@@ -48,6 +51,8 @@ import (
48
51
"sigs.k8s.io/controller-runtime/pkg/webhook"
49
52
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
50
53
54
+ telemetryv1alpha1 "github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1"
55
+
51
56
extensionsControllers "github.com/kube-logging/logging-operator/controllers/extensions"
52
57
loggingControllers "github.com/kube-logging/logging-operator/controllers/logging"
53
58
extensionsv1alpha1 "github.com/kube-logging/logging-operator/pkg/sdk/extensions/api/v1alpha1"
@@ -56,7 +61,6 @@ import (
56
61
loggingv1beta1 "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
57
62
"github.com/kube-logging/logging-operator/pkg/sdk/logging/model/types"
58
63
"github.com/kube-logging/logging-operator/pkg/webhook/podhandler"
59
- telemetryv1alpha1 "github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1"
60
64
// +kubebuilder:scaffold:imports
61
65
)
62
66
@@ -84,6 +88,8 @@ func main() {
84
88
var enableprofile bool
85
89
var namespace string
86
90
var loggingRef string
91
+ var watchLabeledChildren bool
92
+ var watchLabeledSecrets bool
87
93
var finalizerCleanup bool
88
94
var enableTelemetryControllerRoute bool
89
95
var klogLevel int
@@ -98,6 +104,8 @@ func main() {
98
104
flag .BoolVar (& enableprofile , "pprof" , false , "Enable pprof" )
99
105
flag .StringVar (& namespace , "watch-namespace" , "" , "Namespace to filter the list of watched objects" )
100
106
flag .StringVar (& loggingRef , "watch-logging-name" , "" , "Logging resource name to optionally filter the list of watched objects based on which logging they belong to by checking the app.kubernetes.io/managed-by label" )
107
+ flag .BoolVar (& watchLabeledChildren , "watch-labeled-children" , false , "Only watch child resources with logging operator's name label selector: app.kubernetes.io/name: fluentd|fluentbit|syslog-ng" )
108
+ flag .BoolVar (& watchLabeledSecrets , "watch-labeled-secrets" , false , "Only watch secrets with the following label selector: logging.banzaicloud.io/watch: enabled" )
101
109
flag .BoolVar (& finalizerCleanup , "finalizer-cleanup" , false , "Remove finalizers from Logging resources during operator shutdown, useful for Helm uninstallation" )
102
110
flag .BoolVar (& enableTelemetryControllerRoute , "enable-telemetry-controller-route" , false , "Enable the Telemetry Controller route for Logging resources" )
103
111
flag .StringVar (& syncPeriod , "sync-period" , "" , "SyncPeriod determines the minimum frequency at which watched resources are reconciled. Defaults to 10 hours. Parsed using time.ParseDuration." )
@@ -152,7 +160,12 @@ func main() {
152
160
mgrOptions .WebhookServer = webhookServer
153
161
}
154
162
155
- customMgrOptions , err := setupCustomCache (& mgrOptions , syncPeriod , namespace , loggingRef )
163
+ customMgrOptions , err := setupCustomCache (& mgrOptions , syncPeriod , namespace , loggingRef , watchLabeledChildren )
164
+ if watchLabeledSecrets {
165
+ customMgrOptions .Cache .ByObject [& corev1.Secret {}] = cache.ByObject {
166
+ Label : labels.Set {"logging.banzaicloud.io/watch" : "enabled" }.AsSelector (),
167
+ }
168
+ }
156
169
if err != nil {
157
170
setupLog .Error (err , "unable to set up custom cache settings" )
158
171
os .Exit (1 )
@@ -312,7 +325,7 @@ func detectContainerRuntime(ctx context.Context, c client.Reader) error {
312
325
return nil
313
326
}
314
327
315
- func setupCustomCache (mgrOptions * ctrl.Options , syncPeriod string , namespace string , loggingRef string ) (* ctrl.Options , error ) {
328
+ func setupCustomCache (mgrOptions * ctrl.Options , syncPeriod string , namespace string , loggingRef string , watchLabeledChildren bool ) (* ctrl.Options , error ) {
316
329
if syncPeriod != "" {
317
330
duration , err := time .ParseDuration (syncPeriod )
318
331
if err != nil {
@@ -321,7 +334,7 @@ func setupCustomCache(mgrOptions *ctrl.Options, syncPeriod string, namespace str
321
334
mgrOptions .Cache .SyncPeriod = & duration
322
335
}
323
336
324
- if namespace == "" && loggingRef == "" {
337
+ if namespace == "" && loggingRef == "" && ! watchLabeledChildren {
325
338
return mgrOptions , nil
326
339
}
327
340
@@ -333,13 +346,56 @@ func setupCustomCache(mgrOptions *ctrl.Options, syncPeriod string, namespace str
333
346
if loggingRef != "" {
334
347
labelSelector = labels.Set {"app.kubernetes.io/managed-by" : loggingRef }.AsSelector ()
335
348
}
349
+ if watchLabeledChildren {
350
+ if labelSelector == nil {
351
+ labelSelector = labels .NewSelector ()
352
+ }
353
+ // It would be much better to watch for a common label, but we don't have that yet.
354
+ // Adding a new label would recreate statefulsets and daemonsets which would be undesirable.
355
+ // Let's see how this works in the wild. We can optimize in a subsequent iteration.
356
+ req , err := labels .NewRequirement ("app.kubernetes.io/name" , selection .In , []string {
357
+ "fluentd" , "syslog-ng" , "fluentbit" ,
358
+ })
359
+ if err != nil {
360
+ return nil , err
361
+ }
362
+ labelSelector = labelSelector .Add (* req )
363
+ }
336
364
337
365
mgrOptions .Cache = cache.Options {
338
366
ByObject : map [client.Object ]cache.ByObject {
339
367
& corev1.Pod {}: {
340
368
Field : namespaceSelector ,
341
369
Label : labelSelector ,
342
370
},
371
+ & batchv1.Job {}: {
372
+ Field : namespaceSelector ,
373
+ Label : labelSelector ,
374
+ },
375
+ & corev1.Service {}: {
376
+ Field : namespaceSelector ,
377
+ Label : labelSelector ,
378
+ },
379
+ & rbacv1.Role {}: {
380
+ Field : namespaceSelector ,
381
+ Label : labelSelector ,
382
+ },
383
+ & rbacv1.ClusterRole {}: {
384
+ Field : namespaceSelector ,
385
+ Label : labelSelector ,
386
+ },
387
+ & rbacv1.RoleBinding {}: {
388
+ Field : namespaceSelector ,
389
+ Label : labelSelector ,
390
+ },
391
+ & rbacv1.ClusterRoleBinding {}: {
392
+ Field : namespaceSelector ,
393
+ Label : labelSelector ,
394
+ },
395
+ & corev1.ServiceAccount {}: {
396
+ Field : namespaceSelector ,
397
+ Label : labelSelector ,
398
+ },
343
399
& appsv1.DaemonSet {}: {
344
400
Field : namespaceSelector ,
345
401
Label : labelSelector ,
@@ -356,6 +412,10 @@ func setupCustomCache(mgrOptions *ctrl.Options, syncPeriod string, namespace str
356
412
Field : namespaceSelector ,
357
413
Label : labelSelector ,
358
414
},
415
+ & corev1.ConfigMap {}: {
416
+ Field : namespaceSelector ,
417
+ Label : labelSelector ,
418
+ },
359
419
},
360
420
}
361
421
0 commit comments