From a6345a33d00bafd5c12623056ac3d0d5af7fd441 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 27 Sep 2017 14:01:20 +0800 Subject: [PATCH] validate: Code optimization Signed-off-by: zhouhao --- specerror/config-linux.go | 4 ++++ validate/validate.go | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/specerror/config-linux.go b/specerror/config-linux.go index aa2a284ce..b6f679c92 100644 --- a/specerror/config-linux.go +++ b/specerror/config-linux.go @@ -10,6 +10,9 @@ import ( const ( // DefaultFilesystems represents "The following filesystems SHOULD be made available in each container's filesystem:" DefaultFilesystems Code = 0xc001 + iota + + // NSTypeValueError represents "The following namespace types are supported:" + NSTypeValueError // NSPathAbs represents "This value MUST be an absolute path in the runtime mount namespace." NSPathAbs // NSProcInPath represents "The runtime MUST place the container process in the namespace associated with that `path`." @@ -105,6 +108,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) diff --git a/validate/validate.go b/validate/validate.go index 0914bc691..c62e41303 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -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] @@ -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: @@ -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 {