linting / refactor

This commit is contained in:
Yann Hamon 2020-11-15 12:12:37 +01:00
parent 7604a7aa7d
commit 300b571c33
10 changed files with 60 additions and 49 deletions

View file

@ -13,8 +13,8 @@ type Config struct {
Files []string Files []string
SchemaLocations []string SchemaLocations []string
SkipTLS bool SkipTLS bool
SkipKinds map[string]bool SkipKinds map[string]struct{}
RejectKinds map[string]bool RejectKinds map[string]struct{}
OutputFormat string OutputFormat string
KubernetesVersion string KubernetesVersion string
NumberOfWorkers int NumberOfWorkers int
@ -37,13 +37,13 @@ func (ap *arrayParam) Set(value string) error {
return nil return nil
} }
func splitCSV(csvStr string) map[string]bool { func splitCSV(csvStr string) map[string]struct{} {
splitValues := strings.Split(csvStr, ",") splitValues := strings.Split(csvStr, ",")
valuesMap := map[string]bool{} valuesMap := map[string]struct{}{}
for _, kind := range splitValues { for _, kind := range splitValues {
if len(kind) > 0 { if len(kind) > 0 {
valuesMap[kind] = true valuesMap[kind] = struct{}{}
} }
} }

View file

@ -9,27 +9,27 @@ func TestSkipKindMaps(t *testing.T) {
for _, testCase := range []struct { for _, testCase := range []struct {
name string name string
csvSkipKinds string csvSkipKinds string
expect map[string]bool expect map[string]struct{}
}{ }{
{ {
"nothing to skip", "nothing to skip",
"", "",
map[string]bool{}, map[string]struct{}{},
}, },
{ {
"a single kind to skip", "a single kind to skip",
"somekind", "somekind",
map[string]bool{ map[string]struct{}{
"somekind": true, "somekind": {},
}, },
}, },
{ {
"multiple kinds to skip", "multiple kinds to skip",
"somekind,anotherkind,yetsomeotherkind", "somekind,anotherkind,yetsomeotherkind",
map[string]bool{ map[string]struct{}{
"somekind": true, "somekind": {},
"anotherkind": true, "anotherkind": {},
"yetsomeotherkind": true, "yetsomeotherkind": {},
}, },
}, },
} { } {
@ -53,8 +53,8 @@ func TestFromFlags(t *testing.T) {
NumberOfWorkers: 4, NumberOfWorkers: 4,
OutputFormat: "text", OutputFormat: "text",
SchemaLocations: nil, SchemaLocations: nil,
SkipKinds: map[string]bool{}, SkipKinds: map[string]struct{}{},
RejectKinds: map[string]bool{}, RejectKinds: map[string]struct{}{},
}, },
}, },
{ {
@ -66,8 +66,8 @@ func TestFromFlags(t *testing.T) {
NumberOfWorkers: 4, NumberOfWorkers: 4,
OutputFormat: "text", OutputFormat: "text",
SchemaLocations: nil, SchemaLocations: nil,
SkipKinds: map[string]bool{}, SkipKinds: map[string]struct{}{},
RejectKinds: map[string]bool{}, RejectKinds: map[string]struct{}{},
}, },
}, },
{ {
@ -78,8 +78,8 @@ func TestFromFlags(t *testing.T) {
NumberOfWorkers: 4, NumberOfWorkers: 4,
OutputFormat: "text", OutputFormat: "text",
SchemaLocations: nil, SchemaLocations: nil,
SkipKinds: map[string]bool{"a": true, "b": true, "c": true}, SkipKinds: map[string]struct{}{"a": {}, "b": {}, "c": {}},
RejectKinds: map[string]bool{}, RejectKinds: map[string]struct{}{},
}, },
}, },
{ {
@ -90,8 +90,8 @@ func TestFromFlags(t *testing.T) {
NumberOfWorkers: 4, NumberOfWorkers: 4,
OutputFormat: "text", OutputFormat: "text",
SchemaLocations: nil, SchemaLocations: nil,
SkipKinds: map[string]bool{}, SkipKinds: map[string]struct{}{},
RejectKinds: map[string]bool{}, RejectKinds: map[string]struct{}{},
Summary: true, Summary: true,
Verbose: true, Verbose: true,
}, },
@ -107,8 +107,8 @@ func TestFromFlags(t *testing.T) {
NumberOfWorkers: 2, NumberOfWorkers: 2,
OutputFormat: "json", OutputFormat: "json",
SchemaLocations: []string{"folder", "anotherfolder"}, SchemaLocations: []string{"folder", "anotherfolder"},
SkipKinds: map[string]bool{"kinda": true, "kindb": true}, SkipKinds: map[string]struct{}{"kinda": {}, "kindb": {}},
RejectKinds: map[string]bool{"kindc": true, "kindd": true}, RejectKinds: map[string]struct{}{"kindc": {}, "kindd": {}},
Strict: true, Strict: true,
Summary: true, Summary: true,
Verbose: true, Verbose: true,

View file

@ -3,8 +3,9 @@ package output
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/yannh/kubeconform/pkg/validator"
"io" "io"
"github.com/yannh/kubeconform/pkg/validator"
) )
type oresult struct { type oresult struct {

View file

@ -2,9 +2,10 @@ package output
import ( import (
"bytes" "bytes"
"testing"
"github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/resource"
"github.com/yannh/kubeconform/pkg/validator" "github.com/yannh/kubeconform/pkg/validator"
"testing"
) )
func TestJSONWrite(t *testing.T) { func TestJSONWrite(t *testing.T) {

View file

@ -2,8 +2,9 @@ package output
import ( import (
"fmt" "fmt"
"github.com/yannh/kubeconform/pkg/validator"
"os" "os"
"github.com/yannh/kubeconform/pkg/validator"
) )
type Output interface { type Output interface {

View file

@ -2,9 +2,10 @@ package output
import ( import (
"fmt" "fmt"
"github.com/yannh/kubeconform/pkg/validator"
"io" "io"
"sync" "sync"
"github.com/yannh/kubeconform/pkg/validator"
) )
type texto struct { type texto struct {

View file

@ -2,9 +2,10 @@ package output
import ( import (
"bytes" "bytes"
"testing"
"github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/resource"
"github.com/yannh/kubeconform/pkg/validator" "github.com/yannh/kubeconform/pkg/validator"
"testing"
) )
func TestTextWrite(t *testing.T) { func TestTextWrite(t *testing.T) {

View file

@ -3,12 +3,13 @@ package resource_test
import ( import (
"bytes" "bytes"
"context" "context"
"github.com/yannh/kubeconform/pkg/resource"
"io" "io"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
"testing" "testing"
"github.com/yannh/kubeconform/pkg/resource"
) )
func TestFromStream(t *testing.T) { func TestFromStream(t *testing.T) {

View file

@ -1,26 +1,29 @@
// This is the main package to import to embed kubeconform in your software
package validator package validator
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"github.com/yannh/kubeconform/pkg/cache" "github.com/yannh/kubeconform/pkg/cache"
"github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/resource"
"io"
"github.com/xeipuuv/gojsonschema" "github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
// Different types of validation results
type Status int type Status int
const ( const (
_ Status = iota _ Status = iota
Error Error // an error occurred processing the file / resource
Skipped Skipped // resource has been skipped, for example if its Kind was part of the kinds to skip
Valid Valid // resource is valid
Invalid Invalid // resource is invalid
Empty Empty // resource is empty. Note: is triggered for files starting with a --- separator.
) )
// Result contains the details of the result of a resource validation // Result contains the details of the result of a resource validation
@ -30,6 +33,7 @@ type Result struct {
Status Status Status Status
} }
// Validator exposes multiple methods to validate your Kubernetes resources.
type Validator interface { type Validator interface {
ValidateResource(res resource.Resource) Result ValidateResource(res resource.Resource) Result
Validate(filename string, r io.ReadCloser) []Result Validate(filename string, r io.ReadCloser) []Result
@ -38,12 +42,12 @@ type Validator interface {
// Opts contains a set of options for the validator. // Opts contains a set of options for the validator.
type Opts struct { type Opts struct {
SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry
SkipKinds map[string]bool // List of resource Kinds to ignore SkipKinds map[string]struct{} // List of resource Kinds to ignore
RejectKinds map[string]bool // List of resource Kinds to reject RejectKinds map[string]struct{} // List of resource Kinds to reject
KubernetesVersion string // Kubernetes Version - has to match one in https://github.com/instrumenta/kubernetes-json-schema KubernetesVersion string // Kubernetes Version - has to match one in https://github.com/instrumenta/kubernetes-json-schema
Strict bool // thros an error if resources contain undocumented fields Strict bool // thros an error if resources contain undocumented fields
IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found
} }
// New returns a new Validator // New returns a new Validator
@ -63,10 +67,10 @@ func New(schemaLocations []string, opts Opts) Validator {
} }
if opts.SkipKinds == nil { if opts.SkipKinds == nil {
opts.SkipKinds = map[string]bool{} opts.SkipKinds = map[string]struct{}{}
} }
if opts.RejectKinds == nil { if opts.RejectKinds == nil {
opts.RejectKinds = map[string]bool{} opts.RejectKinds = map[string]struct{}{}
} }
return &v{ return &v{
@ -88,8 +92,8 @@ type v struct {
// large resource streams using multiple Go Routines. // large resource streams using multiple Go Routines.
func (val *v) ValidateResource(res resource.Resource) Result { func (val *v) ValidateResource(res resource.Resource) Result {
skip := func(signature resource.Signature) bool { skip := func(signature resource.Signature) bool {
isSkipKind, ok := val.opts.SkipKinds[signature.Kind] _, ok := val.opts.SkipKinds[signature.Kind]
return ok && isSkipKind return ok
} }
reject := func(signature resource.Signature) bool { reject := func(signature resource.Signature) bool {

View file

@ -1,9 +1,10 @@
package validator package validator
import ( import (
"testing"
"github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/resource"
"testing"
"github.com/xeipuuv/gojsonschema" "github.com/xeipuuv/gojsonschema"
) )
@ -136,8 +137,8 @@ lastName: bar
} { } {
val := v{ val := v{
opts: Opts{ opts: Opts{
SkipKinds: map[string]bool{}, SkipKinds: map[string]struct{}{},
RejectKinds: map[string]bool{}, RejectKinds: map[string]struct{}{},
}, },
schemaCache: nil, schemaCache: nil,
schemaDownload: func(_ []registry.Registry, _, _, _ string) (*gojsonschema.Schema, error) { schemaDownload: func(_ []registry.Registry, _, _, _ string) (*gojsonschema.Schema, error) {