Skip to content

Commit

Permalink
docker engine can update resource to unlimit
Browse files Browse the repository at this point in the history
  • Loading branch information
zc authored and CMGS committed Mar 13, 2020
1 parent e21ef27 commit 262c693
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
31 changes: 30 additions & 1 deletion engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"

"github.com/docker/go-connections/nat"
"github.com/docker/go-units"
Expand All @@ -26,6 +28,7 @@ import (

const (
minMemory = units.MiB * 4
maxMemory = math.MaxInt64
restartAlways = "always"
root = "root"
)
Expand Down Expand Up @@ -309,7 +312,33 @@ func (e *Engine) VirtualizationUpdateResource(ctx context.Context, ID string, op
log.Errorf("[VirtualizationUpdateResource] docker engine not support rebinding volume resource: %v", opts.Volumes)
return coretypes.ErrNotSupport
}
newResource := makeResourceSetting(opts.Quota, opts.Memory, opts.CPU, opts.NUMANode, opts.SoftLimit)

memory := opts.Memory
softLimit := opts.SoftLimit
// unlimited memory
if memory == 0 {
memory = maxMemory
softLimit = false
}

quota := opts.Quota
cpuMap := opts.CPU
numaNode := opts.NUMANode
// unlimited cpu
if quota == 0 {
info, err := e.Info(ctx)
if err != nil {
return err
}
quota = -1
numaNode = fmt.Sprintf("0-%d", info.NCPU-1)
cpuMap = map[string]int64{}
for i := 0; i < info.NCPU; i++ {
cpuMap[strconv.Itoa(i)] = int64(e.config.Scheduler.ShareBase)
}
}

newResource := makeResourceSetting(quota, memory, cpuMap, numaNode, softLimit)
updateConfig := dockercontainer.UpdateConfig{Resources: newResource}
_, err := e.client.ContainerUpdate(ctx, ID, updateConfig)
return err
Expand Down
6 changes: 5 additions & 1 deletion engine/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ func makeMountPaths(opts *enginetypes.VirtualizationCreateOptions) ([]string, ma

func makeResourceSetting(cpu float64, memory int64, cpuMap map[string]int64, numaNode string, softlimit bool) dockercontainer.Resources {
resource := dockercontainer.Resources{}

resource.CPUQuota = 0
resource.CPUPeriod = corecluster.CPUPeriodBase
if cpu > 0 {
resource.CPUPeriod = corecluster.CPUPeriodBase
resource.CPUQuota = int64(cpu * float64(corecluster.CPUPeriodBase))
} else if cpu == -1 {
resource.CPUQuota = -1
}

if cpuMap != nil && len(cpuMap) > 0 {
cpuIDs := []string{}
for cpuID := range cpuMap {
Expand Down

0 comments on commit 262c693

Please sign in to comment.