forked from kubernetes/kubernetes
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.go
391 lines (338 loc) · 12.1 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi
import (
"fmt"
"strings"
"github.com/go-openapi/spec"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
)
// groupVersionKindExtensionKey is the key used to lookup the GroupVersionKind value
// for an object definition from the definition's "extensions" map.
const groupVersionKindExtensionKey = "x-kubernetes-group-version-kind"
// Integer is the name for integer types
const Integer = "integer"
// String is the name for string types
const String = "string"
// Bool is the name for boolean types
const Boolean = "boolean"
// Map is the name for map types
// types.go struct fields that are maps will have an open API type "object"
// types.go struct fields that are actual objects appearing as a struct
// in a types.go file will have no type defined
// and have a json pointer reference to the type definition
const Map = "object"
// Array is the name for array types
const Array = "array"
// Resources contains the object definitions for Kubernetes resource apis
// Fields are public for binary serialization (private fields don't get serialized)
type Resources struct {
// GroupVersionKindToName maps GroupVersionKinds to Type names
GroupVersionKindToName map[schema.GroupVersionKind]string
// NameToDefinition maps Type names to TypeDefinitions
NameToDefinition map[string]Kind
}
// LookupResource returns the Kind for the specified groupVersionKind
func (r Resources) LookupResource(groupVersionKind schema.GroupVersionKind) (Kind, bool) {
name, found := r.GroupVersionKindToName[groupVersionKind]
if !found {
return Kind{}, false
}
def, found := r.NameToDefinition[name]
if !found {
return Kind{}, false
}
return def, true
}
// Kind defines a Kubernetes object Kind
type Kind struct {
// Name is the lookup key given to this Kind by the open API spec.
// May not contain any semantic meaning or relation to the API definition,
// simply must be unique for each object definition in the Open API spec.
// e.g. io.k8s.kubernetes.pkg.apis.apps.v1beta1.Deployment
Name string
// IsResource is true if the Kind is a Resource (it has API endpoints)
// e.g. Deployment is a Resource, DeploymentStatus is NOT a Resource
IsResource bool
// GroupVersionKind uniquely defines a resource type in the Kubernetes API
// and is present for all resources.
// Empty for non-resource Kinds (e.g. those without APIs).
// e.g. "Group": "apps", "Version": "v1beta1", "Kind": "Deployment"
GroupVersionKind schema.GroupVersionKind
// Present only for definitions that represent primitive types with additional
// semantic meaning beyond just string, integer, boolean - e.g.
// Fields with a PrimitiveType should follow the validation of the primitive type.
// io.k8s.apimachinery.pkg.apis.meta.v1.Time
// io.k8s.apimachinery.pkg.util.intstr.IntOrString
PrimitiveType string
// Extensions are openapi extensions for the object definition.
Extensions spec.Extensions
// Fields are the fields defined for this Kind
Fields map[string]Type
}
// Type defines a field type and are expected to be one of:
// - IsKind
// - IsMap
// - IsArray
// - IsPrimitive
type Type struct {
// Name is the name of the type
TypeName string
// IsKind is true if the definition represents a Kind
IsKind bool
// IsPrimitive is true if the definition represents a primitive type - e.g. string, boolean, integer
IsPrimitive bool
// IsArray is true if the definition represents an array type
IsArray bool
// IsMap is true if the definition represents a map type
IsMap bool
// ElementType will be specified for arrays and maps
// if IsMap == true, then ElementType is the type of the value (key is always string)
// if IsArray == true, then ElementType is the type of the element
ElementType *Type
// Extensions are extensions for this field and may contain
// metadata from the types.go struct field tags.
// e.g. contains patchStrategy, patchMergeKey, etc
Extensions spec.Extensions
}
// NewOpenAPIData parses the resource definitions in openapi data by groupversionkind and name
func NewOpenAPIData(s *spec.Swagger) (*Resources, error) {
o := &Resources{
GroupVersionKindToName: map[schema.GroupVersionKind]string{},
NameToDefinition: map[string]Kind{},
}
// Parse and index definitions by name
for name, d := range s.Definitions {
definition := o.parseDefinition(name, d)
o.NameToDefinition[name] = definition
if len(definition.GroupVersionKind.Kind) > 0 {
o.GroupVersionKindToName[definition.GroupVersionKind] = name
}
}
if err := o.validate(); err != nil {
return nil, err
}
return o, nil
}
// validate makes sure the definition for each field type is found in the map
func (o *Resources) validate() error {
types := sets.String{}
for _, d := range o.NameToDefinition {
for _, f := range d.Fields {
for _, t := range o.getTypeNames(f) {
types.Insert(t)
}
}
}
for _, n := range types.List() {
_, found := o.NameToDefinition[n]
if !found {
return fmt.Errorf("Unable to find definition for field of type %v", n)
}
}
return nil
}
func (o *Resources) getTypeNames(elem Type) []string {
t := []string{}
if elem.IsKind {
t = append(t, elem.TypeName)
}
if elem.ElementType != nil && elem.ElementType.IsKind {
t = append(t, o.getTypeNames(*elem.ElementType)...)
}
return t
}
func (o *Resources) parseDefinition(name string, s spec.Schema) Kind {
gvk, err := o.getGroupVersionKind(s)
value := Kind{
Name: name,
GroupVersionKind: gvk,
Extensions: s.Extensions,
Fields: map[string]Type{},
}
if err != nil {
glog.V(2).Info(err)
}
// Definition represents a primitive type - e.g.
// io.k8s.apimachinery.pkg.util.intstr.IntOrString
if o.isPrimitive(s) {
value.PrimitiveType = o.getTypeNameForField(s)
}
for fieldname, property := range s.Properties {
value.Fields[fieldname] = o.parseField(property)
}
return value
}
func (o *Resources) parseField(s spec.Schema) Type {
def := Type{
TypeName: o.getTypeNameForField(s),
IsPrimitive: o.isPrimitive(s),
IsArray: o.isArray(s),
IsMap: o.isMap(s),
IsKind: o.isDefinitionReference(s),
}
if elementType, arrayErr := o.getElementType(s); arrayErr == nil {
d := o.parseField(elementType)
def.ElementType = &d
} else if valueType, mapErr := o.getValueType(s); mapErr == nil {
d := o.parseField(valueType)
def.ElementType = &d
}
def.Extensions = s.Extensions
return def
}
// isArray returns true if s is an array type.
func (o *Resources) isArray(s spec.Schema) bool {
if len(s.Properties) > 0 {
// Open API can have embedded type definitions, but Kubernetes doesn't generate these.
// This should just be a sanity check against changing the format.
return false
}
return o.getType(s) == Array
}
// isMap returns true if s is a map type.
func (o *Resources) isMap(s spec.Schema) bool {
if len(s.Properties) > 0 {
// Open API can have embedded type definitions, but Kubernetes doesn't generate these.
// This should just be a sanity check against changing the format.
return false
}
return o.getType(s) == Map
}
// isPrimitive returns true if s is a primitive type
// Note: For object references that represent primitive types - e.g. IntOrString - this will
// be false, and the referenced Kind will have a non-empty "PrimitiveType".
func (o *Resources) isPrimitive(s spec.Schema) bool {
if len(s.Properties) > 0 {
// Open API can have embedded type definitions, but Kubernetes doesn't generate these.
// This should just be a sanity check against changing the format.
return false
}
t := o.getType(s)
if t == Integer || t == Boolean || t == String {
return true
}
return false
}
func (*Resources) getType(s spec.Schema) string {
if len(s.Type) != 1 {
return ""
}
return strings.ToLower(s.Type[0])
}
func (o *Resources) getTypeNameForField(s spec.Schema) string {
// Get the reference for complex types
if o.isDefinitionReference(s) {
return o.nameForDefinitionField(s)
}
// Recurse if type is array
if o.isArray(s) {
return fmt.Sprintf("%s array", o.getTypeNameForField(*s.Items.Schema))
}
if o.isMap(s) {
return fmt.Sprintf("%s map", o.getTypeNameForField(*s.AdditionalProperties.Schema))
}
// Get the value for primitive types
if o.isPrimitive(s) {
return fmt.Sprintf("%s", s.Type[0])
}
return ""
}
// isDefinitionReference returns true s is a complex type that should have a Kind.
func (o *Resources) isDefinitionReference(s spec.Schema) bool {
if len(s.Properties) > 0 {
// Open API can have embedded type definitions, but Kubernetes doesn't generate these.
// This should just be a sanity check against changing the format.
return false
}
if len(s.Type) > 0 {
// Definition references won't have a type
return false
}
p := s.SchemaProps.Ref.GetPointer().String()
return len(p) > 0 && strings.HasPrefix(p, "/definitions/")
}
// getElementType returns the type of an element for arrays
// returns an error if s is not an array.
func (o *Resources) getElementType(s spec.Schema) (spec.Schema, error) {
if !o.isArray(s) {
return spec.Schema{}, fmt.Errorf("%v is not an array type", s.Type)
}
return *s.Items.Schema, nil
}
// getElementType returns the type of an element for maps
// returns an error if s is not a map.
func (o *Resources) getValueType(s spec.Schema) (spec.Schema, error) {
if !o.isMap(s) {
return spec.Schema{}, fmt.Errorf("%v is not an map type", s.Type)
}
return *s.AdditionalProperties.Schema, nil
}
// nameForDefinitionField returns the definition name for the schema (field) if it is a complex type
func (o *Resources) nameForDefinitionField(s spec.Schema) string {
p := s.SchemaProps.Ref.GetPointer().String()
if len(p) == 0 {
return ""
}
// Strip the "definitions/" pieces of the reference
return strings.Replace(p, "/definitions/", "", -1)
}
// getGroupVersionKind implements OpenAPIData
// getGVK parses the gropuversionkind for a resource definition from the x-kubernetes
// extensions
// Expected format for s.Extensions: map[string][]map[string]string
// map[x-kubernetes-group-version-kind:[map[Group:authentication.k8s.io Version:v1 Kind:TokenReview]]]
func (o *Resources) getGroupVersionKind(s spec.Schema) (schema.GroupVersionKind, error) {
empty := schema.GroupVersionKind{}
// Get the extensions
extList, f := s.Extensions[groupVersionKindExtensionKey]
if !f {
return empty, fmt.Errorf("No %s extension present in %v", groupVersionKindExtensionKey, s.Extensions)
}
// Expect a empty of a list with 1 element
extListCasted, ok := extList.([]interface{})
if !ok {
return empty, fmt.Errorf("%s extension has unexpected type %T in %s", groupVersionKindExtensionKey, extListCasted, s.Extensions)
}
if len(extListCasted) == 0 {
return empty, fmt.Errorf("No Group Version Kind found in %v", extListCasted)
}
if len(extListCasted) != 1 {
return empty, fmt.Errorf("Multiple Group Version gvkToName found in %v", extListCasted)
}
gvk := extListCasted[0]
// Expect a empty of a map with 3 entries
gvkMap, ok := gvk.(map[string]interface{})
if !ok {
return empty, fmt.Errorf("%s extension has unexpected type %T in %s", groupVersionKindExtensionKey, gvk, s.Extensions)
}
group, ok := gvkMap["group"].(string)
if !ok {
return empty, fmt.Errorf("%s extension missing Group: %v", groupVersionKindExtensionKey, gvkMap)
}
version, ok := gvkMap["version"].(string)
if !ok {
return empty, fmt.Errorf("%s extension missing Version: %v", groupVersionKindExtensionKey, gvkMap)
}
kind, ok := gvkMap["kind"].(string)
if !ok {
return empty, fmt.Errorf("%s extension missing Kind: %v", groupVersionKindExtensionKey, gvkMap)
}
return schema.GroupVersionKind{
Group: group,
Version: version,
Kind: kind,
}, nil
}