Skip to content

Commit bc68a99

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

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

specerror/config-linux.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,31 @@ 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+
NamespacesPathAbs = "This value MUST be an absolute path in the runtime mount namespace."
15+
MaskedPathsAbs = "Mask over the provided paths inside the container. The values MUST be absolute paths in the container namespace."
16+
ReadonlyPathsAbs = "Set the provided paths as readonly inside the container. The values MUST be absolute paths in the container namespace."
1317
)
1418

1519
var (
1620
defaultFilesystemsRef = func(version string) (reference string, err error) {
1721
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#default-filesystems"), nil
1822
}
23+
namespacesRef = func(version string) (reference string, err error) {
24+
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#namespaces"), nil
25+
}
26+
maskedPathsRef = func(version string) (reference string, err error) {
27+
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#masked-paths"), nil
28+
}
29+
readonlyPathsRef = func(version string) (reference string, err error) {
30+
return fmt.Sprintf(referenceTemplate, version, "config-linux.md#readonly-paths"), nil
31+
}
1932
)
2033

2134
func init() {
2235
register(DefaultFilesystems, rfc2119.Should, defaultFilesystemsRef)
36+
register(NamespacesType, rfc2119.Should, namespacesRef)
37+
register(NamespacesPathAbs, rfc2119.Must, namespacesRef)
38+
register(MaskedPathsAbs, rfc2119.Must, maskedPathsRef)
39+
register(ReadonlyPathsAbs, rfc2119.Must, readonlyPathsRef)
2340
}

validate/validate.go

Lines changed: 10 additions & 8 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]
@@ -701,13 +701,15 @@ func (v *Validator) CheckLinux() (errs error) {
701701

702702
for _, maskedPath := range v.spec.Linux.MaskedPaths {
703703
if !strings.HasPrefix(maskedPath, "/") {
704-
errs = multierror.Append(errs, fmt.Errorf("maskedPath %v is not an absolute path", maskedPath))
704+
errs = multierror.Append(errs,
705+
specerror.NewError(specerror.MaskedPathsAbs, fmt.Errorf("maskedPath %v is not an absolute path", maskedPath), rspec.Version))
705706
}
706707
}
707708

708709
for _, readonlyPath := range v.spec.Linux.ReadonlyPaths {
709710
if !strings.HasPrefix(readonlyPath, "/") {
710-
errs = multierror.Append(errs, fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath))
711+
errs = multierror.Append(errs,
712+
specerror.NewError(specerror.ReadonlyPathsAbs, fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath), rspec.Version))
711713
}
712714
}
713715

@@ -885,7 +887,7 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
885887
return
886888
}
887889

888-
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
890+
func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) error {
889891
switch ns.Type {
890892
case rspec.PIDNamespace:
891893
case rspec.NetworkNamespace:
@@ -895,14 +897,14 @@ func (v *Validator) namespaceValid(ns rspec.LinuxNamespace) bool {
895897
case rspec.UserNamespace:
896898
case rspec.CgroupNamespace:
897899
default:
898-
return false
900+
return specerror.NewError(specerror.NamespacesType, fmt.Errorf("namespace type %s may not be valid", ns.Type), rspec.Version)
899901
}
900902

901903
if ns.Path != "" && !osFilepath.IsAbs(v.platform, ns.Path) {
902-
return false
904+
return specerror.NewError(specerror.NamespacesPathAbs, fmt.Errorf("path %v of namespace %v is not absolute path", ns.Path, ns), rspec.Version)
903905
}
904906

905-
return true
907+
return nil
906908
}
907909

908910
func deviceValid(d rspec.LinuxDevice) bool {

0 commit comments

Comments
 (0)