Ejemplo n.º 1
0
func ValidateSchema(data []byte, schema validation.Schema) error {
	if schema == nil {
		return nil
	}
	data, err := yaml.ToJSON(data)
	if err != nil {
		return fmt.Errorf("error converting to YAML: %v", err)
	}
	if err := schema.ValidateBytes(data); err != nil {
		return fmt.Errorf("error validating data: %v", err)
	}
	return nil
}
Ejemplo n.º 2
0
// ResourceFromFile retrieves the name and namespace from a valid file. If the file does not
// resolve to a known type an error is returned. The returned mapping can be used to determine
// the correct REST endpoint to modify this resource with.
// DEPRECATED: Use resource.Builder
func ResourceFromFile(filename string, typer runtime.ObjectTyper, mapper meta.RESTMapper, schema validation.Schema, cmdVersion string) (mapping *meta.RESTMapping, namespace, name string, data []byte, err error) {
	data, err = ReadConfigData(filename)
	if err != nil {
		return
	}

	objVersion, kind, err := typer.DataVersionAndKind(data)
	if err != nil {
		return
	}

	// TODO: allow unversioned objects?
	if len(objVersion) == 0 {
		err = fmt.Errorf("the resource in the provided file has no apiVersion defined")
	}

	err = schema.ValidateBytes(data)
	if err != nil {
		return
	}

	// decode using the version stored with the object (allows codec to vary across versions)
	mapping, err = mapper.RESTMapping(kind, objVersion)
	if err != nil {
		return
	}

	obj, err := mapping.Codec.Decode(data)
	if err != nil {
		return
	}

	meta := mapping.MetadataAccessor
	namespace, err = meta.Namespace(obj)
	if err != nil {
		return
	}
	name, err = meta.Name(obj)
	if err != nil {
		return
	}

	// if the preferred API version differs, get a different mapper
	if cmdVersion != objVersion {
		mapping, err = mapper.RESTMapping(kind, cmdVersion)
	}
	return
}
Ejemplo n.º 3
0
// ResourceFromFile retrieves the name and namespace from a valid file. If the file does not
// resolve to a known type an error is returned. The returned mapping can be used to determine
// the correct REST endpoint to modify this resource with.
func ResourceFromFile(cmd *cobra.Command, filename string, typer runtime.ObjectTyper, mapper meta.RESTMapper, schema validation.Schema) (mapping *meta.RESTMapping, namespace, name string, data []byte) {
	configData, err := ReadConfigData(filename)
	checkErr(err)
	data = configData

	objVersion, kind, err := typer.DataVersionAndKind(data)
	checkErr(err)

	// TODO: allow unversioned objects?
	if len(objVersion) == 0 {
		checkErr(fmt.Errorf("the resource in the provided file has no apiVersion defined"))
	}

	err = schema.ValidateBytes(data)
	checkErr(err)

	// decode using the version stored with the object (allows codec to vary across versions)
	mapping, err = mapper.RESTMapping(kind, objVersion)
	checkErr(err)

	obj, err := mapping.Codec.Decode(data)
	checkErr(err)

	meta := mapping.MetadataAccessor
	namespace, err = meta.Namespace(obj)
	checkErr(err)
	name, err = meta.Name(obj)
	checkErr(err)

	// if the preferred API version differs, get a different mapper
	version := GetFlagString(cmd, "api-version")
	if version != objVersion {
		mapping, err = mapper.RESTMapping(kind, version)
		checkErr(err)
	}

	return
}