docker / engine-api

DEPRECATED: Please see https://github.com/docker/docker/tree/master/client
Apache License 2.0
264 stars 163 forks source link

CreateService with limit.NanoCPUs ERRO[0052] Error response from daemon: {"message":"rpc error: code = 3 desc = invalid cpu value 2e-09: Must be at least 0.001"} #370

Closed shenshouer closed 8 years ago

shenshouer commented 8 years ago

I create service code :

...
tspec := &serviceSpec.TaskTemplate
if tspec.Resources == nil {
    tspec.Resources = &swarm.ResourceRequirements{}
}

if tspec.Resources.Limits == nil {
    tspec.Resources.Limits = &swarm.Resources{}
}

tspec.Resources.Limits.NanoCPUs = req.Cores

cli.ServiceCreate(ct.Background(), serviceSpec, types.ServiceCreateOptions{})
...

set the req.Cores = 2

and report error:

ERRO[0052] Error response from daemon: {"message":"rpc error: code = 3 desc = invalid cpu value 2e-09: Must be at least 0.001"}

but I run with cli is ok:

core-01 system # docker service create --name echo4 --label=app_id=echo-test2 --replicas=2 -p 8888 --limit-memory 10485760 --limit-cpu 2 dhub.yunpro.cn/shenshouer/echo:latest
e8oedwt69tq6pf1z99d5mqya6
shenshouer commented 8 years ago

I changed code and goes ok :

        nanoCPUs := new(big.Rat).SetInt64(req.Cores)
        cpuRat := new(big.Rat).Mul(nanoCPUs, big.NewRat(1e9, 1))
        if !cpuRat.IsInt() {
            err = fmt.Errorf("CPU value cannot have more than 9 decimal places: %s", req.Cores)
            return
        }
        tspec.Resources.Limits.NanoCPUs = cpuRat.Num().Int64()