From 674a22b24cb1a80feae6249c50093e2330ce98a4 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Tue, 7 May 2024 16:31:39 -0400 Subject: [PATCH 1/3] set things --- .chloggen/set-gomemlimit.yaml | 16 +++++++++++++ internal/manifests/collector/container.go | 23 +++++++++++++++++++ internal/manifests/opampbridge/container.go | 23 +++++++++++++++++++ .../manifests/targetallocator/container.go | 23 +++++++++++++++++++ pkg/featuregate/featuregate.go | 8 +++++++ 5 files changed, 93 insertions(+) create mode 100755 .chloggen/set-gomemlimit.yaml diff --git a/.chloggen/set-gomemlimit.yaml b/.chloggen/set-gomemlimit.yaml new file mode 100755 index 0000000000..9bd1182daf --- /dev/null +++ b/.chloggen/set-gomemlimit.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, target allocator, opamp + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Introduces a new feature gate for `operator.golang.flags` to automatically add the environment variables for GOMAXPROCS and GOMEMLIMIT + +# One or more tracking issues related to the change +issues: [2919, 1456] + +# (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/internal/manifests/collector/container.go b/internal/manifests/collector/container.go index d0585ff9d8..26718368d8 100644 --- a/internal/manifests/collector/container.go +++ b/internal/manifests/collector/container.go @@ -29,6 +29,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters" "github.com/open-telemetry/opentelemetry-operator/internal/naming" + "github.com/open-telemetry/opentelemetry-operator/pkg/featuregate" ) // maxPortLen allows us to truncate a port name according to what is considered valid port syntax: @@ -152,6 +153,28 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1beta1.OpenTeleme } } + if featuregate.SetGolangFlags.IsEnabled() { + envVars = append(envVars, corev1.EnvVar{ + Name: "GOMEMLIMIT", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.memory", + ContainerName: naming.Container(), + }, + }, + }, + corev1.EnvVar{ + Name: "GOMAXPROCS", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.cpu", + ContainerName: naming.Container(), + }, + }, + }, + ) + } + envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...) return corev1.Container{ Name: naming.Container(), diff --git a/internal/manifests/opampbridge/container.go b/internal/manifests/opampbridge/container.go index 131eb040d5..6b5e70a8d6 100644 --- a/internal/manifests/opampbridge/container.go +++ b/internal/manifests/opampbridge/container.go @@ -22,6 +22,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/naming" + "github.com/open-telemetry/opentelemetry-operator/pkg/featuregate" ) // Container builds a container for the given OpAMPBridge. @@ -62,6 +63,28 @@ func Container(cfg config.Config, logger logr.Logger, opampBridge v1alpha1.OpAMP }) } + if featuregate.SetGolangFlags.IsEnabled() { + envVars = append(envVars, corev1.EnvVar{ + Name: "GOMEMLIMIT", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.memory", + ContainerName: naming.OpAMPBridgeContainer(), + }, + }, + }, + corev1.EnvVar{ + Name: "GOMAXPROCS", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.cpu", + ContainerName: naming.OpAMPBridgeContainer(), + }, + }, + }, + ) + } + envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...) return corev1.Container{ diff --git a/internal/manifests/targetallocator/container.go b/internal/manifests/targetallocator/container.go index 17e8d3772f..bb2e74f125 100644 --- a/internal/manifests/targetallocator/container.go +++ b/internal/manifests/targetallocator/container.go @@ -23,6 +23,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/naming" + "github.com/open-telemetry/opentelemetry-operator/pkg/featuregate" ) // Container builds a container for the given TargetAllocator. @@ -66,6 +67,28 @@ func Container(cfg config.Config, logger logr.Logger, instance v1beta1.TargetAll }) } + if featuregate.SetGolangFlags.IsEnabled() { + envVars = append(envVars, corev1.EnvVar{ + Name: "GOMEMLIMIT", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.memory", + ContainerName: naming.TAContainer(), + }, + }, + }, + corev1.EnvVar{ + Name: "GOMAXPROCS", + ValueFrom: &corev1.EnvVarSource{ + ResourceFieldRef: &corev1.ResourceFieldSelector{ + Resource: "limits.cpu", + ContainerName: naming.TAContainer(), + }, + }, + }, + ) + } + var args []string if instance.Spec.PrometheusCR.Enabled { args = append(args, "--enable-prometheus-cr-watcher") diff --git a/pkg/featuregate/featuregate.go b/pkg/featuregate/featuregate.go index 2d633c5276..f50095e874 100644 --- a/pkg/featuregate/featuregate.go +++ b/pkg/featuregate/featuregate.go @@ -32,6 +32,14 @@ var ( featuregate.WithRegisterDescription("enables features associated to the Prometheus Operator"), featuregate.WithRegisterFromVersion("v0.82.0"), ) + // SetGolangFlags is the feature gate that enables automatically setting GOMEMLIMIT and GOMAXPROCS for the + // collector, bridge, and target allocator. + SetGolangFlags = featuregate.GlobalRegistry().MustRegister( + "operator.golang.flags", + featuregate.StageAlpha, + featuregate.WithRegisterDescription("enables feature to set GOMEMLIMIT and GOMAXPROCS automatically"), + featuregate.WithRegisterFromVersion("v0.100.0"), + ) ) // Flags creates a new FlagSet that represents the available featuregate flags using the supplied featuregate registry. From 4bac1c34df44c2ea1a186d2bea42f902da67fc98 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Tue, 7 May 2024 16:53:19 -0400 Subject: [PATCH 2/3] fix kustomize shim --- config/crd/kustomizeconfig.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/crd/kustomizeconfig.yaml b/config/crd/kustomizeconfig.yaml index 6f83d9a94b..8e2d8d6b17 100644 --- a/config/crd/kustomizeconfig.yaml +++ b/config/crd/kustomizeconfig.yaml @@ -5,12 +5,12 @@ nameReference: fieldSpecs: - kind: CustomResourceDefinition group: apiextensions.k8s.io - path: spec/conversion/webhookClientConfig/service/name + path: spec/conversion/webhook/clientConfig/service/name namespace: - kind: CustomResourceDefinition group: apiextensions.k8s.io - path: spec/conversion/webhookClientConfig/service/namespace + path: spec/conversion/webhook/clientConfig/service/namespace create: false varReference: From 983f793caaab3af360735a2e8ab7d330c59e4c75 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Mon, 20 May 2024 11:56:09 -0400 Subject: [PATCH 3/3] restore, better chlog --- .chloggen/set-gomemlimit.yaml | 4 +++- config/crd/kustomizeconfig.yaml | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.chloggen/set-gomemlimit.yaml b/.chloggen/set-gomemlimit.yaml index 9bd1182daf..3f28143953 100755 --- a/.chloggen/set-gomemlimit.yaml +++ b/.chloggen/set-gomemlimit.yaml @@ -13,4 +13,6 @@ issues: [2919, 1456] # (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: +subtext: | + A new featuregate `operator.golang.flags` is added. This featuregate will allow the operator to automatically + set GOMAXPROCS and GOMEMLIMIT equal to the CPU and Memory limit provided respectively for the pod. diff --git a/config/crd/kustomizeconfig.yaml b/config/crd/kustomizeconfig.yaml index 8e2d8d6b17..6f83d9a94b 100644 --- a/config/crd/kustomizeconfig.yaml +++ b/config/crd/kustomizeconfig.yaml @@ -5,12 +5,12 @@ nameReference: fieldSpecs: - kind: CustomResourceDefinition group: apiextensions.k8s.io - path: spec/conversion/webhook/clientConfig/service/name + path: spec/conversion/webhookClientConfig/service/name namespace: - kind: CustomResourceDefinition group: apiextensions.k8s.io - path: spec/conversion/webhook/clientConfig/service/namespace + path: spec/conversion/webhookClientConfig/service/namespace create: false varReference: