mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-24 04:07:02 +00:00
add quiet mode, fix race condition in statistics
This commit is contained in:
parent
b918da9c59
commit
ae5fbffb95
4 changed files with 60 additions and 25 deletions
7
main.go
7
main.go
|
|
@ -65,6 +65,7 @@ func validateFile(f io.Reader, regs []registry.Registry, k8sVersion string, c *c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache both found & not found
|
||||||
c.Set(cacheKey, schema)
|
c.Set(cacheKey, schema)
|
||||||
|
|
||||||
if err != nil { // Not found
|
if err != nil { // Not found
|
||||||
|
|
@ -94,6 +95,7 @@ func realMain() int {
|
||||||
var skipKinds, k8sVersion, outputFormat string
|
var skipKinds, k8sVersion, outputFormat string
|
||||||
var printSummary, strict bool
|
var printSummary, strict bool
|
||||||
var nWorkers int
|
var nWorkers int
|
||||||
|
var quiet bool
|
||||||
|
|
||||||
flag.BoolVar(&printSummary, "printsummary", false, "print a summary at the end")
|
flag.BoolVar(&printSummary, "printsummary", false, "print a summary at the end")
|
||||||
flag.Var(&files, "file", "file to validate (can be specified multiple times)")
|
flag.Var(&files, "file", "file to validate (can be specified multiple times)")
|
||||||
|
|
@ -104,14 +106,15 @@ func realMain() int {
|
||||||
flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore")
|
flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore")
|
||||||
flag.BoolVar(&strict, "strict", false, "activate strict mode")
|
flag.BoolVar(&strict, "strict", false, "activate strict mode")
|
||||||
flag.StringVar(&outputFormat, "output", "text", "output format - text, json")
|
flag.StringVar(&outputFormat, "output", "text", "output format - text, json")
|
||||||
|
flag.BoolVar(&quiet, "quiet", false, "quiet output - only print invalid files, and errors")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var o output.Output
|
var o output.Output
|
||||||
switch {
|
switch {
|
||||||
case outputFormat == "text":
|
case outputFormat == "text":
|
||||||
o = output.NewTextOutput(printSummary)
|
o = output.NewTextOutput(printSummary, quiet)
|
||||||
case outputFormat == "json":
|
case outputFormat == "json":
|
||||||
o = output.NewJSONOutput(printSummary)
|
o = output.NewJSONOutput(printSummary, quiet)
|
||||||
default:
|
default:
|
||||||
log.Fatalf("-output must be text or json")
|
log.Fatalf("-output must be text or json")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
pkg/cache/main.go
vendored
2
pkg/cache/main.go
vendored
|
|
@ -24,7 +24,7 @@ func Key(resourceKind, resourceAPIVersion, k8sVersion string) string {
|
||||||
func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
|
func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
schema, ok := c.schemas[key]
|
schema, ok := c.schemas[key]
|
||||||
return schema, ok
|
return schema, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package output
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
|
|
@ -12,35 +13,51 @@ type result struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONOutput struct {
|
type JSONOutput struct {
|
||||||
withSummary bool
|
sync.Mutex
|
||||||
results []result
|
withSummary bool
|
||||||
|
quiet bool
|
||||||
|
results []result
|
||||||
|
nValid, nInvalid, nErrors, nSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJSONOutput(withSummary bool) Output {
|
func NewJSONOutput(withSummary bool, quiet bool) Output {
|
||||||
return &JSONOutput{
|
return &JSONOutput{
|
||||||
withSummary: withSummary,
|
withSummary: withSummary,
|
||||||
|
quiet: quiet,
|
||||||
results: []result{},
|
results: []result{},
|
||||||
|
nValid: 0,
|
||||||
|
nInvalid: 0,
|
||||||
|
nErrors: 0,
|
||||||
|
nSkipped: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
|
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
|
||||||
|
o.Lock()
|
||||||
|
defer o.Unlock()
|
||||||
msg, st := "", ""
|
msg, st := "", ""
|
||||||
|
|
||||||
s := status(err, skipped)
|
s := status(err, skipped)
|
||||||
switch {
|
switch {
|
||||||
case s == VALID:
|
case s == VALID:
|
||||||
st = "VALID"
|
st = "VALID"
|
||||||
|
o.nValid++
|
||||||
case s == INVALID:
|
case s == INVALID:
|
||||||
st = "INVALID"
|
st = "INVALID"
|
||||||
msg = err.Error()
|
msg = err.Error()
|
||||||
|
o.nInvalid++
|
||||||
case s == ERROR:
|
case s == ERROR:
|
||||||
st = "ERROR"
|
st = "ERROR"
|
||||||
msg = err.Error()
|
msg = err.Error()
|
||||||
|
o.nErrors++
|
||||||
case s == SKIPPED:
|
case s == SKIPPED:
|
||||||
st = "SKIPPED"
|
st = "SKIPPED"
|
||||||
|
o.nSkipped++
|
||||||
}
|
}
|
||||||
|
|
||||||
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
|
if !o.quiet || (s != VALID && s != SKIPPED) {
|
||||||
|
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *JSONOutput) Flush() {
|
func (o *JSONOutput) Flush() {
|
||||||
|
|
@ -58,25 +75,23 @@ func (o *JSONOutput) Flush() {
|
||||||
} `json:"summary"`
|
} `json:"summary"`
|
||||||
}{
|
}{
|
||||||
Resources: o.results,
|
Resources: o.results,
|
||||||
}
|
Summary: struct {
|
||||||
|
Valid int `json:"valid"`
|
||||||
for _, r := range o.results {
|
Invalid int `json:"invalid"`
|
||||||
switch {
|
Errors int `json:"errors"`
|
||||||
case r.Status == "VALID":
|
Skipped int `json:"skipped"`
|
||||||
jsonObj.Summary.Valid++
|
}{
|
||||||
case r.Status == "INVALID":
|
Valid: o.nValid,
|
||||||
jsonObj.Summary.Invalid++
|
Invalid: o.nInvalid,
|
||||||
case r.Status == "ERROR":
|
Errors: o.nErrors,
|
||||||
jsonObj.Summary.Errors++
|
Skipped: o.nSkipped,
|
||||||
case r.Status == "SKIPPED":
|
},
|
||||||
jsonObj.Summary.Skipped++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err = json.MarshalIndent(jsonObj, "", " ")
|
res, err = json.MarshalIndent(jsonObj, "", " ")
|
||||||
} else {
|
} else {
|
||||||
jsonObj := struct {
|
jsonObj := struct {
|
||||||
Resources []result
|
Resources []result `json:"resources"`
|
||||||
}{
|
}{
|
||||||
Resources: o.results,
|
Resources: o.results,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,37 @@ package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextOutput struct {
|
type TextOutput struct {
|
||||||
|
sync.Mutex
|
||||||
withSummary bool
|
withSummary bool
|
||||||
|
quiet bool
|
||||||
nValid, nInvalid, nErrors, nSkipped int
|
nValid, nInvalid, nErrors, nSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTextOutput(withSummary bool) Output {
|
func NewTextOutput(withSummary, quiet bool) Output {
|
||||||
return &TextOutput{withSummary, 0, 0, 0, 0}
|
return &TextOutput{
|
||||||
|
withSummary: withSummary,
|
||||||
|
quiet: quiet,
|
||||||
|
nValid: 0,
|
||||||
|
nInvalid: 0,
|
||||||
|
nErrors: 0,
|
||||||
|
nSkipped: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
||||||
|
o.Lock()
|
||||||
|
defer o.Unlock()
|
||||||
|
|
||||||
s := status(err, skipped)
|
s := status(err, skipped)
|
||||||
switch {
|
switch {
|
||||||
case s == VALID:
|
case s == VALID:
|
||||||
fmt.Printf("file %s is valid\n", filename)
|
if !o.quiet {
|
||||||
|
fmt.Printf("file %s is valid\n", filename)
|
||||||
|
}
|
||||||
o.nValid++
|
o.nValid++
|
||||||
case s == INVALID:
|
case s == INVALID:
|
||||||
fmt.Printf("invalid resource: %s\n", err)
|
fmt.Printf("invalid resource: %s\n", err)
|
||||||
|
|
@ -26,7 +41,9 @@ func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
||||||
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||||
o.nErrors++
|
o.nErrors++
|
||||||
case s == SKIPPED:
|
case s == SKIPPED:
|
||||||
fmt.Printf("skipping resource\n")
|
if !o.quiet {
|
||||||
|
fmt.Printf("skipping resource\n")
|
||||||
|
}
|
||||||
o.nSkipped++
|
o.nSkipped++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue