From 00880f68df191a58b2cda29f054e78c1bdcae27a Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Thu, 4 Jun 2020 23:33:00 +0200 Subject: [PATCH] fail on missing schemas by default, add -ignore-missing-schemas --- Makefile | 6 ++++-- acceptance.bats | 12 +++++++++++- main.go | 15 ++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c1f14ee..dc96e4d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ #!/usr/bin/make -f -.PHONY: test-build test build build-static docker-test docker-build-static acceptance +.PHONY: test-build test build build-static docker-test docker-build-static build-bats docker-acceptance test-build: test build @@ -19,6 +19,8 @@ docker-test: docker-build-static: docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.14 make build-static -docker-acceptance: +build-bats: docker build -t bats -f Dockerfile.bats . + +docker-acceptance: build-bats docker run -t bats acceptance.bats diff --git a/acceptance.bats b/acceptance.bats index a40bd02..e18b1c5 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -43,4 +43,14 @@ @test "Fail when parsing a config with additional properties and strict set" { run bin/kubeconform -strict -k8sversion 1.16.0 -file fixtures/extra_property.yaml [ "$status" -eq 1 ] -} \ No newline at end of file +} + +@test "Fail when parsing a config with CRD" { + run bin/kubeconform -file fixtures/test_crd.yaml + [ "$status" -eq 1 ] +} + +@test "Pass when parsing a config with CRD and ignoring missing schemas" { + run bin/kubeconform -file fixtures/test_crd.yaml -ignore-missing-schemas + [ "$status" -eq 0 ] +} diff --git a/main.go b/main.go index 10da94b..f36a2ee 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func downloadSchema(registries []registry.Registry, kind, version, k8sVersion st // filter returns true if the file should be skipped // Returning an array, this Reader might container multiple resources -func ValidateStream(r io.Reader, regs []registry.Registry, k8sVersion string, c *cache.SchemaCache, skip func(signature resource.Signature) bool) []validationResult { +func ValidateStream(r io.Reader, regs []registry.Registry, k8sVersion string, c *cache.SchemaCache, skip func(signature resource.Signature) bool, ignoreMissingSchemas bool) []validationResult { rawResources, err := resourcesFromReader(r) if err != nil { return []validationResult{{err: fmt.Errorf("failed reading file: %s", err)}} @@ -103,8 +103,12 @@ func ValidateStream(r io.Reader, regs []registry.Registry, k8sVersion string, c if err != nil { validationResults = append(validationResults, validationResult{kind: sig.Kind, version: sig.Version, err: err, skipped: false}) continue - } else if schema == nil { // skip if no schema was found, but there was no error - validationResults = append(validationResults, validationResult{kind: sig.Kind, version: sig.Version, err: nil, skipped: true}) + } else if schema == nil { // skip if no schema was found, but there was no error TODO: Fail by default, provide a -skip-missing-schema + if ignoreMissingSchemas { + validationResults = append(validationResults, validationResult{kind: sig.Kind, version: sig.Version, err: nil, skipped: true}) + } else { + validationResults = append(validationResults, validationResult{kind: sig.Kind, version: sig.Version, err: fmt.Errorf("could not find schema for %s", sig.Kind), skipped: false}) + } if c != nil { c.Set(cacheKey, nil) } @@ -176,7 +180,7 @@ func processResults(o output.Output, validationResults chan []validationResult, func realMain() int { var files, dirs, schemas arrayParam var skipKindsCSV, k8sVersion, outputFormat string - var summary, strict, verbose bool + var summary, strict, verbose, ignoreMissingSchemas bool var nWorkers int var err error @@ -184,6 +188,7 @@ func realMain() int { flag.Var(&files, "file", "file to validate (can be specified multiple times)") flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)") flag.Var(&schemas, "schema", "file containing an additional Schema (can be specified multiple times)") + flag.BoolVar(&ignoreMissingSchemas, "ignore-missing-schemas", false, "skip files with missing schemas instead of failing") flag.BoolVar(&summary, "summary", false, "print a summary at the end") flag.IntVar(&nWorkers, "n", 4, "number of routines to run in parallel") flag.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds to ignore") @@ -254,7 +259,7 @@ func realMain() int { continue } - res := ValidateStream(f, registries, k8sVersion, c, filter) + res := ValidateStream(f, registries, k8sVersion, c, filter, ignoreMissingSchemas) f.Close() for i := range res {