add -dir parameter

This commit is contained in:
Yann Hamon 2020-05-30 15:49:02 +02:00
parent 81aca53a84
commit 2786a84a4c
5 changed files with 84 additions and 30 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/make -f #!/usr/bin/make -f
all: all:
go build go build -o bin/kubeconform

1
bin/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
kubeconform

66
main.go
View file

@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/yannh/kubeconform/pkg/cache" "github.com/yannh/kubeconform/pkg/cache"
"github.com/yannh/kubeconform/pkg/fsutils"
"github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/resource"
"github.com/yannh/kubeconform/pkg/validator" "github.com/yannh/kubeconform/pkg/validator"
@ -72,9 +73,10 @@ func (i *arrayFiles) Set(value string) error {
func realMain() int { func realMain() int {
var files arrayFiles var files, dirs arrayFiles
var skipKinds, k8sVersion string var skipKinds, k8sVersion string
flag.Var(&files, "file", "file to validate (can be specified multiple times)") 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(&k8sVersion, "k8sversion", "1.18.0", "version of Kubernetes to test against")
flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore") flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore")
flag.Parse() flag.Parse()
@ -89,36 +91,54 @@ func realMain() int {
return false return false
} }
for _, filename := range files { fileBatches := make(chan []string)
f, err := os.Open(filename)
if err != nil {
log.Fatalf("failed opening %s\n", filename)
return 1
}
defer f.Close()
r := registry.NewKubernetesRegistry(false) go func() {
res := validateFile(f, []*registry.KubernetesRegistry{r}, k8sVersion, filter) for _, dir := range dirs {
for _, resourceValidation := range res { if err := fsutils.FindYamlInDir(dir, fileBatches, 10); err != nil {
if resourceValidation.skipped { log.Printf("failed processing folder %s: %s", dir, err)
log.Printf("skipping resource\n") }
}
for _, filename := range files {
fileBatches <- []string{filename}
}
close(fileBatches)
}()
r := registry.NewKubernetesRegistry(false)
for fileBatch := range fileBatches {
for _, filename := range fileBatch {
f, err := os.Open(filename)
if err != nil {
log.Printf("failed opening %s\n", filename)
continue continue
} }
if resourceValidation.err != nil { res := validateFile(f, []*registry.KubernetesRegistry{r}, k8sVersion, filter)
if _, ok := resourceValidation.err.(validator.InvalidResourceError); ok { f.Close()
log.Printf("invalid resource: %s\n", resourceValidation.err) for _, resourceValidation := range res {
} else { if resourceValidation.skipped {
log.Printf("failed validating resource: %s\n", resourceValidation.err) log.Printf("skipping resource\n")
continue
} }
continue
}
if resourceValidation.err == nil && !resourceValidation.skipped{ if resourceValidation.err != nil {
log.Printf("file %s is valid\n", filename) 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)
}
} }
} }
} }
return 0 return 0

32
pkg/fsutils/main.go Normal file
View file

@ -0,0 +1,32 @@
package fsutils
import (
"os"
"path/filepath"
"strings"
)
func FindYamlInDir(dir string, fileBatches chan<- []string, batchSize int) error {
files := []string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) {
files = append(files, path)
if len(files) > batchSize {
fileBatches <- files
files = nil
}
}
return nil
})
if len(files) > 0 {
fileBatches <- files
}
return err
}

View file

@ -18,12 +18,13 @@ func (f ValidFormat) IsFormat(input interface{}) bool {
return true return true
} }
func init () { // From kubeval - let's see if absolutely necessary
gojsonschema.FormatCheckers.Add("int64", ValidFormat{}) // func init () {
gojsonschema.FormatCheckers.Add("byte", ValidFormat{}) // gojsonschema.FormatCheckers.Add("int64", ValidFormat{})
gojsonschema.FormatCheckers.Add("int32", ValidFormat{}) // gojsonschema.FormatCheckers.Add("byte", ValidFormat{})
gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{}) // gojsonschema.FormatCheckers.Add("int32", ValidFormat{})
} // gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
// }
func Validate(rawResource []byte, rawSchema []byte) error { func Validate(rawResource []byte, rawSchema []byte) error {
schemaLoader := gojsonschema.NewBytesLoader(rawSchema) schemaLoader := gojsonschema.NewBytesLoader(rawSchema)