mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-22 19:27:01 +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)
|
res := validateFile(f, registries, k8sVersion, filter)
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
|
|
||||||
for _, resourceValidation := range res {
|
for _, resourceValidation := range res {
|
||||||
o.Write(filename, resourceValidation.err, resourceValidation.skipped)
|
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{}
|
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) {
|
return func(resourceKind, resourceAPIVersion, k8sVersion string) (*gojsonschema.Schema, error) {
|
||||||
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
cachedSchema, ok := schemas[cacheKey];
|
cachedSchema, ok := schemas[cacheKey]
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
if ok {
|
if ok {
|
||||||
return cachedSchema, nil
|
return cachedSchema, nil
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package output
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/yannh/kubeconform/pkg/validator"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
|
|
@ -25,23 +24,23 @@ func NewJSONOutput(withSummary bool) Output{
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
|
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
|
||||||
status := "VALID"
|
msg, st := "", ""
|
||||||
msg := ""
|
|
||||||
|
|
||||||
if err != nil {
|
s := status(err, skipped)
|
||||||
|
switch {
|
||||||
|
case s == VALID:
|
||||||
|
st = "VALID"
|
||||||
|
case s == INVALID:
|
||||||
|
st = "INVALID"
|
||||||
msg = err.Error()
|
msg = err.Error()
|
||||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
case s == ERROR:
|
||||||
status = "INVALID"
|
st = "ERROR"
|
||||||
} else {
|
msg = err.Error()
|
||||||
status = "ERROR"
|
case s == SKIPPED:
|
||||||
}
|
st = "SKIPPED"
|
||||||
}
|
}
|
||||||
|
|
||||||
if skipped {
|
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
|
||||||
status = "SKIPPED"
|
|
||||||
}
|
|
||||||
|
|
||||||
o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *JSONOutput) Flush() {
|
func (o *JSONOutput) Flush() {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,31 @@
|
||||||
package output
|
package output
|
||||||
|
|
||||||
|
import "github.com/yannh/kubeconform/pkg/validator"
|
||||||
|
|
||||||
|
const (
|
||||||
|
VALID = iota
|
||||||
|
INVALID = iota
|
||||||
|
ERROR = iota
|
||||||
|
SKIPPED = iota
|
||||||
|
)
|
||||||
|
|
||||||
type Output interface {
|
type Output interface {
|
||||||
Write(filename string, err error, skipped bool)
|
Write(filename string, err error, skipped bool)
|
||||||
Flush()
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/yannh/kubeconform/pkg/validator"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextOutput struct {
|
type TextOutput struct {
|
||||||
|
|
@ -15,26 +14,20 @@ func NewTextOutput(withSummary bool) Output {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
func (o *TextOutput) Write(filename string, err error, skipped bool) {
|
||||||
if skipped {
|
s := status(err, skipped)
|
||||||
fmt.Printf("skipping resource\n")
|
switch {
|
||||||
o.nSkipped++
|
case s == VALID:
|
||||||
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{
|
|
||||||
fmt.Printf("file %s is valid\n", filename)
|
fmt.Printf("file %s is valid\n", filename)
|
||||||
o.nValid++
|
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)
|
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
|
err error
|
||||||
isRetryable bool
|
isRetryable bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDownloadError(err error, isRetryable bool) *downloadError {
|
func newDownloadError(err error, isRetryable bool) *downloadError {
|
||||||
return &downloadError{err, isRetryable}
|
return &downloadError{err, isRetryable}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ package registry
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
"sigs.k8s.io/yaml"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,7 +13,6 @@ type LocalSchemas struct {
|
||||||
schemas map[string]*gojsonschema.Schema
|
schemas map[string]*gojsonschema.Schema
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func NewLocalSchemas(schemaFiles []string) (*LocalSchemas, error) {
|
func NewLocalSchemas(schemaFiles []string) (*LocalSchemas, error) {
|
||||||
schemas := &LocalSchemas{
|
schemas := &LocalSchemas{
|
||||||
schemas: map[string]*gojsonschema.Schema{},
|
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
|
return Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type InvalidResourceError struct{ err string }
|
type InvalidResourceError struct{ err string }
|
||||||
|
|
||||||
func (r InvalidResourceError) Error() string {
|
func (r InvalidResourceError) Error() string {
|
||||||
return r.err
|
return r.err
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +15,7 @@ func (r InvalidResourceError) Error() string{
|
||||||
// ValidFormat is a type for quickly forcing
|
// ValidFormat is a type for quickly forcing
|
||||||
// new formats on the gojsonschema loader
|
// new formats on the gojsonschema loader
|
||||||
type ValidFormat struct{}
|
type ValidFormat struct{}
|
||||||
|
|
||||||
func (f ValidFormat) IsFormat(input interface{}) bool {
|
func (f ValidFormat) IsFormat(input interface{}) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -49,4 +51,3 @@ func Validate(rawResource []byte, schema *gojsonschema.Schema) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue