runtime

package
v0.20.0-alpha2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2016 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Defines conversions between generic types and structs to map query strings to struct objects.

Package runtime includes helper functions for working with API objects that follow the kubernetes API object conventions, which are:

  1. Your API objects have a common metadata struct member, TypeMeta.
  2. Your code refers to an internal set of API objects.
  3. In a separate package, you have an external set of API objects.
  4. The external set is considered to be versioned, and no breaking changes are ever made to it (fields may be added but not changed or removed).
  5. As your api evolves, you'll make an additional versioned package with every major change.
  6. Versioned packages have conversion functions which convert to and from the internal version.
  7. You'll continue to support older versions according to your deprecation policy, and you can easily provide a program/library to update old versions into new versions because of 5.
  8. All of your serializations and deserializations are handled in a centralized place.

Package runtime provides a conversion helper to make 5 easy, and the Encode/Decode/DecodeInto trio to accomplish 7. You can also register additional "codecs" which use a version of your choice. It's recommended that you register your types with runtime in your package's init function.

As a bonus, a few common types useful from all api objects and versions are provided in types.go.

Index

Constants

This section is empty.

Variables

View Source
var DefaultStringConversions = []interface{}{
	convertStringSliceToString,
	convertStringSliceToInt,
	convertStringSliceToBool,
	convertStringSliceToInt64,
}

DefaultStringConversions are helpers for converting []string and string to real values.

Functions

func DecodeList

func DecodeList(objects []Object, decoders ...ObjectDecoder) []error

DecodeList alters the list in place, attempting to decode any objects found in the list that have the runtime.Unknown type. Any errors that occur are returned after the entire list is processed. Decoders are tried in order.

func EncodeOrDie

func EncodeOrDie(codec Codec, obj Object) string

EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.

func FieldPtr

func FieldPtr(v reflect.Value, fieldName string, dest interface{}) error

fieldPtr puts the address of fieldName, which must be a member of v, into dest, which must be an address of a variable to which this field's address can be assigned.

func IsMissingKind

func IsMissingKind(err error) bool

IsMissingKind returns true if the error indicates that the provided object is missing a 'Kind' field.

func IsMissingVersion

func IsMissingVersion(err error) bool

IsMissingVersion returns true if the error indicates that the provided object is missing a 'Versioj' field.

func IsNotRegisteredError

func IsNotRegisteredError(err error) bool

IsNotRegisteredError returns true if the error indicates the provided object or input data is not registered.

func JSONKeyMapper

func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string)

JSONKeyMapper uses the struct tags on a conversion to determine the key value for the other side. Use when mapping from a map[string]* to a struct or vice versa.

func VerifySwaggerDocsExist added in v0.18.4

func VerifySwaggerDocsExist(kubeTypes []KubeTypes, w io.Writer) (int, error)

VerifySwaggerDocsExist writes in a io.Writer a list of structs and fields that are missing of documentation.

func WriteSwaggerDocFunc added in v0.18.4

func WriteSwaggerDocFunc(kubeTypes []KubeTypes, w io.Writer) error

WriteSwaggerDocFunc writes a declaration of a function as a string. This function is used in Swagger as a documentation source for structs and theirs fields

Types

type Codec

type Codec interface {
	Decoder
	Encoder
}

Codec defines methods for serializing and deserializing API objects.

func CodecFor

func CodecFor(codec ObjectCodec, version string) Codec

CodecFor returns a Codec that invokes Encode with the provided version.

func YAMLDecoder

func YAMLDecoder(codec Codec) Codec

YAMLDecoder adds YAML decoding support to a codec that supports JSON.

type ConversionGenerator

type ConversionGenerator interface {
	GenerateConversionsForType(version string, reflection reflect.Type) error
	WriteConversionFunctions(w io.Writer) error
	RegisterConversionFunctions(w io.Writer, pkg string) error
	AddImport(pkg string) string
	RepackImports(exclude sets.String)
	WriteImports(w io.Writer) error
	OverwritePackage(pkg, overwrite string)
	AssumePrivateConversions()
}

func NewConversionGenerator

func NewConversionGenerator(scheme *conversion.Scheme, targetPkg string) ConversionGenerator

type Decoder

type Decoder interface {
	Decode(data []byte) (Object, error)
	// TODO: Remove this method?
	DecodeToVersion(data []byte, version string) (Object, error)
	DecodeInto(data []byte, obj Object) error
	// TODO: Remove this method?
	DecodeIntoWithSpecifiedVersionKind(data []byte, obj Object, kind, version string) error

	DecodeParametersInto(parameters url.Values, obj Object) error
}

Decoder defines methods for deserializing API objects into a given type

type DeepCopyGenerator

type DeepCopyGenerator interface {
	// Adds a type to a generator.
	// If the type is non-struct, it will return an error, otherwise deep-copy
	// functions for this type and all nested types will be generated.
	AddType(inType reflect.Type) error

	// ReplaceType registers a type that should be used instead of the type
	// with the provided pkgPath and name.
	ReplaceType(pkgPath, name string, in interface{})

	// AddImport registers a package name with the generator and returns its
	// short name.
	AddImport(pkgPath string) string

	// RepackImports creates a stable ordering of import short names
	RepackImports()

	// Writes all imports that are necessary for deep-copy function and
	// their registration.
	WriteImports(w io.Writer) error

	// Writes deel-copy functions for all types added via AddType() method
	// and their nested types.
	WriteDeepCopyFunctions(w io.Writer) error

	// Writes an init() function that registers all the generated deep-copy
	// functions.
	RegisterDeepCopyFunctions(w io.Writer, pkg string) error

	// When generating code, all references to "pkg" package name will be
	// replaced with "overwrite". It is used mainly to replace references
	// to name of the package in which the code will be created with empty
	// string.
	OverwritePackage(pkg, overwrite string)
}

TODO(wojtek-t): As suggested in #8320, we should consider the strategy to first do the shallow copy and then recurse into things that need a deep copy (maps, pointers, slices). That sort of copy function would need one parameter - a pointer to the thing it's supposed to expand, and it would involve a lot less memory copying.

func NewDeepCopyGenerator

func NewDeepCopyGenerator(scheme *conversion.Scheme, targetPkg string, include sets.String) DeepCopyGenerator

type EmbeddedObject

type EmbeddedObject struct {
	Object
}

EmbeddedObject has appropriate encoder and decoder functions, such that on the wire, it's stored as a []byte, but in memory, the contained object is accessible as an Object via the Get() function. Only valid API objects may be stored via EmbeddedObject. The purpose of this is to allow an API object of type known only at runtime to be embedded within other API objects.

Note that object assumes that you've registered all of your api types with the api package.

EmbeddedObject and RawExtension can be used together to allow for API object extensions: see the comment for RawExtension.

type Encoder

type Encoder interface {
	Encode(obj Object) (data []byte, err error)
	EncodeToStream(obj Object, stream io.Writer) error
}

Encoder defines methods for serializing API objects into bytes

type FieldLabelConversionFunc

type FieldLabelConversionFunc func(label, value string) (internalLabel, internalValue string, err error)

Function to convert a field selector to internal representation.

type KubeTypes added in v0.18.4

type KubeTypes []Pair

KubeTypes is an array to represent all available types in a parsed file. [0] is for the type itself

func ParseDocumentationFrom added in v0.18.4

func ParseDocumentationFrom(src string) []KubeTypes

ParseDocumentationFrom gets all types' documentation and returns them as an array. Each type is again represented as an array (we have to use arrays as we need to be sure for the order of the fields). This function returns fields and struct definitions that have no documentation as {name, ""}.

type MultiObjectTyper

type MultiObjectTyper []ObjectTyper

MultiObjectTyper returns the types of objects across multiple schemes in order.

func (MultiObjectTyper) DataVersionAndKind

func (m MultiObjectTyper) DataVersionAndKind(data []byte) (version, kind string, err error)

func (MultiObjectTyper) ObjectVersionAndKind

func (m MultiObjectTyper) ObjectVersionAndKind(obj Object) (version, kind string, err error)

func (MultiObjectTyper) Recognizes

func (m MultiObjectTyper) Recognizes(version, kind string) bool

type Object

type Object interface {
	// This function is used only to enforce membership. It's never called.
	// TODO: Consider mass rename in the future to make it do something useful.
	IsAnAPIObject()
}

All api types must support the Object interface. It's deliberately tiny so that this is not an onerous burden. Implement it with a pointer receiver; this will allow us to use the go compiler to check the one thing about our objects that it's capable of checking for us.

type ObjectCodec

type ObjectCodec interface {
	Decoder

	// EncodeToVersion convert and serializes an object in the internal format
	// to a specified output version. An error is returned if the object
	// cannot be converted for any reason.
	EncodeToVersion(obj Object, outVersion string) ([]byte, error)
	EncodeToVersionStream(obj Object, outVersion string, stream io.Writer) error
}

ObjectCodec represents the common mechanisms for converting to and from a particular binary representation of an object. TODO: Remove this interface - it is used only in CodecFor() method.

type ObjectConvertor

type ObjectConvertor interface {
	Convert(in, out interface{}) error
	ConvertToVersion(in Object, outVersion string) (out Object, err error)
	ConvertFieldLabel(version, kind, label, value string) (string, string, error)
}

ObjectConvertor converts an object to a different version.

type ObjectCopier

type ObjectCopier interface {
	// Copy returns an exact copy of the provided Object, or an error if the
	// copy could not be completed.
	Copy(Object) (Object, error)
}

ObjectCopier duplicates an object.

type ObjectCreater

type ObjectCreater interface {
	New(version, kind string) (out Object, err error)
}

ObjectCreater contains methods for instantiating an object by kind and version.

type ObjectDecoder

type ObjectDecoder interface {
	Decoder
	// DataVersionAndKind returns the version and kind of the provided data, or an error
	// if another problem is detected. In many cases this method can be as expensive to
	// invoke as the Decode method.
	DataVersionAndKind([]byte) (version, kind string, err error)
	// Recognizes returns true if the scheme is able to handle the provided version and kind
	// of an object.
	Recognizes(version, kind string) bool
}

ObjectDecoder is a convenience interface for identifying serialized versions of objects and transforming them into Objects. It intentionally overlaps with ObjectTyper and Decoder for use in decode only paths. TODO: Consider removing this interface?

var UnstructuredJSONScheme ObjectDecoder = unstructuredJSONScheme{}

UnstructuredJSONScheme is capable of converting JSON data into the Unstructured type, which can be used for generic access to objects without a predefined scheme.

type ObjectTyper

type ObjectTyper interface {
	// DataVersionAndKind returns the version and kind of the provided data, or an error
	// if another problem is detected. In many cases this method can be as expensive to
	// invoke as the Decode method.
	DataVersionAndKind([]byte) (version, kind string, err error)
	// ObjectVersionAndKind returns the version and kind of the provided object, or an
	// error if the object is not recognized (IsNotRegisteredError will return true).
	ObjectVersionAndKind(Object) (version, kind string, err error)
	// Recognizes returns true if the scheme is able to handle the provided version and kind,
	// or more precisely that the provided version is a possible conversion or decoding
	// target.
	Recognizes(version, kind string) bool
}

ObjectTyper contains methods for extracting the APIVersion and Kind of objects.

type Pair added in v0.18.4

type Pair struct {
	Name, Doc string
}

Pair of strings. We keed the name of fields and the doc

type PluginBase

type PluginBase struct {
	Kind string `json:"kind,omitempty"`
}

PluginBase is like TypeMeta, but it's intended for plugin objects that won't ever be encoded except while embedded in other objects.

type RawExtension

type RawExtension struct {
	RawJSON []byte
}

RawExtension is used with EmbeddedObject to do a two-phase encoding of extension objects.

To use this, make a field which has RawExtension as its type in your external, versioned struct, and EmbeddedObject in your internal struct. You also need to register your various plugin types.

// Internal package:

type MyAPIObject struct {
	runtime.TypeMeta `json:",inline"`
	MyPlugin runtime.EmbeddedObject `json:"myPlugin"`
}
type PluginA struct {
	runtime.PluginBase `json:",inline"`
	AOption string `json:"aOption"`
}

// External package:

type MyAPIObject struct {
	runtime.TypeMeta `json:",inline"`
	MyPlugin runtime.RawExtension `json:"myPlugin"`
}
type PluginA struct {
	runtime.PluginBase `json:",inline"`
	AOption string `json:"aOption"`
}

// On the wire, the JSON will look something like this:

{
	"kind":"MyAPIObject",
	"apiVersion":"v1",
	"myPlugin": {
		"kind":"PluginA",
		"aOption":"foo",
	},
}

So what happens? Decode first uses json or yaml to unmarshal the serialized data into your external MyAPIObject. That causes the raw JSON to be stored, but not unpacked. The next step is to copy (using pkg/conversion) into the internal struct. The runtime package's DefaultScheme has conversion functions installed which will unpack the JSON stored in RawExtension, turning it into the correct object type, and storing it in the EmbeddedObject. (TODO: In the case where the object is of an unknown type, a runtime.Unknown object will be created and stored.)

func (RawExtension) MarshalJSON

func (re RawExtension) MarshalJSON() ([]byte, error)

Marshal may get called on pointers or values, so implement MarshalJSON on value. http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go

func (*RawExtension) UnmarshalJSON

func (re *RawExtension) UnmarshalJSON(in []byte) error

type ResourceVersioner

type ResourceVersioner interface {
	SetResourceVersion(obj Object, version string) error
	ResourceVersion(obj Object) (string, error)
}

ResourceVersioner provides methods for setting and retrieving the resource version from an API object.

type Scheme

type Scheme struct {
	// contains filtered or unexported fields
}

Scheme defines methods for serializing and deserializing API objects. It is an adaptation of conversion's Scheme for our API objects.

func NewScheme

func NewScheme() *Scheme

NewScheme creates a new Scheme. This scheme is pluggable by default.

func (*Scheme) AddConversionFuncs

func (s *Scheme) AddConversionFuncs(conversionFuncs ...interface{}) error

AddConversionFuncs adds a function to the list of conversion functions. The given function should know how to convert between two API objects. We deduce how to call it from the types of its two parameters; see the comment for Converter.RegisterConversionFunction.

Note that, if you need to copy sub-objects that didn't change, it's safe to call Convert() inside your conversionFuncs, as long as you don't start a conversion chain that's infinitely recursive.

Also note that the default behavior, if you don't add a conversion function, is to sanely copy fields that have the same names. It's OK if the destination type has extra fields, but it must not remove any. So you only need to add a conversion function for things with changed/removed fields.

func (*Scheme) AddDeepCopyFuncs

func (s *Scheme) AddDeepCopyFuncs(deepCopyFuncs ...interface{}) error

AddDeepCopyFuncs adds a function to the list of deep-copy functions. For the expected format of deep-copy function, see the comment for Copier.RegisterDeepCopyFunction.

func (*Scheme) AddDefaultingFuncs

func (s *Scheme) AddDefaultingFuncs(defaultingFuncs ...interface{}) error

AddDefaultingFuncs adds a function to the list of value-defaulting functions. We deduce how to call it from the types of its two parameters; see the comment for Converter.RegisterDefaultingFunction.

func (*Scheme) AddFieldLabelConversionFunc

func (s *Scheme) AddFieldLabelConversionFunc(version, kind string, conversionFunc FieldLabelConversionFunc) error

AddFieldLabelConversionFunc adds a conversion function to convert field selectors of the given kind from the given version to internal version representation.

func (*Scheme) AddGeneratedConversionFuncs

func (s *Scheme) AddGeneratedConversionFuncs(conversionFuncs ...interface{}) error

Similar to AddConversionFuncs, but registers conversion functions that were automatically generated.

func (*Scheme) AddGeneratedDeepCopyFuncs

func (s *Scheme) AddGeneratedDeepCopyFuncs(deepCopyFuncs ...interface{}) error

Similar to AddDeepCopyFuncs, but registers deep-copy functions that were automatically generated.

func (*Scheme) AddKnownTypeWithName

func (s *Scheme) AddKnownTypeWithName(version, kind string, obj Object)

AddKnownTypeWithName is like AddKnownTypes, but it lets you specify what this type should be encoded as. Useful for testing when you don't want to make multiple packages to define your structs.

func (*Scheme) AddKnownTypes

func (s *Scheme) AddKnownTypes(version string, types ...Object)

AddKnownTypes registers the types of the arguments to the marshaller of the package api. Encode() refuses the object unless its type is registered with AddKnownTypes.

func (*Scheme) AddStructFieldConversion

func (s *Scheme) AddStructFieldConversion(srcFieldType interface{}, srcFieldName string, destFieldType interface{}, destFieldName string) error

AddStructFieldConversion allows you to specify a mechanical copy for a moved or renamed struct field without writing an entire conversion function. See the comment in conversion.Converter.SetStructFieldCopy for parameter details. Call as many times as needed, even on the same fields.

func (*Scheme) Convert

func (s *Scheme) Convert(in, out interface{}) error

Convert will attempt to convert in into out. Both must be pointers. For easy testing of conversion functions. Returns an error if the conversion isn't possible.

func (*Scheme) ConvertFieldLabel

func (s *Scheme) ConvertFieldLabel(version, kind, label, value string) (string, string, error)

Converts the given field label and value for an kind field selector from versioned representation to an unversioned one.

func (*Scheme) ConvertToVersion

func (s *Scheme) ConvertToVersion(in Object, outVersion string) (Object, error)

ConvertToVersion attempts to convert an input object to its matching Kind in another version within this scheme. Will return an error if the provided version does not contain the inKind (or a mapping by name defined with AddKnownTypeWithName). Will also return an error if the conversion does not result in a valid Object being returned.

func (*Scheme) Copy

func (s *Scheme) Copy(src Object) (Object, error)

Copy does a deep copy of an API object. Useful mostly for tests.

func (*Scheme) CopyOrDie

func (s *Scheme) CopyOrDie(obj Object) Object

func (*Scheme) DataVersionAndKind

func (s *Scheme) DataVersionAndKind(data []byte) (version, kind string, err error)

DataVersionAndKind will return the APIVersion and Kind of the given wire-format encoding of an API Object, or an error.

func (*Scheme) Decode

func (s *Scheme) Decode(data []byte) (Object, error)

Decode converts a YAML or JSON string back into a pointer to an api object. Deduces the type based upon the APIVersion and Kind fields, which are set by Encode. Only versioned objects (APIVersion != "") are accepted. The object will be converted into the in-memory unversioned type before being returned.

func (*Scheme) DecodeInto

func (s *Scheme) DecodeInto(data []byte, obj Object) error

DecodeInto parses a YAML or JSON string and stores it in obj. Returns an error if data.Kind is set and doesn't match the type of obj. Obj should be a pointer to an api type. If obj's APIVersion doesn't match that in data, an attempt will be made to convert data into obj's version. TODO: allow Decode/DecodeInto to take a default apiVersion and a default kind, to be applied if the provided object does not have either field (integrate external apis into the decoding scheme).

func (*Scheme) DecodeIntoWithSpecifiedVersionKind added in v0.18.4

func (s *Scheme) DecodeIntoWithSpecifiedVersionKind(data []byte, obj Object, version, kind string) error

func (*Scheme) DecodeParametersInto

func (s *Scheme) DecodeParametersInto(parameters url.Values, obj Object) error

func (*Scheme) DecodeToVersion added in v0.18.4

func (s *Scheme) DecodeToVersion(data []byte, version string) (Object, error)

DecodeToVersion converts a YAML or JSON string back into a pointer to an api object. Deduces the type based upon the APIVersion and Kind fields, which are set by Encode. Only versioned objects (APIVersion != "") are accepted. The object will be converted into the in-memory versioned type requested before being returned.

func (*Scheme) DeepCopy

func (s *Scheme) DeepCopy(src interface{}) (interface{}, error)

Performs a deep copy of the given object.

func (*Scheme) EncodeToVersion

func (s *Scheme) EncodeToVersion(obj Object, destVersion string) (data []byte, err error)

EncodeToVersion turns the given api object into an appropriate JSON string. Will return an error if the object doesn't have an embedded TypeMeta. Obj may be a pointer to a struct, or a struct. If a struct, a copy must be made. If a pointer, the object may be modified before encoding, but will be put back into its original state before returning.

Memory/wire format differences:

  • Having to keep track of the Kind and APIVersion fields makes tests very annoying, so the rule is that they are set only in wire format (json), not when in native (memory) format. This is possible because both pieces of information are implicit in the go typed object.
  • An exception: note that, if there are embedded API objects of known type, for example, PodList{... Items []Pod ...}, these embedded objects must be of the same version of the object they are embedded within, and their APIVersion and Kind must both be empty.
  • Note that the exception does not apply to the APIObject type, which recursively does Encode()/Decode(), and is capable of expressing any API object.
  • Only versioned objects should be encoded. This means that, if you pass a native object, Encode will convert it to a versioned object. For example, an api.Pod will get converted to a v1.Pod. However, if you pass in an object that's already versioned (v1.Pod), Encode will not modify it.

The purpose of the above complex conversion behavior is to allow us to change the memory format yet not break compatibility with any stored objects, whether they be in our storage layer (e.g., etcd), or in user's config files.

func (*Scheme) EncodeToVersionStream added in v0.18.4

func (s *Scheme) EncodeToVersionStream(obj Object, destVersion string, stream io.Writer) error

func (*Scheme) KnownTypes

func (s *Scheme) KnownTypes(version string) map[string]reflect.Type

KnownTypes returns the types known for the given version. Return value must be treated as read-only.

func (*Scheme) Log

func (s *Scheme) Log(l conversion.DebugLogger)

Log sets a logger on the scheme. For test purposes only

func (*Scheme) New

func (s *Scheme) New(versionName, typeName string) (Object, error)

New returns a new API object of the given version ("" for internal representation) and name, or an error if it hasn't been registered.

func (*Scheme) ObjectVersionAndKind

func (s *Scheme) ObjectVersionAndKind(obj Object) (version, kind string, err error)

ObjectVersionAndKind returns the version and kind of the given Object.

func (*Scheme) Raw

func (self *Scheme) Raw() *conversion.Scheme

func (*Scheme) Recognizes

func (s *Scheme) Recognizes(version, kind string) bool

Recognizes returns true if the scheme is able to handle the provided version and kind of an object.

type SelfLinker

type SelfLinker interface {
	SetSelfLink(obj Object, selfLink string) error
	SelfLink(obj Object) (string, error)

	// Knowing Name is sometimes necessary to use a SelfLinker.
	Name(obj Object) (string, error)
	// Knowing Namespace is sometimes necessary to use a SelfLinker
	Namespace(obj Object) (string, error)
}

SelfLinker provides methods for setting and retrieving the SelfLink field of an API object.

type TypeMeta

type TypeMeta struct {
	APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
	Kind       string `json:"kind,omitempty" yaml:"kind,omitempty"`
}

TypeMeta is shared by all top level objects. The proper way to use it is to inline it in your type, like this:

type MyAwesomeAPIObject struct {
     runtime.TypeMeta    `json:",inline"`
     ... // other fields
}

func (*MyAwesomeAPIObject) IsAnAPIObject() {}

TypeMeta is provided here for convenience. You may use it directly from this package or define your own with the same fields.

type Unknown

type Unknown struct {
	TypeMeta `json:",inline"`
	// RawJSON will hold the complete JSON of the object which couldn't be matched
	// with a registered type. Most likely, nothing should be done with this
	// except for passing it through the system.
	RawJSON []byte
}

Unknown allows api objects with unknown types to be passed-through. This can be used to deal with the API objects from a plug-in. Unknown objects still have functioning TypeMeta features-- kind, version, etc. TODO: Make this object have easy access to field based accessors and settors for metadata and field mutatation.

func (*Unknown) IsAnAPIObject

func (*Unknown) IsAnAPIObject()

type Unstructured

type Unstructured struct {
	TypeMeta `json:",inline"`
	// Object is a JSON compatible map with string, float, int, []interface{}, or map[string]interface{}
	// children.
	Object map[string]interface{}
}

Unstructured allows objects that do not have Golang structs registered to be manipulated generically. This can be used to deal with the API objects from a plug-in. Unstructured objects still have functioning TypeMeta features-- kind, version, etc. TODO: Make this object have easy access to field based accessors and settors for metadata and field mutatation.

func (*Unstructured) IsAnAPIObject

func (*Unstructured) IsAnAPIObject()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL