From 21e23d918527e5cf021a1613f4603273ce5eb044 Mon Sep 17 00:00:00 2001
From: Gowtham Sundara <gowtham.sundara@getcruise.com>
Date: Thu, 17 Oct 2024 22:18:26 -0700
Subject: [PATCH] Add support for merging GO* variables.

---
 internal/manifests/collector/container.go     | 31 +++++++++++++------
 internal/manifests/opampbridge/container.go   | 31 +++++++++++++------
 .../manifests/targetallocator/container.go    | 31 +++++++++++++------
 3 files changed, 66 insertions(+), 27 deletions(-)

diff --git a/internal/manifests/collector/container.go b/internal/manifests/collector/container.go
index 9be69c2234..bb11e2b8ec 100644
--- a/internal/manifests/collector/container.go
+++ b/internal/manifests/collector/container.go
@@ -156,16 +156,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(),
+		skipGoMemLimit, skipGoMaxprocs := false, false
+		for _, v := range envVars {
+			if v.Name == "GOMEMLIMIT" {
+				skipGoMemLimit = true
+			} else if v.Name == "GOMAXPROCS" {
+				skipGoMaxprocs = true
+			}
+		}
+		if !skipGoMemLimit {
+			envVars = append(envVars, corev1.EnvVar{
+				Name: "GOMEMLIMIT",
+				ValueFrom: &corev1.EnvVarSource{
+					ResourceFieldRef: &corev1.ResourceFieldSelector{
+						Resource:      "limits.memory",
+						ContainerName: naming.Container(),
+					},
 				},
 			},
-		},
-			corev1.EnvVar{
+			)
+		}
+		if !skipGoMaxprocs {
+			envVars = append(envVars, corev1.EnvVar{
 				Name: "GOMAXPROCS",
 				ValueFrom: &corev1.EnvVarSource{
 					ResourceFieldRef: &corev1.ResourceFieldSelector{
@@ -174,7 +186,8 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1beta1.OpenTeleme
 					},
 				},
 			},
-		)
+			)
+		}
 	}
 
 	envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...)
diff --git a/internal/manifests/opampbridge/container.go b/internal/manifests/opampbridge/container.go
index 6b5e70a8d6..7c06b6f19c 100644
--- a/internal/manifests/opampbridge/container.go
+++ b/internal/manifests/opampbridge/container.go
@@ -64,16 +64,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(),
+		skipGoMemLimit, skipGoMaxprocs := false, false
+		for _, v := range envVars {
+			if v.Name == "GOMEMLIMIT" {
+				skipGoMemLimit = true
+			} else if v.Name == "GOMAXPROCS" {
+				skipGoMaxprocs = true
+			}
+		}
+		if !skipGoMemLimit {
+			envVars = append(envVars, corev1.EnvVar{
+				Name: "GOMEMLIMIT",
+				ValueFrom: &corev1.EnvVarSource{
+					ResourceFieldRef: &corev1.ResourceFieldSelector{
+						Resource:      "limits.memory",
+						ContainerName: naming.OpAMPBridgeContainer(),
+					},
 				},
 			},
-		},
-			corev1.EnvVar{
+			)
+		}
+		if !skipGoMaxprocs {
+			envVars = append(envVars, corev1.EnvVar{
 				Name: "GOMAXPROCS",
 				ValueFrom: &corev1.EnvVarSource{
 					ResourceFieldRef: &corev1.ResourceFieldSelector{
@@ -82,7 +94,8 @@ func Container(cfg config.Config, logger logr.Logger, opampBridge v1alpha1.OpAMP
 					},
 				},
 			},
-		)
+			)
+		}
 	}
 
 	envVars = append(envVars, proxy.ReadProxyVarsFromEnv()...)
diff --git a/internal/manifests/targetallocator/container.go b/internal/manifests/targetallocator/container.go
index f1e5e78bbc..9aa1bebfe9 100644
--- a/internal/manifests/targetallocator/container.go
+++ b/internal/manifests/targetallocator/container.go
@@ -82,16 +82,28 @@ func Container(cfg config.Config, logger logr.Logger, instance v1alpha1.TargetAl
 	}
 
 	if featuregate.SetGolangFlags.IsEnabled() {
-		envVars = append(envVars, corev1.EnvVar{
-			Name: "GOMEMLIMIT",
-			ValueFrom: &corev1.EnvVarSource{
-				ResourceFieldRef: &corev1.ResourceFieldSelector{
-					Resource:      "limits.memory",
-					ContainerName: naming.TAContainer(),
+		skipGoMemLimit, skipGoMaxprocs := false, false
+		for _, v := range envVars {
+			if v.Name == "GOMEMLIMIT" {
+				skipGoMemLimit = true
+			} else if v.Name == "GOMAXPROCS" {
+				skipGoMaxprocs = true
+			}
+		}
+		if !skipGoMemLimit {
+			envVars = append(envVars, corev1.EnvVar{
+				Name: "GOMEMLIMIT",
+				ValueFrom: &corev1.EnvVarSource{
+					ResourceFieldRef: &corev1.ResourceFieldSelector{
+						Resource:      "limits.memory",
+						ContainerName: naming.TAContainer(),
+					},
 				},
 			},
-		},
-			corev1.EnvVar{
+			)
+		}
+		if !skipGoMaxprocs {
+			envVars = append(envVars, corev1.EnvVar{
 				Name: "GOMAXPROCS",
 				ValueFrom: &corev1.EnvVarSource{
 					ResourceFieldRef: &corev1.ResourceFieldSelector{
@@ -100,7 +112,8 @@ func Container(cfg config.Config, logger logr.Logger, instance v1alpha1.TargetAl
 					},
 				},
 			},
-		)
+			)
+		}
 	}
 
 	var args []string