Skip to content

Commit

Permalink
move common codes to uitls
Browse files Browse the repository at this point in the history
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
  • Loading branch information
Ma Shimiao committed Aug 17, 2017
1 parent ef48dee commit c94e16e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 24 deletions.
11 changes: 5 additions & 6 deletions cmd/oci-runtime-tool/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/runtime-tools/generate/seccomp"
"github.com/opencontainers/runtime-tools/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -495,11 +496,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
}

if context.IsSet("linux-cpus") {
if err := uintListValid(context.String("linux-cpus")); err != nil {
if err := utils.UnitListValid(context.String("linux-cpus")); err != nil {
return err
} else {
g.SetLinuxResourcesCPUCpus(context.String("linux-cpus"))
}
g.SetLinuxResourcesCPUCpus(context.String("linux-cpus"))
}

if context.IsSet("linux-hugepage-limits-add") {
Expand All @@ -521,11 +521,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
}

if context.IsSet("linux-mems") {
if err := uintListValid(context.String("linux-mems")); err != nil {
if err := utils.UnitListValid(context.String("linux-mems")); err != nil {
return err
} else {
g.SetLinuxResourcesCPUMems(context.String("linux-mems"))
}
g.SetLinuxResourcesCPUMems(context.String("linux-mems"))
}

if context.IsSet("linux-mem-limit") {
Expand Down
7 changes: 2 additions & 5 deletions cmd/runtimetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
rfc2119 "github.com/opencontainers/runtime-tools/error"
"github.com/opencontainers/runtime-tools/utils"
"github.com/opencontainers/runtime-tools/validate"
)

Expand Down Expand Up @@ -162,11 +163,7 @@ func validateLinuxProcess(spec *rspec.Spec) error {
}

func validateCapabilities(spec *rspec.Spec) error {
last := capability.CAP_LAST_CAP
// workaround for RHEL6 which has no /proc/sys/kernel/cap_last_cap
if last == capability.Cap(63) {
last = capability.CAP_BLOCK_SUSPEND
}
last := utils.LastCap()

processCaps, err := capability.NewPid(0)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate/seccomp"
"github.com/opencontainers/runtime-tools/utils"
"github.com/opencontainers/runtime-tools/validate"
"github.com/syndtr/gocapability/capability"
)
Expand Down Expand Up @@ -949,7 +950,7 @@ func (g *Generator) SetupPrivileged(privileged bool) {
if privileged { // Add all capabilities in privileged mode.
var finalCapList []string
for _, cap := range capability.List() {
if g.HostSpecific && cap > validate.LastCap() {
if g.HostSpecific && cap > utils.LastCap() {
continue
}
finalCapList = append(finalCapList, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
Expand Down
59 changes: 59 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package utils

import (
"fmt"
"strconv"
"strings"

"github.com/syndtr/gocapability/capability"
)

// LastCap return last cap of system
func LastCap() capability.Cap {
last := capability.CAP_LAST_CAP
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
if last == capability.Cap(63) {
last = capability.CAP_BLOCK_SUSPEND
}

return last
}

// UnitListValid checks strings whether is valid for
// cpuset.cpus and cpuset.mems, duplicates are allowed
// Supported formats:
// 1
// 0-3
// 0-2,1,3
// 0-2,1-3,4
func UnitListValid(val string) error {
if val == "" {
return nil
}

split := strings.Split(val, ",")
errInvalidFormat := fmt.Errorf("invalid format: %s", val)

for _, r := range split {
if !strings.Contains(r, "-") {
_, err := strconv.Atoi(r)
if err != nil {
return errInvalidFormat
}
} else {
split := strings.SplitN(r, "-", 2)
min, err := strconv.Atoi(split[0])
if err != nil {
return errInvalidFormat
}
max, err := strconv.Atoi(split[1])
if err != nil {
return errInvalidFormat
}
if max < min {
return errInvalidFormat
}
}
}
return nil
}
29 changes: 17 additions & 12 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/blang/semver"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/utils"
"github.com/sirupsen/logrus"
"github.com/syndtr/gocapability/capability"
)
Expand Down Expand Up @@ -633,6 +634,20 @@ func (v *Validator) CheckLinuxResources() (msgs []string) {
logrus.Debugf("check linux resources")

r := v.spec.Linux.Resources

if r.CPU != nil {
if r.CPU.Cpus != "" {
if err := utils.UnitListValid(r.CPU.Cpus); err != nil {
msgs = append(msgs, err.Error())
}
}
if r.CPU.Mems != "" {
if err := utils.UnitListValid(r.CPU.Mems); err != nil {
msgs = append(msgs, err.Error())
}
}
}

if r.Memory != nil {
if r.Memory.Limit != nil && r.Memory.Swap != nil && uint64(*r.Memory.Limit) > uint64(*r.Memory.Swap) {
msgs = append(msgs, fmt.Sprintf("Minimum memoryswap should be larger than memory limit"))
Expand All @@ -641,6 +656,7 @@ func (v *Validator) CheckLinuxResources() (msgs []string) {
msgs = append(msgs, fmt.Sprintf("Minimum memory limit should be larger than memory reservation"))
}
}

if r.Network != nil && v.HostSpecific {
var exist bool
interfaces, err := net.Interfaces()
Expand Down Expand Up @@ -715,7 +731,7 @@ func CapValid(c string, hostSpecific bool) error {
}
for _, cap := range capability.List() {
if c == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) {
if hostSpecific && cap > LastCap() {
if hostSpecific && cap > utils.LastCap() {
return fmt.Errorf("CAP_%s is not supported on the current host", c)
}
isValid = true
Expand All @@ -729,17 +745,6 @@ func CapValid(c string, hostSpecific bool) error {
return nil
}

// LastCap return last cap of system
func LastCap() capability.Cap {
last := capability.CAP_LAST_CAP
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
if last == capability.Cap(63) {
last = capability.CAP_BLOCK_SUSPEND
}

return last
}

func envValid(env string) bool {
items := strings.Split(env, "=")
if len(items) < 2 {
Expand Down

0 comments on commit c94e16e

Please sign in to comment.