feat: kubesphere 4.0 (#6115)

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

---------

Signed-off-by: ci-bot <ci-bot@kubesphere.io>
Co-authored-by: ks-ci-bot <ks-ci-bot@example.com>
Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
KubeSphere CI Bot
2024-09-06 11:05:52 +08:00
committed by GitHub
parent b5015ec7b9
commit 447a51f08b
8557 changed files with 546695 additions and 1146174 deletions

View File

@@ -0,0 +1,65 @@
/*
Copyright 2019 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 validation
import (
"strings"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kube-openapi/pkg/validation/spec"
)
var supportedFormats = sets.NewString(
"bsonobjectid", // bson object ID
"uri", // an URI as parsed by Golang net/url.ParseRequestURI
"email", // an email address as parsed by Golang net/mail.ParseAddress
"hostname", // a valid representation for an Internet host name, as defined by RFC 1034, section 3.1 [RFC1034].
"ipv4", // an IPv4 IP as parsed by Golang net.ParseIP
"ipv6", // an IPv6 IP as parsed by Golang net.ParseIP
"cidr", // a CIDR as parsed by Golang net.ParseCIDR
"mac", // a MAC address as parsed by Golang net.ParseMAC
"uuid", // an UUID that allows uppercase defined by the regex (?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$
"uuid3", // an UUID3 that allows uppercase defined by the regex (?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?3[0-9a-f]{3}-?[0-9a-f]{4}-?[0-9a-f]{12}$
"uuid4", // an UUID4 that allows uppercase defined by the regex (?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?4[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$
"uuid5", // an UUID6 that allows uppercase defined by the regex (?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?5[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$
"isbn", // an ISBN10 or ISBN13 number string like "0321751043" or "978-0321751041"
"isbn10", // an ISBN10 number string like "0321751043"
"isbn13", // an ISBN13 number string like "978-0321751041"
"creditcard", // a credit card number defined by the regex ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$ with any non digit characters mixed in
"ssn", // a U.S. social security number following the regex ^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$
"hexcolor", // an hexadecimal color code like "#FFFFFF", following the regex ^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
"rgbcolor", // an RGB color code like rgb like "rgb(255,255,2559"
"byte", // base64 encoded binary data
"password", // any kind of string
"date", // a date string like "2006-01-02" as defined by full-date in RFC3339
"duration", // a duration string like "22 ns" as parsed by Golang time.ParseDuration or compatible with Scala duration format
"datetime", // a date time string like "2014-12-15T19:30:20.000Z" as defined by date-time in RFC3339
)
// StripUnsupportedFormatsPostProcess sets unsupported formats to empty string.
func StripUnsupportedFormatsPostProcess(s *spec.Schema) error {
if len(s.Format) == 0 {
return nil
}
normalized := strings.Replace(s.Format, "-", "", -1) // go-openapi default format name normalization
if !supportedFormats.Has(normalized) {
s.Format = ""
}
return nil
}

View File

@@ -0,0 +1,66 @@
/*
Copyright 2023 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 validation
import (
"time"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)
const (
namespace = "apiextensions_apiserver"
subsystem = "validation"
)
// Interface to stub for tests
type ValidationMetrics interface {
ObserveRatchetingTime(d time.Duration)
}
var Metrics ValidationMetrics = &validationMetrics{
RatchetingTime: metrics.NewHistogram(&metrics.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "ratcheting_seconds",
Help: "Time for comparison of old to new for the purposes of CRDValidationRatcheting during an UPDATE in seconds.",
StabilityLevel: metrics.ALPHA,
// Start 0.01ms with the last bucket being [~2.5s, +Inf)
Buckets: metrics.ExponentialBuckets(0.00001, 4, 10),
}),
}
func init() {
legacyregistry.MustRegister(Metrics.(*validationMetrics).RatchetingTime)
}
type validationMetrics struct {
RatchetingTime *metrics.Histogram
}
// ObserveRatchetingTime records the time spent on ratcheting
func (m *validationMetrics) ObserveRatchetingTime(d time.Duration) {
m.RatchetingTime.Observe(d.Seconds())
}
// Reset resets the metrics. This is meant to be used for testing. Panics
// if the metrics cannot be re-registered. Returns all the reset metrics
func (m *validationMetrics) Reset() []metrics.Registerable {
m.RatchetingTime = metrics.NewHistogram(m.RatchetingTime.HistogramOpts)
return []metrics.Registerable{m.RatchetingTime}
}

View File

@@ -0,0 +1,212 @@
/*
Copyright 2023 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 validation
import (
"reflect"
"k8s.io/apiserver/pkg/cel/common"
celopenapi "k8s.io/apiserver/pkg/cel/openapi"
"k8s.io/kube-openapi/pkg/validation/spec"
"k8s.io/kube-openapi/pkg/validation/strfmt"
"k8s.io/kube-openapi/pkg/validation/validate"
)
// schemaArgs are the arguments to constructor for OpenAPI schema validator,
// NewSchemaValidator
type schemaArgs struct {
schema *spec.Schema
root interface{}
path string
knownFormats strfmt.Registry
options []validate.Option
}
// RatchetingSchemaValidator wraps kube-openapis SchemaValidator to provide a
// ValidateUpdate function which allows ratcheting
type RatchetingSchemaValidator struct {
schemaArgs
}
func NewRatchetingSchemaValidator(schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry, options ...validate.Option) *RatchetingSchemaValidator {
return &RatchetingSchemaValidator{
schemaArgs: schemaArgs{
schema: schema,
root: rootSchema,
path: root,
knownFormats: formats,
options: options,
},
}
}
func (r *RatchetingSchemaValidator) Validate(new interface{}, options ...ValidationOption) *validate.Result {
sv := validate.NewSchemaValidator(r.schema, r.root, r.path, r.knownFormats, r.options...)
return sv.Validate(new)
}
func (r *RatchetingSchemaValidator) ValidateUpdate(new, old interface{}, options ...ValidationOption) *validate.Result {
opts := NewValidationOptions(options...)
if !opts.Ratcheting {
sv := validate.NewSchemaValidator(r.schema, r.root, r.path, r.knownFormats, r.options...)
return sv.Validate(new)
}
correlation := opts.CorrelatedObject
if correlation == nil {
correlation = common.NewCorrelatedObject(new, old, &celopenapi.Schema{Schema: r.schema})
}
return newRatchetingValueValidator(
correlation,
r.schemaArgs,
).Validate(new)
}
// ratchetingValueValidator represents an invocation of SchemaValidator.ValidateUpdate
// for specific arguments for `old` and `new`
//
// It follows the openapi SchemaValidator down its traversal of the new value
// by injecting validate.Option into each recursive invocation.
//
// A ratchetingValueValidator will be constructed and added to the tree for
// each explored sub-index and sub-property during validation.
//
// It's main job is to keep the old/new values correlated as the traversal
// continues, and postprocess errors according to our ratcheting policy.
//
// ratchetingValueValidator is not thread safe.
type ratchetingValueValidator struct {
// schemaArgs provides the arguments to use in the temporary SchemaValidator
// that is created during a call to Validate.
schemaArgs
correlation *common.CorrelatedObject
}
func newRatchetingValueValidator(correlation *common.CorrelatedObject, args schemaArgs) *ratchetingValueValidator {
return &ratchetingValueValidator{
schemaArgs: args,
correlation: correlation,
}
}
// getValidateOption provides a kube-openapi validate.Option for SchemaValidator
// that injects a ratchetingValueValidator to be used for all subkeys and subindices
func (r *ratchetingValueValidator) getValidateOption() validate.Option {
return func(svo *validate.SchemaValidatorOptions) {
svo.NewValidatorForField = r.SubPropertyValidator
svo.NewValidatorForIndex = r.SubIndexValidator
}
}
// Validate validates the update from r.oldValue to r.value
//
// During evaluation, a temporary tree of ratchetingValueValidator is built for all
// traversed field paths. It is necessary to build the tree to take advantage of
// DeepEqual checks performed by lower levels of the object during validation without
// greatly modifying `kube-openapi`'s implementation.
//
// The tree, and all cache storage/scratch space for the validation of a single
// call to `Validate` is thrown away at the end of the top-level call
// to `Validate`.
//
// `Validate` will create a node in the tree to for each of the explored children.
// The node's main purpose is to store a lazily computed DeepEqual check between
// the oldValue and the currently passed value. If the check is performed, it
// will be stored in the node to be re-used by a parent node during a DeepEqual
// comparison, if necessary.
//
// This call has a side-effect of populating it's `children` variable with
// the explored nodes of the object tree.
func (r *ratchetingValueValidator) Validate(new interface{}) *validate.Result {
opts := append([]validate.Option{
r.getValidateOption(),
}, r.options...)
s := validate.NewSchemaValidator(r.schema, r.root, r.path, r.knownFormats, opts...)
res := s.Validate(r.correlation.Value)
if res.IsValid() {
return res
}
// Current ratcheting rule is to ratchet errors if DeepEqual(old, new) is true.
if r.correlation.CachedDeepEqual() {
newRes := &validate.Result{}
newRes.MergeAsWarnings(res)
return newRes
}
return res
}
// SubPropertyValidator overrides the standard validator constructor for sub-properties by
// returning our special ratcheting variant.
//
// If we can correlate an old value, we return a ratcheting validator to
// use for the child.
//
// If the old value cannot be correlated, then default validation is used.
func (r *ratchetingValueValidator) SubPropertyValidator(field string, schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry, options ...validate.Option) validate.ValueValidator {
childNode := r.correlation.Key(field)
if childNode == nil {
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
}
return newRatchetingValueValidator(childNode, schemaArgs{
schema: schema,
root: rootSchema,
path: root,
knownFormats: formats,
options: options,
})
}
// SubIndexValidator overrides the standard validator constructor for sub-indicies by
// returning our special ratcheting variant.
//
// If we can correlate an old value, we return a ratcheting validator to
// use for the child.
//
// If the old value cannot be correlated, then default validation is used.
func (r *ratchetingValueValidator) SubIndexValidator(index int, schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry, options ...validate.Option) validate.ValueValidator {
childNode := r.correlation.Index(index)
if childNode == nil {
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
}
return newRatchetingValueValidator(childNode, schemaArgs{
schema: schema,
root: rootSchema,
path: root,
knownFormats: formats,
options: options,
})
}
var _ validate.ValueValidator = (&ratchetingValueValidator{})
func (r ratchetingValueValidator) SetPath(path string) {
// Do nothing
// Unused by kube-openapi
}
func (r ratchetingValueValidator) Applies(source interface{}, valueKind reflect.Kind) bool {
return true
}

View File

@@ -0,0 +1,489 @@
/*
Copyright 2017 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 validation
import (
"encoding/json"
"strings"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/features"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/cel/common"
utilfeature "k8s.io/apiserver/pkg/util/feature"
openapierrors "k8s.io/kube-openapi/pkg/validation/errors"
"k8s.io/kube-openapi/pkg/validation/spec"
"k8s.io/kube-openapi/pkg/validation/strfmt"
"k8s.io/kube-openapi/pkg/validation/validate"
)
type SchemaValidator interface {
SchemaCreateValidator
ValidateUpdate(new, old interface{}, options ...ValidationOption) *validate.Result
}
type SchemaCreateValidator interface {
Validate(value interface{}, options ...ValidationOption) *validate.Result
}
type ValidationOptions struct {
// Whether errors from unchanged portions of the schema should be ratcheted
// This field is ignored for Validate
Ratcheting bool
// Correlation between old and new arguments.
// If set, this is expected to be the correlation between the `new` and
// `old` arguments to ValidateUpdate, and values for `new` and `old` will
// be taken from the correlation.
//
// This field is ignored for Validate
//
// Used for ratcheting, but left as a separate field since it may be used
// for other purposes in the future.
CorrelatedObject *common.CorrelatedObject
}
type ValidationOption func(*ValidationOptions)
func NewValidationOptions(opts ...ValidationOption) ValidationOptions {
options := ValidationOptions{}
for _, opt := range opts {
opt(&options)
}
return options
}
func WithRatcheting(correlation *common.CorrelatedObject) ValidationOption {
return func(options *ValidationOptions) {
options.Ratcheting = true
options.CorrelatedObject = correlation
}
}
// basicSchemaValidator wraps a kube-openapi SchemaCreateValidator to
// support ValidateUpdate. It implements ValidateUpdate by simply validating
// the new value via kube-openapi, ignoring the old value
type basicSchemaValidator struct {
*validate.SchemaValidator
}
func (s basicSchemaValidator) Validate(new interface{}, options ...ValidationOption) *validate.Result {
return s.SchemaValidator.Validate(new)
}
func (s basicSchemaValidator) ValidateUpdate(new, old interface{}, options ...ValidationOption) *validate.Result {
return s.Validate(new, options...)
}
// NewSchemaValidator creates an openapi schema validator for the given CRD validation.
//
// If feature `CRDValidationRatcheting` is disabled, this returns validator which
// validates all `Update`s and `Create`s as a `Create` - without considering old value.
//
// If feature `CRDValidationRatcheting` is enabled - the validator returned
// will support ratcheting unchanged correlatable fields across an update.
func NewSchemaValidator(customResourceValidation *apiextensions.JSONSchemaProps) (SchemaValidator, *spec.Schema, error) {
// Convert CRD schema to openapi schema
openapiSchema := &spec.Schema{}
if customResourceValidation != nil {
// TODO: replace with NewStructural(...).ToGoOpenAPI
if err := ConvertJSONSchemaPropsWithPostProcess(customResourceValidation, openapiSchema, StripUnsupportedFormatsPostProcess); err != nil {
return nil, nil, err
}
}
return NewSchemaValidatorFromOpenAPI(openapiSchema), openapiSchema, nil
}
func NewSchemaValidatorFromOpenAPI(openapiSchema *spec.Schema) SchemaValidator {
if utilfeature.DefaultFeatureGate.Enabled(features.CRDValidationRatcheting) {
return NewRatchetingSchemaValidator(openapiSchema, nil, "", strfmt.Default)
}
return basicSchemaValidator{validate.NewSchemaValidator(openapiSchema, nil, "", strfmt.Default)}
}
// ValidateCustomResourceUpdate validates the transition of Custom Resource from
// `old` to `new` against the schema in the CustomResourceDefinition.
// Both customResource and old represent a JSON data structures.
//
// If feature `CRDValidationRatcheting` is disabled, this behaves identically to
// ValidateCustomResource(customResource).
func ValidateCustomResourceUpdate(fldPath *field.Path, customResource, old interface{}, validator SchemaValidator, options ...ValidationOption) field.ErrorList {
// Additional feature gate check for sanity
if !utilfeature.DefaultFeatureGate.Enabled(features.CRDValidationRatcheting) {
return ValidateCustomResource(nil, customResource, validator)
} else if validator == nil {
return nil
}
result := validator.ValidateUpdate(customResource, old, options...)
if result.IsValid() {
return nil
}
return kubeOpenAPIResultToFieldErrors(fldPath, result)
}
// ValidateCustomResource validates the Custom Resource against the schema in the CustomResourceDefinition.
// CustomResource is a JSON data structure.
func ValidateCustomResource(fldPath *field.Path, customResource interface{}, validator SchemaCreateValidator, options ...ValidationOption) field.ErrorList {
if validator == nil {
return nil
}
result := validator.Validate(customResource, options...)
if result.IsValid() {
return nil
}
return kubeOpenAPIResultToFieldErrors(fldPath, result)
}
func kubeOpenAPIResultToFieldErrors(fldPath *field.Path, result *validate.Result) field.ErrorList {
var allErrs field.ErrorList
for _, err := range result.Errors {
switch err := err.(type) {
case *openapierrors.Validation:
errPath := fldPath
if len(err.Name) > 0 && err.Name != "." {
errPath = errPath.Child(strings.TrimPrefix(err.Name, "."))
}
switch err.Code() {
case openapierrors.RequiredFailCode:
allErrs = append(allErrs, field.Required(errPath, ""))
case openapierrors.EnumFailCode:
values := []string{}
for _, allowedValue := range err.Values {
if s, ok := allowedValue.(string); ok {
values = append(values, s)
} else {
allowedJSON, _ := json.Marshal(allowedValue)
values = append(values, string(allowedJSON))
}
}
allErrs = append(allErrs, field.NotSupported(errPath, err.Value, values))
case openapierrors.TooLongFailCode:
value := interface{}("")
if err.Value != nil {
value = err.Value
}
max := int64(-1)
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooLongMaxLength(errPath, value, int(max)))
case openapierrors.MaxItemsFailCode:
actual := int64(-1)
if i, ok := err.Value.(int64); ok {
actual = i
}
max := int64(-1)
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooMany(errPath, int(actual), int(max)))
case openapierrors.TooManyPropertiesCode:
actual := int64(-1)
if i, ok := err.Value.(int64); ok {
actual = i
}
max := int64(-1)
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooMany(errPath, int(actual), int(max)))
case openapierrors.InvalidTypeCode:
value := interface{}("")
if err.Value != nil {
value = err.Value
}
allErrs = append(allErrs, field.TypeInvalid(errPath, value, err.Error()))
default:
value := interface{}("")
if err.Value != nil {
value = err.Value
}
allErrs = append(allErrs, field.Invalid(errPath, value, err.Error()))
}
default:
allErrs = append(allErrs, field.Invalid(fldPath, "", err.Error()))
}
}
return allErrs
}
// ConvertJSONSchemaProps converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema.
func ConvertJSONSchemaProps(in *apiextensions.JSONSchemaProps, out *spec.Schema) error {
return ConvertJSONSchemaPropsWithPostProcess(in, out, nil)
}
// PostProcessFunc post-processes one node of a spec.Schema.
type PostProcessFunc func(*spec.Schema) error
// ConvertJSONSchemaPropsWithPostProcess converts the schema from apiextensions.JSONSchemaPropos to go-openapi/spec.Schema
// and run a post process step on each JSONSchemaProps node. postProcess is never called for nil schemas.
func ConvertJSONSchemaPropsWithPostProcess(in *apiextensions.JSONSchemaProps, out *spec.Schema, postProcess PostProcessFunc) error {
if in == nil {
return nil
}
out.ID = in.ID
out.Schema = spec.SchemaURL(in.Schema)
out.Description = in.Description
if in.Type != "" {
out.Type = spec.StringOrArray([]string{in.Type})
}
if in.XIntOrString {
out.VendorExtensible.AddExtension("x-kubernetes-int-or-string", true)
out.Type = spec.StringOrArray{"integer", "string"}
}
out.Nullable = in.Nullable
out.Format = in.Format
out.Title = in.Title
out.Maximum = in.Maximum
out.ExclusiveMaximum = in.ExclusiveMaximum
out.Minimum = in.Minimum
out.ExclusiveMinimum = in.ExclusiveMinimum
out.MaxLength = in.MaxLength
out.MinLength = in.MinLength
out.Pattern = in.Pattern
out.MaxItems = in.MaxItems
out.MinItems = in.MinItems
out.UniqueItems = in.UniqueItems
out.MultipleOf = in.MultipleOf
out.MaxProperties = in.MaxProperties
out.MinProperties = in.MinProperties
out.Required = in.Required
if in.Default != nil {
out.Default = *(in.Default)
}
if in.Example != nil {
out.Example = *(in.Example)
}
if in.Enum != nil {
out.Enum = make([]interface{}, len(in.Enum))
for k, v := range in.Enum {
out.Enum[k] = v
}
}
if err := convertSliceOfJSONSchemaProps(&in.AllOf, &out.AllOf, postProcess); err != nil {
return err
}
if err := convertSliceOfJSONSchemaProps(&in.OneOf, &out.OneOf, postProcess); err != nil {
return err
}
if err := convertSliceOfJSONSchemaProps(&in.AnyOf, &out.AnyOf, postProcess); err != nil {
return err
}
if in.Not != nil {
in, out := &in.Not, &out.Not
*out = new(spec.Schema)
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err
}
}
var err error
out.Properties, err = convertMapOfJSONSchemaProps(in.Properties, postProcess)
if err != nil {
return err
}
out.PatternProperties, err = convertMapOfJSONSchemaProps(in.PatternProperties, postProcess)
if err != nil {
return err
}
out.Definitions, err = convertMapOfJSONSchemaProps(in.Definitions, postProcess)
if err != nil {
return err
}
if in.Ref != nil {
out.Ref, err = spec.NewRef(*in.Ref)
if err != nil {
return err
}
}
if in.AdditionalProperties != nil {
in, out := &in.AdditionalProperties, &out.AdditionalProperties
*out = new(spec.SchemaOrBool)
if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
return err
}
}
if in.AdditionalItems != nil {
in, out := &in.AdditionalItems, &out.AdditionalItems
*out = new(spec.SchemaOrBool)
if err := convertJSONSchemaPropsorBool(*in, *out, postProcess); err != nil {
return err
}
}
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = new(spec.SchemaOrArray)
if err := convertJSONSchemaPropsOrArray(*in, *out, postProcess); err != nil {
return err
}
}
if in.Dependencies != nil {
in, out := &in.Dependencies, &out.Dependencies
*out = make(spec.Dependencies, len(*in))
for key, val := range *in {
newVal := new(spec.SchemaOrStringArray)
if err := convertJSONSchemaPropsOrStringArray(&val, newVal, postProcess); err != nil {
return err
}
(*out)[key] = *newVal
}
}
if in.ExternalDocs != nil {
out.ExternalDocs = &spec.ExternalDocumentation{}
out.ExternalDocs.Description = in.ExternalDocs.Description
out.ExternalDocs.URL = in.ExternalDocs.URL
}
if postProcess != nil {
if err := postProcess(out); err != nil {
return err
}
}
if in.XPreserveUnknownFields != nil {
out.VendorExtensible.AddExtension("x-kubernetes-preserve-unknown-fields", *in.XPreserveUnknownFields)
}
if in.XEmbeddedResource {
out.VendorExtensible.AddExtension("x-kubernetes-embedded-resource", true)
}
if len(in.XListMapKeys) != 0 {
out.VendorExtensible.AddExtension("x-kubernetes-list-map-keys", convertSliceToInterfaceSlice(in.XListMapKeys))
}
if in.XListType != nil {
out.VendorExtensible.AddExtension("x-kubernetes-list-type", *in.XListType)
}
if in.XMapType != nil {
out.VendorExtensible.AddExtension("x-kubernetes-map-type", *in.XMapType)
}
if len(in.XValidations) != 0 {
var serializationValidationRules apiextensionsv1.ValidationRules
if err := apiextensionsv1.Convert_apiextensions_ValidationRules_To_v1_ValidationRules(&in.XValidations, &serializationValidationRules, nil); err != nil {
return err
}
out.VendorExtensible.AddExtension("x-kubernetes-validations", convertSliceToInterfaceSlice(serializationValidationRules))
}
return nil
}
func convertSliceToInterfaceSlice[T any](in []T) []interface{} {
var res []interface{}
for _, v := range in {
res = append(res, v)
}
return res
}
func convertSliceOfJSONSchemaProps(in *[]apiextensions.JSONSchemaProps, out *[]spec.Schema, postProcess PostProcessFunc) error {
if in != nil {
for _, jsonSchemaProps := range *in {
schema := spec.Schema{}
if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
return err
}
*out = append(*out, schema)
}
}
return nil
}
func convertMapOfJSONSchemaProps(in map[string]apiextensions.JSONSchemaProps, postProcess PostProcessFunc) (map[string]spec.Schema, error) {
if in == nil {
return nil, nil
}
out := make(map[string]spec.Schema)
for k, jsonSchemaProps := range in {
schema := spec.Schema{}
if err := ConvertJSONSchemaPropsWithPostProcess(&jsonSchemaProps, &schema, postProcess); err != nil {
return nil, err
}
out[k] = schema
}
return out, nil
}
func convertJSONSchemaPropsOrArray(in *apiextensions.JSONSchemaPropsOrArray, out *spec.SchemaOrArray, postProcess PostProcessFunc) error {
if in.Schema != nil {
in, out := &in.Schema, &out.Schema
*out = new(spec.Schema)
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err
}
}
if in.JSONSchemas != nil {
in, out := &in.JSONSchemas, &out.Schemas
*out = make([]spec.Schema, len(*in))
for i := range *in {
if err := ConvertJSONSchemaPropsWithPostProcess(&(*in)[i], &(*out)[i], postProcess); err != nil {
return err
}
}
}
return nil
}
func convertJSONSchemaPropsorBool(in *apiextensions.JSONSchemaPropsOrBool, out *spec.SchemaOrBool, postProcess PostProcessFunc) error {
out.Allows = in.Allows
if in.Schema != nil {
in, out := &in.Schema, &out.Schema
*out = new(spec.Schema)
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err
}
}
return nil
}
func convertJSONSchemaPropsOrStringArray(in *apiextensions.JSONSchemaPropsOrStringArray, out *spec.SchemaOrStringArray, postProcess PostProcessFunc) error {
out.Property = in.Property
if in.Schema != nil {
in, out := &in.Schema, &out.Schema
*out = new(spec.Schema)
if err := ConvertJSONSchemaPropsWithPostProcess(*in, *out, postProcess); err != nil {
return err
}
}
return nil
}