Skip to content

Commit a9dbd7e

Browse files
committed
cmd/oci-runtime-tool: Implement --compliance-level
The man-page entry and Bash completions for this option were added in 4029999 (oci error: add error level and reference, 2017-03-31, #354), but the option was left unimplemented. The implementation in this commit is very similar to the existing runtimetest implementation from 4029999, except we warn instead of silently skipping non-fatal spec violations. The warning (vs. error) on invalid level arguments follows the runtimetest implementation from 6316a4e (use released version as reference; improve Parse error, 2017-04-12, #354). I'd personally prefer hard errors on invalid level arguments, but didn't want to break runtime-tools consistency over that. Signed-off-by: W. Trevor King <[email protected]>
1 parent 2b007bc commit a9dbd7e

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

cmd/oci-runtime-tool/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func main() {
2222
}
2323
app.Usage = "OCI (Open Container Initiative) runtime tools"
2424
app.Flags = []cli.Flag{
25+
cli.StringFlag{
26+
Name: "compliance-level",
27+
Value: "must",
28+
Usage: "compliance level (may, should, or must).",
29+
},
2530
cli.BoolFlag{
2631
Name: "host-specific",
2732
Usage: "generate host-specific configs or do host-specific validations",

cmd/oci-runtime-tool/validate.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/hashicorp/go-multierror"
7+
rfc2119 "github.com/opencontainers/runtime-tools/error"
8+
"github.com/opencontainers/runtime-tools/specerror"
69
"github.com/opencontainers/runtime-tools/validate"
10+
"github.com/sirupsen/logrus"
711
"github.com/urfave/cli"
812
)
913

@@ -19,6 +23,12 @@ var bundleValidateCommand = cli.Command{
1923
Before: before,
2024
Action: func(context *cli.Context) error {
2125
hostSpecific := context.GlobalBool("host-specific")
26+
complianceLevelString := context.GlobalString("compliance-level")
27+
complianceLevel, err := rfc2119.ParseLevel(complianceLevelString)
28+
if err != nil {
29+
complianceLevel = rfc2119.Must
30+
logrus.Warningf("%s, using 'MUST' by default.", err.Error())
31+
}
2232
inputPath := context.String("path")
2333
platform := context.String("platform")
2434
v, err := validate.NewValidatorFromPath(inputPath, hostSpecific, platform)
@@ -27,8 +37,20 @@ var bundleValidateCommand = cli.Command{
2737
}
2838

2939
if err := v.CheckAll(); err != nil {
30-
return err
31-
40+
merr, ok := err.(*multierror.Error)
41+
if !ok {
42+
return err
43+
}
44+
var validationErrors error
45+
for _, err = range merr.Errors {
46+
e, ok := err.(*specerror.Error)
47+
if ok && e.Err.Level < complianceLevel {
48+
logrus.Warn(e)
49+
continue
50+
}
51+
validationErrors = multierror.Append(validationErrors, err)
52+
}
53+
return validationErrors
3254
}
3355
fmt.Println("Bundle validation succeeded.")
3456
return nil

man/oci-runtime-tool.1.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ oci-runtime-tool is a collection of tools for working with the [OCI runtime spec
3333
Log level (panic, fatal, error, warn, info, or debug) (default: "error").
3434

3535
**--compliance-level**=LEVEL
36-
Compliance level (may, should or must) (default: "must").
36+
Compliance level (`may`, `should`, or `must`) (default: `must`).
37+
For example, a SHOULD-level violation is fatal if `--compliance-level` is `may` or `should` but non-fatal if `--compliance-level` is `must`.
3738

3839
**-v**, **--version**
3940
Print version information.

0 commit comments

Comments
 (0)