fix - do try following schema registries when schema not found in local registry

This commit is contained in:
Yann Hamon 2020-12-29 19:16:28 +01:00
parent 9a56fc4176
commit 1a76217195
4 changed files with 18 additions and 23 deletions

View file

@ -115,6 +115,11 @@
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
} }
@test "Pass when parsing a Custom Resource and specifying several local registries, the last one having the appropriate CRD" {
run bin/kubeconform -schema-location 'fixtures/{{ .ResourceKind }}.json' -schema-location './fixtures/registry/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/test_crd.yaml
[ "$status" -eq 0 ]
}
@test "Pass when parsing a config with additional properties" { @test "Pass when parsing a config with additional properties" {
run bin/kubeconform -summary fixtures/extra_property.yaml run bin/kubeconform -summary fixtures/extra_property.yaml
[ "$status" -eq 0 ] [ "$status" -eq 0 ]

View file

@ -19,16 +19,6 @@ type SchemaRegistry struct {
strict bool strict bool
} }
// NotFoundError is returned when the registry does not contain a schema for the resource
type NotFoundError struct {
err error
}
func newNetFoundError(err error) *NotFoundError {
return &NotFoundError{err}
}
func (e *NotFoundError) Error() string { return e.err.Error() }
func newHTTPRegistry(schemaPathTemplate string, strict bool, skipTLS bool) *SchemaRegistry { func newHTTPRegistry(schemaPathTemplate string, strict bool, skipTLS bool) *SchemaRegistry {
reghttp := &http.Transport{ reghttp := &http.Transport{
MaxIdleConns: 100, MaxIdleConns: 100,
@ -61,7 +51,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound { if resp.StatusCode == http.StatusNotFound {
return nil, newNetFoundError(fmt.Errorf("no schema found")) return nil, newNotFoundError(fmt.Errorf("no schema found"))
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {

View file

@ -11,17 +11,6 @@ type LocalRegistry struct {
strict bool strict bool
} }
type fileNotFoundError struct {
err error
isRetryable bool
}
func newFileNotFoundError(err error, isRetryable bool) *fileNotFoundError {
return &fileNotFoundError{err, isRetryable}
}
func (e *fileNotFoundError) IsNotFound() bool { return e.isRetryable }
func (e *fileNotFoundError) Error() string { return e.err.Error() }
// NewLocalSchemas creates a new "registry", that will serve schemas from files, given a list of schema filenames // NewLocalSchemas creates a new "registry", that will serve schemas from files, given a list of schema filenames
func newLocalRegistry(pathTemplate string, strict bool) *LocalRegistry { func newLocalRegistry(pathTemplate string, strict bool) *LocalRegistry {
return &LocalRegistry{ return &LocalRegistry{
@ -39,7 +28,7 @@ func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersi
f, err := os.Open(schemaFile) f, err := os.Open(schemaFile)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, newFileNotFoundError(fmt.Errorf("no schema found"), false) return nil, newNotFoundError(fmt.Errorf("no schema found"))
} }
return nil, fmt.Errorf("failed to open schema %s", schemaFile) return nil, fmt.Errorf("failed to open schema %s", schemaFile)
} }

View file

@ -21,6 +21,17 @@ type Retryable interface {
IsNotFound() bool IsNotFound() bool
} }
// NotFoundError is returned when the registry does not contain a schema for the resource
type NotFoundError struct {
err error
}
func newNotFoundError(err error) *NotFoundError {
return &NotFoundError{err}
}
func (e *NotFoundError) Error() string { return e.err.Error() }
func (e *NotFoundError) Retryable() bool { return false }
func schemaPath(tpl, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error) { func schemaPath(tpl, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error) {
normalisedVersion := k8sVersion normalisedVersion := k8sVersion
if normalisedVersion != "master" { if normalisedVersion != "master" {