|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package crds |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/santhosh-tekuri/jsonschema/v5" |
| 23 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// AssertMajorVersion checks the given majorVersion exists for the given kind and CRD version |
| 27 | +func AssertMajorVersion(specVersion *apiextensionsv1.CustomResourceDefinitionVersion, kind string, majorVersion string) error { |
| 28 | + props, err := GetOpenAPIProperties(kind, specVersion) |
| 29 | + if err != nil { |
| 30 | + return fmt.Errorf("failed to enumerate CRD schema properties: %w", err) |
| 31 | + } |
| 32 | + specProps, err := GetSpecPropertiesFor(kind, props, "spec") |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to enumerate CRD spec properties: %w", err) |
| 35 | + } |
| 36 | + _, ok := specProps[majorVersion] |
| 37 | + if !ok { |
| 38 | + return fmt.Errorf("failed to match the CRD spec version %q in schema", majorVersion) |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// CompileCRDSchema compiles the given JSON schema properties |
| 44 | +func CompileCRDSchema(openAPISchema *apiextensionsv1.JSONSchemaProps) (*jsonschema.Schema, error) { |
| 45 | + schemaBytes, err := json.Marshal(openAPISchema) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to marshal CRD schema to JSON: %w", err) |
| 48 | + } |
| 49 | + compiler := jsonschema.NewCompiler() |
| 50 | + if err := compiler.AddResource("schema.json", bytes.NewReader(schemaBytes)); err != nil { |
| 51 | + return nil, fmt.Errorf("failed to add schema resource: %w", err) |
| 52 | + } |
| 53 | + schema, err := compiler.Compile("schema.json") |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to compile schema: %w", err) |
| 56 | + } |
| 57 | + return schema, nil |
| 58 | +} |
| 59 | + |
| 60 | +// SelectVersion returns the version from the CRD spec that matches the given version string |
| 61 | +func SelectVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version string) *apiextensionsv1.CustomResourceDefinitionVersion { |
| 62 | + if len(spec.Versions) == 0 { |
| 63 | + return nil |
| 64 | + } |
| 65 | + if version == "" { |
| 66 | + return &spec.Versions[0] |
| 67 | + } |
| 68 | + for _, v := range spec.Versions { |
| 69 | + if v.Name == version { |
| 70 | + return &v |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// GetOpenAPIProperties digs up the schema properties of a given kind on a given CRD version |
| 77 | +func GetOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDefinitionVersion) (map[string]apiextensionsv1.JSONSchemaProps, error) { |
| 78 | + if version == nil { |
| 79 | + return nil, fmt.Errorf("missing version (nil) from %v spec", kind) |
| 80 | + } |
| 81 | + if version.Schema == nil { |
| 82 | + return nil, fmt.Errorf("missing version schema from %v spec", kind) |
| 83 | + } |
| 84 | + if version.Schema.OpenAPIV3Schema == nil { |
| 85 | + return nil, fmt.Errorf("missing version OpenAPI Schema from %v spec", kind) |
| 86 | + } |
| 87 | + if version.Schema.OpenAPIV3Schema.Properties == nil { |
| 88 | + return nil, fmt.Errorf("missing version OpenAPI Properties from %v spec", kind) |
| 89 | + } |
| 90 | + return version.Schema.OpenAPIV3Schema.Properties, nil |
| 91 | +} |
| 92 | + |
| 93 | +// GetSpecPropertiesFor takes the properties value of a given field of a kind's properties set |
| 94 | +func GetSpecPropertiesFor(kind string, props map[string]apiextensionsv1.JSONSchemaProps, field string) (map[string]apiextensionsv1.JSONSchemaProps, error) { |
| 95 | + prop, ok := props[field] |
| 96 | + if !ok { |
| 97 | + return nil, fmt.Errorf("kind %q spec is missing field %q on", kind, field) |
| 98 | + } |
| 99 | + if prop.Type != "object" { |
| 100 | + return nil, fmt.Errorf("kind %q field %q expected to be object but is %v", kind, field, prop.Type) |
| 101 | + } |
| 102 | + return prop.Properties, nil |
| 103 | +} |
0 commit comments