CDI: update go.mod and vendor deps
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
This commit is contained in:
27
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/Makefile
generated
vendored
Normal file
27
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/Makefile
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
VALIDATE ?= ../bin/validate
|
||||
SCHEMA ?= schema.json
|
||||
|
||||
test:
|
||||
@FMT_RED=$$(tput setaf 1); \
|
||||
FMT_BLUE=$$(tput setaf 12); \
|
||||
FMT_CLEAR=$$(tput sgr0); \
|
||||
echo "Running Good Tests"; \
|
||||
for FILE in $$(ls "testdata/good"); do \
|
||||
FILE_PATH="testdata/good/$${FILE}"; \
|
||||
if $(VALIDATE) --schema "$(SCHEMA)" "$${FILE_PATH}" > /dev/null ; then \
|
||||
printf '%s[OK]%s %s\n' "$${FMT_BLUE}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
|
||||
else \
|
||||
printf '%s[KO]%s %s\n' "$${FMT_RED}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
|
||||
exit 1; \
|
||||
fi \
|
||||
done; \
|
||||
echo "Running Bad Tests"; \
|
||||
for FILE in $$(ls "testdata/bad"); do \
|
||||
FILE_PATH="testdata/bad/$${FILE}"; \
|
||||
if $(VALIDATE) --schema "$(SCHEMA)" "$${FILE_PATH}" > /dev/null ; then \
|
||||
printf '%s[KO]%s %s\n' "$${FMT_RED}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
|
||||
exit 1; \
|
||||
else \
|
||||
printf '%s[OK]%s %s\n' "$${FMT_BLUE}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
|
||||
fi \
|
||||
done
|
||||
127
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/defs.json
generated
vendored
Normal file
127
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/defs.json
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
{
|
||||
"description": "Definitions used throughout the Container Device Interface Specification",
|
||||
"definitions": {
|
||||
"uint32": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"maximum": 4294967295
|
||||
},
|
||||
"int64": {
|
||||
"type": "integer",
|
||||
"minimum": -9223372036854775808,
|
||||
"maximum": 9223372036854775807
|
||||
},
|
||||
"ArrayOfStrings": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"FilePath": {
|
||||
"type": "string"
|
||||
},
|
||||
"Env": {
|
||||
"$ref": "#/definitions/ArrayOfStrings"
|
||||
},
|
||||
"DeviceNode": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"$ref": "#/definitions/FilePath"
|
||||
},
|
||||
"permissions": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"major": {
|
||||
"$ref": "#/definitions/int64"
|
||||
},
|
||||
"minor": {
|
||||
"$ref": "#/definitions/int64"
|
||||
},
|
||||
"uid": {
|
||||
"$ref": "#/definitions/uint32"
|
||||
},
|
||||
"gid": {
|
||||
"$ref": "#/definitions/uint32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
]
|
||||
},
|
||||
"Mount": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"hostPath": {
|
||||
"$ref": "#/definitions/FilePath"
|
||||
},
|
||||
"containerPath": {
|
||||
"$ref": "#/definitions/FilePath"
|
||||
},
|
||||
"options": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"hostPath",
|
||||
"containerPath"
|
||||
]
|
||||
},
|
||||
"Hook": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"hookName": {
|
||||
"type": "string"
|
||||
},
|
||||
"path": {
|
||||
"$ref": "#/definitions/FilePath"
|
||||
},
|
||||
"args": {
|
||||
"$ref": "#/definitions/ArrayOfStrings"
|
||||
},
|
||||
"env": {
|
||||
"$ref": "#/definitions/ArrayOfStrings"
|
||||
},
|
||||
"timeout": {
|
||||
"$ref": "#/definitions/uint32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"hookName",
|
||||
"path"
|
||||
]
|
||||
},
|
||||
"containerEdits": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"env": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"ref": "#definitions/Env"
|
||||
}
|
||||
},
|
||||
"deviceNodes": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/DeviceNode"
|
||||
}
|
||||
},
|
||||
"mounts": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Mount"
|
||||
}
|
||||
},
|
||||
"hooks": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Hook"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
253
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/schema.go
generated
vendored
Normal file
253
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/schema.go
generated
vendored
Normal file
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
Copyright © 2022 The CDI Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/pkg/errors"
|
||||
schema "github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
const (
|
||||
// BuiltinSchemaName references the builtin schema for Load()/Set().
|
||||
BuiltinSchemaName = "builtin"
|
||||
// NoneSchemaName references a disabled/NOP schema for Load()/Set().
|
||||
NoneSchemaName = "none"
|
||||
// DefaultSchemaName is the none schema.
|
||||
DefaultSchemaName = NoneSchemaName
|
||||
|
||||
// builtinSchemaFile is the builtin schema URI in our embedded FS.
|
||||
builtinSchemaFile = "file:///schema.json"
|
||||
)
|
||||
|
||||
// Schema is a JSON validation schema.
|
||||
type Schema struct {
|
||||
schema *schema.Schema
|
||||
}
|
||||
|
||||
// Error wraps a JSON validation result.
|
||||
type Error struct {
|
||||
Result *schema.Result
|
||||
}
|
||||
|
||||
// Set sets the default validating JSON schema.
|
||||
func Set(s *Schema) {
|
||||
current = s
|
||||
}
|
||||
|
||||
// Get returns the active validating JSON schema.
|
||||
func Get() *Schema {
|
||||
return current
|
||||
}
|
||||
|
||||
// BuiltinSchema returns the builtin validating JSON Schema.
|
||||
func BuiltinSchema() *Schema {
|
||||
return builtin
|
||||
}
|
||||
|
||||
// NopSchema returns an validating JSON Schema that does no real validation.
|
||||
func NopSchema() *Schema {
|
||||
return &Schema{}
|
||||
}
|
||||
|
||||
// ReadAndValidate all data from the given reader, using the active schema for validation.
|
||||
func ReadAndValidate(r io.Reader) ([]byte, error) {
|
||||
return current.ReadAndValidate(r)
|
||||
}
|
||||
|
||||
// Validate validates the data read from an io.Reader against the active schema.
|
||||
func Validate(r io.Reader) error {
|
||||
return current.Validate(r)
|
||||
}
|
||||
|
||||
// ValidateData validates the given JSON document against the active schema.
|
||||
func ValidateData(data []byte) error {
|
||||
return current.ValidateData(data)
|
||||
}
|
||||
|
||||
// ValidateFile validates the given JSON file against the active schema.
|
||||
func ValidateFile(path string) error {
|
||||
return current.ValidateFile(path)
|
||||
}
|
||||
|
||||
// ValidateType validates a go object against the schema.
|
||||
func ValidateType(obj interface{}) error {
|
||||
return current.ValidateType(obj)
|
||||
}
|
||||
|
||||
// Load the given JSON Schema.
|
||||
func Load(source string) (*Schema, error) {
|
||||
var (
|
||||
loader schema.JSONLoader
|
||||
err error
|
||||
s *schema.Schema
|
||||
)
|
||||
|
||||
source = strings.TrimSpace(source)
|
||||
|
||||
switch {
|
||||
case source == BuiltinSchemaName:
|
||||
return BuiltinSchema(), nil
|
||||
case source == NoneSchemaName, source == "":
|
||||
return NopSchema(), nil
|
||||
case strings.HasPrefix(source, "file://"):
|
||||
case strings.HasPrefix(source, "http://"):
|
||||
case strings.HasPrefix(source, "https://"):
|
||||
default:
|
||||
if strings.Index(source, "://") < 0 {
|
||||
source, err = filepath.Abs(source)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err,
|
||||
"failed to get JSON schema absolute path for %s", source)
|
||||
}
|
||||
source = "file://" + source
|
||||
}
|
||||
}
|
||||
|
||||
loader = schema.NewReferenceLoader(source)
|
||||
|
||||
s, err = schema.NewSchema(loader)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to load JSON schema")
|
||||
}
|
||||
|
||||
return &Schema{schema: s}, nil
|
||||
}
|
||||
|
||||
// ReadAndValidate all data from the given reader, using the schema for validation.
|
||||
func (s *Schema) ReadAndValidate(r io.Reader) ([]byte, error) {
|
||||
loader, reader := schema.NewReaderLoader(r)
|
||||
data, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to read data for validation")
|
||||
}
|
||||
return data, s.validate(loader)
|
||||
}
|
||||
|
||||
// Validate validates the data read from an io.Reader against the schema.
|
||||
func (s *Schema) Validate(r io.Reader) error {
|
||||
_, err := s.ReadAndValidate(r)
|
||||
return err
|
||||
}
|
||||
|
||||
// ValidateData validates the given JSON data against the schema.
|
||||
func (s *Schema) ValidateData(data []byte) error {
|
||||
var (
|
||||
any interface{}
|
||||
err error
|
||||
)
|
||||
|
||||
if !bytes.HasPrefix(bytes.TrimSpace(data), []byte{'{'}) {
|
||||
err = yaml.Unmarshal(data, &any)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to YAML unmarshal data for validation")
|
||||
}
|
||||
data, err = json.Marshal(any)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to JSON remarshal data for validation")
|
||||
}
|
||||
}
|
||||
|
||||
return s.validate(schema.NewBytesLoader(data))
|
||||
}
|
||||
|
||||
// ValidateFile validates the given JSON file against the schema.
|
||||
func (s *Schema) ValidateFile(path string) error {
|
||||
if filepath.Ext(path) == ".json" {
|
||||
return s.validate(schema.NewReferenceLoader("file://" + path))
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.ValidateData(data)
|
||||
}
|
||||
|
||||
// ValidateType validates a go object against the schema.
|
||||
func (s *Schema) ValidateType(obj interface{}) error {
|
||||
l := schema.NewGoLoader(obj)
|
||||
return s.validate(l)
|
||||
}
|
||||
|
||||
// Validate the (to be) loaded doc against the schema.
|
||||
func (s *Schema) validate(doc schema.JSONLoader) error {
|
||||
if s == nil || s.schema == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
docErr, jsonErr := s.schema.Validate(doc)
|
||||
if jsonErr != nil {
|
||||
return errors.Wrap(jsonErr, "failed to load JSON data for validation")
|
||||
}
|
||||
if docErr.Valid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Error{Result: docErr}
|
||||
}
|
||||
|
||||
// Error returns the given Result's error as a multierror(.Error()).
|
||||
func (e *Error) Error() string {
|
||||
if e == nil || e.Result == nil || e.Result.Valid() {
|
||||
return ""
|
||||
}
|
||||
|
||||
var multi error
|
||||
for _, err := range e.Result.Errors() {
|
||||
multi = multierror.Append(multi, errors.Errorf("%v", err))
|
||||
}
|
||||
return strings.TrimRight(multi.Error(), "\n")
|
||||
}
|
||||
|
||||
var (
|
||||
// our builtin schema
|
||||
builtin *Schema
|
||||
// currently loaded schema, builtin by default
|
||||
current *Schema
|
||||
)
|
||||
|
||||
//go:embed *.json
|
||||
var builtinFS embed.FS
|
||||
|
||||
func init() {
|
||||
s, err := schema.NewSchema(
|
||||
schema.NewReferenceLoaderFileSystem(
|
||||
builtinSchemaFile,
|
||||
http.FS(builtinFS),
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
builtin = NopSchema()
|
||||
} else {
|
||||
builtin = &Schema{schema: s}
|
||||
}
|
||||
|
||||
current = builtin
|
||||
}
|
||||
39
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/schema.json
generated
vendored
Normal file
39
vendor/github.com/container-orchestrated-devices/container-device-interface/schema/schema.json
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Configuration Schema for the Container Device Interface",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"cdiVersion": {
|
||||
"description": "The version of the Container Device Interface Specification that the document complies with",
|
||||
"type": "string"
|
||||
},
|
||||
"kind": {
|
||||
"description": "The kind of the device usually of the form 'vendor.com/device'",
|
||||
"type": "string"
|
||||
},
|
||||
"devices": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"description": "The name of the device",
|
||||
"type": "string"
|
||||
},
|
||||
"containerEdits": {
|
||||
"$ref": "defs.json#/definitions/containerEdits"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"name",
|
||||
"containerEdits"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"cdiVersion",
|
||||
"kind",
|
||||
"devices"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user