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: diff --git a/main.go b/main.go index fca734ce3b..f5cd4dd108 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,11 @@ 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" + corev1 "k8s.io/api/core/v1" 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" @@ -38,6 +42,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" @@ -273,9 +278,27 @@ 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{}: {}, + &corev1.Pod{}: {}, + &v1.Job{}: {}, + &v1.CronJob{}: {}, + &appsv1.ReplicaSet{}: {}, + &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.