From 300b571c332b944f154a55498f0b645140f1604e Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 15 Nov 2020 12:12:37 +0100 Subject: [PATCH] linting / refactor --- pkg/config/config.go | 10 ++++----- pkg/config/config_test.go | 36 +++++++++++++++---------------- pkg/output/json.go | 3 ++- pkg/output/json_test.go | 3 ++- pkg/output/output.go | 3 ++- pkg/output/text.go | 3 ++- pkg/output/text_test.go | 3 ++- pkg/resource/stream_test.go | 3 ++- pkg/validator/validator.go | 38 ++++++++++++++++++--------------- pkg/validator/validator_test.go | 7 +++--- 10 files changed, 60 insertions(+), 49 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 95ce14f..ede6d3e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,8 +13,8 @@ type Config struct { Files []string SchemaLocations []string SkipTLS bool - SkipKinds map[string]bool - RejectKinds map[string]bool + SkipKinds map[string]struct{} + RejectKinds map[string]struct{} OutputFormat string KubernetesVersion string NumberOfWorkers int @@ -37,13 +37,13 @@ func (ap *arrayParam) Set(value string) error { return nil } -func splitCSV(csvStr string) map[string]bool { +func splitCSV(csvStr string) map[string]struct{} { splitValues := strings.Split(csvStr, ",") - valuesMap := map[string]bool{} + valuesMap := map[string]struct{}{} for _, kind := range splitValues { if len(kind) > 0 { - valuesMap[kind] = true + valuesMap[kind] = struct{}{} } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index e0a326e..ac1db9c 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -9,27 +9,27 @@ func TestSkipKindMaps(t *testing.T) { for _, testCase := range []struct { name string csvSkipKinds string - expect map[string]bool + expect map[string]struct{} }{ { "nothing to skip", "", - map[string]bool{}, + map[string]struct{}{}, }, { "a single kind to skip", "somekind", - map[string]bool{ - "somekind": true, + map[string]struct{}{ + "somekind": {}, }, }, { "multiple kinds to skip", "somekind,anotherkind,yetsomeotherkind", - map[string]bool{ - "somekind": true, - "anotherkind": true, - "yetsomeotherkind": true, + map[string]struct{}{ + "somekind": {}, + "anotherkind": {}, + "yetsomeotherkind": {}, }, }, } { @@ -53,8 +53,8 @@ func TestFromFlags(t *testing.T) { NumberOfWorkers: 4, OutputFormat: "text", SchemaLocations: nil, - SkipKinds: map[string]bool{}, - RejectKinds: map[string]bool{}, + SkipKinds: map[string]struct{}{}, + RejectKinds: map[string]struct{}{}, }, }, { @@ -66,8 +66,8 @@ func TestFromFlags(t *testing.T) { NumberOfWorkers: 4, OutputFormat: "text", SchemaLocations: nil, - SkipKinds: map[string]bool{}, - RejectKinds: map[string]bool{}, + SkipKinds: map[string]struct{}{}, + RejectKinds: map[string]struct{}{}, }, }, { @@ -78,8 +78,8 @@ func TestFromFlags(t *testing.T) { NumberOfWorkers: 4, OutputFormat: "text", SchemaLocations: nil, - SkipKinds: map[string]bool{"a": true, "b": true, "c": true}, - RejectKinds: map[string]bool{}, + SkipKinds: map[string]struct{}{"a": {}, "b": {}, "c": {}}, + RejectKinds: map[string]struct{}{}, }, }, { @@ -90,8 +90,8 @@ func TestFromFlags(t *testing.T) { NumberOfWorkers: 4, OutputFormat: "text", SchemaLocations: nil, - SkipKinds: map[string]bool{}, - RejectKinds: map[string]bool{}, + SkipKinds: map[string]struct{}{}, + RejectKinds: map[string]struct{}{}, Summary: true, Verbose: true, }, @@ -107,8 +107,8 @@ func TestFromFlags(t *testing.T) { NumberOfWorkers: 2, OutputFormat: "json", SchemaLocations: []string{"folder", "anotherfolder"}, - SkipKinds: map[string]bool{"kinda": true, "kindb": true}, - RejectKinds: map[string]bool{"kindc": true, "kindd": true}, + SkipKinds: map[string]struct{}{"kinda": {}, "kindb": {}}, + RejectKinds: map[string]struct{}{"kindc": {}, "kindd": {}}, Strict: true, Summary: true, Verbose: true, diff --git a/pkg/output/json.go b/pkg/output/json.go index 1d42f6c..872d22c 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -3,8 +3,9 @@ package output import ( "encoding/json" "fmt" - "github.com/yannh/kubeconform/pkg/validator" "io" + + "github.com/yannh/kubeconform/pkg/validator" ) type oresult struct { diff --git a/pkg/output/json_test.go b/pkg/output/json_test.go index 57e20ba..e5ce27d 100644 --- a/pkg/output/json_test.go +++ b/pkg/output/json_test.go @@ -2,9 +2,10 @@ package output import ( "bytes" + "testing" + "github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/validator" - "testing" ) func TestJSONWrite(t *testing.T) { diff --git a/pkg/output/output.go b/pkg/output/output.go index eaabbbe..82b3dd7 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -2,8 +2,9 @@ package output import ( "fmt" - "github.com/yannh/kubeconform/pkg/validator" "os" + + "github.com/yannh/kubeconform/pkg/validator" ) type Output interface { diff --git a/pkg/output/text.go b/pkg/output/text.go index c38181a..5663bae 100644 --- a/pkg/output/text.go +++ b/pkg/output/text.go @@ -2,9 +2,10 @@ package output import ( "fmt" - "github.com/yannh/kubeconform/pkg/validator" "io" "sync" + + "github.com/yannh/kubeconform/pkg/validator" ) type texto struct { diff --git a/pkg/output/text_test.go b/pkg/output/text_test.go index 8976d85..39df12a 100644 --- a/pkg/output/text_test.go +++ b/pkg/output/text_test.go @@ -2,9 +2,10 @@ package output import ( "bytes" + "testing" + "github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/validator" - "testing" ) func TestTextWrite(t *testing.T) { diff --git a/pkg/resource/stream_test.go b/pkg/resource/stream_test.go index 5754571..9f17842 100644 --- a/pkg/resource/stream_test.go +++ b/pkg/resource/stream_test.go @@ -3,12 +3,13 @@ package resource_test import ( "bytes" "context" - "github.com/yannh/kubeconform/pkg/resource" "io" "reflect" "strings" "sync" "testing" + + "github.com/yannh/kubeconform/pkg/resource" ) func TestFromStream(t *testing.T) { diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 175b483..0eead1d 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -1,26 +1,29 @@ +// This is the main package to import to embed kubeconform in your software package validator import ( "context" "fmt" + "io" + "github.com/yannh/kubeconform/pkg/cache" "github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/resource" - "io" "github.com/xeipuuv/gojsonschema" "sigs.k8s.io/yaml" ) +// Different types of validation results type Status int const ( - _ Status = iota - Error - Skipped - Valid - Invalid - Empty + _ Status = iota + Error // an error occurred processing the file / resource + Skipped // resource has been skipped, for example if its Kind was part of the kinds to skip + Valid // resource is valid + Invalid // resource is invalid + Empty // resource is empty. Note: is triggered for files starting with a --- separator. ) // Result contains the details of the result of a resource validation @@ -30,6 +33,7 @@ type Result struct { Status Status } +// Validator exposes multiple methods to validate your Kubernetes resources. type Validator interface { ValidateResource(res resource.Resource) Result Validate(filename string, r io.ReadCloser) []Result @@ -38,12 +42,12 @@ type Validator interface { // Opts contains a set of options for the validator. type Opts struct { - SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry - SkipKinds map[string]bool // List of resource Kinds to ignore - RejectKinds map[string]bool // List of resource Kinds to reject - 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 - IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found + SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry + SkipKinds map[string]struct{} // List of resource Kinds to ignore + 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 + Strict bool // thros an error if resources contain undocumented fields + IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found } // New returns a new Validator @@ -63,10 +67,10 @@ func New(schemaLocations []string, opts Opts) Validator { } if opts.SkipKinds == nil { - opts.SkipKinds = map[string]bool{} + opts.SkipKinds = map[string]struct{}{} } if opts.RejectKinds == nil { - opts.RejectKinds = map[string]bool{} + opts.RejectKinds = map[string]struct{}{} } return &v{ @@ -88,8 +92,8 @@ type v struct { // large resource streams using multiple Go Routines. func (val *v) ValidateResource(res resource.Resource) Result { skip := func(signature resource.Signature) bool { - isSkipKind, ok := val.opts.SkipKinds[signature.Kind] - return ok && isSkipKind + _, ok := val.opts.SkipKinds[signature.Kind] + return ok } reject := func(signature resource.Signature) bool { diff --git a/pkg/validator/validator_test.go b/pkg/validator/validator_test.go index 8e7e053..168c7fd 100644 --- a/pkg/validator/validator_test.go +++ b/pkg/validator/validator_test.go @@ -1,9 +1,10 @@ package validator import ( + "testing" + "github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/resource" - "testing" "github.com/xeipuuv/gojsonschema" ) @@ -136,8 +137,8 @@ lastName: bar } { val := v{ opts: Opts{ - SkipKinds: map[string]bool{}, - RejectKinds: map[string]bool{}, + SkipKinds: map[string]struct{}{}, + RejectKinds: map[string]struct{}{}, }, schemaCache: nil, schemaDownload: func(_ []registry.Registry, _, _, _ string) (*gojsonschema.Schema, error) {