mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 15:11:47 +09:00
The merge completes an object field by field only when its schema declares properties. An atomic object (x-kubernetes-map-type: atomic), a dictionary value, a free-form object and an object that also takes arbitrary keys are stored as written, so nothing can complete them later. A default osImage.builtin plus a user's osImage.factory gave a spec the kubernetes-nodes chart refuses to render, and an entry such as a RabbitMQ vhost without roles was accepted and stored the same way. The merge now copies such an object whole or leaves the user's value alone, and an entry is validated for it as a whole, like an array row. A branch of an allOf, anyOf or oneOf that names a property is judged like the property declared next to it. An entry that gets this wrong is skipped with a warning like any other invalid entry. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
281 lines
11 KiB
Go
281 lines
11 KiB
Go
/*
|
|
Copyright 2026 The Cozystack 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 application
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
internalapiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
|
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
apiextvalidation "k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
kubejson "k8s.io/apimachinery/pkg/util/json"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
func (r *REST) applyInstallationDefaults(ctx context.Context, app *appsv1alpha1.Application) error {
|
|
if r.defaultsReader == nil {
|
|
return nil
|
|
}
|
|
readCtx, cancel := context.WithTimeout(ctx, r.defaultsReadLimit)
|
|
defer cancel()
|
|
cm := &corev1.ConfigMap{}
|
|
if err := r.defaultsReader.Get(readCtx, client.ObjectKey{Namespace: InstallationDefaultsNamespace, Name: InstallationDefaultsName}, cm); err != nil {
|
|
if apierrors.IsNotFound(err) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("read application defaults: %w", err)
|
|
}
|
|
defaults, err := parseInstallationDefaults(cm.Data["defaults.json"])
|
|
if err != nil {
|
|
return fmt.Errorf("invalid application defaults: %w", err)
|
|
}
|
|
values, configured := defaults[r.gvr.Resource]
|
|
if !configured {
|
|
return nil
|
|
}
|
|
path := field.NewPath("applicationDefaults").Key(r.gvr.Resource)
|
|
if r.defaultsSchemaErr != nil {
|
|
return fmt.Errorf("%s: cannot validate installation defaults: %w", path, r.defaultsSchemaErr)
|
|
}
|
|
if r.defaultsValidator == nil {
|
|
return fmt.Errorf("%s: application schema is unavailable", path)
|
|
}
|
|
// OpenAPI validation expects native numbers, including inside allOf branches.
|
|
// Keep the original JSON numbers for persistence without a float round-trip.
|
|
validationValues := runtime.DeepCopyJSONValue(values).(map[string]any)
|
|
if err := kubejson.ConvertMapNumbers(validationValues, 0); err != nil {
|
|
return fmt.Errorf("%s: invalid configured number: %w", path, err)
|
|
}
|
|
if errs := apiextvalidation.ValidateCustomResource(path, validationValues, r.defaultsValidator); len(errs) > 0 {
|
|
return fmt.Errorf("invalid installation defaults: %w", errs.ToAggregate())
|
|
}
|
|
spec := map[string]any{}
|
|
if app.Spec != nil && len(app.Spec.Raw) > 0 {
|
|
if err := decodeDefaultsJSON(app.Spec.Raw, &spec); err != nil {
|
|
return apierrors.NewBadRequest(fmt.Sprintf("decode application spec: %v", err))
|
|
}
|
|
}
|
|
if spec == nil {
|
|
spec = map[string]any{}
|
|
}
|
|
fillInstallationDefaults(spec, values, r.defaultsSchema)
|
|
raw, err := json.Marshal(spec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
app.Spec = &apiextv1.JSON{Raw: raw}
|
|
return nil
|
|
}
|
|
|
|
func buildInstallationDefaultsValidator(raw string) (apiextvalidation.SchemaCreateValidator, error) {
|
|
var external apiextv1.JSONSchemaProps
|
|
if err := json.Unmarshal([]byte(raw), &external); err != nil {
|
|
return nil, fmt.Errorf("read application schema: %w", err)
|
|
}
|
|
if external.Type != "object" {
|
|
return nil, fmt.Errorf("application schema must describe an object")
|
|
}
|
|
var schema internalapiext.JSONSchemaProps
|
|
if err := apiextv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(&external, &schema, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
prepareInstallationDefaultsSchema(&schema, true)
|
|
validator, _, err := apiextvalidation.NewSchemaValidator(&schema)
|
|
return validator, err
|
|
}
|
|
|
|
func prepareInstallationDefaultsSchema(schema *internalapiext.JSONSchemaProps, partial bool) {
|
|
prepareInstallationDefaultsFragment(schema, partial, nil)
|
|
}
|
|
|
|
// prepareInstallationDefaultsFragment prepares schema. For a branch of an allOf, anyOf
|
|
// or oneOf, declared holds the properties of the object it belongs to: the branch
|
|
// constrains a property declared next to it, so that property is judged like the
|
|
// declared one, and is partial only if the merge completes the declared one.
|
|
func prepareInstallationDefaultsFragment(schema *internalapiext.JSONSchemaProps, partial bool, declared map[string]internalapiext.JSONSchemaProps) {
|
|
// Objects can be completed by user fields; replacement arrays cannot. What an
|
|
// object must contain cannot be judged on a partial one, so required and the
|
|
// lower bound on its properties are dropped, and so is its oneOf: with required
|
|
// removed every branch that tells alternatives apart by presence matches. A
|
|
// scalar's oneOf constrains its value and stays. Only an object that
|
|
// fillInstallationDefaults completes field by field is partial: a dictionary
|
|
// value or any other object it stores whole is judged whole, like an array row.
|
|
if partial {
|
|
schema.Required = nil
|
|
schema.MinProperties = nil
|
|
if objectSchema(schema, declared) {
|
|
schema.OneOf = nil
|
|
}
|
|
}
|
|
// Omitted additionalProperties is open in JSON Schema. Installation
|
|
// configuration requires an explicit declaration before accepting extra keys.
|
|
if objectSchema(schema, declared) && schema.AdditionalProperties == nil &&
|
|
(schema.XPreserveUnknownFields == nil || !*schema.XPreserveUnknownFields) {
|
|
schema.AdditionalProperties = &internalapiext.JSONSchemaPropsOrBool{Allows: false}
|
|
}
|
|
for key, child := range schema.Properties {
|
|
whole, nested := storedWhole(&child), map[string]internalapiext.JSONSchemaProps(nil)
|
|
if sibling, ok := declared[key]; ok {
|
|
whole, nested = storedWhole(&sibling), sibling.Properties
|
|
}
|
|
prepareInstallationDefaultsFragment(&child, partial && !whole, nested)
|
|
schema.Properties[key] = child
|
|
}
|
|
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
|
|
prepareInstallationDefaultsSchema(schema.AdditionalProperties.Schema, false)
|
|
}
|
|
if schema.Items != nil {
|
|
if schema.Items.Schema != nil {
|
|
prepareInstallationDefaultsSchema(schema.Items.Schema, false)
|
|
}
|
|
for i := range schema.Items.JSONSchemas {
|
|
prepareInstallationDefaultsSchema(&schema.Items.JSONSchemas[i], false)
|
|
}
|
|
}
|
|
siblings := declared
|
|
if siblings == nil {
|
|
siblings = schema.Properties
|
|
}
|
|
for _, branches := range [][]internalapiext.JSONSchemaProps{schema.AllOf, schema.AnyOf, schema.OneOf} {
|
|
for i := range branches {
|
|
prepareInstallationDefaultsFragment(&branches[i], partial, siblings)
|
|
}
|
|
}
|
|
}
|
|
|
|
// objectSchema reports whether schema describes an object. A hand-written schema
|
|
// may list properties without repeating type: object, and the merge already
|
|
// completes such an object field by field, so its entry is checked as one too.
|
|
// A branch of an allOf, anyOf or oneOf (declared is set) lists only the
|
|
// properties it constrains, so it is never read as the whole object this way.
|
|
func objectSchema(schema *internalapiext.JSONSchemaProps, declared map[string]internalapiext.JSONSchemaProps) bool {
|
|
return schema.Type == "object" || (declared == nil && schema.Type == "" && len(schema.Properties) > 0)
|
|
}
|
|
|
|
// storedWhole reports whether an object of this schema is never completed from
|
|
// the defaults, which is the inverse of declaredProperty, so whatever the entry
|
|
// holds for it is the final value and must be complete.
|
|
func storedWhole(schema *internalapiext.JSONSchemaProps) bool {
|
|
if open := schema.AdditionalProperties; open != nil && (open.Allows || open.Schema != nil) {
|
|
return true
|
|
}
|
|
if schema.XPreserveUnknownFields != nil && *schema.XPreserveUnknownFields {
|
|
return true
|
|
}
|
|
if schema.XMapType != nil && *schema.XMapType == "atomic" {
|
|
return true
|
|
}
|
|
return len(schema.Properties) == 0
|
|
}
|
|
|
|
func parseInstallationDefaults(raw string) (map[string]map[string]any, error) {
|
|
var defaults map[string]map[string]any
|
|
if err := decodeDefaultsJSON([]byte(raw), &defaults); err != nil {
|
|
return nil, err
|
|
}
|
|
if defaults == nil {
|
|
return nil, fmt.Errorf("defaults.json must be an object keyed by application plural")
|
|
}
|
|
for plural, values := range defaults {
|
|
if plural == "" || strings.HasPrefix(plural, "_") || values == nil {
|
|
return nil, fmt.Errorf("%q must name an application plural with an object value", plural)
|
|
}
|
|
for key := range values {
|
|
if strings.HasPrefix(key, "_") {
|
|
return nil, fmt.Errorf("%s: field %q is reserved", plural, key)
|
|
}
|
|
}
|
|
}
|
|
return defaults, nil
|
|
}
|
|
|
|
func decodeDefaultsJSON(raw []byte, target any) error {
|
|
decoder := json.NewDecoder(bytes.NewReader(raw))
|
|
decoder.UseNumber()
|
|
if err := decoder.Decode(target); err != nil {
|
|
return err
|
|
}
|
|
if decoder.Decode(new(any)) != io.EOF {
|
|
return fmt.Errorf("expected a single JSON object")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fillInstallationDefaults copies missing fields from defaults into spec. It
|
|
// recurses only into objects whose schema declares properties; dictionaries,
|
|
// free-form objects and objects marked x-kubernetes-map-type: atomic are
|
|
// replaced as a whole, like arrays, so a user's map is never completed with
|
|
// entries nobody wrote.
|
|
func fillInstallationDefaults(spec, defaults map[string]any, schema *apiextv1.JSONSchemaProps) {
|
|
for key, value := range defaults {
|
|
current, present := spec[key]
|
|
if !present {
|
|
spec[key] = runtime.DeepCopyJSONValue(value)
|
|
continue
|
|
}
|
|
obj, isObject := current.(map[string]any)
|
|
fallback, isFallbackObject := value.(map[string]any)
|
|
if !isObject || len(obj) == 0 || !isFallbackObject {
|
|
continue
|
|
}
|
|
if child, declared := declaredProperty(schema, key); declared {
|
|
fillInstallationDefaults(obj, fallback, child)
|
|
}
|
|
}
|
|
}
|
|
|
|
func declaredProperty(schema *apiextv1.JSONSchemaProps, key string) (*apiextv1.JSONSchemaProps, bool) {
|
|
if schema == nil {
|
|
return nil, false
|
|
}
|
|
child, ok := schema.Properties[key]
|
|
if !ok || len(child.Properties) == 0 {
|
|
return nil, false
|
|
}
|
|
// An object that also takes arbitrary keys is a map, whatever properties it names.
|
|
if open := child.AdditionalProperties; open != nil && (open.Allows || open.Schema != nil) {
|
|
return nil, false
|
|
}
|
|
if child.XPreserveUnknownFields != nil && *child.XPreserveUnknownFields {
|
|
return nil, false
|
|
}
|
|
// An atomic object is one value: its children exclude each other or only make
|
|
// sense together, so a user's part of it is never completed from the defaults.
|
|
if child.XMapType != nil && *child.XMapType == "atomic" {
|
|
return nil, false
|
|
}
|
|
return &child, true
|
|
}
|
|
|
|
func parseDefaultsSchema(raw string) *apiextv1.JSONSchemaProps {
|
|
var schema apiextv1.JSONSchemaProps
|
|
if err := json.Unmarshal([]byte(raw), &schema); err != nil {
|
|
return nil
|
|
}
|
|
return &schema
|
|
}
|