Skip to content

Commit 674d195

Browse files
author
zhouhao
committed
validate: Code optimization
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
1 parent 66b64a9 commit 674d195

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

specerror/config-linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ 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+
NamespacesType = "The following namespace types are supported:"
14+
NamespacesPath = "This value MUST be an absolute path."
1315
)
1416

1517
var (
1618
defaultFilesystemsRef = func(version string) (reference string, err error) {
1719
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#default-filesystems"), nil
1820
}
21+
namespacesRef = func(version string) (reference string, err error) {
22+
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#namespaces"), nil
23+
}
1924
)
2025

2126
func init() {
2227
register(DefaultFilesystems, rfc2119.Should, defaultFilesystemsRef)
28+
register(NamespacesType, rfc2119.Should, namespacesRef)
29+
register(NamespacesPath, rfc2119.Must, namespacesRef)
2330
}

validate/validate.go

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

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

567567
tmpItem := nsTypeList[ns.Type]
@@ -885,7 +885,7 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
885885
return
886886
}
887887

888-
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
888+
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) error {
889889
switch ns.Type {
890890
case rspec.PIDNamespace:
891891
case rspec.NetworkNamespace:
@@ -895,14 +895,14 @@ func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
895895
case rspec.UserNamespace:
896896
case rspec.CgroupNamespace:
897897
default:
898-
return false
898+
return specerror.NewError(specerror.NamespacesType, fmt.Errorf("namespace type %s may not be valid", ns.Type), v.spec.Version)
899899
}
900900

901901
if ns.Path != "" && !osFilepath.IsAbs(v.platform, ns.Path) {
902-
return false
902+
return specerror.NewError(specerror.NamespacesPath, fmt.Errorf("path %v of namespace %v is not absolute path", ns.Path, ns), v.spec.Version)
903903
}
904904

905-
return true
905+
return nil
906906
}
907907

908908
func deviceValid(d rspec.LinuxDevice) bool {

0 commit comments

Comments
 (0)