mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-21 19:07:00 +00:00
fail early when a broken -schema-location template is given
This commit is contained in:
parent
905f5e7417
commit
651d3b2c50
5 changed files with 32 additions and 8 deletions
|
|
@ -128,6 +128,13 @@
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "Fail early when passing a non valid -schema-location template" {
|
||||||
|
run bin/kubeconform -schema-location 'foo {{ .Foo }}' fixtures/valid.yaml
|
||||||
|
[[ "$output" = failed\ initialising* ]]
|
||||||
|
[[ `echo "$output" | wc -l` -eq 1 ]]
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
}
|
||||||
|
|
||||||
@test "Pass with a valid input when validating against openshift manifests" {
|
@test "Pass with a valid input when validating against openshift manifests" {
|
||||||
run bin/kubeconform -kubernetes-version 3.8.0 -schema-location 'https://raw.githubusercontent.com/garethr/openshift-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}.json' -summary fixtures/valid.yaml
|
run bin/kubeconform -kubernetes-version 3.8.0 -schema-location 'https://raw.githubusercontent.com/garethr/openshift-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}.json' -summary fixtures/valid.yaml
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ func realMain() int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
v := validator.New(cfg.SchemaLocations, validator.Opts{
|
v, err := validator.New(cfg.SchemaLocations, validator.Opts{
|
||||||
SkipTLS: cfg.SkipTLS,
|
SkipTLS: cfg.SkipTLS,
|
||||||
SkipKinds: cfg.SkipKinds,
|
SkipKinds: cfg.SkipKinds,
|
||||||
RejectKinds: cfg.RejectKinds,
|
RejectKinds: cfg.RejectKinds,
|
||||||
|
|
@ -73,6 +73,10 @@ func realMain() int {
|
||||||
Strict: cfg.Strict,
|
Strict: cfg.Strict,
|
||||||
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
|
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
validationResults := make(chan validator.Result)
|
validationResults := make(chan validator.Result)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,10 @@ func main() {
|
||||||
log.Fatalf("failed opening %s: %s", filepath, err)
|
log.Fatalf("failed opening %s: %s", filepath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
v := validator.New(nil, validator.Opts{Strict: true})
|
v, err := validator.New(nil, validator.Opts{Strict: true})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed initializing validator: %s", err)
|
||||||
|
}
|
||||||
for i, res := range v.Validate(filepath, f) { // A file might contain multiple resources
|
for i, res := range v.Validate(filepath, f) { // A file might contain multiple resources
|
||||||
// File starts with ---, the parser assumes a first empty resource
|
// File starts with ---, the parser assumes a first empty resource
|
||||||
if res.Status == validator.Invalid {
|
if res.Status == validator.Invalid {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
@ -67,14 +68,19 @@ func schemaPath(tpl, resourceKind, resourceAPIVersion, k8sVersion string, strict
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(schemaLocation string, strict bool, skipTLS bool) Registry {
|
func New(schemaLocation string, strict bool, skipTLS bool) (Registry, error) {
|
||||||
if !strings.HasSuffix(schemaLocation, "json") { // If we dont specify a full templated path, we assume the paths of kubernetesjsonschema.dev
|
if !strings.HasSuffix(schemaLocation, "json") { // If we dont specify a full templated path, we assume the paths of kubernetesjsonschema.dev
|
||||||
schemaLocation += "/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json"
|
schemaLocation += "/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We try a fake compilation of the schemaLocation template to ensure it is valid
|
||||||
|
if _, err := schemaPath(schemaLocation, "Deployment", "v1", "1.18.0", true); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed initialising schema location registry: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(schemaLocation, "http") {
|
if strings.HasPrefix(schemaLocation, "http") {
|
||||||
return newHTTPRegistry(schemaLocation, strict, skipTLS)
|
return newHTTPRegistry(schemaLocation, strict, skipTLS), nil
|
||||||
} else {
|
} else {
|
||||||
return newLocalRegistry(schemaLocation, strict)
|
return newLocalRegistry(schemaLocation, strict), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ type Opts struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Validator
|
// New returns a new Validator
|
||||||
func New(schemaLocations []string, opts Opts) Validator {
|
func New(schemaLocations []string, opts Opts) (Validator, error) {
|
||||||
// Default to kubernetesjsonschema.dev
|
// Default to kubernetesjsonschema.dev
|
||||||
if schemaLocations == nil || len(schemaLocations) == 0 {
|
if schemaLocations == nil || len(schemaLocations) == 0 {
|
||||||
schemaLocations = []string{"https://kubernetesjsonschema.dev"}
|
schemaLocations = []string{"https://kubernetesjsonschema.dev"}
|
||||||
|
|
@ -59,7 +59,11 @@ func New(schemaLocations []string, opts Opts) Validator {
|
||||||
|
|
||||||
registries := []registry.Registry{}
|
registries := []registry.Registry{}
|
||||||
for _, schemaLocation := range schemaLocations {
|
for _, schemaLocation := range schemaLocations {
|
||||||
registries = append(registries, registry.New(schemaLocation, opts.Strict, opts.SkipTLS))
|
reg, err := registry.New(schemaLocation, opts.Strict, opts.SkipTLS)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
registries = append(registries, reg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.KubernetesVersion == "" {
|
if opts.KubernetesVersion == "" {
|
||||||
|
|
@ -78,7 +82,7 @@ func New(schemaLocations []string, opts Opts) Validator {
|
||||||
schemaDownload: downloadSchema,
|
schemaDownload: downloadSchema,
|
||||||
schemaCache: cache.New(),
|
schemaCache: cache.New(),
|
||||||
regs: registries,
|
regs: registries,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type v struct {
|
type v struct {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue