Add example how to use kubeconform as a library

This commit is contained in:
Yann Hamon 2020-11-15 10:01:36 +01:00
parent 4672ded043
commit 4e96b44a8b
7 changed files with 93 additions and 18 deletions

View file

@ -88,10 +88,6 @@ func FromFlags(progName string, args []string) (Config, string, error) {
c.RejectKinds = splitCSV(rejectKindsCSV)
c.IgnoreFilenamePatterns = ignoreFilenamePatterns
c.SchemaLocations = schemaLocationsParam
if len(c.SchemaLocations) == 0 {
c.SchemaLocations = append(c.SchemaLocations, "https://kubernetesjsonschema.dev") // if not specified, default behaviour is to use kubernetesjson-schema.dev as registry
}
c.Files = flags.Args()
if c.Help {

View file

@ -52,7 +52,7 @@ func TestFromFlags(t *testing.T) {
KubernetesVersion: "1.18.0",
NumberOfWorkers: 4,
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SchemaLocations: nil,
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
},
@ -65,7 +65,7 @@ func TestFromFlags(t *testing.T) {
KubernetesVersion: "1.18.0",
NumberOfWorkers: 4,
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SchemaLocations: nil,
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
},
@ -77,7 +77,7 @@ func TestFromFlags(t *testing.T) {
KubernetesVersion: "1.18.0",
NumberOfWorkers: 4,
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SchemaLocations: nil,
SkipKinds: map[string]bool{"a": true, "b": true, "c": true},
RejectKinds: map[string]bool{},
},
@ -89,7 +89,7 @@ func TestFromFlags(t *testing.T) {
KubernetesVersion: "1.18.0",
NumberOfWorkers: 4,
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SchemaLocations: nil,
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
Summary: true,
@ -116,10 +116,10 @@ func TestFromFlags(t *testing.T) {
},
}
for _, testCase := range testCases {
for i, testCase := range testCases {
cfg, _, _ := FromFlags("kubeconform", testCase.args)
if reflect.DeepEqual(cfg, testCase.conf) != true {
t.Errorf("failed parsing config - expected , got: \n%+v\n%+v", testCase.conf, cfg)
t.Errorf("test %d: failed parsing config - expected , got: \n%+v\n%+v", i, testCase.conf, cfg)
}
}
}

View file

@ -1,10 +1,12 @@
package validator
import (
"context"
"fmt"
"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"
@ -29,7 +31,8 @@ type Result struct {
}
type Validator interface {
Validate(res resource.Resource) Result
ValidateResource(res resource.Resource) Result
Validate(filename string, r io.Reader) []Result
}
type Opts struct {
@ -42,11 +45,20 @@ type Opts struct {
}
func New(schemaLocations []string, opts Opts) Validator {
// Default to kubernetesjsonschema.dev
if schemaLocations == nil || len(schemaLocations) == 0 {
schemaLocations = []string{"https://kubernetesjsonschema.dev"}
}
registries := []registry.Registry{}
for _, schemaLocation := range schemaLocations {
registries = append(registries, registry.New(schemaLocation, opts.Strict, opts.SkipTLS))
}
if opts.KubernetesVersion == "" {
opts.KubernetesVersion = "1.18.0"
}
if opts.SkipKinds == nil {
opts.SkipKinds = map[string]bool{}
}
@ -69,7 +81,7 @@ type v struct {
regs []registry.Registry
}
func (val *v) Validate(res resource.Resource) Result {
func (val *v) ValidateResource(res resource.Resource) Result {
skip := func(signature resource.Signature) bool {
isSkipKind, ok := val.opts.SkipKinds[signature.Kind]
return ok && isSkipKind
@ -151,6 +163,33 @@ func (val *v) Validate(res resource.Resource) Result {
return Result{Resource: res, Status: Invalid, Err: fmt.Errorf("%s", msg)}
}
func (val *v) ValidateWithContext(ctx context.Context, filename string, r io.Reader) []Result {
validationResults := []Result{}
resourcesChan, _ := resource.FromStream(ctx, filename, r)
for {
select {
case res, ok := <-resourcesChan:
validationResults = append(validationResults, val.ValidateResource(res))
if !ok {
resourcesChan = nil
}
case <-ctx.Done():
break
}
if resourcesChan == nil {
break
}
}
return validationResults
}
func (val *v) Validate(filename string, r io.Reader) []Result {
return val.ValidateWithContext(context.Background(), filename, r)
}
func downloadSchema(registries []registry.Registry, kind, version, k8sVersion string) (*gojsonschema.Schema, error) {
var err error
var schemaBytes []byte

View file

@ -149,7 +149,7 @@ lastName: bar
},
regs: nil,
}
if got := val.Validate(resource.Resource{Bytes: testCase.rawResource}); got.Status != testCase.expect {
if got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource}); got.Status != testCase.expect {
t.Errorf("%d - expected %d, got %d", i, testCase.expect, got.Status)
}
}