blob: bded5bf1591f72dd0cef72d86ff6eef5395da4b5 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package runtime
18
19import (
20 "io"
21 "net/url"
22
23 "k8s.io/apimachinery/pkg/runtime/schema"
24)
25
26const (
27 // APIVersionInternal may be used if you are registering a type that should not
28 // be considered stable or serialized - it is a convention only and has no
29 // special behavior in this package.
30 APIVersionInternal = "__internal"
31)
32
33// GroupVersioner refines a set of possible conversion targets into a single option.
34type GroupVersioner interface {
35 // KindForGroupVersionKinds returns a desired target group version kind for the given input, or returns ok false if no
36 // target is known. In general, if the return target is not in the input list, the caller is expected to invoke
37 // Scheme.New(target) and then perform a conversion between the current Go type and the destination Go type.
38 // Sophisticated implementations may use additional information about the input kinds to pick a destination kind.
39 KindForGroupVersionKinds(kinds []schema.GroupVersionKind) (target schema.GroupVersionKind, ok bool)
40}
41
42// Encoder writes objects to a serialized form
43type Encoder interface {
44 // Encode writes an object to a stream. Implementations may return errors if the versions are
45 // incompatible, or if no conversion is defined.
46 Encode(obj Object, w io.Writer) error
47}
48
49// Decoder attempts to load an object from data.
50type Decoder interface {
51 // Decode attempts to deserialize the provided data using either the innate typing of the scheme or the
52 // default kind, group, and version provided. It returns a decoded object as well as the kind, group, and
53 // version from the serialized data, or an error. If into is non-nil, it will be used as the target type
54 // and implementations may choose to use it rather than reallocating an object. However, the object is not
55 // guaranteed to be populated. The returned object is not guaranteed to match into. If defaults are
56 // provided, they are applied to the data by default. If no defaults or partial defaults are provided, the
57 // type of the into may be used to guide conversion decisions.
58 Decode(data []byte, defaults *schema.GroupVersionKind, into Object) (Object, *schema.GroupVersionKind, error)
59}
60
61// Serializer is the core interface for transforming objects into a serialized format and back.
62// Implementations may choose to perform conversion of the object, but no assumptions should be made.
63type Serializer interface {
64 Encoder
65 Decoder
66}
67
68// Codec is a Serializer that deals with the details of versioning objects. It offers the same
69// interface as Serializer, so this is a marker to consumers that care about the version of the objects
70// they receive.
71type Codec Serializer
72
73// ParameterCodec defines methods for serializing and deserializing API objects to url.Values and
74// performing any necessary conversion. Unlike the normal Codec, query parameters are not self describing
75// and the desired version must be specified.
76type ParameterCodec interface {
77 // DecodeParameters takes the given url.Values in the specified group version and decodes them
78 // into the provided object, or returns an error.
79 DecodeParameters(parameters url.Values, from schema.GroupVersion, into Object) error
80 // EncodeParameters encodes the provided object as query parameters or returns an error.
81 EncodeParameters(obj Object, to schema.GroupVersion) (url.Values, error)
82}
83
84// Framer is a factory for creating readers and writers that obey a particular framing pattern.
85type Framer interface {
86 NewFrameReader(r io.ReadCloser) io.ReadCloser
87 NewFrameWriter(w io.Writer) io.Writer
88}
89
90// SerializerInfo contains information about a specific serialization format
91type SerializerInfo struct {
92 // MediaType is the value that represents this serializer over the wire.
93 MediaType string
David Bainbridge86971522019-09-26 22:09:39 +000094 // MediaTypeType is the first part of the MediaType ("application" in "application/json").
95 MediaTypeType string
96 // MediaTypeSubType is the second part of the MediaType ("json" in "application/json").
97 MediaTypeSubType string
Zack Williamse940c7a2019-08-21 14:25:39 -070098 // EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
99 EncodesAsText bool
100 // Serializer is the individual object serializer for this media type.
101 Serializer Serializer
102 // PrettySerializer, if set, can serialize this object in a form biased towards
103 // readability.
104 PrettySerializer Serializer
105 // StreamSerializer, if set, describes the streaming serialization format
106 // for this media type.
107 StreamSerializer *StreamSerializerInfo
108}
109
110// StreamSerializerInfo contains information about a specific stream serialization format
111type StreamSerializerInfo struct {
112 // EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
113 EncodesAsText bool
114 // Serializer is the top level object serializer for this type when streaming
115 Serializer
116 // Framer is the factory for retrieving streams that separate objects on the wire
117 Framer
118}
119
120// NegotiatedSerializer is an interface used for obtaining encoders, decoders, and serializers
121// for multiple supported media types. This would commonly be accepted by a server component
122// that performs HTTP content negotiation to accept multiple formats.
123type NegotiatedSerializer interface {
124 // SupportedMediaTypes is the media types supported for reading and writing single objects.
125 SupportedMediaTypes() []SerializerInfo
126
127 // EncoderForVersion returns an encoder that ensures objects being written to the provided
128 // serializer are in the provided group version.
129 EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
130 // DecoderForVersion returns a decoder that ensures objects being read by the provided
131 // serializer are in the provided group version by default.
132 DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
133}
134
135// StorageSerializer is an interface used for obtaining encoders, decoders, and serializers
136// that can read and write data at rest. This would commonly be used by client tools that must
137// read files, or server side storage interfaces that persist restful objects.
138type StorageSerializer interface {
139 // SupportedMediaTypes are the media types supported for reading and writing objects.
140 SupportedMediaTypes() []SerializerInfo
141
142 // UniversalDeserializer returns a Serializer that can read objects in multiple supported formats
143 // by introspecting the data at rest.
144 UniversalDeserializer() Decoder
145
146 // EncoderForVersion returns an encoder that ensures objects being written to the provided
147 // serializer are in the provided group version.
148 EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
149 // DecoderForVersion returns a decoder that ensures objects being read by the provided
150 // serializer are in the provided group version by default.
151 DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
152}
153
154// NestedObjectEncoder is an optional interface that objects may implement to be given
155// an opportunity to encode any nested Objects / RawExtensions during serialization.
156type NestedObjectEncoder interface {
157 EncodeNestedObjects(e Encoder) error
158}
159
160// NestedObjectDecoder is an optional interface that objects may implement to be given
161// an opportunity to decode any nested Objects / RawExtensions during serialization.
162type NestedObjectDecoder interface {
163 DecodeNestedObjects(d Decoder) error
164}
165
166///////////////////////////////////////////////////////////////////////////////
167// Non-codec interfaces
168
169type ObjectDefaulter interface {
170 // Default takes an object (must be a pointer) and applies any default values.
171 // Defaulters may not error.
172 Default(in Object)
173}
174
175type ObjectVersioner interface {
176 ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
177}
178
179// ObjectConvertor converts an object to a different version.
180type ObjectConvertor interface {
181 // Convert attempts to convert one object into another, or returns an error. This
182 // method does not mutate the in object, but the in and out object might share data structures,
183 // i.e. the out object cannot be mutated without mutating the in object as well.
184 // The context argument will be passed to all nested conversions.
185 Convert(in, out, context interface{}) error
186 // ConvertToVersion takes the provided object and converts it the provided version. This
187 // method does not mutate the in object, but the in and out object might share data structures,
188 // i.e. the out object cannot be mutated without mutating the in object as well.
189 // This method is similar to Convert() but handles specific details of choosing the correct
190 // output version.
191 ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
192 ConvertFieldLabel(gvk schema.GroupVersionKind, label, value string) (string, string, error)
193}
194
195// ObjectTyper contains methods for extracting the APIVersion and Kind
196// of objects.
197type ObjectTyper interface {
198 // ObjectKinds returns the all possible group,version,kind of the provided object, true if
199 // the object is unversioned, or an error if the object is not recognized
200 // (IsNotRegisteredError will return true).
201 ObjectKinds(Object) ([]schema.GroupVersionKind, bool, error)
202 // Recognizes returns true if the scheme is able to handle the provided version and kind,
203 // or more precisely that the provided version is a possible conversion or decoding
204 // target.
205 Recognizes(gvk schema.GroupVersionKind) bool
206}
207
208// ObjectCreater contains methods for instantiating an object by kind and version.
209type ObjectCreater interface {
210 New(kind schema.GroupVersionKind) (out Object, err error)
211}
212
David Bainbridge86971522019-09-26 22:09:39 +0000213// EquivalentResourceMapper provides information about resources that address the same underlying data as a specified resource
214type EquivalentResourceMapper interface {
215 // EquivalentResourcesFor returns a list of resources that address the same underlying data as resource.
216 // If subresource is specified, only equivalent resources which also have the same subresource are included.
217 // The specified resource can be included in the returned list.
218 EquivalentResourcesFor(resource schema.GroupVersionResource, subresource string) []schema.GroupVersionResource
219 // KindFor returns the kind expected by the specified resource[/subresource].
220 // A zero value is returned if the kind is unknown.
221 KindFor(resource schema.GroupVersionResource, subresource string) schema.GroupVersionKind
222}
223
224// EquivalentResourceRegistry provides an EquivalentResourceMapper interface,
225// and allows registering known resource[/subresource] -> kind
226type EquivalentResourceRegistry interface {
227 EquivalentResourceMapper
228 // RegisterKindFor registers the existence of the specified resource[/subresource] along with its expected kind.
229 RegisterKindFor(resource schema.GroupVersionResource, subresource string, kind schema.GroupVersionKind)
230}
231
Zack Williamse940c7a2019-08-21 14:25:39 -0700232// ResourceVersioner provides methods for setting and retrieving
233// the resource version from an API object.
234type ResourceVersioner interface {
235 SetResourceVersion(obj Object, version string) error
236 ResourceVersion(obj Object) (string, error)
237}
238
239// SelfLinker provides methods for setting and retrieving the SelfLink field of an API object.
240type SelfLinker interface {
241 SetSelfLink(obj Object, selfLink string) error
242 SelfLink(obj Object) (string, error)
243
244 // Knowing Name is sometimes necessary to use a SelfLinker.
245 Name(obj Object) (string, error)
246 // Knowing Namespace is sometimes necessary to use a SelfLinker
247 Namespace(obj Object) (string, error)
248}
249
250// Object interface must be supported by all API types registered with Scheme. Since objects in a scheme are
251// expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
252// serializers to set the kind, version, and group the object is represented as. An Object may choose
253// to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
254type Object interface {
255 GetObjectKind() schema.ObjectKind
256 DeepCopyObject() Object
257}
258
259// Unstructured objects store values as map[string]interface{}, with only values that can be serialized
260// to JSON allowed.
261type Unstructured interface {
262 Object
David Bainbridge86971522019-09-26 22:09:39 +0000263 // NewEmptyInstance returns a new instance of the concrete type containing only kind/apiVersion and no other data.
264 // This should be called instead of reflect.New() for unstructured types because the go type alone does not preserve kind/apiVersion info.
265 NewEmptyInstance() Unstructured
Zack Williamse940c7a2019-08-21 14:25:39 -0700266 // UnstructuredContent returns a non-nil map with this object's contents. Values may be
267 // []interface{}, map[string]interface{}, or any primitive type. Contents are typically serialized to
268 // and from JSON. SetUnstructuredContent should be used to mutate the contents.
269 UnstructuredContent() map[string]interface{}
270 // SetUnstructuredContent updates the object content to match the provided map.
271 SetUnstructuredContent(map[string]interface{})
272 // IsList returns true if this type is a list or matches the list convention - has an array called "items".
273 IsList() bool
274 // EachListItem should pass a single item out of the list as an Object to the provided function. Any
275 // error should terminate the iteration. If IsList() returns false, this method should return an error
276 // instead of calling the provided function.
277 EachListItem(func(Object) error) error
278}