-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathschema.go
More file actions
320 lines (285 loc) · 10.4 KB
/
schema.go
File metadata and controls
320 lines (285 loc) · 10.4 KB
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
// Copyright 2016-2020, Pulumi Corporation.
//
// 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 codegen
import (
"fmt"
"sort"
"strconv"
"github.com/iancoleman/strcase"
"github.com/pulumi/crd2pulumi/internal/slices"
"github.com/pulumi/crd2pulumi/internal/unstruct"
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// DefaultName specifies the default value for the package name
const DefaultName = "crds"
const (
Boolean string = "boolean"
Integer string = "integer"
Number string = "number"
String string = "string"
Array string = "array"
Object string = "object"
)
const anyTypeRef = "pulumi.json#/Any"
var anyTypeSpec = pschema.TypeSpec{
Ref: anyTypeRef,
}
var arbitraryJSONTypeSpec = pschema.TypeSpec{
Type: Object,
AdditionalProperties: &anyTypeSpec,
}
var emptySpec = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: Object,
Properties: map[string]pschema.PropertySpec{},
},
}
const objectMetaRef = "#/types/kubernetes:meta/v1:ObjectMeta"
const objectMetaToken = "kubernetes:meta/v1:ObjectMeta"
// Union type of integer and string
var intOrStringTypeSpec = pschema.TypeSpec{
OneOf: []pschema.TypeSpec{
{
Type: Integer,
},
{
Type: String,
},
},
}
// Returns the Pulumi package given a types map and a slice of the token types
// of every CustomResource. If includeObjectMetaType is true, then a
// ObjectMetaType type is also generated.
func genPackage(version string, types map[string]pschema.ComplexTypeSpec, resourceTokens []string, includeObjectMetaType bool) (*pschema.Package, error) {
if includeObjectMetaType {
types[objectMetaToken] = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: "object",
},
}
}
packages := map[string]bool{DefaultName: true, "kubernetes": true}
resources := map[string]pschema.ResourceSpec{}
for _, baseRef := range resourceTokens {
complexTypeSpec := types[baseRef]
resources[baseRef] = pschema.ResourceSpec{
ObjectTypeSpec: complexTypeSpec.ObjectTypeSpec,
InputProperties: complexTypeSpec.Properties,
}
packages[string(tokens.ModuleMember(baseRef).Package())] = true
}
allowedPackages := make([]string, 0, len(packages))
for pkg := range packages {
allowedPackages = append(allowedPackages, pkg)
}
sort.Strings(allowedPackages)
pkgSpec := pschema.PackageSpec{
Name: DefaultName,
Version: version,
Types: types,
Resources: resources,
AllowedPackageNames: allowedPackages,
}
pkg, err := pschema.ImportSpec(pkgSpec, nil)
if err != nil {
return &pschema.Package{}, fmt.Errorf("could not import spec: %w", err)
}
if includeObjectMetaType {
delete(types, objectMetaToken)
}
return pkg, nil
}
// Returns true if the given TypeSpec is of type any; returns false otherwise
func isAnyType(typeSpec pschema.TypeSpec) bool {
return typeSpec.Ref == anyTypeRef
}
// AddType converts the given OpenAPI `schema` to a ObjectTypeSpec and adds it
// to the `types` map under the given `name`. Recursively converts and adds all
// nested schemas as well.
func AddType(schema map[string]any, name string, types map[string]pschema.ComplexTypeSpec) {
properties, foundProperties, _ := unstructured.NestedMap(schema, "properties")
description, _, _ := unstructured.NestedString(schema, "description")
schemaType, _, _ := unstructured.NestedString(schema, "type")
required, _, _ := unstructured.NestedStringSlice(schema, "required")
propertySpecs := map[string]pschema.PropertySpec{}
for propertyName := range properties {
propertySchema, _, _ := unstructured.NestedMap(properties, propertyName)
propertyDescription, _, _ := unstructured.NestedString(propertySchema, "description")
defaultValue, _, _ := unstructured.NestedFieldNoCopy(propertySchema, "default")
propertySpecs[propertyName] = pschema.PropertySpec{
TypeSpec: GetTypeSpec(propertySchema, name+strcase.ToCamel(propertyName), types),
Description: propertyDescription,
Default: defaultValue,
}
}
// If the type wasn't specified but we found properties, then we can infer that the type is an object
if foundProperties && schemaType == "" {
schemaType = Object
}
types[name] = pschema.ComplexTypeSpec{
ObjectTypeSpec: pschema.ObjectTypeSpec{
Type: schemaType,
Properties: propertySpecs,
Required: required,
Description: description,
}}
}
// GetTypeSpec returns the corresponding pschema.TypeSpec for a OpenAPI v3
// schema. Handles nested pschema.TypeSpecs in case the schema type is an array,
// object, or "combined schema" (oneOf, allOf, anyOf). Also recursively converts
// and adds all schemas of type object to the types map.
func GetTypeSpec(schema map[string]any, name string, types map[string]pschema.ComplexTypeSpec) pschema.TypeSpec {
if schema == nil {
return anyTypeSpec
}
intOrString, foundIntOrString, _ := unstructured.NestedBool(schema, "x-kubernetes-int-or-string")
if foundIntOrString && intOrString {
return intOrStringTypeSpec
}
// If the schema is of the `oneOf` type: return a TypeSpec with the `OneOf`
// field filled with the TypeSpec of all sub-schemas.
oneOf, foundOneOf, _ := unstruct.NestedMapSlice(schema, "oneOf")
if foundOneOf {
oneOfTypeSpecs := make([]pschema.TypeSpec, 0, len(oneOf))
for i, oneOfSchema := range oneOf {
oneOfTypeSpec := GetTypeSpec(oneOfSchema, name+"OneOf"+strconv.Itoa(i), types)
if isAnyType(oneOfTypeSpec) {
return anyTypeSpec
}
oneOfTypeSpecs = append(oneOfTypeSpecs, oneOfTypeSpec)
}
return pschema.TypeSpec{
OneOf: oneOfTypeSpecs,
}
}
// If the schema is of `allOf` type: combine `properties` and `required`
// fields of sub-schemas into a single schema. Then return the `TypeSpec`
// of that combined schema.
allOf, foundAllOf, _ := unstruct.NestedMapSlice(schema, "allOf")
if foundAllOf {
combinedSchema := CombineSchemas(true, allOf...)
return GetTypeSpec(combinedSchema, name, types)
}
// If the schema is of `anyOf` type: combine only `properties` of
// sub-schemas into a single schema, with all `properties` set to optional.
// Then return the `TypeSpec` of that combined schema.
anyOf, foundAnyOf, _ := unstruct.NestedMapSlice(schema, "anyOf")
if foundAnyOf {
combinedSchema := CombineSchemas(false, anyOf...)
return GetTypeSpec(combinedSchema, name, types)
}
preserveUnknownFields, foundPreserveUnknownFields, _ := unstructured.NestedBool(schema, "x-kubernetes-preserve-unknown-fields")
if foundPreserveUnknownFields && preserveUnknownFields {
return arbitraryJSONTypeSpec
}
// If the the schema wasn't some combination of other types (`oneOf`,
// `allOf`, `anyOf`), then it must have a "type" field, otherwise we
// cannot represent it. If we cannot represent it, we simply set it to be
// any type.
schemaType, foundSchemaType, _ := unstructured.NestedString(schema, "type")
if !foundSchemaType {
return anyTypeSpec
}
switch schemaType {
case Array:
items, _, _ := unstructured.NestedMap(schema, "items")
arrayTypeSpec := GetTypeSpec(items, name, types)
return pschema.TypeSpec{
Type: Array,
Items: &arrayTypeSpec,
}
case Object:
AddType(schema, name, types)
// If `additionalProperties` has a sub-schema, then we generate a type for a map from string --> sub-schema type
additionalProperties, foundAdditionalProperties, _ := unstructured.NestedMap(schema, "additionalProperties")
if foundAdditionalProperties {
additionalPropertiesTypeSpec := GetTypeSpec(additionalProperties, name, types)
return pschema.TypeSpec{
Type: Object,
AdditionalProperties: &additionalPropertiesTypeSpec,
}
}
// `additionalProperties: true` is equivalent to `additionalProperties: {}`, meaning a map from string -> any
additionalPropertiesIsTrue, additionalPropertiesIsTrueFound, _ := unstructured.NestedBool(schema, "additionalProperties")
if additionalPropertiesIsTrueFound && additionalPropertiesIsTrue {
return pschema.TypeSpec{
Type: Object,
AdditionalProperties: &anyTypeSpec,
}
}
// If no properties are found, then it can be arbitrary JSON
_, foundProperties, _ := unstructured.NestedMap(schema, "properties")
if !foundProperties {
return arbitraryJSONTypeSpec
}
// If properties are found, then we must specify those in a seperate interface
return pschema.TypeSpec{
Type: Object,
Ref: "#/types/" + name,
}
case Integer:
fallthrough
case Boolean:
fallthrough
case String:
fallthrough
case Number:
return pschema.TypeSpec{
Type: schemaType,
}
default:
return anyTypeSpec
}
}
// CombineSchemas combines the `properties` fields of the given sub-schemas into
// a single schema. Returns nil if no schemas are given. Returns the schema if
// only 1 schema is given. If combineRequired == true, then each sub-schema's
// `required` fields are also combined. In this case the combined schema's
// `required` field is of type []any, not []string.
func CombineSchemas(combineRequired bool, schemas ...map[string]any) map[string]any {
if len(schemas) == 0 {
return nil
}
if len(schemas) == 1 {
return schemas[0]
}
combinedProperties := map[string]any{}
combinedRequired := make([]string, 0)
for _, schema := range schemas {
properties, _, _ := unstructured.NestedMap(schema, "properties")
for propertyName := range properties {
propertySchema, _, _ := unstructured.NestedMap(properties, propertyName)
combinedProperties[propertyName] = propertySchema
}
if combineRequired {
required, foundRequired, _ := unstructured.NestedStringSlice(schema, "required")
if foundRequired {
combinedRequired = append(combinedRequired, required...)
}
}
}
combinedSchema := map[string]any{
"type": Object,
"properties": combinedProperties,
}
if combineRequired {
combinedSchema["required"] = slices.ToAny(combinedRequired)
}
return combinedSchema
}
func getToken(group, version, kind string) string {
return fmt.Sprintf("kubernetes:%s/%s:%s", group, version, kind)
}