From d371347441d856cc52ac97ea417e6828707ba743 Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Wed, 30 Dec 2015 13:49:08 +0800 Subject: [PATCH] fix bugs to parse oci specs Signed-off-by: Ma Shimiao --- generate.go | 58 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/generate.go b/generate.go index 1a13570..591dbe2 100644 --- a/generate.go +++ b/generate.go @@ -134,10 +134,13 @@ func modify(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, context *cli.C spec.Platform.Arch = context.String("arch") spec.Process.Cwd = context.String("cwd") spec.Process.Terminal = context.Bool("terminal") - rspec.Linux.CgroupsPath = context.String("cgroupspath") + cgroupspath := context.String("cgroupspath") + rspec.Linux.CgroupsPath = &cgroupspath rspec.Linux.ApparmorProfile = context.String("apparmor") - rspec.Linux.Resources.DisableOOMKiller = context.Bool("disableoomiller") - rspec.Linux.Resources.Pids.Limit = int64(context.Int("pids")) + disableoomiller := context.Bool("disableoomiller") + rspec.Linux.Resources.DisableOOMKiller = &disableoomiller + pids := int64(context.Int("pids")) + rspec.Linux.Resources.Pids.Limit = &pids rspec.Linux.Resources.Network.ClassID = context.String("networkid") for i, a := range context.StringSlice("args") { @@ -236,21 +239,23 @@ func addBlockIO(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, context *c if context.Int("blockio-weight") > 1000 || context.Int("blockio-weight") < 10 { return fmt.Errorf("blockio-weight range is from 10 to 1000") } - rspec.Linux.Resources.BlockIO.Weight = uint16(context.Int("blockio-weight")) + blkioweight := uint16(context.Int("blockio-weight")) + rspec.Linux.Resources.BlockIO.Weight = &blkioweight } if context.Int("blockio-leafweight") != 0 { if context.Int("blockio-leafweight") > 1000 || context.Int("blockio-leafweight") < 10 { return fmt.Errorf("blockio-leafweight range is from 10 to 1000") } - rspec.Linux.Resources.BlockIO.LeafWeight = uint16(context.Int("blockio-leafweight")) + blkioleafweight := uint16(context.Int("blockio-leafweight")) + rspec.Linux.Resources.BlockIO.LeafWeight = &blkioleafweight } for _, trbds := range context.StringSlice("throttlereadbpsdevice") { trbd := strings.Split(trbds, ":") if len(trbd) == 2 { fmt.Println("trbd=" + trbd[0]) blockIODevicestr := trbd[0] - rate, err := strconv.Atoi(trbd[1]) + rate, err := strconv.ParseUint(trbd[1], 10, 64) b := strings.Split(blockIODevicestr, ",") if err != nil { return err @@ -264,7 +269,7 @@ func addBlockIO(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, context *c // td := specs.ThrottleDevice{Rate: uint64(rate)} // td.blockIODevice.Major = int64(major) // td.blockIODevice.Minor = int64(minor) - td := specs.ThrottleDevice{Rate: uint64(rate)} + td := specs.ThrottleDevice{Rate: &rate} rspec.Linux.Resources.BlockIO.ThrottleReadBpsDevice = append(rspec.Linux.Resources.BlockIO.ThrottleReadBpsDevice, &td) } else { return fmt.Errorf("throttlereadbpsdevice error: %s", blockIODevicestr) @@ -314,11 +319,11 @@ func addHugepageLimit(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, cont for _, hpls := range context.StringSlice("hugepagelimit") { hpl := strings.Split(hpls, ":") if len(hpl) == 2 { - limits, err := strconv.Atoi(hpl[1]) + limits, err := strconv.ParseUint(hpl[1], 10, 64) if err != nil { return err } - hp := specs.HugepageLimit{hpl[0], uint64(limits)} + hp := specs.HugepageLimit{&hpl[0], &limits} rspec.Linux.Resources.HugepageLimits = append(rspec.Linux.Resources.HugepageLimits, hp) } else { return fmt.Errorf("hugepagelimit error: %s", hpls) @@ -334,16 +339,18 @@ func setResourceCPU(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, contex } cpu := strings.Split(cpustr, ":") if len(cpu) == 7 { - shares, err := strconv.Atoi(cpu[0]) - quota, err := strconv.Atoi(cpu[1]) - period, err := strconv.Atoi(cpu[2]) - realtimeruntime, err := strconv.Atoi(cpu[3]) - realtimeperiod, err := strconv.Atoi(cpu[4]) + shares, err := strconv.ParseUint(cpu[0], 10, 64) + quota, err := strconv.ParseUint(cpu[1], 10, 64) + period, err := strconv.ParseUint(cpu[2], 10, 64) + realtimeruntime, err := strconv.ParseUint(cpu[3], 10, 64) + realtimeperiod, err := strconv.ParseUint(cpu[4], 10, 64) if err != nil { return err } - cpustruct := specs.CPU{uint64(shares), uint64(quota), uint64(period), uint64(realtimeruntime), uint64(realtimeperiod), cpu[5], cpu[6]} - rspec.Linux.Resources.CPU = cpustruct + cpus := cpu[5] + mems := cpu[6] + cpustruct := specs.CPU{&shares, "a, &period, &realtimeruntime, &realtimeperiod, &cpus, &mems} + rspec.Linux.Resources.CPU = &cpustruct } else { return fmt.Errorf("cpu error: %s", cpustr) } @@ -357,16 +364,17 @@ func setResourceMemory(spec *specs.LinuxSpec, rspec *specs.LinuxRuntimeSpec, con } mem := strings.Split(mems, ":") if len(mem) == 5 { - limit, err := strconv.Atoi(mem[0]) - reservation, err := strconv.Atoi(mem[1]) - swap, err := strconv.Atoi(mem[2]) - kernel, err := strconv.Atoi(mem[3]) - swapniess, err := strconv.Atoi(mem[4]) + limit, err := strconv.ParseUint(mem[0], 10, 64) + reservation, err := strconv.ParseUint(mem[1], 10, 64) + swap, err := strconv.ParseUint(mem[2], 10, 64) + kernel, err := strconv.ParseUint(mem[3], 10, 64) + kerneltcp, err := strconv.ParseUint(mem[4], 10, 64) + swapniess, err := strconv.ParseUint(mem[5], 10, 64) if err != nil { return err } - memorystruct := specs.Memory{uint64(limit), uint64(reservation), uint64(swap), uint64(kernel), uint64(swapniess)} - rspec.Linux.Resources.Memory = memorystruct + memorystruct := specs.Memory{&limit, &reservation, &swap, &kernel, &kerneltcp, &swapniess} + rspec.Linux.Resources.Memory = &memorystruct } else { return fmt.Errorf("memory error: %s", mems) } @@ -971,8 +979,8 @@ func getDefaultTemplate() (specs.LinuxSpec, specs.LinuxRuntimeSpec) { }, }, Resources: &specs.Resources{ - Memory: specs.Memory{ - Swappiness: 1, + Memory: &specs.Memory{ + Swappiness: nil, }, }, },