Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[featuregate] Automatically set GOMEMLIMIT and GOMAXPROCS for collector, target allocator, opamp bridge #2933

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .chloggen/set-gomemlimit.yaml
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to put this behind a feature gate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking we would featuregate to roll out slowly so we could test it in case this doesn't have the effect we intend


# 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.
23 changes: 23 additions & 0 deletions internal/manifests/collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down
23 changes: 23 additions & 0 deletions internal/manifests/opampbridge/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down
23 changes: 23 additions & 0 deletions internal/manifests/targetallocator/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions pkg/featuregate/featuregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading