Resource limits

This commit is contained in:
Laszlo Fogas
2019-05-29 08:33:12 +02:00
parent f2e41fe687
commit cc4101ef35
7 changed files with 48 additions and 4 deletions

View File

@@ -34,6 +34,8 @@ pipeline:
commands: sh .drone.sh
when:
event: [ push, tag ]
# resources:
# cpu_limit: "12"
publish_server_alpine:
image: plugins/docker

Binary file not shown.

View File

@@ -54,14 +54,31 @@ func Pod(namespace string, step *backend.Step) *v1.Pod {
hostAliases = append(hostAliases, v1.HostAlias{IP: host[1], Hostnames: []string{host[0]}})
}
if step.Resources.CPULimit == "" {
step.Resources.CPULimit = "2"
}
if step.Resources.MemoryLimit == "" {
step.Resources.MemoryLimit = "2G"
}
memoryLimit := resource.MustParse(step.Resources.MemoryLimit)
CPULimit := resource.MustParse(step.Resources.CPULimit)
memoryLimitValue, _ := memoryLimit.AsInt64()
CPULimitValue, _ := CPULimit.AsInt64()
loadfactor := 0.5
memoryRequest := resource.NewQuantity(int64(float64(memoryLimitValue)*loadfactor), resource.DecimalSI)
CPURequest := resource.NewQuantity(int64(float64(CPULimitValue)*loadfactor), resource.DecimalSI)
resources := v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("256Mi"),
v1.ResourceCPU: resource.MustParse("500m"),
v1.ResourceMemory: *memoryRequest,
v1.ResourceCPU: *CPURequest,
},
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("512Mi"),
v1.ResourceCPU: resource.MustParse("1000m"),
v1.ResourceMemory: memoryLimit,
v1.ResourceCPU: CPULimit,
},
}

View File

@@ -49,6 +49,7 @@ type (
IpcMode string `json:"ipc_mode,omitempty"`
Sysctls map[string]string `json:"sysctls,omitempty"`
Ports []string `json:"ports,omitempty"`
Resources Resources `json:"resources,omitempty"`
}
// Auth defines registry authentication credentials.
@@ -86,6 +87,12 @@ type (
Mask bool `json:"mask,omitempty"`
}
// Resources defines pod resources limits.
Resources struct {
CPULimit string `json:"cpu_limit"`
MemoryLimit string `json:"mem_limit"`
}
// State defines a container state.
State struct {
// Container exit code

View File

@@ -143,6 +143,10 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
cpuSet = c.reslimit.CPUSet
}
ports := container.Ports
resources := backend.Resources{
MemoryLimit: container.Resources.MemoryLimit,
CPULimit: container.Resources.CPULimit,
}
return &backend.Step{
Name: name,
@@ -178,5 +182,6 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
NetworkMode: network_mode,
IpcMode: ipc_mode,
Ports: ports,
Resources: resources,
}
}

View File

@@ -59,9 +59,16 @@ type (
Constraints Constraints `yaml:"when,omitempty"`
Vargs map[string]interface{} `yaml:",inline"`
Ports []string `yaml:"ports,omitempty"`
Resources Resources `yaml:"resources,omitempty"`
}
)
// Resources defines pod resources limits.
type Resources struct {
CPULimit string `yaml:"cpu_limit,omitempty"`
MemoryLimit string `yaml:"mem_limit,omitempty"`
}
// UnmarshalYAML implements the Unmarshaller interface.
func (c *Containers) UnmarshalYAML(unmarshal func(interface{}) error) error {
slice := yaml.MapSlice{}

View File

@@ -62,6 +62,10 @@ tmpfs:
- /var/lib/test
when:
branch: master
ports: ["5432"]
resources:
mem_limit: 1G
cpu_limit: 1
`)
func TestUnmarshalContainer(t *testing.T) {
@@ -91,6 +95,8 @@ func TestUnmarshalContainer(t *testing.T) {
MemSwapLimit: libcompose.MemStringorInt(1024),
MemSwappiness: libcompose.MemStringorInt(1024),
Name: "my-build-container",
Ports: []string{"5432"},
Resources: Resources{MemoryLimit: "1G", CPULimit: "1"},
Networks: libcompose.Networks{
Networks: []*libcompose.Network{
{Name: "some-network"},