From f4c35e70d1584c97d5d1eb506d078f53c8404a80 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 2 Jul 2015 09:55:24 -0700 Subject: [PATCH] Depend on Spec types from specs repository Signed-off-by: Michael Crosby --- restore.go | 3 +- run.go | 3 +- spec.go | 13 +++++---- spec_linux.go | 80 +++++++++++++++++++++++++++------------------------ utils.go | 3 +- 5 files changed, 55 insertions(+), 47 deletions(-) diff --git a/restore.go b/restore.go index 8170bfca..10ad7911 100644 --- a/restore.go +++ b/restore.go @@ -12,6 +12,7 @@ import ( "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/utils" + "github.com/opencontainers/specs" ) var restoreCommand = cli.Command{ @@ -46,7 +47,7 @@ var restoreCommand = cli.Command{ }, } -func restoreContainer(context *cli.Context, spec *Spec, config *configs.Config, imagePath string) (code int, err error) { +func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *configs.Config, imagePath string) (code int, err error) { rootuid := 0 factory, err := loadFactory(context) if err != nil { diff --git a/run.go b/run.go index 80d2b748..07012ee8 100644 --- a/run.go +++ b/run.go @@ -10,6 +10,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" "github.com/opencontainers/runc/libcontainer" + "github.com/opencontainers/specs" ) func init() { @@ -24,7 +25,7 @@ func init() { } } -func execContainer(context *cli.Context, spec *Spec) (int, error) { +func execContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { config, err := createLibcontainerConfig(spec) if err != nil { return -1, err diff --git a/spec.go b/spec.go index fb0bdb47..60ed0f77 100644 --- a/spec.go +++ b/spec.go @@ -7,25 +7,26 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" + "github.com/opencontainers/specs" ) var specCommand = cli.Command{ Name: "spec", Usage: "create a new specification file", Action: func(context *cli.Context) { - spec := PortableSpec{ + spec := specs.Spec{ Version: version, - Platform: Platform{ + Platform: specs.Platform{ OS: runtime.GOOS, Arch: runtime.GOARCH, }, - Root: Root{ + Root: specs.Root{ Path: "rootfs", Readonly: true, }, - Process: Process{ + Process: specs.Process{ Terminal: true, - User: User{}, + User: specs.User{}, Args: []string{ "sh", }, @@ -35,7 +36,7 @@ var specCommand = cli.Command{ }, }, Hostname: "shell", - Mounts: []Mount{ + Mounts: []specs.Mount{ { Type: "proc", Source: "proc", diff --git a/spec_linux.go b/spec_linux.go index ca467e79..ff3b9dd9 100644 --- a/spec_linux.go +++ b/spec_linux.go @@ -13,6 +13,7 @@ import ( "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/devices" + "github.com/opencontainers/specs" ) var namespaceMapping = map[string]configs.NamespaceType{ @@ -26,7 +27,7 @@ var namespaceMapping = map[string]configs.NamespaceType{ // loadSpec loads the specification from the provided path. // If the path is empty then the default path will be "config.json" -func loadSpec(path string) (*Spec, error) { +func loadSpec(path string) (*specs.LinuxSpec, error) { if path == "" { path = "config.json" } @@ -38,7 +39,7 @@ func loadSpec(path string) (*Spec, error) { return nil, err } defer f.Close() - var s *Spec + var s *specs.LinuxSpec if err := json.NewDecoder(f).Decode(&s); err != nil { return nil, err } @@ -47,14 +48,14 @@ func loadSpec(path string) (*Spec, error) { // checkSpecVersion makes sure that the spec version matches runc's while we are in the initial // development period. It is better to hard fail than have missing fields or options in the spec. -func checkSpecVersion(s *Spec) error { +func checkSpecVersion(s *specs.LinuxSpec) error { if s.Version != version { return fmt.Errorf("spec version is not compatible with runc version %q: spec %q", version, s.Version) } return nil } -func createLibcontainerConfig(spec *Spec) (*configs.Config, error) { +func createLibcontainerConfig(spec *specs.LinuxSpec) (*configs.Config, error) { cwd, err := os.Getwd() if err != nil { return nil, err @@ -103,7 +104,7 @@ func createLibcontainerConfig(spec *Spec) (*configs.Config, error) { return config, nil } -func createLibcontainerMount(cwd string, m Mount) *configs.Mount { +func createLibcontainerMount(cwd string, m specs.Mount) *configs.Mount { flags, data := parseMountOptions(m.Options) source := m.Source if m.Type == "bind" { @@ -120,7 +121,7 @@ func createLibcontainerMount(cwd string, m Mount) *configs.Mount { } } -func createCgroupConfig(spec *Spec, devices []*configs.Device) (*configs.Cgroup, error) { +func createCgroupConfig(spec *specs.LinuxSpec, devices []*configs.Device) (*configs.Cgroup, error) { myCgroupPath, err := cgroups.GetThisCgroupDir("devices") if err != nil { return nil, err @@ -133,41 +134,41 @@ func createCgroupConfig(spec *Spec, devices []*configs.Device) (*configs.Cgroup, MemorySwappiness: -1, } if r := spec.Linux.Resources; r != nil { - c.MemoryReservation = r.MemoryReservation - c.MemorySwap = r.MemorySwap - c.KernelMemory = r.KernelMemory - c.CpuShares = r.CpuShares - c.CpuQuota = r.CpuQuota - c.CpuPeriod = r.CpuPeriod - c.CpuRtRuntime = r.CpuRtRuntime - c.CpuRtPeriod = r.CpuRtPeriod - c.CpusetCpus = r.CpusetCpus - c.CpusetMems = r.CpusetMems - c.BlkioThrottleReadBpsDevice = r.BlkioThrottleReadBpsDevice - c.BlkioThrottleWriteBpsDevice = r.BlkioThrottleWriteBpsDevice - c.BlkioThrottleReadIOpsDevice = r.BlkioThrottleReadIOpsDevice - c.BlkioThrottleWriteIOpsDevice = r.BlkioThrottleWriteIOpsDevice - c.BlkioWeight = r.BlkioWeight - c.BlkioWeightDevice = r.BlkioWeightDevice - for _, l := range r.HugetlbLimit { + c.MemoryReservation = r.Memory.Reservation + c.MemorySwap = r.Memory.Swap + c.KernelMemory = r.Memory.Kernel + c.CpuShares = r.CPU.Shares + c.CpuQuota = r.CPU.Quota + c.CpuPeriod = r.CPU.Period + c.CpuRtRuntime = r.CPU.RealtimeRuntime + c.CpuRtPeriod = r.CPU.RealtimePeriod + c.CpusetCpus = r.CPU.Cpus + c.CpusetMems = r.CPU.Mems + c.BlkioThrottleReadBpsDevice = r.BlockIO.ThrottleReadBpsDevice + c.BlkioThrottleWriteBpsDevice = r.BlockIO.ThrottleWriteBpsDevice + c.BlkioThrottleReadIOpsDevice = r.BlockIO.ThrottleReadIOpsDevice + c.BlkioThrottleWriteIOpsDevice = r.BlockIO.ThrottleWriteIOpsDevice + c.BlkioWeight = r.BlockIO.Weight + c.BlkioWeightDevice = r.BlockIO.WeightDevice + for _, l := range r.HugepageLimits { c.HugetlbLimit = append(c.HugetlbLimit, &configs.HugepageLimit{ Pagesize: l.Pagesize, Limit: l.Limit, }) } c.OomKillDisable = r.DisableOOMKiller - for _, m := range r.NetPrioIfpriomap { + c.NetClsClassid = r.Network.ClassID + for _, m := range r.Network.Priorities { c.NetPrioIfpriomap = append(c.NetPrioIfpriomap, &configs.IfPrioMap{ - Interface: m.Interface, + Interface: m.Name, Priority: m.Priority, }) } - c.NetClsClassid = r.NetClsClassid } return c, nil } -func createDevices(spec *Spec, config *configs.Config) error { +func createDevices(spec *specs.LinuxSpec, config *configs.Config) error { for _, name := range spec.Linux.Devices { d, err := devices.DeviceFromPath(filepath.Join("/dev", name), "rwm") if err != nil { @@ -186,21 +187,24 @@ func setReadonly(config *configs.Config) { } } -func setupUserNamespace(spec *Spec, config *configs.Config) error { - if len(spec.Linux.UserMapping) == 0 { +func setupUserNamespace(spec *specs.LinuxSpec, config *configs.Config) error { + if len(spec.Linux.UidMapping) == 0 { return nil } config.Namespaces.Add(configs.NEWUSER, "") - mappings := make(map[string][]configs.IDMap) - for k, v := range spec.Linux.UserMapping { - mappings[k] = append(mappings[k], configs.IDMap{ - ContainerID: v.From, - HostID: v.To, - Size: v.Count, - }) + create := func(m specs.IDMapping) configs.IDMap { + return configs.IDMap{ + ContainerID: int(m.From), + HostID: int(m.To), + Size: int(m.Count), + } + } + for _, m := range spec.Linux.UidMapping { + config.UidMappings = append(config.UidMappings, create(m)) + } + for _, m := range spec.Linux.GidMapping { + config.GidMappings = append(config.GidMappings, create(m)) } - config.UidMappings = mappings["uid"] - config.GidMappings = mappings["gid"] rootUid, err := config.HostUID() if err != nil { return err diff --git a/utils.go b/utils.go index 8d9f5a67..cc4cc429 100644 --- a/utils.go +++ b/utils.go @@ -10,6 +10,7 @@ import ( "github.com/codegangsta/cli" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/configs" + "github.com/opencontainers/specs" ) const wildcard = -1 @@ -162,7 +163,7 @@ func getDefaultImagePath(context *cli.Context) string { // newProcess returns a new libcontainer Process with the arguments from the // spec and stdio from the current process. -func newProcess(p Process) *libcontainer.Process { +func newProcess(p specs.Process) *libcontainer.Process { return &libcontainer.Process{ Args: p.Args, Env: p.Env,