/
schema.go
167 lines (148 loc) · 5.84 KB
/
schema.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
/*
Copyright 2021 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 variables
import (
"encoding/json"
"fmt"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
// convertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
// NOTE: This is used whenever we want to use one of the upstream libraries, as they use apiextensions.JSONSchemaProp.
// NOTE: If new fields are added to clusterv1.JSONSchemaProps (e.g. to support complex types), the corresponding
// schema validation must be added to validateRootSchema too.
func convertToAPIExtensionsJSONSchemaProps(schema *clusterv1.JSONSchemaProps, fldPath *field.Path) (*apiextensions.JSONSchemaProps, field.ErrorList) {
var allErrs field.ErrorList
props := &apiextensions.JSONSchemaProps{
Type: schema.Type,
Required: schema.Required,
MaxItems: schema.MaxItems,
MinItems: schema.MinItems,
UniqueItems: schema.UniqueItems,
Format: schema.Format,
MaxLength: schema.MaxLength,
MinLength: schema.MinLength,
Pattern: schema.Pattern,
ExclusiveMaximum: schema.ExclusiveMaximum,
ExclusiveMinimum: schema.ExclusiveMinimum,
}
// Only set XPreserveUnknownFields to true if it's true.
// apiextensions.JSONSchemaProps only allows setting XPreserveUnknownFields
// to true or undefined, false is forbidden.
if schema.XPreserveUnknownFields {
props.XPreserveUnknownFields = pointer.Bool(true)
}
if schema.Default != nil && schema.Default.Raw != nil {
var v interface{}
if err := json.Unmarshal(schema.Default.Raw, &v); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("default"), string(schema.Default.Raw),
fmt.Sprintf("default is not valid JSON: %v", err)))
} else {
var v apiextensions.JSON
err := apiextensionsv1.Convert_v1_JSON_To_apiextensions_JSON(schema.Default, &v, nil)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("default"), string(schema.Default.Raw),
fmt.Sprintf("failed to convert default: %v", err)))
} else {
props.Default = &v
}
}
}
if len(schema.Enum) > 0 {
for i, enum := range schema.Enum {
if enum.Raw == nil {
continue
}
var v interface{}
if err := json.Unmarshal(enum.Raw, &v); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("enum").Index(i), string(enum.Raw),
fmt.Sprintf("enum value is not valid JSON: %v", err)))
} else {
var v apiextensions.JSON
err := apiextensionsv1.Convert_v1_JSON_To_apiextensions_JSON(&schema.Enum[i], &v, nil)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("enum").Index(i), string(enum.Raw),
fmt.Sprintf("failed to convert enum value: %v", err)))
} else {
props.Enum = append(props.Enum, v)
}
}
}
}
if schema.Example != nil && schema.Example.Raw != nil {
var v interface{}
if err := json.Unmarshal(schema.Example.Raw, &v); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("example"), string(schema.Example.Raw),
fmt.Sprintf("example is not valid JSON: %v", err)))
} else {
var value apiextensions.JSON
err := apiextensionsv1.Convert_v1_JSON_To_apiextensions_JSON(schema.Example, &value, nil)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("example"), string(schema.Example.Raw),
fmt.Sprintf("failed to convert example value: %v", err)))
} else {
props.Example = &value
}
}
}
if schema.Maximum != nil {
f := float64(*schema.Maximum)
props.Maximum = &f
}
if schema.Minimum != nil {
f := float64(*schema.Minimum)
props.Minimum = &f
}
if schema.AdditionalProperties != nil {
apiExtensionsSchema, err := convertToAPIExtensionsJSONSchemaProps(schema.AdditionalProperties, fldPath.Child("additionalProperties"))
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("additionalProperties"), "",
fmt.Sprintf("failed to convert schema: %v", err)))
} else {
props.AdditionalProperties = &apiextensions.JSONSchemaPropsOrBool{
// Allows must be true to allow "additional properties".
// Otherwise only the ones from .Properties are allowed.
Allows: true,
Schema: apiExtensionsSchema,
}
}
}
if len(schema.Properties) > 0 {
props.Properties = map[string]apiextensions.JSONSchemaProps{}
for propertyName, propertySchema := range schema.Properties {
p := propertySchema
apiExtensionsSchema, err := convertToAPIExtensionsJSONSchemaProps(&p, fldPath.Child("properties").Key(propertyName))
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("properties").Key(propertyName), "",
fmt.Sprintf("failed to convert schema: %v", err)))
} else {
props.Properties[propertyName] = *apiExtensionsSchema
}
}
}
if schema.Items != nil {
apiExtensionsSchema, err := convertToAPIExtensionsJSONSchemaProps(schema.Items, fldPath.Child("items"))
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("items"), "",
fmt.Sprintf("failed to convert schema: %v", err)))
} else {
props.Items = &apiextensions.JSONSchemaPropsOrArray{
Schema: apiExtensionsSchema,
}
}
}
return props, allErrs
}