diff --git a/.chloggen/set-gomemlimit.yaml b/.chloggen/set-gomemlimit.yaml new file mode 100755 index 0000000000..3f28143953 --- /dev/null +++ b/.chloggen/set-gomemlimit.yaml @@ -0,0 +1,18 @@ +# 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: | + 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/internal/manifests/collector/container.go b/internal/manifests/collector/container.go index 193596f038..e0ed0babb7 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: @@ -163,6 +164,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.