Skip to content

Commit 300b571

Browse files
committed
linting / refactor
1 parent 7604a7a commit 300b571

File tree

10 files changed

+60
-49
lines changed

10 files changed

+60
-49
lines changed

pkg/config/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Config struct {
1313
Files []string
1414
SchemaLocations []string
1515
SkipTLS bool
16-
SkipKinds map[string]bool
17-
RejectKinds map[string]bool
16+
SkipKinds map[string]struct{}
17+
RejectKinds map[string]struct{}
1818
OutputFormat string
1919
KubernetesVersion string
2020
NumberOfWorkers int
@@ -37,13 +37,13 @@ func (ap *arrayParam) Set(value string) error {
3737
return nil
3838
}
3939

40-
func splitCSV(csvStr string) map[string]bool {
40+
func splitCSV(csvStr string) map[string]struct{} {
4141
splitValues := strings.Split(csvStr, ",")
42-
valuesMap := map[string]bool{}
42+
valuesMap := map[string]struct{}{}
4343

4444
for _, kind := range splitValues {
4545
if len(kind) > 0 {
46-
valuesMap[kind] = true
46+
valuesMap[kind] = struct{}{}
4747
}
4848
}
4949

pkg/config/config_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ func TestSkipKindMaps(t *testing.T) {
99
for _, testCase := range []struct {
1010
name string
1111
csvSkipKinds string
12-
expect map[string]bool
12+
expect map[string]struct{}
1313
}{
1414
{
1515
"nothing to skip",
1616
"",
17-
map[string]bool{},
17+
map[string]struct{}{},
1818
},
1919
{
2020
"a single kind to skip",
2121
"somekind",
22-
map[string]bool{
23-
"somekind": true,
22+
map[string]struct{}{
23+
"somekind": {},
2424
},
2525
},
2626
{
2727
"multiple kinds to skip",
2828
"somekind,anotherkind,yetsomeotherkind",
29-
map[string]bool{
30-
"somekind": true,
31-
"anotherkind": true,
32-
"yetsomeotherkind": true,
29+
map[string]struct{}{
30+
"somekind": {},
31+
"anotherkind": {},
32+
"yetsomeotherkind": {},
3333
},
3434
},
3535
} {
@@ -53,8 +53,8 @@ func TestFromFlags(t *testing.T) {
5353
NumberOfWorkers: 4,
5454
OutputFormat: "text",
5555
SchemaLocations: nil,
56-
SkipKinds: map[string]bool{},
57-
RejectKinds: map[string]bool{},
56+
SkipKinds: map[string]struct{}{},
57+
RejectKinds: map[string]struct{}{},
5858
},
5959
},
6060
{
@@ -66,8 +66,8 @@ func TestFromFlags(t *testing.T) {
6666
NumberOfWorkers: 4,
6767
OutputFormat: "text",
6868
SchemaLocations: nil,
69-
SkipKinds: map[string]bool{},
70-
RejectKinds: map[string]bool{},
69+
SkipKinds: map[string]struct{}{},
70+
RejectKinds: map[string]struct{}{},
7171
},
7272
},
7373
{
@@ -78,8 +78,8 @@ func TestFromFlags(t *testing.T) {
7878
NumberOfWorkers: 4,
7979
OutputFormat: "text",
8080
SchemaLocations: nil,
81-
SkipKinds: map[string]bool{"a": true, "b": true, "c": true},
82-
RejectKinds: map[string]bool{},
81+
SkipKinds: map[string]struct{}{"a": {}, "b": {}, "c": {}},
82+
RejectKinds: map[string]struct{}{},
8383
},
8484
},
8585
{
@@ -90,8 +90,8 @@ func TestFromFlags(t *testing.T) {
9090
NumberOfWorkers: 4,
9191
OutputFormat: "text",
9292
SchemaLocations: nil,
93-
SkipKinds: map[string]bool{},
94-
RejectKinds: map[string]bool{},
93+
SkipKinds: map[string]struct{}{},
94+
RejectKinds: map[string]struct{}{},
9595
Summary: true,
9696
Verbose: true,
9797
},
@@ -107,8 +107,8 @@ func TestFromFlags(t *testing.T) {
107107
NumberOfWorkers: 2,
108108
OutputFormat: "json",
109109
SchemaLocations: []string{"folder", "anotherfolder"},
110-
SkipKinds: map[string]bool{"kinda": true, "kindb": true},
111-
RejectKinds: map[string]bool{"kindc": true, "kindd": true},
110+
SkipKinds: map[string]struct{}{"kinda": {}, "kindb": {}},
111+
RejectKinds: map[string]struct{}{"kindc": {}, "kindd": {}},
112112
Strict: true,
113113
Summary: true,
114114
Verbose: true,

pkg/output/json.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package output
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/yannh/kubeconform/pkg/validator"
76
"io"
7+
8+
"github.com/yannh/kubeconform/pkg/validator"
89
)
910

1011
type oresult struct {

pkg/output/json_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package output
22

33
import (
44
"bytes"
5+
"testing"
6+
57
"github.com/yannh/kubeconform/pkg/resource"
68
"github.com/yannh/kubeconform/pkg/validator"
7-
"testing"
89
)
910

1011
func TestJSONWrite(t *testing.T) {

pkg/output/output.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package output
22

33
import (
44
"fmt"
5-
"github.com/yannh/kubeconform/pkg/validator"
65
"os"
6+
7+
"github.com/yannh/kubeconform/pkg/validator"
78
)
89

910
type Output interface {

pkg/output/text.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package output
22

33
import (
44
"fmt"
5-
"github.com/yannh/kubeconform/pkg/validator"
65
"io"
76
"sync"
7+
8+
"github.com/yannh/kubeconform/pkg/validator"
89
)
910

1011
type texto struct {

pkg/output/text_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package output
22

33
import (
44
"bytes"
5+
"testing"
6+
57
"github.com/yannh/kubeconform/pkg/resource"
68
"github.com/yannh/kubeconform/pkg/validator"
7-
"testing"
89
)
910

1011
func TestTextWrite(t *testing.T) {

pkg/resource/stream_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package resource_test
33
import (
44
"bytes"
55
"context"
6-
"github.com/yannh/kubeconform/pkg/resource"
76
"io"
87
"reflect"
98
"strings"
109
"sync"
1110
"testing"
11+
12+
"github.com/yannh/kubeconform/pkg/resource"
1213
)
1314

1415
func TestFromStream(t *testing.T) {

pkg/validator/validator.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1+
// This is the main package to import to embed kubeconform in your software
12
package validator
23

34
import (
45
"context"
56
"fmt"
7+
"io"
8+
69
"github.com/yannh/kubeconform/pkg/cache"
710
"github.com/yannh/kubeconform/pkg/registry"
811
"github.com/yannh/kubeconform/pkg/resource"
9-
"io"
1012

1113
"github.com/xeipuuv/gojsonschema"
1214
"sigs.k8s.io/yaml"
1315
)
1416

17+
// Different types of validation results
1518
type Status int
1619

1720
const (
18-
_ Status = iota
19-
Error
20-
Skipped
21-
Valid
22-
Invalid
23-
Empty
21+
_ Status = iota
22+
Error // an error occurred processing the file / resource
23+
Skipped // resource has been skipped, for example if its Kind was part of the kinds to skip
24+
Valid // resource is valid
25+
Invalid // resource is invalid
26+
Empty // resource is empty. Note: is triggered for files starting with a --- separator.
2427
)
2528

2629
// Result contains the details of the result of a resource validation
@@ -30,6 +33,7 @@ type Result struct {
3033
Status Status
3134
}
3235

36+
// Validator exposes multiple methods to validate your Kubernetes resources.
3337
type Validator interface {
3438
ValidateResource(res resource.Resource) Result
3539
Validate(filename string, r io.ReadCloser) []Result
@@ -38,12 +42,12 @@ type Validator interface {
3842

3943
// Opts contains a set of options for the validator.
4044
type Opts struct {
41-
SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry
42-
SkipKinds map[string]bool // List of resource Kinds to ignore
43-
RejectKinds map[string]bool // List of resource Kinds to reject
44-
KubernetesVersion string // Kubernetes Version - has to match one in https://github.com/instrumenta/kubernetes-json-schema
45-
Strict bool // thros an error if resources contain undocumented fields
46-
IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found
45+
SkipTLS bool // skip TLS validation when downloading from an HTTP Schema Registry
46+
SkipKinds map[string]struct{} // List of resource Kinds to ignore
47+
RejectKinds map[string]struct{} // List of resource Kinds to reject
48+
KubernetesVersion string // Kubernetes Version - has to match one in https://github.com/instrumenta/kubernetes-json-schema
49+
Strict bool // thros an error if resources contain undocumented fields
50+
IgnoreMissingSchemas bool // skip a resource if no schema for that resource can be found
4751
}
4852

4953
// New returns a new Validator
@@ -63,10 +67,10 @@ func New(schemaLocations []string, opts Opts) Validator {
6367
}
6468

6569
if opts.SkipKinds == nil {
66-
opts.SkipKinds = map[string]bool{}
70+
opts.SkipKinds = map[string]struct{}{}
6771
}
6872
if opts.RejectKinds == nil {
69-
opts.RejectKinds = map[string]bool{}
73+
opts.RejectKinds = map[string]struct{}{}
7074
}
7175

7276
return &v{
@@ -88,8 +92,8 @@ type v struct {
8892
// large resource streams using multiple Go Routines.
8993
func (val *v) ValidateResource(res resource.Resource) Result {
9094
skip := func(signature resource.Signature) bool {
91-
isSkipKind, ok := val.opts.SkipKinds[signature.Kind]
92-
return ok && isSkipKind
95+
_, ok := val.opts.SkipKinds[signature.Kind]
96+
return ok
9397
}
9498

9599
reject := func(signature resource.Signature) bool {

pkg/validator/validator_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package validator
22

33
import (
4+
"testing"
5+
46
"github.com/yannh/kubeconform/pkg/registry"
57
"github.com/yannh/kubeconform/pkg/resource"
6-
"testing"
78

89
"github.com/xeipuuv/gojsonschema"
910
)
@@ -136,8 +137,8 @@ lastName: bar
136137
} {
137138
val := v{
138139
opts: Opts{
139-
SkipKinds: map[string]bool{},
140-
RejectKinds: map[string]bool{},
140+
SkipKinds: map[string]struct{}{},
141+
RejectKinds: map[string]struct{}{},
141142
},
142143
schemaCache: nil,
143144
schemaDownload: func(_ []registry.Registry, _, _, _ string) (*gojsonschema.Schema, error) {

0 commit comments

Comments
 (0)