Skip to content

Commit

Permalink
Skip resource annotations if disabled through injector annotations (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahhenkens authored Sep 3, 2020
1 parent 48972fb commit d5298ad
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 40 deletions.
43 changes: 27 additions & 16 deletions agent-inject/agent/container_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,42 @@ func (a *Agent) parseResources() (corev1.ResourceRequirements, error) {
requests := corev1.ResourceList{}

// Limits
cpu, err := parseQuantity(a.LimitsCPU)
if err != nil {
return resources, err
if a.LimitsCPU != "" {
cpu, err := parseQuantity(a.LimitsCPU)
if err != nil {
return resources, err
}

limits[corev1.ResourceCPU] = cpu
}
limits[corev1.ResourceCPU] = cpu

mem, err := parseQuantity(a.LimitsMem)
if err != nil {
return resources, err
if a.LimitsMem != "" {
mem, err := parseQuantity(a.LimitsMem)
if err != nil {
return resources, err
}
limits[corev1.ResourceMemory] = mem
}
limits[corev1.ResourceMemory] = mem

resources.Limits = limits

// Requests
cpu, err = parseQuantity(a.RequestsCPU)
if err != nil {
return resources, err
if a.RequestsCPU != "" {
cpu, err := parseQuantity(a.RequestsCPU)
if err != nil {
return resources, err
}
requests[corev1.ResourceCPU] = cpu
}
requests[corev1.ResourceCPU] = cpu

mem, err = parseQuantity(a.RequestsMem)
if err != nil {
return resources, err
if a.RequestsMem != "" {
mem, err := parseQuantity(a.RequestsMem)
if err != nil {
return resources, err
}
requests[corev1.ResourceMemory] = mem
}
requests[corev1.ResourceMemory] = mem

resources.Requests = requests

return resources, nil
Expand Down
76 changes: 52 additions & 24 deletions agent-inject/agent/container_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ func TestContainerSidecarConfigMap(t *testing.T) {
}

func TestContainerSidecarCustomResources(t *testing.T) {
absent := "absent"

tests := []struct {
name string
agent Agent
Expand Down Expand Up @@ -334,6 +336,20 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsCPU: "",
RequestsMem: "",
},
expectedLimitCPU: absent,
expectedLimitMem: absent,
expectedRequestCPU: absent,
expectedRequestMem: absent,
expectedErr: false,
},
{
name: "valid 0",
agent: Agent{
LimitsCPU: "0",
LimitsMem: "0",
RequestsCPU: "0",
RequestsMem: "0",
},
expectedLimitCPU: "0",
expectedLimitMem: "0",
expectedRequestCPU: "0",
Expand All @@ -350,8 +366,8 @@ func TestContainerSidecarCustomResources(t *testing.T) {
},
expectedLimitCPU: "500Mi",
expectedLimitMem: "128m",
expectedRequestCPU: "0",
expectedRequestMem: "0",
expectedRequestCPU: absent,
expectedRequestMem: absent,
expectedErr: false,
},
{
Expand All @@ -362,8 +378,8 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsCPU: "250Mi",
RequestsMem: "64m",
},
expectedLimitCPU: "0",
expectedLimitMem: "0",
expectedLimitCPU: absent,
expectedLimitMem: absent,
expectedRequestCPU: "250Mi",
expectedRequestMem: "64m",
expectedErr: false,
Expand All @@ -377,9 +393,9 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsMem: "",
},
expectedLimitCPU: "500Mi",
expectedLimitMem: "0",
expectedRequestCPU: "0",
expectedRequestMem: "0",
expectedLimitMem: absent,
expectedRequestCPU: absent,
expectedRequestMem: absent,
expectedErr: false,
},
{
Expand All @@ -390,10 +406,10 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsCPU: "",
RequestsMem: "",
},
expectedLimitCPU: "0",
expectedLimitCPU: absent,
expectedLimitMem: "128m",
expectedRequestCPU: "0",
expectedRequestMem: "0",
expectedRequestCPU: absent,
expectedRequestMem: absent,
expectedErr: false,
},
{
Expand All @@ -404,10 +420,10 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsCPU: "500Mi",
RequestsMem: "",
},
expectedLimitCPU: "0",
expectedLimitMem: "0",
expectedLimitCPU: absent,
expectedLimitMem: absent,
expectedRequestCPU: "500Mi",
expectedRequestMem: "0",
expectedRequestMem: absent,
expectedErr: false,
},
{
Expand All @@ -418,9 +434,9 @@ func TestContainerSidecarCustomResources(t *testing.T) {
RequestsCPU: "",
RequestsMem: "128m",
},
expectedLimitCPU: "0",
expectedLimitMem: "0",
expectedRequestCPU: "0",
expectedLimitCPU: absent,
expectedLimitMem: absent,
expectedRequestCPU: absent,
expectedRequestMem: "128m",
expectedErr: false,
},
Expand Down Expand Up @@ -508,20 +524,32 @@ func TestContainerSidecarCustomResources(t *testing.T) {
}

if !tt.expectedErr {
if resources.Limits.Cpu().String() != tt.expectedLimitCPU {
t.Errorf("expected cpu limit mismatch: wanted %s, got %s", tt.expectedLimitCPU, resources.Limits.Cpu().String())
cpu, exists := resources.Limits["cpu"]
if tt.expectedLimitCPU == absent && exists {
t.Errorf("expected cpu limit to not exist")
} else if tt.expectedLimitCPU != absent && cpu.String() != tt.expectedLimitCPU {
t.Errorf("expected cpu limit mismatch: wanted %s, got %s", tt.expectedLimitCPU, cpu.String())
}

if resources.Limits.Memory().String() != tt.expectedLimitMem {
t.Errorf("expected mem limit mismatch: wanted %s, got %s", tt.expectedLimitMem, resources.Limits.Memory().String())
mem, exists := resources.Limits["memory"]
if tt.expectedLimitMem == absent && exists {
t.Errorf("expected mem limit to not exist")
} else if tt.expectedLimitMem != absent && mem.String() != tt.expectedLimitMem {
t.Errorf("expected mem limit mismatch: wanted %s, got %s", tt.expectedLimitMem, mem.String())
}

if resources.Requests.Cpu().String() != tt.expectedRequestCPU {
t.Errorf("%s expected cpu request mismatch: wanted %s, got %s", tt.name, tt.expectedLimitCPU, resources.Requests.Cpu().String())
cpu, exists = resources.Requests["cpu"]
if tt.expectedRequestCPU == absent && exists {
t.Errorf("expected cpu request to not exist")
} else if tt.expectedRequestCPU != absent && cpu.String() != tt.expectedRequestCPU {
t.Errorf("expected cpu request mismatch: wanted %s, got %s", tt.expectedRequestCPU, cpu.String())
}

if resources.Requests.Memory().String() != tt.expectedRequestMem {
t.Errorf("%s expected mem request mismatch: wanted %s, got %s", tt.name, tt.expectedLimitMem, resources.Requests.Memory().String())
mem, exists = resources.Requests["memory"]
if tt.expectedRequestMem == absent && exists {
t.Errorf("expected mem limit to not exist")
} else if tt.expectedRequestMem != absent && mem.String() != tt.expectedRequestMem {
t.Errorf("expected mem request mismatch: wanted %s, got %s", tt.expectedRequestMem, mem.String())
}
}
})
Expand Down

0 comments on commit d5298ad

Please sign in to comment.