Add cpu and memory limits

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-06-24 09:44:47 +02:00
parent b84be73ce7
commit 0a57fd7d5c
5 changed files with 47 additions and 9 deletions

View File

@ -179,6 +179,14 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
Ports: ports, Ports: ports,
Labels: r.Labels, Labels: r.Labels,
Volumes: serviceConfigVolumes, Volumes: serviceConfigVolumes,
Deploy: &types.DeployConfig{
Resources: types.Resources{
Limits: &types.Resource{
NanoCPUs: fmt.Sprintf("%f", r.CpuLimit),
MemoryBytes: types.UnitBytes(r.MemLimit.Value()),
},
},
},
}, },
}, },
Volumes: projectVolumes, Volumes: projectVolumes,

View File

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strconv"
"strings" "strings"
"github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance" "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
@ -262,18 +263,30 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c
} else { } else {
volumes = &allVolumes volumes = &allVolumes
} }
memLimit := 1. // Default 1 Gb
var cpuLimit float64 = 1
if s.Deploy != nil && s.Deploy.Resources.Limits != nil {
memLimit = float64(bytesToGb(s.Deploy.Resources.Limits.MemoryBytes))
if s.Deploy.Resources.Limits.NanoCPUs != "" {
cpuLimit, err = strconv.ParseFloat(s.Deploy.Resources.Limits.NanoCPUs, 0)
if err != nil {
return containerinstance.Container{}, err
}
}
}
return containerinstance.Container{ return containerinstance.Container{
Name: to.StringPtr(s.Name), Name: to.StringPtr(s.Name),
ContainerProperties: &containerinstance.ContainerProperties{ ContainerProperties: &containerinstance.ContainerProperties{
Image: to.StringPtr(s.Image), Image: to.StringPtr(s.Image),
Resources: &containerinstance.ResourceRequirements{ Resources: &containerinstance.ResourceRequirements{
Limits: &containerinstance.ResourceLimits{ Limits: &containerinstance.ResourceLimits{
MemoryInGB: to.Float64Ptr(1), MemoryInGB: to.Float64Ptr(memLimit),
CPU: to.Float64Ptr(1), CPU: to.Float64Ptr(cpuLimit),
}, },
Requests: &containerinstance.ResourceRequests{ Requests: &containerinstance.ResourceRequests{
MemoryInGB: to.Float64Ptr(1), MemoryInGB: to.Float64Ptr(memLimit), // FIXME To implement
CPU: to.Float64Ptr(1), CPU: to.Float64Ptr(cpuLimit), // FIXME To implement
}, },
}, },
VolumeMounts: volumes, VolumeMounts: volumes,
@ -282,6 +295,10 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c
} }
func bytesToGb(b types.UnitBytes) int64 {
return int64(b) / 1024 / 1024 / 1024 // from bytes to gigabytes
}
// ContainerGroupToContainer composes a Container from an ACI container definition // ContainerGroupToContainer composes a Container from an ACI container definition
func ContainerGroupToContainer(containerID string, cg containerinstance.ContainerGroup, cc containerinstance.Container) (containers.Container, error) { func ContainerGroupToContainer(containerID string, cg containerinstance.ContainerGroup, cc containerinstance.Container) (containers.Container, error) {
memLimits := -1. memLimits := -1.

View File

@ -44,6 +44,8 @@ func Command() *cobra.Command {
cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container") cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target") cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target")
cmd.Flags().BoolP("detach", "d", true, "Run container in background and print container ID") cmd.Flags().BoolP("detach", "d", true, "Run container in background and print container ID")
cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
return cmd return cmd
} }

View File

@ -21,6 +21,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/docker/cli/opts"
"github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
@ -33,6 +34,8 @@ type Opts struct {
Publish []string Publish []string
Labels []string Labels []string
Volumes []string Volumes []string
Cpus float64
Memory opts.MemBytes
} }
// ToContainerConfig convert run options to a container configuration // ToContainerConfig convert run options to a container configuration
@ -52,11 +55,13 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
} }
return containers.ContainerConfig{ return containers.ContainerConfig{
ID: r.Name, ID: r.Name,
Image: image, Image: image,
Ports: publish, Ports: publish,
Labels: labels, Labels: labels,
Volumes: r.Volumes, Volumes: r.Volumes,
MemLimit: r.Memory,
CpuLimit: r.Cpus,
}, nil }, nil
} }

View File

@ -19,6 +19,8 @@ package containers
import ( import (
"context" "context"
"io" "io"
"github.com/docker/cli/opts"
) )
// Container represents a created container // Container represents a created container
@ -60,6 +62,10 @@ type ContainerConfig struct {
Labels map[string]string Labels map[string]string
// Volumes to be mounted // Volumes to be mounted
Volumes []string Volumes []string
// Memlimit
MemLimit opts.MemBytes
// CPUlimit
CpuLimit float64
} }
// LogsRequest contains configuration about a log request // LogsRequest contains configuration about a log request