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

Allow configurable resource requests and limits received by helm chart. #196

Merged
merged 21 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
54 changes: 42 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ func stringFlagOrEnv(p *string, name string, envName string, defaultValue string
pflag.StringVar(p, name, defaultValue, usage)
}

func setLangEnvVarsForResource(langStr string, resourceStr string, resource map[string]string) {
if cpu, ok := resource["cpu"]; ok {
os.Setenv("AUTO_INSTRUMENTATION_"+langStr+"_CPU_"+resourceStr, cpu)
}
if memory, ok := resource["memory"]; ok {
os.Setenv("AUTO_INSTRUMENTATION_"+langStr+"_MEM_"+resourceStr, memory)
}
}

func setLangEnvVars(langStr string, cfg map[string]map[string]string) {
if limits, ok := cfg["limits"]; ok {
setLangEnvVarsForResource(langStr, "LIMIT", limits)
}
if requests, ok := cfg["requests"]; ok {
setLangEnvVarsForResource(langStr, "REQUEST", requests)
}
}

func main() {
// registers any flags that underlying libraries might use
opts := zap.Options{}
Expand All @@ -93,18 +111,19 @@ func main() {

// add flags related to this operator
var (
metricsAddr string
probeAddr string
pprofAddr string
agentImage string
autoInstrumentationJava string
autoInstrumentationPython string
autoInstrumentationDotNet string
autoAnnotationConfigStr string
webhookPort int
tlsOpt tlsConfig
dcgmExporterImage string
neuronMonitorImage string
metricsAddr string
probeAddr string
pprofAddr string
agentImage string
autoInstrumentationJava string
autoInstrumentationPython string
autoInstrumentationDotNet string
autoAnnotationConfigStr string
musa-asad marked this conversation as resolved.
Show resolved Hide resolved
autoInstrumentationConfigStr string
webhookPort int
tlsOpt tlsConfig
dcgmExporterImage string
neuronMonitorImage string
)

pflag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
Expand All @@ -115,10 +134,21 @@ func main() {
stringFlagOrEnv(&autoInstrumentationPython, "auto-instrumentation-python-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_PYTHON", fmt.Sprintf("%s:%s", autoInstrumentationPythonImageRepository, v.AutoInstrumentationPython), "The default OpenTelemetry Python instrumentation image. This image is used when no image is specified in the CustomResource.")
stringFlagOrEnv(&autoInstrumentationDotNet, "auto-instrumentation-dotnet-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_DOTNET", fmt.Sprintf("%s:%s", autoInstrumentationDotNetImageRepository, v.AutoInstrumentationDotNet), "The default OpenTelemetry Dotnet instrumentation image. This image is used when no image is specified in the CustomResource.")
stringFlagOrEnv(&autoAnnotationConfigStr, "auto-annotation-config", "AUTO_ANNOTATION_CONFIG", "", "The configuration for auto-annotation.")
pflag.StringVar(&autoInstrumentationConfigStr, "auto-instrumentation-config", "", "The configuration for auto-instrumentation.")
stringFlagOrEnv(&dcgmExporterImage, "dcgm-exporter-image", "RELATED_IMAGE_DCGM_EXPORTER", fmt.Sprintf("%s:%s", dcgmExporterImageRepository, v.DcgmExporter), "The default DCGM Exporter image. This image is used when no image is specified in the CustomResource.")
stringFlagOrEnv(&neuronMonitorImage, "neuron-monitor-image", "RELATED_IMAGE_NEURON_MONITOR", fmt.Sprintf("%s:%s", neuronMonitorImageRepository, v.NeuronMonitor), "The default Neuron monitor image. This image is used when no image is specified in the CustomResource.")
pflag.Parse()

// set instrumentation cpu and memory limits in environment variables to be used for default instrumentation
musa-asad marked this conversation as resolved.
Show resolved Hide resolved
autoInstrumentationConfig := map[string]map[string]map[string]string{"java": {"limits": {"cpu": "64Mi", "memory": "500m"}, "requests": {"cpu": "64Mi", "memory": "50m"}}, "python": {"limits": {"cpu": "32Mi", "memory": "500m"}, "requests": {"cpu": "32Mi", "memory": "50m"}}, "dotnet": {"limits": {"cpu": "128Mi", "memory": "500m"}, "requests": {"cpu": "128Mi", "memory": "50m"}}}
err := json.Unmarshal([]byte(autoInstrumentationConfigStr), &autoInstrumentationConfig)
if err != nil {
setupLog.Info(fmt.Sprintf("Using default values: %v", autoInstrumentationConfig))
}
setLangEnvVars("JAVA", autoInstrumentationConfig["java"])
setLangEnvVars("PYTHON", autoInstrumentationConfig["python"])
setLangEnvVars("DOTNET", autoInstrumentationConfig["dotnet"])

// set supported language instrumentation images in environment variable to be used for default instrumentation
os.Setenv("AUTO_INSTRUMENTATION_JAVA", autoInstrumentationJava)
os.Setenv("AUTO_INSTRUMENTATION_PYTHON", autoInstrumentationPython)
Expand Down
29 changes: 29 additions & 0 deletions pkg/instrumentation/defaultinstrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package instrumentation
import (
"errors"
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"os"

corev1 "k8s.io/api/core/v1"
Expand All @@ -25,6 +26,22 @@ const (
https = "https"
)

func getInstrumentationConfigForResource(langStr string, resourceStr string) corev1.ResourceList {
instrumentationConfigCpu, _ := os.LookupEnv("AUTO_INSTRUMENTATION_" + langStr + "_CPU_" + resourceStr)
instrumentationConfigMemory, _ := os.LookupEnv("AUTO_INSTRUMENTATION_" + langStr + "_MEM_" + resourceStr)

instrumentationConfigForResource := corev1.ResourceList{}
instrumentationConfigCpuQuantity, err := resource.ParseQuantity(instrumentationConfigCpu)
if err == nil {
instrumentationConfigForResource[corev1.ResourceCPU] = instrumentationConfigCpuQuantity
}
instrumentationConfigMemoryQuantity, err := resource.ParseQuantity(instrumentationConfigMemory)
if err == nil {
instrumentationConfigForResource[corev1.ResourceMemory] = instrumentationConfigMemoryQuantity
}
musa-asad marked this conversation as resolved.
Show resolved Hide resolved
return instrumentationConfigForResource
}

func getDefaultInstrumentation(agentConfig *adapters.CwaConfig, isWindowsPod bool) (*v1alpha1.Instrumentation, error) {
javaInstrumentationImage, ok := os.LookupEnv("AUTO_INSTRUMENTATION_JAVA")
if !ok {
Expand Down Expand Up @@ -86,6 +103,10 @@ func getDefaultInstrumentation(agentConfig *adapters.CwaConfig, isWindowsPod boo
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: getInstrumentationConfigForResource("JAVA", "LIMIT"),
musa-asad marked this conversation as resolved.
Show resolved Hide resolved
Requests: getInstrumentationConfigForResource("JAVA", "REQUEST"),
},
},
Python: v1alpha1.Python{
Image: pythonInstrumentationImage,
Expand All @@ -103,6 +124,10 @@ func getDefaultInstrumentation(agentConfig *adapters.CwaConfig, isWindowsPod boo
{Name: "OTEL_PYTHON_CONFIGURATOR", Value: "aws_configurator"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: getInstrumentationConfigForResource("PYTHON", "LIMIT"),
Requests: getInstrumentationConfigForResource("PYTHON", "REQUEST"),
},
},
DotNet: v1alpha1.DotNet{
Image: dotNetInstrumentationImage,
Expand All @@ -120,6 +145,10 @@ func getDefaultInstrumentation(agentConfig *adapters.CwaConfig, isWindowsPod boo
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
{Name: "OTEL_DOTNET_AUTO_PLUGINS", Value: "AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation"},
},
Resources: corev1.ResourceRequirements{
Limits: getInstrumentationConfigForResource("DOTNET", "LIMIT"),
Requests: getInstrumentationConfigForResource("DOTNET", "REQUEST"),
},
},
},
}, nil
Expand Down
145 changes: 145 additions & 0 deletions pkg/instrumentation/defaultinstrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package instrumentation

import (
"k8s.io/apimachinery/pkg/api/resource"
"os"
"reflect"
"testing"
Expand All @@ -19,6 +20,18 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
os.Setenv("AUTO_INSTRUMENTATION_JAVA", defaultJavaInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_PYTHON", defaultPythonInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_DOTNET", defaultDotNetInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_JAVA_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_MEM_LIMIT", "64Mi")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_MEM_REQUEST", "64Mi")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_MEM_LIMIT", "32Mi")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_MEM_REQUEST", "32Mi")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_LIMIT", "128Mi")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_REQUEST", "128Mi")

httpInst := &v1alpha1.Instrumentation{
Status: v1alpha1.InstrumentationStatus{},
Expand Down Expand Up @@ -51,6 +64,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
},
},
Python: v1alpha1.Python{
Image: defaultPythonInstrumentationImage,
Expand All @@ -68,6 +91,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_PYTHON_CONFIGURATOR", Value: "aws_configurator"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
},
},
DotNet: v1alpha1.DotNet{
Image: defaultDotNetInstrumentationImage,
Expand All @@ -85,6 +118,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
{Name: "OTEL_DOTNET_AUTO_PLUGINS", Value: "AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
},
}
Expand Down Expand Up @@ -119,6 +162,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
},
},
Python: v1alpha1.Python{
Image: defaultPythonInstrumentationImage,
Expand All @@ -136,6 +189,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_PYTHON_CONFIGURATOR", Value: "aws_configurator"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
},
},
DotNet: v1alpha1.DotNet{
Image: defaultDotNetInstrumentationImage,
Expand All @@ -153,6 +216,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) {
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
{Name: "OTEL_DOTNET_AUTO_PLUGINS", Value: "AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
},
}
Expand Down Expand Up @@ -218,6 +291,18 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
os.Setenv("AUTO_INSTRUMENTATION_JAVA", defaultJavaInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_PYTHON", defaultPythonInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_DOTNET", defaultDotNetInstrumentationImage)
os.Setenv("AUTO_INSTRUMENTATION_JAVA_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_MEM_LIMIT", "64Mi")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_JAVA_MEM_REQUEST", "64Mi")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_MEM_LIMIT", "32Mi")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_PYTHON_MEM_REQUEST", "32Mi")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_LIMIT", "500m")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_LIMIT", "128Mi")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_REQUEST", "50m")
os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_REQUEST", "128Mi")

httpInst := &v1alpha1.Instrumentation{
Status: v1alpha1.InstrumentationStatus{},
Expand Down Expand Up @@ -250,6 +335,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
},
},
Python: v1alpha1.Python{
Image: defaultPythonInstrumentationImage,
Expand All @@ -267,6 +362,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_PYTHON_CONFIGURATOR", Value: "aws_configurator"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
},
},
DotNet: v1alpha1.DotNet{
Image: defaultDotNetInstrumentationImage,
Expand All @@ -284,6 +389,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
{Name: "OTEL_DOTNET_AUTO_PLUGINS", Value: "AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
},
}
Expand Down Expand Up @@ -318,6 +433,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
},
},
Python: v1alpha1.Python{
Image: defaultPythonInstrumentationImage,
Expand All @@ -335,6 +460,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_PYTHON_CONFIGURATOR", Value: "aws_configurator"},
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("32Mi"),
},
},
},
DotNet: v1alpha1.DotNet{
Image: defaultDotNetInstrumentationImage,
Expand All @@ -352,6 +487,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) {
{Name: "OTEL_LOGS_EXPORTER", Value: "none"},
{Name: "OTEL_DOTNET_AUTO_PLUGINS", Value: "AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
},
}
Expand Down
Loading