Skip to content

Commit

Permalink
validate: Code optimization
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Nov 1, 2017
1 parent afb01db commit 57601a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions specerror/config-linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
const (
// DefaultFilesystems represents "The following filesystems SHOULD be made available in each container's filesystem:"
DefaultFilesystems = "The following filesystems SHOULD be made available in each container's filesystem:"
// NSTypeValueError represents "The following namespace types are supported:"
NSTypeValueError = "The following namespace types are supported:"
// NSPathAbs represents "This value MUST be an absolute path in the runtime mount namespace."
NSPathAbs = "This value MUST be an absolute path in the runtime mount namespace."
// NSProcInPath represents "The runtime MUST place the container process in the namespace associated with that `path`."
Expand Down Expand Up @@ -105,6 +107,7 @@ var (

func init() {
register(DefaultFilesystems, rfc2119.Should, defaultFilesystemsRef)
register(NSTypeValueError, rfc2119.Should, namespacesRef)
register(NSPathAbs, rfc2119.Must, namespacesRef)
register(NSProcInPath, rfc2119.Must, namespacesRef)
register(NSPathMatchTypeError, rfc2119.Must, namespacesRef)
Expand Down
12 changes: 6 additions & 6 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ func (v *Validator) CheckLinux() (errs error) {

for index := 0; index < len(v.spec.Linux.Namespaces); index++ {
ns := v.spec.Linux.Namespaces[index]
if !v.namespaceValid(ns) {
errs = multierror.Append(errs, fmt.Errorf("namespace %v is invalid", ns))
if err := v.namespaceValid(ns); err != nil {
errs = multierror.Append(errs, err)
}

tmpItem := nsTypeList[ns.Type]
Expand Down Expand Up @@ -936,7 +936,7 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
return
}

func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) error {
switch ns.Type {
case rspec.PIDNamespace:
case rspec.NetworkNamespace:
Expand All @@ -946,14 +946,14 @@ func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
case rspec.UserNamespace:
case rspec.CgroupNamespace:
default:
return false
return specerror.NewError(specerror.NSTypeValueError, fmt.Errorf("namespace type %s may not be valid", ns.Type), rspec.Version)
}

if ns.Path != "" && !osFilepath.IsAbs(v.platform, ns.Path) {
return false
return specerror.NewError(specerror.NSPathAbs, fmt.Errorf("path %v of namespace %v is not absolute path", ns.Path, ns), rspec.Version)
}

return true
return nil
}

func deviceValid(d rspec.LinuxDevice) bool {
Expand Down

0 comments on commit 57601a4

Please sign in to comment.