From e65bef2c8a00b29763ab8a360967bcc35d4aeec1 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Fri, 5 Apr 2024 16:11:10 -0400 Subject: [PATCH 1/5] set watch option --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index fca734ce3b..eb3797e3f4 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ import ( "github.com/spf13/pflag" colfeaturegate "go.opentelemetry.io/collector/featuregate" networkingv1 "k8s.io/api/networking/v1" + "k8s.io/apimachinery/pkg/labels" k8sruntime "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" @@ -275,6 +276,9 @@ func main() { }), Cache: cache.Options{ DefaultNamespaces: namespaces, + DefaultLabelSelector: labels.SelectorFromSet(map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + }), }, } From aaa14fb65d11269419479c066c5b9208a009cd25 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Fri, 5 Apr 2024 16:12:55 -0400 Subject: [PATCH 2/5] perf improvement --- .chloggen/set-cache-watch-option.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 .chloggen/set-cache-watch-option.yaml diff --git a/.chloggen/set-cache-watch-option.yaml b/.chloggen/set-cache-watch-option.yaml new file mode 100755 index 0000000000..a7d9655df8 --- /dev/null +++ b/.chloggen/set-cache-watch-option.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) +component: collector + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Adds a label selector to the operator's informer cache to only select resources with the label selector + +# One or more tracking issues related to the change +issues: [2822] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: From fd49ea7b17335dfc203f84c0265cbd126a20c4d8 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Mon, 8 Apr 2024 12:22:24 -0400 Subject: [PATCH 3/5] FF --- main.go | 19 +++++++++++++++++-- pkg/featuregate/featuregate.go | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index eb3797e3f4..ab7b706dbb 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,8 @@ import ( monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" "github.com/spf13/pflag" colfeaturegate "go.opentelemetry.io/collector/featuregate" + appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/batch/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/labels" k8sruntime "k8s.io/apimachinery/pkg/runtime" @@ -39,6 +41,7 @@ import ( k8sapiflag "k8s.io/component-base/cli/flag" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" @@ -274,12 +277,24 @@ func main() { Port: webhookPort, TLSOpts: optionsTlSOptsFuncs, }), - Cache: cache.Options{ + } + + if featuregate.EnableCacheOptions.IsEnabled() { + mgrOptions.Cache = cache.Options{ DefaultNamespaces: namespaces, DefaultLabelSelector: labels.SelectorFromSet(map[string]string{ "app.kubernetes.io/managed-by": "opentelemetry-operator", }), - }, + ByObject: map[client.Object]cache.ByObject{ + &otelv1alpha1.OpenTelemetryCollector{}: {}, + &otelv1alpha1.OpAMPBridge{}: {}, + &otelv1alpha1.Instrumentation{}: {}, + &v1.Job{}: {}, + &appsv1.Deployment{}: {}, + &appsv1.DaemonSet{}: {}, + &appsv1.StatefulSet{}: {}, + }, + } } mgr, err := ctrl.NewManager(restConfig, mgrOptions) diff --git a/pkg/featuregate/featuregate.go b/pkg/featuregate/featuregate.go index a20d589b35..39b45e60eb 100644 --- a/pkg/featuregate/featuregate.go +++ b/pkg/featuregate/featuregate.go @@ -60,6 +60,14 @@ var ( featuregate.WithRegisterDescription("enables features associated to the Prometheus Operator"), featuregate.WithRegisterFromVersion("v0.82.0"), ) + + // EnableCacheOptions is the feature gate that sets a cache label selector to improve operator performance. + EnableCacheOptions = featuregate.GlobalRegistry().MustRegister( + "operator.cache.selector", + featuregate.StageBeta, // TODO: this is temporary and will be reverted prior to an e2e run + featuregate.WithRegisterDescription("enables the operator's cache to only select a subset of objects"), + featuregate.WithRegisterFromVersion("v0.98.0"), + ) ) // Flags creates a new FlagSet that represents the available featuregate flags using the supplied featuregate registry. From d8bf46ace4177580361edfe825b5319db1ee574b Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Mon, 8 Apr 2024 14:03:04 -0400 Subject: [PATCH 4/5] more --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index ab7b706dbb..2f6c251741 100644 --- a/main.go +++ b/main.go @@ -290,6 +290,8 @@ func main() { &otelv1alpha1.OpAMPBridge{}: {}, &otelv1alpha1.Instrumentation{}: {}, &v1.Job{}: {}, + &v1.CronJob{}: {}, + &appsv1.ReplicaSet{}: {}, &appsv1.Deployment{}: {}, &appsv1.DaemonSet{}: {}, &appsv1.StatefulSet{}: {}, From fe900f54824ab9a861a24baf5b634c9e2d63ae34 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Mon, 8 Apr 2024 14:17:40 -0400 Subject: [PATCH 5/5] need pod --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 2f6c251741..f5cd4dd108 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ import ( colfeaturegate "go.opentelemetry.io/collector/featuregate" appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/labels" k8sruntime "k8s.io/apimachinery/pkg/runtime" @@ -289,6 +290,7 @@ func main() { &otelv1alpha1.OpenTelemetryCollector{}: {}, &otelv1alpha1.OpAMPBridge{}: {}, &otelv1alpha1.Instrumentation{}: {}, + &corev1.Pod{}: {}, &v1.Job{}: {}, &v1.CronJob{}: {}, &appsv1.ReplicaSet{}: {},