Skip to content

Commit 0fc41b9

Browse files
author
zhouhao
committed
validate: Code optimization
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
1 parent ccb1390 commit 0fc41b9

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ func (v *Validator) CheckLinux() (errs error) {
564564

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

571571
tmpItem := nsTypeList[ns.Type]
@@ -705,13 +705,15 @@ func (v *Validator) CheckLinux() (errs error) {
705705

706706
for _, maskedPath := range v.spec.Linux.MaskedPaths {
707707
if !strings.HasPrefix(maskedPath, "/") {
708-
errs = multierror.Append(errs, fmt.Errorf("maskedPath %v is not an absolute path", maskedPath))
708+
errs = multierror.Append(errs,
709+
specerror.NewError(specerror.MaskedPathsAbs, fmt.Errorf("maskedPath %v is not an absolute path", maskedPath), rspec.Version))
709710
}
710711
}
711712

712713
for _, readonlyPath := range v.spec.Linux.ReadonlyPaths {
713714
if !strings.HasPrefix(readonlyPath, "/") {
714-
errs = multierror.Append(errs, fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath))
715+
errs = multierror.Append(errs,
716+
specerror.NewError(specerror.ReadonlyPathsAbs, fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath), rspec.Version))
715717
}
716718
}
717719

@@ -889,7 +891,7 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
889891
return
890892
}
891893

892-
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
894+
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) error {
893895
switch ns.Type {
894896
case rspec.PIDNamespace:
895897
case rspec.NetworkNamespace:
@@ -899,14 +901,14 @@ func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
899901
case rspec.UserNamespace:
900902
case rspec.CgroupNamespace:
901903
default:
902-
return false
904+
return specerror.NewError(specerror.NSTypeValueError, fmt.Errorf("namespace type %s may not be valid", ns.Type), rspec.Version)
903905
}
904906

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

909-
return true
911+
return nil
910912
}
911913

912914
func deviceValid(d rspec.LinuxDevice) bool {

0 commit comments

Comments
 (0)