Skip to content

Commit 4ef2d6f

Browse files
authored
[featuregate] Automatically set GOMEMLIMIT and GOMAXPROCS for collector, target allocator, opamp bridge (open-telemetry#2933)
* set things * fix kustomize shim * restore, better chlog
1 parent 9aaa6c9 commit 4ef2d6f

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

.chloggen/set-gomemlimit.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector, target allocator, opamp
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Introduces a new feature gate for `operator.golang.flags` to automatically add the environment variables for GOMAXPROCS and GOMEMLIMIT
9+
10+
# One or more tracking issues related to the change
11+
issues: [2919, 1456]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: |
17+
A new featuregate `operator.golang.flags` is added. This featuregate will allow the operator to automatically
18+
set GOMAXPROCS and GOMEMLIMIT equal to the CPU and Memory limit provided respectively for the pod.

internal/manifests/collector/container.go

+23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/open-telemetry/opentelemetry-operator/internal/config"
3030
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
3131
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
32+
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
3233
)
3334

3435
// 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
163164
}
164165
}
165166

167+
if featuregate.SetGolangFlags.IsEnabled() {
168+
envVars = append(envVars, corev1.EnvVar{
169+
Name: "GOMEMLIMIT",
170+
ValueFrom: &corev1.EnvVarSource{
171+
ResourceFieldRef: &corev1.ResourceFieldSelector{
172+
Resource: "limits.memory",
173+
ContainerName: naming.Container(),
174+
},
175+
},
176+
},
177+
corev1.EnvVar{
178+
Name: "GOMAXPROCS",
179+
ValueFrom: &corev1.EnvVarSource{
180+
ResourceFieldRef: &corev1.ResourceFieldSelector{
181+
Resource: "limits.cpu",
182+
ContainerName: naming.Container(),
183+
},
184+
},
185+
},
186+
)
187+
}
188+
166189
envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...)
167190
return corev1.Container{
168191
Name: naming.Container(),

internal/manifests/opampbridge/container.go

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2323
"github.com/open-telemetry/opentelemetry-operator/internal/config"
2424
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
25+
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
2526
)
2627

2728
// Container builds a container for the given OpAMPBridge.
@@ -62,6 +63,28 @@ func Container(cfg config.Config, logger logr.Logger, opampBridge v1alpha1.OpAMP
6263
})
6364
}
6465

66+
if featuregate.SetGolangFlags.IsEnabled() {
67+
envVars = append(envVars, corev1.EnvVar{
68+
Name: "GOMEMLIMIT",
69+
ValueFrom: &corev1.EnvVarSource{
70+
ResourceFieldRef: &corev1.ResourceFieldSelector{
71+
Resource: "limits.memory",
72+
ContainerName: naming.OpAMPBridgeContainer(),
73+
},
74+
},
75+
},
76+
corev1.EnvVar{
77+
Name: "GOMAXPROCS",
78+
ValueFrom: &corev1.EnvVarSource{
79+
ResourceFieldRef: &corev1.ResourceFieldSelector{
80+
Resource: "limits.cpu",
81+
ContainerName: naming.OpAMPBridgeContainer(),
82+
},
83+
},
84+
},
85+
)
86+
}
87+
6588
envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...)
6689

6790
return corev1.Container{

internal/manifests/targetallocator/container.go

+23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2424
"github.com/open-telemetry/opentelemetry-operator/internal/config"
2525
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
26+
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
2627
)
2728

2829
// Container builds a container for the given TargetAllocator.
@@ -66,6 +67,28 @@ func Container(cfg config.Config, logger logr.Logger, instance v1alpha1.TargetAl
6667
})
6768
}
6869

70+
if featuregate.SetGolangFlags.IsEnabled() {
71+
envVars = append(envVars, corev1.EnvVar{
72+
Name: "GOMEMLIMIT",
73+
ValueFrom: &corev1.EnvVarSource{
74+
ResourceFieldRef: &corev1.ResourceFieldSelector{
75+
Resource: "limits.memory",
76+
ContainerName: naming.TAContainer(),
77+
},
78+
},
79+
},
80+
corev1.EnvVar{
81+
Name: "GOMAXPROCS",
82+
ValueFrom: &corev1.EnvVarSource{
83+
ResourceFieldRef: &corev1.ResourceFieldSelector{
84+
Resource: "limits.cpu",
85+
ContainerName: naming.TAContainer(),
86+
},
87+
},
88+
},
89+
)
90+
}
91+
6992
var args []string
7093
if instance.Spec.PrometheusCR.Enabled {
7194
args = append(args, "--enable-prometheus-cr-watcher")

pkg/featuregate/featuregate.go

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ var (
3232
featuregate.WithRegisterDescription("enables features associated to the Prometheus Operator"),
3333
featuregate.WithRegisterFromVersion("v0.82.0"),
3434
)
35+
// SetGolangFlags is the feature gate that enables automatically setting GOMEMLIMIT and GOMAXPROCS for the
36+
// collector, bridge, and target allocator.
37+
SetGolangFlags = featuregate.GlobalRegistry().MustRegister(
38+
"operator.golang.flags",
39+
featuregate.StageAlpha,
40+
featuregate.WithRegisterDescription("enables feature to set GOMEMLIMIT and GOMAXPROCS automatically"),
41+
featuregate.WithRegisterFromVersion("v0.100.0"),
42+
)
3543
)
3644

3745
// Flags creates a new FlagSet that represents the available featuregate flags using the supplied featuregate registry.

0 commit comments

Comments
 (0)