mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-27 05:11:59 +00:00
better logic mgmt in output plugins, go fmt
This commit is contained in:
parent
224e9ca17d
commit
8eb297d4c4
11 changed files with 93 additions and 79 deletions
1
main.go
1
main.go
|
|
@ -151,7 +151,6 @@ func realMain() int {
|
|||
res := validateFile(f, registries, k8sVersion, filter)
|
||||
f.Close()
|
||||
|
||||
|
||||
for _, resourceValidation := range res {
|
||||
o.Write(filename, resourceValidation.err, resourceValidation.skipped)
|
||||
}
|
||||
|
|
|
|||
4
pkg/cache/main.go
vendored
4
pkg/cache/main.go
vendored
|
|
@ -14,11 +14,11 @@ func init () {
|
|||
schemas = map[string]*gojsonschema.Schema{}
|
||||
}
|
||||
|
||||
func WithCache(downloadSchema func(string, string, string) (*gojsonschema.Schema, error)) (func (string, string, string) (*gojsonschema.Schema, error)) {
|
||||
func WithCache(downloadSchema func(string, string, string) (*gojsonschema.Schema, error)) func(string, string, string) (*gojsonschema.Schema, error) {
|
||||
return func(resourceKind, resourceAPIVersion, k8sVersion string) (*gojsonschema.Schema, error) {
|
||||
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
||||
mu.Lock()
|
||||
cachedSchema, ok := schemas[cacheKey];
|
||||
cachedSchema, ok := schemas[cacheKey]
|
||||
mu.Unlock()
|
||||
if ok {
|
||||
return cachedSchema, nil
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package output
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
|
|
@ -25,23 +24,23 @@ func NewJSONOutput(withSummary bool) Output{
|
|||
}
|
||||
|
||||
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
|
||||
status := "VALID"
|
||||
msg := ""
|
||||
msg, st := "", ""
|
||||
|
||||
if err != nil {
|
||||
s := status(err, skipped)
|
||||
switch {
|
||||
case s == VALID:
|
||||
st = "VALID"
|
||||
case s == INVALID:
|
||||
st = "INVALID"
|
||||
msg = err.Error()
|
||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||
status = "INVALID"
|
||||
} else {
|
||||
status = "ERROR"
|
||||
}
|
||||
case s == ERROR:
|
||||
st = "ERROR"
|
||||
msg = err.Error()
|
||||
case s == SKIPPED:
|
||||
st = "SKIPPED"
|
||||
}
|
||||
|
||||
if skipped {
|
||||
status = "SKIPPED"
|
||||
}
|
||||
|
||||
o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg})
|
||||
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
|
||||
}
|
||||
|
||||
func (o *JSONOutput) Flush() {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,31 @@
|
|||
package output
|
||||
|
||||
import "github.com/yannh/kubeconform/pkg/validator"
|
||||
|
||||
const (
|
||||
VALID = iota
|
||||
INVALID = iota
|
||||
ERROR = iota
|
||||
SKIPPED = iota
|
||||
)
|
||||
|
||||
type Output interface {
|
||||
Write(filename string, err error, skipped bool)
|
||||
Flush()
|
||||
}
|
||||
|
||||
func status(err error, skipped bool) int {
|
||||
if err != nil {
|
||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||
return INVALID
|
||||
} else {
|
||||
return ERROR
|
||||
}
|
||||
}
|
||||
|
||||
if skipped {
|
||||
return SKIPPED
|
||||
}
|
||||
|
||||
return VALID
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package output
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
)
|
||||
|
||||
type TextOutput struct {
|
||||
|
|
@ -15,26 +14,20 @@ func NewTextOutput(withSummary bool) Output {
|
|||
}
|
||||
|
||||
func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
||||
if skipped {
|
||||
fmt.Printf("skipping resource\n")
|
||||
o.nSkipped++
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||
fmt.Printf("invalid resource: %s\n", err)
|
||||
o.nInvalid++
|
||||
} else {
|
||||
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||
o.nErrors++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !skipped{
|
||||
s := status(err, skipped)
|
||||
switch {
|
||||
case s == VALID:
|
||||
fmt.Printf("file %s is valid\n", filename)
|
||||
o.nValid++
|
||||
case s == INVALID:
|
||||
fmt.Printf("invalid resource: %s\n", err)
|
||||
o.nInvalid++
|
||||
case s == ERROR:
|
||||
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||
o.nErrors++
|
||||
case s == SKIPPED:
|
||||
fmt.Printf("skipping resource\n")
|
||||
o.nSkipped++
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,4 +36,3 @@ func (o *TextOutput) Flush() {
|
|||
fmt.Printf("Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ type downloadError struct {
|
|||
err error
|
||||
isRetryable bool
|
||||
}
|
||||
|
||||
func newDownloadError(err error, isRetryable bool) *downloadError {
|
||||
return &downloadError{err, isRetryable}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ package registry
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
"sigs.k8s.io/yaml"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sigs.k8s.io/yaml"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -13,7 +13,6 @@ type LocalSchemas struct {
|
|||
schemas map[string]*gojsonschema.Schema
|
||||
}
|
||||
|
||||
|
||||
func NewLocalSchemas(schemaFiles []string) (*LocalSchemas, error) {
|
||||
schemas := &LocalSchemas{
|
||||
schemas: map[string]*gojsonschema.Schema{},
|
||||
|
|
|
|||
|
|
@ -21,4 +21,3 @@ func SignatureFromBytes(s []byte) (Signature, error) {
|
|||
|
||||
return Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
)
|
||||
|
||||
type InvalidResourceError struct{ err string }
|
||||
|
||||
func (r InvalidResourceError) Error() string {
|
||||
return r.err
|
||||
}
|
||||
|
|
@ -14,6 +15,7 @@ func (r InvalidResourceError) Error() string{
|
|||
// ValidFormat is a type for quickly forcing
|
||||
// new formats on the gojsonschema loader
|
||||
type ValidFormat struct{}
|
||||
|
||||
func (f ValidFormat) IsFormat(input interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
|
@ -49,4 +51,3 @@ func Validate(rawResource []byte, schema *gojsonschema.Schema) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue