mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
add configurable output
This commit is contained in:
parent
2786a84a4c
commit
8a25986e79
4 changed files with 106 additions and 18 deletions
35
main.go
35
main.go
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/yannh/kubeconform/pkg/output"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
|
@ -74,13 +75,24 @@ func (i *arrayFiles) Set(value string) error {
|
|||
|
||||
func realMain() int {
|
||||
var files, dirs arrayFiles
|
||||
var skipKinds, k8sVersion string
|
||||
var skipKinds, k8sVersion, outputFormat string
|
||||
flag.Var(&files, "file", "file to validate (can be specified multiple times)")
|
||||
flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)")
|
||||
flag.StringVar(&k8sVersion, "k8sversion", "1.18.0", "version of Kubernetes to test against")
|
||||
flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore")
|
||||
flag.StringVar(&outputFormat, "output", "text", "output format - text, json")
|
||||
flag.Parse()
|
||||
|
||||
var o output.Output
|
||||
switch {
|
||||
case outputFormat == "text":
|
||||
o = output.NewTextOutput()
|
||||
case outputFormat == "json":
|
||||
o = output.NewJSONOutput()
|
||||
default:
|
||||
log.Fatalf("-output must be text or json")
|
||||
}
|
||||
|
||||
filter := func(signature resource.Signature) bool {
|
||||
kinds := strings.Split(skipKinds, ",")
|
||||
for _, kind := range kinds {
|
||||
|
|
@ -119,27 +131,14 @@ func realMain() int {
|
|||
|
||||
res := validateFile(f, []*registry.KubernetesRegistry{r}, k8sVersion, filter)
|
||||
f.Close()
|
||||
|
||||
|
||||
for _, resourceValidation := range res {
|
||||
if resourceValidation.skipped {
|
||||
log.Printf("skipping resource\n")
|
||||
continue
|
||||
}
|
||||
|
||||
if resourceValidation.err != nil {
|
||||
if _, ok := resourceValidation.err.(validator.InvalidResourceError); ok {
|
||||
log.Printf("invalid resource: %s\n", resourceValidation.err)
|
||||
} else {
|
||||
log.Printf("failed validating resource in file %s: %s\n", filename, resourceValidation.err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if resourceValidation.err == nil && !resourceValidation.skipped{
|
||||
log.Printf("file %s is valid\n", filename)
|
||||
}
|
||||
o.Write(filename, resourceValidation.err, resourceValidation.skipped)
|
||||
}
|
||||
}
|
||||
}
|
||||
o.Flush()
|
||||
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
45
pkg/output/json.go
Normal file
45
pkg/output/json.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
Filename string `json:"filename"`
|
||||
Status string `json:"status"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type JSONOutput struct {
|
||||
results []result
|
||||
}
|
||||
|
||||
func NewJSONOutput() Output{
|
||||
return &JSONOutput{
|
||||
results: []result{},
|
||||
}
|
||||
}
|
||||
|
||||
func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
||||
status := "VALID"
|
||||
msg := ""
|
||||
if err != nil {
|
||||
status = "ERROR"
|
||||
msg = err.Error()
|
||||
}
|
||||
if skipped {
|
||||
status = "SKIPPED"
|
||||
}
|
||||
|
||||
o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg})
|
||||
}
|
||||
|
||||
func (o *JSONOutput) Flush() {
|
||||
res, err := json.MarshalIndent(o.results,"", " ")
|
||||
if err != nil {
|
||||
fmt.Printf("error print results: %s", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", res)
|
||||
}
|
||||
7
pkg/output/main.go
Normal file
7
pkg/output/main.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package output
|
||||
|
||||
type Output interface {
|
||||
Write (filename string, err error, skipped bool)
|
||||
Flush ()
|
||||
}
|
||||
|
||||
37
pkg/output/text.go
Normal file
37
pkg/output/text.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
"log"
|
||||
)
|
||||
|
||||
type TextOutput struct {
|
||||
}
|
||||
|
||||
func NewTextOutput() Output {
|
||||
return &TextOutput{}
|
||||
}
|
||||
|
||||
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||
if skipped {
|
||||
log.Printf("skipping resource\n")
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||
log.Printf("invalid resource: %s\n", err)
|
||||
} else {
|
||||
log.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !skipped{
|
||||
log.Printf("file %s is valid\n", filename)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *TextOutput) Flush() {
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue