sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package runtime |
| 18 | |
| 19 | // Note that the types provided in this file are not versioned and are intended to be |
| 20 | // safe to use from within all versions of every API object. |
| 21 | |
| 22 | // TypeMeta is shared by all top level objects. The proper way to use it is to inline it in your type, |
| 23 | // like this: |
| 24 | // type MyAwesomeAPIObject struct { |
| 25 | // runtime.TypeMeta `json:",inline"` |
| 26 | // ... // other fields |
| 27 | // } |
| 28 | // func (obj *MyAwesomeAPIObject) SetGroupVersionKind(gvk *metav1.GroupVersionKind) { metav1.UpdateTypeMeta(obj,gvk) }; GroupVersionKind() *GroupVersionKind |
| 29 | // |
| 30 | // TypeMeta is provided here for convenience. You may use it directly from this package or define |
| 31 | // your own with the same fields. |
| 32 | // |
| 33 | // +k8s:deepcopy-gen=false |
| 34 | // +protobuf=true |
| 35 | // +k8s:openapi-gen=true |
| 36 | type TypeMeta struct { |
| 37 | // +optional |
| 38 | APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty" protobuf:"bytes,1,opt,name=apiVersion"` |
| 39 | // +optional |
| 40 | Kind string `json:"kind,omitempty" yaml:"kind,omitempty" protobuf:"bytes,2,opt,name=kind"` |
| 41 | } |
| 42 | |
| 43 | const ( |
| 44 | ContentTypeJSON string = "application/json" |
| 45 | ) |
| 46 | |
| 47 | // RawExtension is used to hold extensions in external versions. |
| 48 | // |
| 49 | // To use this, make a field which has RawExtension as its type in your external, versioned |
| 50 | // struct, and Object in your internal struct. You also need to register your |
| 51 | // various plugin types. |
| 52 | // |
| 53 | // // Internal package: |
| 54 | // type MyAPIObject struct { |
| 55 | // runtime.TypeMeta `json:",inline"` |
| 56 | // MyPlugin runtime.Object `json:"myPlugin"` |
| 57 | // } |
| 58 | // type PluginA struct { |
| 59 | // AOption string `json:"aOption"` |
| 60 | // } |
| 61 | // |
| 62 | // // External package: |
| 63 | // type MyAPIObject struct { |
| 64 | // runtime.TypeMeta `json:",inline"` |
| 65 | // MyPlugin runtime.RawExtension `json:"myPlugin"` |
| 66 | // } |
| 67 | // type PluginA struct { |
| 68 | // AOption string `json:"aOption"` |
| 69 | // } |
| 70 | // |
| 71 | // // On the wire, the JSON will look something like this: |
| 72 | // { |
| 73 | // "kind":"MyAPIObject", |
| 74 | // "apiVersion":"v1", |
| 75 | // "myPlugin": { |
| 76 | // "kind":"PluginA", |
| 77 | // "aOption":"foo", |
| 78 | // }, |
| 79 | // } |
| 80 | // |
| 81 | // So what happens? Decode first uses json or yaml to unmarshal the serialized data into |
| 82 | // your external MyAPIObject. That causes the raw JSON to be stored, but not unpacked. |
| 83 | // The next step is to copy (using pkg/conversion) into the internal struct. The runtime |
| 84 | // package's DefaultScheme has conversion functions installed which will unpack the |
| 85 | // JSON stored in RawExtension, turning it into the correct object type, and storing it |
| 86 | // in the Object. (TODO: In the case where the object is of an unknown type, a |
| 87 | // runtime.Unknown object will be created and stored.) |
| 88 | // |
| 89 | // +k8s:deepcopy-gen=true |
| 90 | // +protobuf=true |
| 91 | // +k8s:openapi-gen=true |
| 92 | type RawExtension struct { |
| 93 | // Raw is the underlying serialization of this object. |
| 94 | // |
| 95 | // TODO: Determine how to detect ContentType and ContentEncoding of 'Raw' data. |
| 96 | Raw []byte `protobuf:"bytes,1,opt,name=raw"` |
| 97 | // Object can hold a representation of this extension - useful for working with versioned |
| 98 | // structs. |
| 99 | Object Object `json:"-"` |
| 100 | } |
| 101 | |
| 102 | // Unknown allows api objects with unknown types to be passed-through. This can be used |
| 103 | // to deal with the API objects from a plug-in. Unknown objects still have functioning |
| 104 | // TypeMeta features-- kind, version, etc. |
| 105 | // TODO: Make this object have easy access to field based accessors and settors for |
| 106 | // metadata and field mutatation. |
| 107 | // |
| 108 | // +k8s:deepcopy-gen=true |
| 109 | // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 110 | // +protobuf=true |
| 111 | // +k8s:openapi-gen=true |
| 112 | type Unknown struct { |
| 113 | TypeMeta `json:",inline" protobuf:"bytes,1,opt,name=typeMeta"` |
| 114 | // Raw will hold the complete serialized object which couldn't be matched |
| 115 | // with a registered type. Most likely, nothing should be done with this |
| 116 | // except for passing it through the system. |
| 117 | Raw []byte `protobuf:"bytes,2,opt,name=raw"` |
| 118 | // ContentEncoding is encoding used to encode 'Raw' data. |
| 119 | // Unspecified means no encoding. |
| 120 | ContentEncoding string `protobuf:"bytes,3,opt,name=contentEncoding"` |
| 121 | // ContentType is serialization method used to serialize 'Raw'. |
| 122 | // Unspecified means ContentTypeJSON. |
| 123 | ContentType string `protobuf:"bytes,4,opt,name=contentType"` |
| 124 | } |
| 125 | |
| 126 | // VersionedObjects is used by Decoders to give callers a way to access all versions |
| 127 | // of an object during the decoding process. |
| 128 | // |
| 129 | // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 130 | // +k8s:deepcopy-gen=true |
| 131 | type VersionedObjects struct { |
| 132 | // Objects is the set of objects retrieved during decoding, in order of conversion. |
| 133 | // The 0 index is the object as serialized on the wire. If conversion has occurred, |
| 134 | // other objects may be present. The right most object is the same as would be returned |
| 135 | // by a normal Decode call. |
| 136 | Objects []Object |
| 137 | } |