Use the new runtime update command to process UpdateResources requests

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-05-09 10:25:52 -07:00
parent 2f69e11b1a
commit 1ebdf69a58

View file

@ -1,6 +1,7 @@
package runtime package runtime
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -260,23 +261,42 @@ func (c *container) RemoveProcess(pid string) error {
return os.RemoveAll(filepath.Join(c.root, c.id, pid)) return os.RemoveAll(filepath.Join(c.root, c.id, pid))
} }
func u64Ptr(i uint64) *uint64 { return &i }
func (c *container) UpdateResources(r *Resource) error { func (c *container) UpdateResources(r *Resource) error {
container, err := c.getLibctContainer() sr := ocs.Resources{
if err != nil { Memory: &ocs.Memory{
Limit: u64Ptr(uint64(r.Memory)),
Reservation: u64Ptr(uint64(r.MemoryReservation)),
Swap: u64Ptr(uint64(r.MemorySwap)),
Kernel: u64Ptr(uint64(r.KernelMemory)),
},
CPU: &ocs.CPU{
Shares: u64Ptr(uint64(r.CPUShares)),
Quota: u64Ptr(uint64(r.CPUQuota)),
Period: u64Ptr(uint64(r.CPUPeriod)),
Cpus: &r.CpusetCpus,
Mems: &r.CpusetMems,
},
BlockIO: &ocs.BlockIO{
Weight: &r.BlkioWeight,
},
}
srStr := bytes.NewBuffer(nil)
if err := json.NewEncoder(srStr).Encode(&sr); err != nil {
return err return err
} }
config := container.Config()
config.Cgroups.Resources.CpuShares = r.CPUShares args := c.runtimeArgs
config.Cgroups.Resources.BlkioWeight = r.BlkioWeight args = append(args, "update", "-r", "-", c.id)
config.Cgroups.Resources.CpuPeriod = r.CPUPeriod cmd := exec.Command(c.runtime, args...)
config.Cgroups.Resources.CpuQuota = r.CPUQuota cmd.Stdin = srStr
config.Cgroups.Resources.CpusetCpus = r.CpusetCpus b, err := cmd.CombinedOutput()
config.Cgroups.Resources.CpusetMems = r.CpusetMems if err != nil {
config.Cgroups.Resources.KernelMemory = r.KernelMemory return fmt.Errorf(string(b))
config.Cgroups.Resources.Memory = r.Memory }
config.Cgroups.Resources.MemoryReservation = r.MemoryReservation return nil
config.Cgroups.Resources.MemorySwap = r.MemorySwap
return container.Set(config)
} }
func getRootIDs(s *specs.Spec) (int, int, error) { func getRootIDs(s *specs.Spec) (int, int, error) {