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

[clusteragent] Remove the CPU/memory limits by default for the init container #30266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
mountPath = "/datadog-lib"

// defaultMilliCPURequest defines default milli cpu request number.
defaultMilliCPURequest int64 = 50 // 0.05 core
defaultMilliCPURequest int64 = 300 // 0.3 core
// defaultMemoryRequest defines default memory request size.
defaultMemoryRequest int64 = 100 * 1024 * 1024 // 100 MB (recommended minimum by Alpine)

Expand Down Expand Up @@ -651,7 +651,6 @@ func (w *Webhook) injectAutoInstruConfig(pod *corev1.Pod, config extractedPodLib
}

func initResources() (corev1.ResourceRequirements, error) {

var resources = corev1.ResourceRequirements{Limits: corev1.ResourceList{}, Requests: corev1.ResourceList{}}

if pkgconfigsetup.Datadog().IsSet("admission_controller.auto_instrumentation.init_resources.cpu") {
Expand All @@ -660,10 +659,16 @@ func initResources() (corev1.ResourceRequirements, error) {
return resources, err
}
resources.Requests[corev1.ResourceCPU] = quantity
resources.Limits[corev1.ResourceCPU] = quantity
} else {
resources.Requests[corev1.ResourceCPU] = *resource.NewMilliQuantity(defaultMilliCPURequest, resource.DecimalSI)
resources.Limits[corev1.ResourceCPU] = *resource.NewMilliQuantity(defaultMilliCPURequest, resource.DecimalSI)
}

if pkgconfigsetup.Datadog().IsSet("admission_controller.auto_instrumentation.init_resources.cpu_limit") {
quantity, err := resource.ParseQuantity(pkgconfigsetup.Datadog().GetString("admission_controller.auto_instrumentation.init_resources.cpu_limit"))
if err != nil {
return resources, err
}
resources.Limits[corev1.ResourceCPU] = quantity
Copy link
Author

Choose a reason for hiding this comment

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

Is it OK to not set a limit at all (same for the memory)?

}

if pkgconfigsetup.Datadog().IsSet("admission_controller.auto_instrumentation.init_resources.memory") {
Expand All @@ -672,10 +677,16 @@ func initResources() (corev1.ResourceRequirements, error) {
return resources, err
}
resources.Requests[corev1.ResourceMemory] = quantity
resources.Limits[corev1.ResourceMemory] = quantity
} else {
resources.Requests[corev1.ResourceMemory] = *resource.NewQuantity(defaultMemoryRequest, resource.DecimalSI)
resources.Limits[corev1.ResourceMemory] = *resource.NewQuantity(defaultMemoryRequest, resource.DecimalSI)
}

if pkgconfigsetup.Datadog().IsSet("admission_controller.auto_instrumentation.init_resources.memory_limit") {
quantity, err := resource.ParseQuantity(pkgconfigsetup.Datadog().GetString("admission_controller.auto_instrumentation.init_resources.memory_limit"))
if err != nil {
return resources, err
}
resources.Limits[corev1.ResourceMemory] = quantity
}

return resources, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,24 +1080,26 @@ func TestInjectLibConfig(t *testing.T) {

func TestInjectLibInitContainer(t *testing.T) {
tests := []struct {
name string
cpu string
mem string
pod *corev1.Pod
image string
lang language
wantErr bool
wantCPU string
wantMem string
secCtx *corev1.SecurityContext
name string
cpu string
cpuLimit string
mem string
memLimit string
pod *corev1.Pod
image string
lang language
wantErr bool
wantCPU string
wantMem string
secCtx *corev1.SecurityContext
}{
{
name: "no resources, no security context",
pod: common.FakePod("java-pod"),
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: false,
wantCPU: "50m",
wantCPU: "300m",
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{},
},
Expand All @@ -1124,14 +1126,26 @@ func TestInjectLibInitContainer(t *testing.T) {
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{},
},
{
name: "cpu and memory limits",
pod: common.FakePod("java-pod"),
cpuLimit: "600m",
memLimit: "400Mi",
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: false,
wantCPU: "300m",
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{},
},
{
name: "memory only",
pod: common.FakePod("java-pod"),
mem: "512Mi",
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: false,
wantCPU: "50m",
wantCPU: "300m",
wantMem: "512Mi",
secCtx: &corev1.SecurityContext{},
},
Expand All @@ -1142,7 +1156,7 @@ func TestInjectLibInitContainer(t *testing.T) {
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: true,
wantCPU: "50m",
wantCPU: "300m",
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{},
},
Expand All @@ -1152,7 +1166,7 @@ func TestInjectLibInitContainer(t *testing.T) {
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: false,
wantCPU: "50m",
wantCPU: "300m",
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Expand Down Expand Up @@ -1190,7 +1204,7 @@ func TestInjectLibInitContainer(t *testing.T) {
image: "gcr.io/datadoghq/dd-lib-java-init:v1",
lang: java,
wantErr: false,
wantCPU: "50m",
wantCPU: "300m",
wantMem: "100Mi",
secCtx: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Expand All @@ -1217,9 +1231,15 @@ func TestInjectLibInitContainer(t *testing.T) {
if tt.cpu != "" {
conf.SetWithoutSource("admission_controller.auto_instrumentation.init_resources.cpu", tt.cpu)
}
if tt.cpuLimit != "" {
conf.SetWithoutSource("admission_controller.auto_instrumentation.init_resources.cpu_limit", tt.cpuLimit)
}
if tt.mem != "" {
conf.SetWithoutSource("admission_controller.auto_instrumentation.init_resources.memory", tt.mem)
}
if tt.memLimit != "" {
conf.SetWithoutSource("admission_controller.auto_instrumentation.init_resources.memory_limit", tt.memLimit)
}

wh, err := NewWebhook(wmeta, GetInjectionFilter())
if (err != nil) != tt.wantErr {
Expand All @@ -1245,16 +1265,28 @@ func TestInjectLibInitContainer(t *testing.T) {
require.Len(t, tt.pod.Spec.InitContainers, 1)

req := tt.pod.Spec.InitContainers[0].Resources.Requests[corev1.ResourceCPU]
lim := tt.pod.Spec.InitContainers[0].Resources.Limits[corev1.ResourceCPU]
wantCPUQuantity := resource.MustParse(tt.wantCPU)
require.Zero(t, wantCPUQuantity.Cmp(req)) // Cmp returns 0 if equal
require.Zero(t, wantCPUQuantity.Cmp(lim))
require.Zero(t, wantCPUQuantity.Cmp(req))

lim, ok := tt.pod.Spec.InitContainers[0].Resources.Limits[corev1.ResourceCPU]
if tt.cpuLimit != "" {
limCPU := resource.MustParse(tt.cpuLimit)
require.Zero(t, lim.Cmp(limCPU))
} else {
require.False(t, ok)
}

req = tt.pod.Spec.InitContainers[0].Resources.Requests[corev1.ResourceMemory]
lim = tt.pod.Spec.InitContainers[0].Resources.Limits[corev1.ResourceMemory]
wantMemQuantity := resource.MustParse(tt.wantMem)
require.Zero(t, wantMemQuantity.Cmp(req))
require.Zero(t, wantMemQuantity.Cmp(lim))

lim, ok = tt.pod.Spec.InitContainers[0].Resources.Limits[corev1.ResourceMemory]
if tt.memLimit != "" {
limMem := resource.MustParse(tt.memLimit)
require.Zero(t, lim.Cmp(limMem))
} else {
require.False(t, ok)
}

expSecCtx := tt.pod.Spec.InitContainers[0].SecurityContext
require.Equal(t, tt.secCtx, expSecCtx)
Expand Down
16 changes: 14 additions & 2 deletions pkg/config/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3108,16 +3108,28 @@ api_key:

## @param cpu - string - optional
## @env DD_ADMISSION_CONTROLLER_AUTO_INSTRUMENTATION_INIT_RESOURCES_CPU - string - optional
## Configures the CPU request and limit for the init containers.
## Configures the CPU request for the init containers.
#
# cpu:

## @param cpu_limit - string - optional
## @env DD_ADMISSION_CONTROLLER_AUTO_INSTRUMENTATION_INIT_RESOURCES_CPU_LIMIT - string - optional
## Configures the CPU limit for the init containers.
#
# cpu:

## @param memory - string - optional
## @env DD_ADMISSION_CONTROLLER_AUTO_INSTRUMENTATION_INIT_RESOURCES_MEMORY - string - optional
## Configures the memory request and limit for the init containers.
## Configures the memory request for the init containers.
#
# memory:

## @param memory_limit - string - optional
## @env DD_ADMISSION_CONTROLLER_AUTO_INSTRUMENTATION_INIT_RESOURCES_MEMORY_LIMIT - string - optional
## Configures the memory limit for the init containers.
#
# memory_limit:

## @param init_security_context - json - optional
## @env DD_ADMISSION_CONTROLLER_AUTO_INSTRUMENTATION_INIT_SECURITY_CONTEXT - json - optional
## Security context for the init containers in JSON format. Follows the Kubernetes security context spec,
Expand Down
Loading