예제 #1
0
// CreateObjects creates bulk of resources provided by items list. Each item must
// be valid API type. It requires ObjectTyper to parse the Version and Kind and
// RESTMapper to get the resource URI and REST client that knows how to create
// given type
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientFunc, objects []runtime.Object) util.ErrorList {
	allErrors := util.ErrorList{}
	for i, obj := range objects {
		version, kind, err := typer.ObjectVersionAndKind(obj)
		if err != nil {
			allErrors = append(allErrors, fmt.Errorf("Config.item[%d] kind: %v", i, err))
			continue
		}

		mapping, err := mapper.RESTMapping(version, kind)
		if err != nil {
			allErrors = append(allErrors, fmt.Errorf("Config.item[%d] mapping: %v", i, err))
			continue
		}

		client, err := clientFor(mapping)
		if err != nil {
			allErrors = append(allErrors, fmt.Errorf("Config.item[%d] client: %v", i, err))
			continue
		}

		if err := CreateObject(client, mapping, obj); err != nil {
			allErrors = append(allErrors, fmt.Errorf("Config.item[%d]: %v", i, err))
		}
	}

	return allErrors
}
예제 #2
0
// CreateObjects creates bulk of resources provided by items list. Each item must
// be valid API type. It requires ObjectTyper to parse the Version and Kind and
// RESTMapper to get the resource URI and REST client that knows how to create
// given type
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientFunc, objects []runtime.Object) errs.ValidationErrorList {
	allErrors := errs.ValidationErrorList{}
	for i, obj := range objects {
		version, kind, err := typer.ObjectVersionAndKind(obj)
		if err != nil {
			reportError(&allErrors, i, errs.NewFieldInvalid("kind", obj))
			continue
		}

		mapping, err := mapper.RESTMapping(version, kind)
		if err != nil {
			reportError(&allErrors, i, errs.NewFieldNotSupported("mapping", err))
			continue
		}

		client, err := clientFor(mapping)
		if err != nil {
			reportError(&allErrors, i, errs.NewFieldNotSupported("client", obj))
			continue
		}

		if err := CreateObject(client, mapping, obj); err != nil {
			reportError(&allErrors, i, *err)
		}
	}

	return allErrors.Prefix("Config")
}
예제 #3
0
// transformDecodeError adds additional information when a decode fails.
func transformDecodeError(typer runtime.ObjectTyper, baseErr error, into runtime.Object, body []byte) error {
	_, kind, err := typer.ObjectVersionAndKind(into)
	if err != nil {
		return err
	}
	if version, dataKind, err := typer.DataVersionAndKind(body); err == nil && len(dataKind) > 0 {
		return errors.NewBadRequest(fmt.Sprintf("%s in version %s cannot be handled as a %s: %v", dataKind, version, kind, baseErr))
	}
	return errors.NewBadRequest(fmt.Sprintf("the object provided is unrecognized (must be of type %s): %v", kind, baseErr))
}
// objectMetaAndKind retrieves kind and ObjectMeta from a runtime object, or returns an error.
func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.ObjectMeta, string, error) {
	objectMeta, err := api.ObjectMetaFor(obj)
	if err != nil {
		return nil, "", errors.NewInternalError(err)
	}
	_, kind, err := typer.ObjectVersionAndKind(obj)
	if err != nil {
		return nil, "", errors.NewInternalError(err)
	}
	return objectMeta, kind, nil
}