Skip to content

Commit 57601a4

Browse files
author
zhouhao
committed
validate: Code optimization
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
1 parent afb01db commit 57601a4

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

specerror/config-linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
const (
1111
// DefaultFilesystems represents "The following filesystems SHOULD be made available in each container's filesystem:"
1212
DefaultFilesystems = "The following filesystems SHOULD be made available in each container's filesystem:"
13+
// NSTypeValueError represents "The following namespace types are supported:"
14+
NSTypeValueError = "The following namespace types are supported:"
1315
// NSPathAbs represents "This value MUST be an absolute path in the runtime mount namespace."
1416
NSPathAbs = "This value MUST be an absolute path in the runtime mount namespace."
1517
// NSProcInPath represents "The runtime MUST place the container process in the namespace associated with that `path`."
@@ -105,6 +107,7 @@ var (
105107

106108
func init() {
107109
register(DefaultFilesystems, rfc2119.Should, defaultFilesystemsRef)
110+
register(NSTypeValueError, rfc2119.Should, namespacesRef)
108111
register(NSPathAbs, rfc2119.Must, namespacesRef)
109112
register(NSProcInPath, rfc2119.Must, namespacesRef)
110113
register(NSPathMatchTypeError, rfc2119.Must, namespacesRef)

validate/validate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,8 @@ func (v *Validator) CheckLinux() (errs error) {
597597

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

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

939-
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
939+
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) error {
940940
switch ns.Type {
941941
case rspec.PIDNamespace:
942942
case rspec.NetworkNamespace:
@@ -946,14 +946,14 @@ func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
946946
case rspec.UserNamespace:
947947
case rspec.CgroupNamespace:
948948
default:
949-
return false
949+
return specerror.NewError(specerror.NSTypeValueError, fmt.Errorf("namespace type %s may not be valid", ns.Type), rspec.Version)
950950
}
951951

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

956-
return true
956+
return nil
957957
}
958958

959959
func deviceValid(d rspec.LinuxDevice) bool {

0 commit comments

Comments
 (0)