mirror of
https://github.com/yannh/kubeconform.git
synced 2026-05-05 09:44:26 +00:00
Migrate to santhosh-tekuri/jsonschema (#168)
* Migrate to santhosh-tekuri/jsonschema
This commit is contained in:
parent
84afe70659
commit
ee7c498580
65 changed files with 4651 additions and 9106 deletions
|
|
@ -61,15 +61,15 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool,
|
|||
}
|
||||
|
||||
// DownloadSchema downloads the schema for a particular resource from an HTTP server
|
||||
func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) {
|
||||
url, err := schemaPath(r.schemaPathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
if r.cache != nil {
|
||||
if b, err := r.cache.Get(resourceKind, resourceAPIVersion, k8sVersion); err == nil {
|
||||
return b.([]byte), nil
|
||||
return url, b.([]byte), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
if r.debug {
|
||||
log.Println(msg)
|
||||
}
|
||||
return nil, errors.New(msg)
|
||||
return url, nil, errors.New(msg)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, newNotFoundError(errors.New(msg))
|
||||
return url, nil, newNotFoundError(errors.New(msg))
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
|
@ -96,7 +96,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, fmt.Errorf(msg)
|
||||
return url, nil, fmt.Errorf(msg)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
|
@ -105,7 +105,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, errors.New(msg)
|
||||
return url, nil, errors.New(msg)
|
||||
}
|
||||
|
||||
if r.debug {
|
||||
|
|
@ -114,9 +114,9 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
|
||||
if r.cache != nil {
|
||||
if err := r.cache.Set(resourceKind, resourceAPIVersion, k8sVersion, body); err != nil {
|
||||
return nil, fmt.Errorf("failed writing schema to cache: %s", err)
|
||||
return url, nil, fmt.Errorf("failed writing schema to cache: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return body, nil
|
||||
return url, body, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ func TestDownloadSchema(t *testing.T) {
|
|||
strict: testCase.strict,
|
||||
}
|
||||
|
||||
res, err := reg.DownloadSchema(testCase.resourceKind, testCase.resourceAPIVersion, testCase.k8sversion)
|
||||
_, res, err := reg.DownloadSchema(testCase.resourceKind, testCase.resourceAPIVersion, testCase.k8sversion)
|
||||
if err == nil || testCase.expectErr == nil {
|
||||
if err != testCase.expectErr {
|
||||
t.Errorf("during test '%s': expected error, got:\n%s\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
|
|
|
|||
|
|
@ -24,10 +24,10 @@ func newLocalRegistry(pathTemplate string, strict bool, debug bool) (*LocalRegis
|
|||
}
|
||||
|
||||
// DownloadSchema retrieves the schema from a file for the resource
|
||||
func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) {
|
||||
schemaFile, err := schemaPath(r.pathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
|
||||
if err != nil {
|
||||
return []byte{}, nil
|
||||
return schemaFile, []byte{}, nil
|
||||
}
|
||||
f, err := os.Open(schemaFile)
|
||||
if err != nil {
|
||||
|
|
@ -36,14 +36,14 @@ func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersi
|
|||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, newNotFoundError(errors.New(msg))
|
||||
return schemaFile, nil, newNotFoundError(errors.New(msg))
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("failed to open schema at %s: %s", schemaFile, err)
|
||||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, errors.New(msg)
|
||||
return schemaFile, nil, errors.New(msg)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
|
@ -53,11 +53,11 @@ func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersi
|
|||
if r.debug {
|
||||
log.Print(msg)
|
||||
}
|
||||
return nil, err
|
||||
return schemaFile, nil, err
|
||||
}
|
||||
|
||||
if r.debug {
|
||||
log.Printf("using schema found at %s", schemaFile)
|
||||
}
|
||||
return content, nil
|
||||
return schemaFile, content, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ type Manifest struct {
|
|||
|
||||
// Registry is an interface that should be implemented by any source of Kubernetes schemas
|
||||
type Registry interface {
|
||||
DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error)
|
||||
DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error)
|
||||
}
|
||||
|
||||
// Retryable indicates whether an error is a temporary or a permanent failure
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue