mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
linting / refactor
This commit is contained in:
parent
7604a7aa7d
commit
300b571c33
10 changed files with 60 additions and 49 deletions
|
|
@ -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{}{}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@ package output
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
"os"
|
||||
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
)
|
||||
|
||||
type Output interface {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue