scheme.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runtime
  14. import (
  15. "context"
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "k8s.io/apimachinery/pkg/api/operation"
  20. "k8s.io/apimachinery/pkg/conversion"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apimachinery/pkg/util/naming"
  23. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  24. "k8s.io/apimachinery/pkg/util/sets"
  25. "k8s.io/apimachinery/pkg/util/validation/field"
  26. "k8s.io/kube-openapi/pkg/util"
  27. )
  28. // Scheme defines methods for serializing and deserializing API objects, a type
  29. // registry for converting group, version, and kind information to and from Go
  30. // schemas, and mappings between Go schemas of different versions. A scheme is the
  31. // foundation for a versioned API and versioned configuration over time.
  32. //
  33. // In a Scheme, a Type is a particular Go struct, a Version is a point-in-time
  34. // identifier for a particular representation of that Type (typically backwards
  35. // compatible), a Kind is the unique name for that Type within the Version, and a
  36. // Group identifies a set of Versions, Kinds, and Types that evolve over time. An
  37. // Unversioned Type is one that is not yet formally bound to a type and is promised
  38. // to be backwards compatible (effectively a "v1" of a Type that does not expect
  39. // to break in the future).
  40. //
  41. // Schemes are not expected to change at runtime and are only threadsafe after
  42. // registration is complete.
  43. type Scheme struct {
  44. // gvkToType allows one to figure out the go type of an object with
  45. // the given version and name.
  46. gvkToType map[schema.GroupVersionKind]reflect.Type
  47. // typeToGVK allows one to find metadata for a given go object.
  48. // The reflect.Type we index by should *not* be a pointer.
  49. typeToGVK map[reflect.Type][]schema.GroupVersionKind
  50. // unversionedTypes are transformed without conversion in ConvertToVersion.
  51. unversionedTypes map[reflect.Type]schema.GroupVersionKind
  52. // unversionedKinds are the names of kinds that can be created in the context of any group
  53. // or version
  54. // TODO: resolve the status of unversioned types.
  55. unversionedKinds map[string]reflect.Type
  56. // Map from version and resource to the corresponding func to convert
  57. // resource field labels in that version to internal version.
  58. fieldLabelConversionFuncs map[schema.GroupVersionKind]FieldLabelConversionFunc
  59. // defaulterFuncs is a map to funcs to be called with an object to provide defaulting
  60. // the provided object must be a pointer.
  61. defaulterFuncs map[reflect.Type]func(interface{})
  62. // validationFuncs is a map to funcs to be called with an object to perform validation.
  63. // The provided object must be a pointer.
  64. // If oldObject is non-nil, update validation is performed and may perform additional
  65. // validation such as transition rules and immutability checks.
  66. validationFuncs map[reflect.Type]func(ctx context.Context, op operation.Operation, object, oldObject interface{}) field.ErrorList
  67. // converter stores all registered conversion functions. It also has
  68. // default converting behavior.
  69. converter *conversion.Converter
  70. // versionPriority is a map of groups to ordered lists of versions for those groups indicating the
  71. // default priorities of these versions as registered in the scheme
  72. versionPriority map[string][]string
  73. // observedVersions keeps track of the order we've seen versions during type registration
  74. observedVersions []schema.GroupVersion
  75. // schemeName is the name of this scheme. If you don't specify a name, the stack of the NewScheme caller will be used.
  76. // This is useful for error reporting to indicate the origin of the scheme.
  77. schemeName string
  78. }
  79. // FieldLabelConversionFunc converts a field selector to internal representation.
  80. type FieldLabelConversionFunc func(label, value string) (internalLabel, internalValue string, err error)
  81. // NewScheme creates a new Scheme. This scheme is pluggable by default.
  82. func NewScheme() *Scheme {
  83. s := &Scheme{
  84. gvkToType: map[schema.GroupVersionKind]reflect.Type{},
  85. typeToGVK: map[reflect.Type][]schema.GroupVersionKind{},
  86. unversionedTypes: map[reflect.Type]schema.GroupVersionKind{},
  87. unversionedKinds: map[string]reflect.Type{},
  88. fieldLabelConversionFuncs: map[schema.GroupVersionKind]FieldLabelConversionFunc{},
  89. defaulterFuncs: map[reflect.Type]func(interface{}){},
  90. validationFuncs: map[reflect.Type]func(ctx context.Context, op operation.Operation, object, oldObject interface{}) field.ErrorList{},
  91. versionPriority: map[string][]string{},
  92. schemeName: naming.GetNameFromCallsite(internalPackages...),
  93. }
  94. s.converter = conversion.NewConverter(nil)
  95. // Enable couple default conversions by default.
  96. utilruntime.Must(RegisterEmbeddedConversions(s))
  97. utilruntime.Must(RegisterStringConversions(s))
  98. return s
  99. }
  100. // Converter allows access to the converter for the scheme
  101. func (s *Scheme) Converter() *conversion.Converter {
  102. return s.converter
  103. }
  104. // AddUnversionedTypes registers the provided types as "unversioned", which means that they follow special rules.
  105. // Whenever an object of this type is serialized, it is serialized with the provided group version and is not
  106. // converted. Thus unversioned objects are expected to remain backwards compatible forever, as if they were in an
  107. // API group and version that would never be updated.
  108. //
  109. // TODO: there is discussion about removing unversioned and replacing it with objects that are manifest into
  110. // every version with particular schemas. Resolve this method at that point.
  111. func (s *Scheme) AddUnversionedTypes(version schema.GroupVersion, types ...Object) {
  112. s.addObservedVersion(version)
  113. s.AddKnownTypes(version, types...)
  114. for _, obj := range types {
  115. t := reflect.TypeOf(obj).Elem()
  116. gvk := version.WithKind(t.Name())
  117. s.unversionedTypes[t] = gvk
  118. if old, ok := s.unversionedKinds[gvk.Kind]; ok && t != old {
  119. panic(fmt.Sprintf("%v.%v has already been registered as unversioned kind %q - kind name must be unique in scheme %q", old.PkgPath(), old.Name(), gvk, s.schemeName))
  120. }
  121. s.unversionedKinds[gvk.Kind] = t
  122. }
  123. }
  124. // AddKnownTypes registers all types passed in 'types' as being members of version 'version'.
  125. // All objects passed to types should be pointers to structs. The name that go reports for
  126. // the struct becomes the "kind" field when encoding. Version may not be empty - use the
  127. // APIVersionInternal constant if you have a type that does not have a formal version.
  128. func (s *Scheme) AddKnownTypes(gv schema.GroupVersion, types ...Object) {
  129. s.addObservedVersion(gv)
  130. for _, obj := range types {
  131. t := reflect.TypeOf(obj)
  132. if t.Kind() != reflect.Pointer {
  133. panic("All types must be pointers to structs.")
  134. }
  135. t = t.Elem()
  136. s.AddKnownTypeWithName(gv.WithKind(t.Name()), obj)
  137. }
  138. }
  139. // AddKnownTypeWithName is like AddKnownTypes, but it lets you specify what this type should
  140. // be encoded as. Useful for testing when you don't want to make multiple packages to define
  141. // your structs. Version may not be empty - use the APIVersionInternal constant if you have a
  142. // type that does not have a formal version.
  143. func (s *Scheme) AddKnownTypeWithName(gvk schema.GroupVersionKind, obj Object) {
  144. s.addObservedVersion(gvk.GroupVersion())
  145. t := reflect.TypeOf(obj)
  146. if len(gvk.Version) == 0 {
  147. panic(fmt.Sprintf("version is required on all types: %s %v", gvk, t))
  148. }
  149. if t.Kind() != reflect.Pointer {
  150. panic("All types must be pointers to structs.")
  151. }
  152. t = t.Elem()
  153. if t.Kind() != reflect.Struct {
  154. panic("All types must be pointers to structs.")
  155. }
  156. if oldT, found := s.gvkToType[gvk]; found && oldT != t {
  157. panic(fmt.Sprintf("Double registration of different types for %v: old=%v.%v, new=%v.%v in scheme %q", gvk, oldT.PkgPath(), oldT.Name(), t.PkgPath(), t.Name(), s.schemeName))
  158. }
  159. s.gvkToType[gvk] = t
  160. for _, existingGvk := range s.typeToGVK[t] {
  161. if existingGvk == gvk {
  162. return
  163. }
  164. }
  165. s.typeToGVK[t] = append(s.typeToGVK[t], gvk)
  166. // if the type implements DeepCopyInto(<obj>), register a self-conversion
  167. if m := reflect.ValueOf(obj).MethodByName("DeepCopyInto"); m.IsValid() && m.Type().NumIn() == 1 && m.Type().NumOut() == 0 && m.Type().In(0) == reflect.TypeOf(obj) {
  168. if err := s.AddGeneratedConversionFunc(obj, obj, func(a, b interface{}, scope conversion.Scope) error {
  169. // copy a to b
  170. reflect.ValueOf(a).MethodByName("DeepCopyInto").Call([]reflect.Value{reflect.ValueOf(b)})
  171. // clear TypeMeta to match legacy reflective conversion
  172. b.(Object).GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
  173. return nil
  174. }); err != nil {
  175. panic(err)
  176. }
  177. }
  178. }
  179. // KnownTypes returns the types known for the given version.
  180. func (s *Scheme) KnownTypes(gv schema.GroupVersion) map[string]reflect.Type {
  181. types := make(map[string]reflect.Type)
  182. for gvk, t := range s.gvkToType {
  183. if gv != gvk.GroupVersion() {
  184. continue
  185. }
  186. types[gvk.Kind] = t
  187. }
  188. return types
  189. }
  190. // VersionsForGroupKind returns the versions that a particular GroupKind can be converted to within the given group.
  191. // A GroupKind might be converted to a different group. That information is available in EquivalentResourceMapper.
  192. func (s *Scheme) VersionsForGroupKind(gk schema.GroupKind) []schema.GroupVersion {
  193. availableVersions := []schema.GroupVersion{}
  194. for gvk := range s.gvkToType {
  195. if gk != gvk.GroupKind() {
  196. continue
  197. }
  198. availableVersions = append(availableVersions, gvk.GroupVersion())
  199. }
  200. // order the return for stability
  201. ret := []schema.GroupVersion{}
  202. for _, version := range s.PrioritizedVersionsForGroup(gk.Group) {
  203. for _, availableVersion := range availableVersions {
  204. if version != availableVersion {
  205. continue
  206. }
  207. ret = append(ret, availableVersion)
  208. }
  209. }
  210. return ret
  211. }
  212. // AllKnownTypes returns the all known types.
  213. func (s *Scheme) AllKnownTypes() map[schema.GroupVersionKind]reflect.Type {
  214. return s.gvkToType
  215. }
  216. // ObjectKinds returns all possible group,version,kind of the go object, true if the
  217. // object is considered unversioned, or an error if it's not a pointer or is unregistered.
  218. func (s *Scheme) ObjectKinds(obj Object) ([]schema.GroupVersionKind, bool, error) {
  219. // Unstructured objects are always considered to have their declared GVK
  220. if _, ok := obj.(Unstructured); ok {
  221. // we require that the GVK be populated in order to recognize the object
  222. gvk := obj.GetObjectKind().GroupVersionKind()
  223. if len(gvk.Kind) == 0 {
  224. return nil, false, NewMissingKindErr("unstructured object has no kind")
  225. }
  226. if len(gvk.Version) == 0 {
  227. return nil, false, NewMissingVersionErr("unstructured object has no version")
  228. }
  229. return []schema.GroupVersionKind{gvk}, false, nil
  230. }
  231. v, err := conversion.EnforcePtr(obj)
  232. if err != nil {
  233. return nil, false, err
  234. }
  235. t := v.Type()
  236. gvks, ok := s.typeToGVK[t]
  237. if !ok {
  238. return nil, false, NewNotRegisteredErrForType(s.schemeName, t)
  239. }
  240. _, unversionedType := s.unversionedTypes[t]
  241. return gvks, unversionedType, nil
  242. }
  243. // Recognizes returns true if the scheme is able to handle the provided group,version,kind
  244. // of an object.
  245. func (s *Scheme) Recognizes(gvk schema.GroupVersionKind) bool {
  246. _, exists := s.gvkToType[gvk]
  247. return exists
  248. }
  249. func (s *Scheme) IsUnversioned(obj Object) (bool, bool) {
  250. v, err := conversion.EnforcePtr(obj)
  251. if err != nil {
  252. return false, false
  253. }
  254. t := v.Type()
  255. if _, ok := s.typeToGVK[t]; !ok {
  256. return false, false
  257. }
  258. _, ok := s.unversionedTypes[t]
  259. return ok, true
  260. }
  261. // New returns a new API object of the given version and name, or an error if it hasn't
  262. // been registered. The version and kind fields must be specified.
  263. func (s *Scheme) New(kind schema.GroupVersionKind) (Object, error) {
  264. if t, exists := s.gvkToType[kind]; exists {
  265. return reflect.New(t).Interface().(Object), nil
  266. }
  267. if t, exists := s.unversionedKinds[kind.Kind]; exists {
  268. return reflect.New(t).Interface().(Object), nil
  269. }
  270. return nil, NewNotRegisteredErrForKind(s.schemeName, kind)
  271. }
  272. // AddIgnoredConversionType identifies a pair of types that should be skipped by
  273. // conversion (because the data inside them is explicitly dropped during
  274. // conversion).
  275. func (s *Scheme) AddIgnoredConversionType(from, to interface{}) error {
  276. return s.converter.RegisterIgnoredConversion(from, to)
  277. }
  278. // AddConversionFunc registers a function that converts between a and b by passing objects of those
  279. // types to the provided function. The function *must* accept objects of a and b - this machinery will not enforce
  280. // any other guarantee.
  281. func (s *Scheme) AddConversionFunc(a, b interface{}, fn conversion.ConversionFunc) error {
  282. return s.converter.RegisterUntypedConversionFunc(a, b, fn)
  283. }
  284. // AddGeneratedConversionFunc registers a function that converts between a and b by passing objects of those
  285. // types to the provided function. The function *must* accept objects of a and b - this machinery will not enforce
  286. // any other guarantee.
  287. func (s *Scheme) AddGeneratedConversionFunc(a, b interface{}, fn conversion.ConversionFunc) error {
  288. return s.converter.RegisterGeneratedUntypedConversionFunc(a, b, fn)
  289. }
  290. // AddFieldLabelConversionFunc adds a conversion function to convert field selectors
  291. // of the given kind from the given version to internal version representation.
  292. func (s *Scheme) AddFieldLabelConversionFunc(gvk schema.GroupVersionKind, conversionFunc FieldLabelConversionFunc) error {
  293. s.fieldLabelConversionFuncs[gvk] = conversionFunc
  294. return nil
  295. }
  296. // AddTypeDefaultingFunc registers a function that is passed a pointer to an
  297. // object and can default fields on the object. These functions will be invoked
  298. // when Default() is called. The function will never be called unless the
  299. // defaulted object matches srcType. If this function is invoked twice with the
  300. // same srcType, the fn passed to the later call will be used instead.
  301. func (s *Scheme) AddTypeDefaultingFunc(srcType Object, fn func(interface{})) {
  302. s.defaulterFuncs[reflect.TypeOf(srcType)] = fn
  303. }
  304. // Default sets defaults on the provided Object.
  305. func (s *Scheme) Default(src Object) {
  306. if fn, ok := s.defaulterFuncs[reflect.TypeOf(src)]; ok {
  307. fn(src)
  308. }
  309. }
  310. // AddValidationFunc registered a function that can validate the object, and
  311. // oldObject. These functions will be invoked when Validate() or ValidateUpdate()
  312. // is called. The function will never be called unless the validated object
  313. // matches srcType. If this function is invoked twice with the same srcType, the
  314. // fn passed to the later call will be used instead.
  315. func (s *Scheme) AddValidationFunc(srcType Object, fn func(ctx context.Context, op operation.Operation, object, oldObject interface{}) field.ErrorList) {
  316. s.validationFuncs[reflect.TypeOf(srcType)] = fn
  317. }
  318. // Validate validates the provided Object according to the generated declarative validation code.
  319. // WARNING: This does not validate all objects! The handwritten validation code in validation.go
  320. // is not run when this is called. Only the generated zz_generated.validations.go validation code is run.
  321. func (s *Scheme) Validate(ctx context.Context, options []string, object Object, subresources ...string) field.ErrorList {
  322. if fn, ok := s.validationFuncs[reflect.TypeOf(object)]; ok {
  323. return fn(ctx, operation.Operation{Type: operation.Create, Request: operation.Request{Subresources: subresources}, Options: options}, object, nil)
  324. }
  325. return nil
  326. }
  327. // ValidateUpdate validates the provided object and oldObject according to the generated declarative validation code.
  328. // WARNING: This does not validate all objects! The handwritten validation code in validation.go
  329. // is not run when this is called. Only the generated zz_generated.validations.go validation code is run.
  330. func (s *Scheme) ValidateUpdate(ctx context.Context, options []string, object, oldObject Object, subresources ...string) field.ErrorList {
  331. if fn, ok := s.validationFuncs[reflect.TypeOf(object)]; ok {
  332. return fn(ctx, operation.Operation{Type: operation.Update, Request: operation.Request{Subresources: subresources}, Options: options}, object, oldObject)
  333. }
  334. return nil
  335. }
  336. // Convert will attempt to convert in into out. Both must be pointers. For easy
  337. // testing of conversion functions. Returns an error if the conversion isn't
  338. // possible. You can call this with types that haven't been registered (for example,
  339. // a to test conversion of types that are nested within registered types). The
  340. // context interface is passed to the convertor. Convert also supports Unstructured
  341. // types and will convert them intelligently.
  342. func (s *Scheme) Convert(in, out interface{}, context interface{}) error {
  343. unstructuredIn, okIn := in.(Unstructured)
  344. unstructuredOut, okOut := out.(Unstructured)
  345. switch {
  346. case okIn && okOut:
  347. // converting unstructured input to an unstructured output is a straight copy - unstructured
  348. // is a "smart holder" and the contents are passed by reference between the two objects
  349. unstructuredOut.SetUnstructuredContent(unstructuredIn.UnstructuredContent())
  350. return nil
  351. case okOut:
  352. // if the output is an unstructured object, use the standard Go type to unstructured
  353. // conversion. The object must not be internal.
  354. obj, ok := in.(Object)
  355. if !ok {
  356. return fmt.Errorf("unable to convert object type %T to Unstructured, must be a runtime.Object", in)
  357. }
  358. gvks, unversioned, err := s.ObjectKinds(obj)
  359. if err != nil {
  360. return err
  361. }
  362. gvk := gvks[0]
  363. // if no conversion is necessary, convert immediately
  364. if unversioned || gvk.Version != APIVersionInternal {
  365. content, err := DefaultUnstructuredConverter.ToUnstructured(in)
  366. if err != nil {
  367. return err
  368. }
  369. unstructuredOut.SetUnstructuredContent(content)
  370. unstructuredOut.GetObjectKind().SetGroupVersionKind(gvk)
  371. return nil
  372. }
  373. // attempt to convert the object to an external version first.
  374. target, ok := context.(GroupVersioner)
  375. if !ok {
  376. return fmt.Errorf("unable to convert the internal object type %T to Unstructured without providing a preferred version to convert to", in)
  377. }
  378. // Convert is implicitly unsafe, so we don't need to perform a safe conversion
  379. versioned, err := s.UnsafeConvertToVersion(obj, target)
  380. if err != nil {
  381. return err
  382. }
  383. content, err := DefaultUnstructuredConverter.ToUnstructured(versioned)
  384. if err != nil {
  385. return err
  386. }
  387. unstructuredOut.SetUnstructuredContent(content)
  388. return nil
  389. case okIn:
  390. // converting an unstructured object to any type is modeled by first converting
  391. // the input to a versioned type, then running standard conversions
  392. typed, err := s.unstructuredToTyped(unstructuredIn)
  393. if err != nil {
  394. return err
  395. }
  396. in = typed
  397. }
  398. meta := s.generateConvertMeta(in)
  399. meta.Context = context
  400. return s.converter.Convert(in, out, meta)
  401. }
  402. // ConvertFieldLabel alters the given field label and value for an kind field selector from
  403. // versioned representation to an unversioned one or returns an error.
  404. func (s *Scheme) ConvertFieldLabel(gvk schema.GroupVersionKind, label, value string) (string, string, error) {
  405. conversionFunc, ok := s.fieldLabelConversionFuncs[gvk]
  406. if !ok {
  407. return DefaultMetaV1FieldSelectorConversion(label, value)
  408. }
  409. return conversionFunc(label, value)
  410. }
  411. // ConvertToVersion attempts to convert an input object to its matching Kind in another
  412. // version within this scheme. Will return an error if the provided version does not
  413. // contain the inKind (or a mapping by name defined with AddKnownTypeWithName). Will also
  414. // return an error if the conversion does not result in a valid Object being
  415. // returned. Passes target down to the conversion methods as the Context on the scope.
  416. func (s *Scheme) ConvertToVersion(in Object, target GroupVersioner) (Object, error) {
  417. return s.convertToVersion(true, in, target)
  418. }
  419. // UnsafeConvertToVersion will convert in to the provided target if such a conversion is possible,
  420. // but does not guarantee the output object does not share fields with the input object. It attempts to be as
  421. // efficient as possible when doing conversion.
  422. func (s *Scheme) UnsafeConvertToVersion(in Object, target GroupVersioner) (Object, error) {
  423. return s.convertToVersion(false, in, target)
  424. }
  425. // convertToVersion handles conversion with an optional copy.
  426. func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (Object, error) {
  427. var t reflect.Type
  428. if u, ok := in.(Unstructured); ok {
  429. typed, err := s.unstructuredToTyped(u)
  430. if err != nil {
  431. return nil, err
  432. }
  433. in = typed
  434. // unstructuredToTyped returns an Object, which must be a pointer to a struct.
  435. t = reflect.TypeOf(in).Elem()
  436. } else {
  437. // determine the incoming kinds with as few allocations as possible.
  438. t = reflect.TypeOf(in)
  439. if t.Kind() != reflect.Pointer {
  440. return nil, fmt.Errorf("only pointer types may be converted: %v", t)
  441. }
  442. t = t.Elem()
  443. if t.Kind() != reflect.Struct {
  444. return nil, fmt.Errorf("only pointers to struct types may be converted: %v", t)
  445. }
  446. }
  447. kinds, ok := s.typeToGVK[t]
  448. if !ok || len(kinds) == 0 {
  449. return nil, NewNotRegisteredErrForType(s.schemeName, t)
  450. }
  451. gvk, ok := target.KindForGroupVersionKinds(kinds)
  452. if !ok {
  453. // try to see if this type is listed as unversioned (for legacy support)
  454. // TODO: when we move to server API versions, we should completely remove the unversioned concept
  455. if unversionedKind, ok := s.unversionedTypes[t]; ok {
  456. if gvk, ok := target.KindForGroupVersionKinds([]schema.GroupVersionKind{unversionedKind}); ok {
  457. return copyAndSetTargetKind(copy, in, gvk)
  458. }
  459. return copyAndSetTargetKind(copy, in, unversionedKind)
  460. }
  461. return nil, NewNotRegisteredErrForTarget(s.schemeName, t, target)
  462. }
  463. // target wants to use the existing type, set kind and return (no conversion necessary)
  464. for _, kind := range kinds {
  465. if gvk == kind {
  466. return copyAndSetTargetKind(copy, in, gvk)
  467. }
  468. }
  469. // type is unversioned, no conversion necessary
  470. if unversionedKind, ok := s.unversionedTypes[t]; ok {
  471. if gvk, ok := target.KindForGroupVersionKinds([]schema.GroupVersionKind{unversionedKind}); ok {
  472. return copyAndSetTargetKind(copy, in, gvk)
  473. }
  474. return copyAndSetTargetKind(copy, in, unversionedKind)
  475. }
  476. out, err := s.New(gvk)
  477. if err != nil {
  478. return nil, err
  479. }
  480. if copy {
  481. in = in.DeepCopyObject()
  482. }
  483. meta := s.generateConvertMeta(in)
  484. meta.Context = target
  485. if err := s.converter.Convert(in, out, meta); err != nil {
  486. return nil, err
  487. }
  488. setTargetKind(out, gvk)
  489. return out, nil
  490. }
  491. // unstructuredToTyped attempts to transform an unstructured object to a typed
  492. // object if possible. It will return an error if conversion is not possible, or the versioned
  493. // Go form of the object. Note that this conversion will lose fields.
  494. func (s *Scheme) unstructuredToTyped(in Unstructured) (Object, error) {
  495. // the type must be something we recognize
  496. gvks, _, err := s.ObjectKinds(in)
  497. if err != nil {
  498. return nil, err
  499. }
  500. typed, err := s.New(gvks[0])
  501. if err != nil {
  502. return nil, err
  503. }
  504. if err := DefaultUnstructuredConverter.FromUnstructured(in.UnstructuredContent(), typed); err != nil {
  505. return nil, fmt.Errorf("unable to convert unstructured object to %v: %v", gvks[0], err)
  506. }
  507. return typed, nil
  508. }
  509. // generateConvertMeta constructs the meta value we pass to Convert.
  510. func (s *Scheme) generateConvertMeta(in interface{}) *conversion.Meta {
  511. return s.converter.DefaultMeta(reflect.TypeOf(in))
  512. }
  513. // copyAndSetTargetKind performs a conditional copy before returning the object, or an error if copy was not successful.
  514. func copyAndSetTargetKind(copy bool, obj Object, kind schema.GroupVersionKind) (Object, error) {
  515. if copy {
  516. obj = obj.DeepCopyObject()
  517. }
  518. setTargetKind(obj, kind)
  519. return obj, nil
  520. }
  521. // setTargetKind sets the kind on an object, taking into account whether the target kind is the internal version.
  522. func setTargetKind(obj Object, kind schema.GroupVersionKind) {
  523. if kind.Version == APIVersionInternal {
  524. // internal is a special case
  525. // TODO: look at removing the need to special case this
  526. obj.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
  527. return
  528. }
  529. obj.GetObjectKind().SetGroupVersionKind(kind)
  530. }
  531. // SetVersionPriority allows specifying a precise order of priority. All specified versions must be in the same group,
  532. // and the specified order overwrites any previously specified order for this group
  533. func (s *Scheme) SetVersionPriority(versions ...schema.GroupVersion) error {
  534. groups := sets.String{}
  535. order := []string{}
  536. for _, version := range versions {
  537. if len(version.Version) == 0 || version.Version == APIVersionInternal {
  538. return fmt.Errorf("internal versions cannot be prioritized: %v", version)
  539. }
  540. groups.Insert(version.Group)
  541. order = append(order, version.Version)
  542. }
  543. if len(groups) != 1 {
  544. return fmt.Errorf("must register versions for exactly one group: %v", strings.Join(groups.List(), ", "))
  545. }
  546. s.versionPriority[groups.List()[0]] = order
  547. return nil
  548. }
  549. // PrioritizedVersionsForGroup returns versions for a single group in priority order
  550. func (s *Scheme) PrioritizedVersionsForGroup(group string) []schema.GroupVersion {
  551. ret := []schema.GroupVersion{}
  552. for _, version := range s.versionPriority[group] {
  553. ret = append(ret, schema.GroupVersion{Group: group, Version: version})
  554. }
  555. for _, observedVersion := range s.observedVersions {
  556. if observedVersion.Group != group {
  557. continue
  558. }
  559. found := false
  560. for _, existing := range ret {
  561. if existing == observedVersion {
  562. found = true
  563. break
  564. }
  565. }
  566. if !found {
  567. ret = append(ret, observedVersion)
  568. }
  569. }
  570. return ret
  571. }
  572. // PrioritizedVersionsAllGroups returns all known versions in their priority order. Groups are random, but
  573. // versions for a single group are prioritized
  574. func (s *Scheme) PrioritizedVersionsAllGroups() []schema.GroupVersion {
  575. ret := []schema.GroupVersion{}
  576. for group, versions := range s.versionPriority {
  577. for _, version := range versions {
  578. ret = append(ret, schema.GroupVersion{Group: group, Version: version})
  579. }
  580. }
  581. for _, observedVersion := range s.observedVersions {
  582. found := false
  583. for _, existing := range ret {
  584. if existing == observedVersion {
  585. found = true
  586. break
  587. }
  588. }
  589. if !found {
  590. ret = append(ret, observedVersion)
  591. }
  592. }
  593. return ret
  594. }
  595. // PreferredVersionAllGroups returns the most preferred version for every group.
  596. // group ordering is random.
  597. func (s *Scheme) PreferredVersionAllGroups() []schema.GroupVersion {
  598. ret := []schema.GroupVersion{}
  599. for group, versions := range s.versionPriority {
  600. for _, version := range versions {
  601. ret = append(ret, schema.GroupVersion{Group: group, Version: version})
  602. break
  603. }
  604. }
  605. for _, observedVersion := range s.observedVersions {
  606. found := false
  607. for _, existing := range ret {
  608. if existing.Group == observedVersion.Group {
  609. found = true
  610. break
  611. }
  612. }
  613. if !found {
  614. ret = append(ret, observedVersion)
  615. }
  616. }
  617. return ret
  618. }
  619. // IsGroupRegistered returns true if types for the group have been registered with the scheme
  620. func (s *Scheme) IsGroupRegistered(group string) bool {
  621. for _, observedVersion := range s.observedVersions {
  622. if observedVersion.Group == group {
  623. return true
  624. }
  625. }
  626. return false
  627. }
  628. // IsVersionRegistered returns true if types for the version have been registered with the scheme
  629. func (s *Scheme) IsVersionRegistered(version schema.GroupVersion) bool {
  630. for _, observedVersion := range s.observedVersions {
  631. if observedVersion == version {
  632. return true
  633. }
  634. }
  635. return false
  636. }
  637. func (s *Scheme) addObservedVersion(version schema.GroupVersion) {
  638. if len(version.Version) == 0 || version.Version == APIVersionInternal {
  639. return
  640. }
  641. for _, observedVersion := range s.observedVersions {
  642. if observedVersion == version {
  643. return
  644. }
  645. }
  646. s.observedVersions = append(s.observedVersions, version)
  647. }
  648. func (s *Scheme) Name() string {
  649. return s.schemeName
  650. }
  651. // internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
  652. // call chains to NewReflector, so they'd be low entropy names for reflectors
  653. var internalPackages = []string{"k8s.io/apimachinery/pkg/runtime/scheme.go"}
  654. // ToOpenAPIDefinitionName returns the REST-friendly OpenAPI definition name known type identified by groupVersionKind.
  655. // If the groupVersionKind does not identify a known type, an error is returned.
  656. // The Version field of groupVersionKind is required, and the Group and Kind fields are required for unstructured.Unstructured
  657. // types. If a required field is empty, an error is returned.
  658. //
  659. // The OpenAPI definition name is the canonical name of the type, with the group and version removed.
  660. // For example, the OpenAPI definition name of Pod is `io.k8s.api.core.v1.Pod`.
  661. //
  662. // This respects the util.OpenAPIModelNamer interface and will return the name returned by
  663. // OpenAPIModelName() if it is defined on the type.
  664. //
  665. // A known type that is registered as an unstructured.Unstructured type is treated as a custom resource and
  666. // which has an OpenAPI definition name of the form `<reversed-group>.<version.<kind>`.
  667. // For example, the OpenAPI definition name of `group: stable.example.com, version: v1, kind: Pod` is
  668. // `com.example.stable.v1.Pod`.
  669. func (s *Scheme) ToOpenAPIDefinitionName(groupVersionKind schema.GroupVersionKind) (string, error) {
  670. if groupVersionKind.Version == "" { // Empty version is not allowed by New() so check it first to avoid a panic.
  671. return "", fmt.Errorf("version is required on all types: %v", groupVersionKind)
  672. }
  673. example, err := s.New(groupVersionKind)
  674. if err != nil {
  675. return "", err
  676. }
  677. // Use a namer if provided
  678. if namer, ok := example.(util.OpenAPIModelNamer); ok {
  679. return namer.OpenAPIModelName(), nil
  680. }
  681. if _, ok := example.(Unstructured); ok {
  682. if groupVersionKind.Group == "" || groupVersionKind.Kind == "" {
  683. return "", fmt.Errorf("unable to convert GroupVersionKind with empty fields to unstructured type to an OpenAPI definition name: %v", groupVersionKind)
  684. }
  685. return reverseParts(groupVersionKind.Group) + "." + groupVersionKind.Version + "." + groupVersionKind.Kind, nil
  686. }
  687. rtype := reflect.TypeOf(example).Elem()
  688. name := toOpenAPIDefinitionName(rtype.PkgPath() + "." + rtype.Name())
  689. return name, nil
  690. }
  691. // toOpenAPIDefinitionName converts Golang package/type canonical name into REST friendly OpenAPI name.
  692. // Input is expected to be `PkgPath + "." TypeName.
  693. //
  694. // Examples of REST friendly OpenAPI name:
  695. //
  696. // Input: k8s.io/api/core/v1.Pod
  697. // Output: io.k8s.api.core.v1.Pod
  698. //
  699. // Input: k8s.io/api/core/v1
  700. // Output: io.k8s.api.core.v1
  701. //
  702. // Input: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  703. // Output: io.k8s.storage.csi.v1alpha1.CSINodeInfo
  704. //
  705. // Note that this is a copy of ToRESTFriendlyName from k8s.io/kube-openapi/pkg/util. It is duplicated here to avoid
  706. // a dependency on kube-openapi.
  707. func toOpenAPIDefinitionName(name string) string {
  708. nameParts := strings.Split(name, "/")
  709. // Reverse first part. e.g., io.k8s... instead of k8s.io...
  710. if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") {
  711. nameParts[0] = reverseParts(nameParts[0])
  712. }
  713. return strings.Join(nameParts, ".")
  714. }
  715. func reverseParts(dotSeparatedName string) string {
  716. parts := strings.Split(dotSeparatedName, ".")
  717. for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
  718. parts[i], parts[j] = parts[j], parts[i]
  719. }
  720. return strings.Join(parts, ".")
  721. }