proto_models.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright 2022 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 schemaconv
  14. import (
  15. "errors"
  16. "path"
  17. "strings"
  18. "k8s.io/kube-openapi/pkg/util/proto"
  19. "sigs.k8s.io/structured-merge-diff/v4/schema"
  20. )
  21. // ToSchema converts openapi definitions into a schema suitable for structured
  22. // merge (i.e. kubectl apply v2).
  23. func ToSchema(models proto.Models) (*schema.Schema, error) {
  24. return ToSchemaWithPreserveUnknownFields(models, false)
  25. }
  26. // ToSchemaWithPreserveUnknownFields converts openapi definitions into a schema suitable for structured
  27. // merge (i.e. kubectl apply v2), it will preserve unknown fields if specified.
  28. func ToSchemaWithPreserveUnknownFields(models proto.Models, preserveUnknownFields bool) (*schema.Schema, error) {
  29. c := convert{
  30. preserveUnknownFields: preserveUnknownFields,
  31. output: &schema.Schema{},
  32. }
  33. for _, name := range models.ListModels() {
  34. model := models.LookupModel(name)
  35. var a schema.Atom
  36. c2 := c.push(name, &a)
  37. model.Accept(c2)
  38. c.pop(c2)
  39. c.insertTypeDef(name, a)
  40. }
  41. if len(c.errorMessages) > 0 {
  42. return nil, errors.New(strings.Join(c.errorMessages, "\n"))
  43. }
  44. c.addCommonTypes()
  45. return c.output, nil
  46. }
  47. func (c *convert) makeRef(model proto.Schema, preserveUnknownFields bool) schema.TypeRef {
  48. var tr schema.TypeRef
  49. if r, ok := model.(*proto.Ref); ok {
  50. if r.Reference() == "io.k8s.apimachinery.pkg.runtime.RawExtension" {
  51. return schema.TypeRef{
  52. NamedType: &untypedName,
  53. }
  54. }
  55. // reference a named type
  56. _, n := path.Split(r.Reference())
  57. tr.NamedType = &n
  58. mapRelationship, err := getMapElementRelationship(model.GetExtensions())
  59. if err != nil {
  60. c.reportError(err.Error())
  61. }
  62. // empty string means unset.
  63. if len(mapRelationship) > 0 {
  64. tr.ElementRelationship = &mapRelationship
  65. }
  66. } else {
  67. // compute the type inline
  68. c2 := c.push("inlined in "+c.currentName, &tr.Inlined)
  69. c2.preserveUnknownFields = preserveUnknownFields
  70. model.Accept(c2)
  71. c.pop(c2)
  72. if tr == (schema.TypeRef{}) {
  73. // emit warning?
  74. tr.NamedType = &untypedName
  75. }
  76. }
  77. return tr
  78. }
  79. func (c *convert) VisitKind(k *proto.Kind) {
  80. preserveUnknownFields := c.preserveUnknownFields
  81. if p, ok := k.GetExtensions()["x-kubernetes-preserve-unknown-fields"]; ok && p == true {
  82. preserveUnknownFields = true
  83. }
  84. a := c.top()
  85. a.Map = &schema.Map{}
  86. for _, name := range k.FieldOrder {
  87. member := k.Fields[name]
  88. tr := c.makeRef(member, preserveUnknownFields)
  89. a.Map.Fields = append(a.Map.Fields, schema.StructField{
  90. Name: name,
  91. Type: tr,
  92. Default: member.GetDefault(),
  93. })
  94. }
  95. unions, err := makeUnions(k.GetExtensions())
  96. if err != nil {
  97. c.reportError(err.Error())
  98. return
  99. }
  100. // TODO: We should check that the fields and discriminator
  101. // specified in the union are actual fields in the struct.
  102. a.Map.Unions = unions
  103. if preserveUnknownFields {
  104. a.Map.ElementType = schema.TypeRef{
  105. NamedType: &deducedName,
  106. }
  107. }
  108. a.Map.ElementRelationship, err = getMapElementRelationship(k.GetExtensions())
  109. if err != nil {
  110. c.reportError(err.Error())
  111. }
  112. }
  113. func (c *convert) VisitArray(a *proto.Array) {
  114. relationship, mapKeys, err := getListElementRelationship(a.GetExtensions())
  115. if err != nil {
  116. c.reportError(err.Error())
  117. }
  118. atom := c.top()
  119. atom.List = &schema.List{
  120. ElementType: c.makeRef(a.SubType, c.preserveUnknownFields),
  121. ElementRelationship: relationship,
  122. Keys: mapKeys,
  123. }
  124. }
  125. func (c *convert) VisitMap(m *proto.Map) {
  126. relationship, err := getMapElementRelationship(m.GetExtensions())
  127. if err != nil {
  128. c.reportError(err.Error())
  129. }
  130. a := c.top()
  131. a.Map = &schema.Map{
  132. ElementType: c.makeRef(m.SubType, c.preserveUnknownFields),
  133. ElementRelationship: relationship,
  134. }
  135. }
  136. func (c *convert) VisitPrimitive(p *proto.Primitive) {
  137. a := c.top()
  138. if c.currentName == quantityResource {
  139. a.Scalar = ptr(schema.Scalar("untyped"))
  140. } else {
  141. *a = convertPrimitive(p.Type, p.Format)
  142. }
  143. }
  144. func (c *convert) VisitArbitrary(a *proto.Arbitrary) {
  145. *c.top() = deducedDef.Atom
  146. }
  147. func (c *convert) VisitReference(proto.Reference) {
  148. // Do nothing, we handle references specially
  149. }