/
match_schema.go
49 lines (41 loc) 路 1.64 KB
/
match_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
package target
import (
"fmt"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
)
var matchJSONSchemaProps apiextensions.JSONSchemaProps
func init() {
matchCRD := &apiextensionsv1.CustomResourceDefinition{}
if err := yaml.Unmarshal([]byte(matchYAML), matchCRD); err != nil {
panic(fmt.Errorf("failed to unmarshal match yaml: %w", err))
}
// Sanity checks to ensure the CRD was generated properly
if len(matchCRD.Spec.Versions) != 1 {
panic(fmt.Errorf("generated match CRD does not contain any versions"))
}
if matchCRD.Spec.Versions[0].Schema.OpenAPIV3Schema == nil {
panic(fmt.Errorf("generated match CRD has nil OpenAPIV3Schema"))
}
// Convert v1 JSONSchemaProps to versionless
rt := runtime.NewScheme()
if err := apiextensions.AddToScheme(rt); err != nil {
panic(fmt.Errorf("could not add apiextensions to scheme: %w", err))
}
if err := apiextensionsv1.AddToScheme(rt); err != nil {
panic(fmt.Errorf("could not add apiextensionsv1 to scheme: %w", err))
}
embedded := matchCRD.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["embeddedMatch"]
if err := rt.Convert(&embedded, &matchJSONSchemaProps, nil); err != nil {
panic(fmt.Errorf("could not convert match JSONSchemaProps from v1 to versionless: %w", err))
}
// Verify conversion worked by checking properties field
if _, exists := matchJSONSchemaProps.Properties["kinds"]; !exists {
panic("converted match schema does not have 'kinds' field")
}
}
func matchSchema() apiextensions.JSONSchemaProps {
return *matchJSONSchemaProps.DeepCopy()
}