-
Notifications
You must be signed in to change notification settings - Fork 8
/
crd.go
269 lines (236 loc) · 8.27 KB
/
crd.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
package kuberesource
import (
"fmt"
"sort"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/hashstructure"
"github.com/solo-io/skv2/codegen/util/stringutils"
"github.com/solo-io/skv2/pkg/crdutils"
"golang.org/x/exp/maps"
"github.com/rotisserie/eris"
"github.com/solo-io/skv2/codegen/model"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Create CRDs for a group
func CustomResourceDefinitions(
groups []*model.Group,
) (objects []GlooCustomResourceDefinition, err error) {
resourcesByKind := make(map[string][]model.Resource)
skipHashByKind := make(map[string]bool)
for _, group := range groups {
for i, resource := range group.Resources {
if resource.CodegenOnly {
continue
}
resourcesByKind[resource.Kind] = append(resourcesByKind[resource.Kind], group.Resources[i])
skipHashByKind[resource.Kind] = skipHashByKind[resource.Kind] || resource.Group.SkipSpecHash
}
}
// Make ordering of crds in a group deterministic
kinds := maps.Keys(resourcesByKind)
sort.Strings(kinds)
for _, kind := range kinds {
resources := resourcesByKind[kind]
// make version ordering deterministic
sort.Slice(resources, func(i, j int) bool { return resources[i].Version < resources[j].Version })
validationSchemas, err := constructValidationSchemas(resources)
if err != nil {
return nil, err
}
crd, err := CustomResourceDefinition(resources, validationSchemas, skipHashByKind[kind])
if err != nil {
return nil, err
}
objects = append(objects, *crd)
}
return objects, nil
}
func constructValidationSchemas(resources []model.Resource) (map[string]*apiextv1.CustomResourceValidation, error) {
validationSchemas := make(map[string]*apiextv1.CustomResourceValidation)
for _, resource := range resources {
var validationSchema *apiextv1.CustomResourceValidation
validationSchema, err := constructValidationSchema(
resource.Group.RenderValidationSchemas,
resource,
resource.Group.OpenApiSchemas,
)
if err != nil {
return nil, err
}
validationSchemas[resource.Group.String()] = validationSchema
}
return validationSchemas, nil
}
func constructValidationSchema(
renderValidationSchema bool,
resource model.Resource,
oapiSchemas model.OpenApiSchemas,
) (*apiextv1.CustomResourceValidation, error) {
// Even if we do not want to render validation schemas, we should include
// the top level schema definition and preserve unknown fields since Helm
// requires that some sort of schema is defined
if !renderValidationSchema {
preserveUnknownFields := true
return &apiextv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1.JSONSchemaProps{
Type: "object",
XPreserveUnknownFields: &preserveUnknownFields,
},
}, nil
}
validationSchema := &apiextv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextv1.JSONSchemaProps{},
},
}
// Spec validation schema
specSchema, err := getJsonSchema(resource.Spec.Type.Name, oapiSchemas)
if err != nil {
return nil, eris.Wrapf(err, "constructing spec validation schema for Kind %s", resource.Kind)
}
validationSchema.OpenAPIV3Schema.Properties["spec"] = *specSchema
// Status validation schema
if resource.Status != nil {
statusSchema, err := getJsonSchema(resource.Status.Type.Name, oapiSchemas)
if err != nil {
return nil, eris.Wrapf(err, "constructing status validation schema for Kind %s", resource.Kind)
}
validationSchema.OpenAPIV3Schema.Properties["status"] = *statusSchema
}
return validationSchema, nil
}
func getJsonSchema(
schemaName string,
schemas map[string]*apiextv1.JSONSchemaProps,
) (*apiextv1.JSONSchemaProps, error) {
schema, ok := schemas[schemaName]
if !ok {
return nil, eris.Errorf("Could not find open api schema for %s", schemaName)
}
if err := validateStructural(schema); err != nil {
return nil, err
}
return schema, nil
}
// Lifted from https://github.com/istio/tools/blob/477454adf7995dd3070129998495cdc8aaec5aff/cmd/cue-gen/crd.go#L108
func validateStructural(s *apiextv1.JSONSchemaProps) error {
out := &apiext.JSONSchemaProps{}
if err := apiextv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(s, out, nil); err != nil {
return fmt.Errorf("cannot convert v1beta1 JSONSchemaProps to JSONSchemaProps: %v", err)
}
r, err := structuralschema.NewStructural(out)
if err != nil {
return fmt.Errorf("cannot convert to a structural schema: %v", err)
}
if errs := structuralschema.ValidateStructural(nil, r); len(errs) != 0 {
return fmt.Errorf("schema is not structural: %v", errs.ToAggregate().Error())
}
return nil
}
func validateCRDResources(resources []model.Resource) error {
if len(resources) < 2 {
return nil
}
scope := resources[0].ClusterScoped
shortNames := resources[0].ShortNames
categories := resources[0].Categories
for i := 1; i < len(resources); i++ {
if resources[i].ClusterScoped != scope {
return fmt.Errorf("mismatched 'currentScope' in versions of CRD for resource kind %s", resources[i].Kind)
}
if !cmp.Equal(resources[i].ShortNames, shortNames) {
return fmt.Errorf("mismatched 'ShortNames' in versions of CRD for resource kind %s", resources[i].Kind)
}
if !cmp.Equal(resources[i].Categories, categories) {
return fmt.Errorf("mismatched 'Categories' in versions of CRD for resource kind %s", resources[i].Kind)
}
}
return nil
}
type GlooCustomResourceDefinition struct {
*apiextv1.CustomResourceDefinition
// Optional: if specified from the resource, this crd definition will be wrapped in the given conditional
//
// E.g: `and (.Values.customValueA) (.Values.customValueB)`
CustomEnableCondition string `json:"-" yaml:"-"`
}
func CustomResourceDefinition(
resources []model.Resource,
validationSchemas map[string]*apiextv1.CustomResourceValidation,
withoutSpecHash bool,
) (*GlooCustomResourceDefinition, error) {
err := validateCRDResources(resources)
if err != nil {
return nil, err
}
group := resources[0].Group.Group
kind := resources[0].Kind
kindLowerPlural := strings.ToLower(stringutils.Pluralize(kind))
kindLower := strings.ToLower(kind)
customEnableCondition := resources[0].CustomEnableCondition
scope := apiextv1.NamespaceScoped
if resources[0].ClusterScoped {
scope = apiextv1.ClusterScoped
}
versions := make([]apiextv1.CustomResourceDefinitionVersion, 0, len(resources))
for _, resource := range resources {
var status *apiextv1.CustomResourceSubresourceStatus
if resource.Status != nil {
status = &apiextv1.CustomResourceSubresourceStatus{}
}
v := apiextv1.CustomResourceDefinitionVersion{
Name: resource.Group.Version,
Served: true,
Storage: resource.Stored,
Deprecated: resource.Deprecated,
AdditionalPrinterColumns: resource.AdditionalPrinterColumns,
Subresources: &apiextv1.CustomResourceSubresources{
Status: status,
},
Schema: validationSchemas[resource.Group.String()],
}
versions = append(versions, v)
}
crd := &apiextv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: apiextv1.SchemeGroupVersion.String(),
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", kindLowerPlural, group),
},
Spec: apiextv1.CustomResourceDefinitionSpec{
Group: group,
Scope: scope,
Versions: versions,
Names: apiextv1.CustomResourceDefinitionNames{
Plural: kindLowerPlural,
Singular: kindLower,
Kind: kind,
ShortNames: resources[0].ShortNames,
ListKind: kind + "List",
Categories: resources[0].Categories,
},
},
}
if !withoutSpecHash {
// hashstructure of the crd spec:
specHash, err := hashstructure.Hash(crd.Spec, nil)
if err != nil {
panic(err)
}
crd.Annotations = map[string]string{
crdutils.CRDSpecHashKey: fmt.Sprintf("%x", specHash),
}
}
if len(validationSchemas) > 0 {
// Setting PreserveUnknownFields to false ensures that objects with unknown fields are rejected.
crd.Spec.PreserveUnknownFields = false
}
return &GlooCustomResourceDefinition{CustomResourceDefinition: crd, CustomEnableCondition: customEnableCondition}, nil
}