Skip to content

Commit 4029999

Browse files
committed
oci error: add error level and reference
Signed-off-by: liangchenye <[email protected]>
1 parent ca03d44 commit 4029999

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

cmd/runtimetest/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"github.com/hashicorp/go-multierror"
1919
"github.com/mndrix/tap-go"
2020
rspec "github.com/opencontainers/runtime-spec/specs-go"
21-
"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
2221
"github.com/syndtr/gocapability/capability"
2322
"github.com/urfave/cli"
23+
24+
"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
25+
ociErr "github.com/opencontainers/runtime-tools/validate"
2426
)
2527

2628
// PrGetNoNewPrivs isn't exposed in Golang so we define it ourselves copying the value from
@@ -660,11 +662,16 @@ func validate(context *cli.Context) error {
660662
t := tap.New()
661663
t.Header(0)
662664

665+
complianceLevelString := context.String("compliance-level")
666+
complianceLevel := ociErr.ParseLevel(complianceLevelString)
663667
var validationErrors error
664668
for _, v := range defaultValidations {
665669
err := v.test(spec)
666670
t.Ok(err == nil, v.description)
667671
if err != nil {
672+
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
673+
continue
674+
}
668675
validationErrors = multierror.Append(validationErrors, err)
669676
}
670677
}
@@ -674,6 +681,9 @@ func validate(context *cli.Context) error {
674681
err := v.test(spec)
675682
t.Ok(err == nil, v.description)
676683
if err != nil {
684+
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
685+
continue
686+
}
677687
validationErrors = multierror.Append(validationErrors, err)
678688
}
679689
}
@@ -700,6 +710,11 @@ func main() {
700710
Value: ".",
701711
Usage: "Path to the configuration",
702712
},
713+
cli.StringFlag{
714+
Name: "compliance-level",
715+
Value: "must",
716+
Usage: "Compliance level (must or should)",
717+
},
703718
}
704719

705720
app.Action = validate

completions/bash/oci-runtime-tool

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ __oci-runtime-tool_complete_log_level() {
119119
" -- "$cur" ) )
120120
}
121121

122+
__oci-runtime-tool_complete_compliance_level() {
123+
COMPREPLY=( $( compgen -W "
124+
must
125+
should
126+
" -- "$cur" ) )
127+
}
128+
122129
__oci-runtime-tool_complete_propagations() {
123130
COMPREPLY=( $( compgen -W "
124131
private
@@ -218,6 +225,10 @@ _oci-runtime-tool_oci-runtime-tool() {
218225
--log-level
219226
"
220227

228+
local options_with_args="
229+
--compliance-level
230+
"
231+
221232
local boolean_options="
222233
--help -h
223234
--host-specific
@@ -231,6 +242,10 @@ _oci-runtime-tool_oci-runtime-tool() {
231242
__oci-runtime-tool_complete_log_level
232243
return
233244
;;
245+
--compliance-level)
246+
__oci-runtime-tool_complete_compliance_level
247+
return
248+
;;
234249
esac
235250

236251
case "$cur" in

man/oci-runtime-tool.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ oci-runtime-tool is a collection of tools for working with the [OCI runtime spec
3232
**--log-level**=LEVEL
3333
Log level (panic, fatal, error, warn, info, or debug) (default: "error").
3434

35+
**--compliance-level**=LEVEL
36+
Compliance level (must or should) (default: "must").
37+
3538
**-v**, **--version**
3639
Print version information.
3740

validate/error.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package validate
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
// ComplianceLevel represents the OCI compliance levels
10+
type ComplianceLevel int
11+
12+
const (
13+
ComplianceOptional ComplianceLevel = iota
14+
ComplianceMay
15+
ComplianceRecommended
16+
ComplianceShould
17+
ComplianceShouldNot
18+
ComplianceShall
19+
ComplianceShallNot
20+
ComplianceRequired
21+
ComplianceMustNot
22+
ComplianceMust
23+
)
24+
25+
// OCIErrorCode represents the compliance content
26+
type OCIErrorCode int
27+
28+
const (
29+
DefaultFilesystems OCIErrorCode = iota
30+
)
31+
32+
// OCIError represents an error with compliance level and OCI reference
33+
type OCIError struct {
34+
Level ComplianceLevel
35+
Reference string
36+
Err error
37+
}
38+
39+
//FIXME: change to tagged spec releases
40+
const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/"
41+
42+
var ociErrors = map[OCIErrorCode]OCIError{
43+
DefaultFilesystems: OCIError{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"},
44+
}
45+
46+
// ParseLevel takes a string level and returns the OCI compliance level constant
47+
func ParseLevel(level string) ComplianceLevel {
48+
switch strings.ToUpper(level) {
49+
case "SHOULD":
50+
return ComplianceShould
51+
case "MUST":
52+
return ComplianceMust
53+
default:
54+
return ComplianceMust
55+
}
56+
}
57+
58+
// NewOCIError creates an OCIError by OCIErrorCode and message
59+
func NewOCIError(code OCIErrorCode, msg string) error {
60+
err := ociErrors[code]
61+
err.Err = errors.New(msg)
62+
63+
return &err
64+
}
65+
66+
// Error returns the error message with OCI reference
67+
func (oci *OCIError) Error() string {
68+
return fmt.Sprintf("%s\nRefer to: %s%s", oci.Err.Error(), referencePrefix, oci.Reference)
69+
}

0 commit comments

Comments
 (0)