Skip to content

Commit 929bd5e

Browse files
author
Zhou Hao
authored
Merge pull request #492 from wking/validate-compliance-level
cmd/oci-runtime-tool: Implement --compliance-level
2 parents c1aa987 + 6089f63 commit 929bd5e

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

cmd/oci-runtime-tool/main.go

+5
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

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package main
33
import (
44
"fmt"
55

6+
rfc2119 "github.com/opencontainers/runtime-tools/error"
7+
"github.com/opencontainers/runtime-tools/specerror"
68
"github.com/opencontainers/runtime-tools/validate"
9+
"github.com/sirupsen/logrus"
710
"github.com/urfave/cli"
811
)
912

@@ -19,6 +22,12 @@ var bundleValidateCommand = cli.Command{
1922
Before: before,
2023
Action: func(context *cli.Context) error {
2124
hostSpecific := context.GlobalBool("host-specific")
25+
complianceLevelString := context.GlobalString("compliance-level")
26+
complianceLevel, err := rfc2119.ParseLevel(complianceLevelString)
27+
if err != nil {
28+
complianceLevel = rfc2119.Must
29+
logrus.Warningf("%s, using 'MUST' by default.", err.Error())
30+
}
2231
inputPath := context.String("path")
2332
platform := context.String("platform")
2433
v, err := validate.NewValidatorFromPath(inputPath, hostSpecific, platform)
@@ -27,8 +36,14 @@ var bundleValidateCommand = cli.Command{
2736
}
2837

2938
if err := v.CheckAll(); err != nil {
30-
return err
31-
39+
levelErrors, err := specerror.SplitLevel(err, complianceLevel)
40+
if err != nil {
41+
return err
42+
}
43+
for _, e := range levelErrors.Warnings {
44+
logrus.Warn(e)
45+
}
46+
return levelErrors.Error
3247
}
3348
fmt.Println("Bundle validation succeeded.")
3449
return nil

man/oci-runtime-tool.1.md

+2-1
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.

specerror/error.go

+30
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ type Error struct {
6969
Code Code
7070
}
7171

72+
// LevelErrors represents Errors filtered into fatal and warnings.
73+
type LevelErrors struct {
74+
// Warnings holds Errors that were below a compliance-level threshold.
75+
Warnings []*Error
76+
77+
// Error holds errors that were at or above a compliance-level
78+
// threshold, as well as errors that are not Errors.
79+
Error *multierror.Error
80+
}
81+
7282
var (
7383
containerFormatRef = func(version string) (reference string, err error) {
7484
return fmt.Sprintf(referenceTemplate, version, "bundle.md#container-format"), nil
@@ -168,3 +178,23 @@ func FindError(err error, code Code) Code {
168178
}
169179
return NonRFCError
170180
}
181+
182+
// SplitLevel removes RFC 2119 errors with a level less than 'level'
183+
// from the source error. If the source error is not a multierror, it
184+
// is returned unchanged.
185+
func SplitLevel(errIn error, level rfc2119.Level) (levelErrors LevelErrors, errOut error) {
186+
merr, ok := errIn.(*multierror.Error)
187+
if !ok {
188+
return levelErrors, errIn
189+
}
190+
for _, err := range merr.Errors {
191+
e, ok := err.(*Error)
192+
if ok && e.Err.Level < level {
193+
fmt.Println(e)
194+
levelErrors.Warnings = append(levelErrors.Warnings, e)
195+
continue
196+
}
197+
levelErrors.Error = multierror.Append(levelErrors.Error, err)
198+
}
199+
return levelErrors, nil
200+
}

0 commit comments

Comments
 (0)