-
Notifications
You must be signed in to change notification settings - Fork 8
/
crd.go
174 lines (150 loc) · 4.95 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
package kuberesource
import (
"fmt"
"strings"
"github.com/mitchellh/hashstructure"
"github.com/solo-io/skv2/codegen/util/stringutils"
"github.com/solo-io/skv2/pkg/crdutils"
"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(
group model.Group,
) (objects []metav1.Object, err error) {
for _, resource := range group.Resources {
var validationSchema *apiextv1.CustomResourceValidation
if group.RenderValidationSchemas {
validationSchema, err = constructValidationSchema(resource, group.OpenApiSchemas)
if err != nil {
return nil, err
}
}
objects = append(objects, CustomResourceDefinition(resource, validationSchema, group.SkipSpecHash))
}
return objects, nil
}
func constructValidationSchema(
resource model.Resource,
oapiSchemas model.OpenApiSchemas,
) (*apiextv1.CustomResourceValidation, error) {
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 CustomResourceDefinition(
resource model.Resource,
validationSchema *apiextv1.CustomResourceValidation,
withoutSpecHash bool,
) *apiextv1.CustomResourceDefinition {
group := resource.Group.Group
version := resource.Group.Version
kind := resource.Kind
kindLowerPlural := strings.ToLower(stringutils.Pluralize(kind))
kindLower := strings.ToLower(kind)
var status *apiextv1.CustomResourceSubresourceStatus
if resource.Status != nil {
status = &apiextv1.CustomResourceSubresourceStatus{}
}
scope := apiextv1.NamespaceScoped
if resource.ClusterScoped {
scope = apiextv1.ClusterScoped
}
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: []apiextv1.CustomResourceDefinitionVersion{
{
Name: version,
Served: true,
Storage: true,
AdditionalPrinterColumns: resource.AdditionalPrinterColumns,
Subresources: &apiextv1.CustomResourceSubresources{
Status: status,
},
Schema: validationSchema,
},
},
Names: apiextv1.CustomResourceDefinitionNames{
Plural: kindLowerPlural,
Singular: kindLower,
Kind: kind,
ShortNames: resource.ShortNames,
ListKind: kind + "List",
},
},
}
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 validationSchema != nil {
// Setting PreserveUnknownFields to false ensures that objects with unknown fields are rejected.
crd.Spec.PreserveUnknownFields = false
}
return crd
}