|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package application |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "sort" |
| 23 | + "strings" |
| 24 | + |
| 25 | + appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" |
| 26 | + apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 27 | + structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" |
| 28 | +) |
| 29 | + |
| 30 | +// maxWarnedNullPaths bounds the number of field paths named in the warning |
| 31 | +// handed back to the client, so a spec that nulls out dozens of fields does not |
| 32 | +// turn into a multi-kilobyte response header. |
| 33 | +const maxWarnedNullPaths = 8 |
| 34 | + |
| 35 | +// normalizeSpecNulls repairs a JSON null on a field the schema marks required, |
| 36 | +// by removing the key so the chart's own default applies to it. |
| 37 | +// |
| 38 | +// The spec reaches flux verbatim as HelmRelease .spec.values (see |
| 39 | +// convertApplicationToHelmRelease), and Helm's value coalescing deletes a |
| 40 | +// null-valued key together with the chart default underneath it, before the |
| 41 | +// result is validated against values.schema.json. A required field written with |
| 42 | +// nothing under it is therefore rejected for being absent - "at '/talos': |
| 43 | +// missing property 'registryMirrors'", naming a property the spec does contain - |
| 44 | +// and the whole release fails to render. Nothing reaches the user that would |
| 45 | +// point at the empty key they actually typed. |
| 46 | +// |
| 47 | +// Only required properties are repaired, and that boundary is the point. A |
| 48 | +// required property's null cannot be load-bearing for anyone, because today it |
| 49 | +// fails the render outright; resolving it to the chart default is pure repair. |
| 50 | +// Where the schema permits the field to be absent, a null keeps the meaning it |
| 51 | +// has today - the chart default is suppressed and the key does not reach the |
| 52 | +// template - and that meaning is worth keeping: 167 optional fields across the |
| 53 | +// catalogue carry a non-trivial default, placeholders such as "<password>" and |
| 54 | +// example S3 buckets among them, so resolving a user's null to one of those |
| 55 | +// would be worse than leaving the key out. |
| 56 | +// |
| 57 | +// Filling in the schema default instead of removing the key would also pin |
| 58 | +// today's value into the stored HelmRelease and freeze the application on it, |
| 59 | +// so a later bump of that default in the chart would never reach an application |
| 60 | +// that had once been created with an empty key. |
| 61 | +// |
| 62 | +// Free-form subtrees are left alone, as is a null map entry whose key the user |
| 63 | +// chose (a node group, a registry host): there the null may be the payload, and |
| 64 | +// dropping the entry would drop the user's intent along with it. Array elements |
| 65 | +// keep their positions for the same reason. |
| 66 | +// |
| 67 | +// Returns the dotted paths that were removed, sorted, so the caller can tell |
| 68 | +// the user what happened to them. |
| 69 | +func (r *REST) normalizeSpecNulls(app *appsv1alpha1.Application) ([]string, error) { |
| 70 | + if r.specSchema == nil || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 { |
| 71 | + return nil, nil |
| 72 | + } |
| 73 | + var spec map[string]any |
| 74 | + if err := json.Unmarshal(app.Spec.Raw, &spec); err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + if spec == nil { |
| 78 | + return nil, nil |
| 79 | + } |
| 80 | + |
| 81 | + var stripped []string |
| 82 | + stripDeclaredNulls(spec, r.specSchema, "", &stripped) |
| 83 | + if len(stripped) == 0 { |
| 84 | + return nil, nil |
| 85 | + } |
| 86 | + |
| 87 | + raw, err := json.Marshal(spec) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + app.Spec = &apiextv1.JSON{Raw: raw} |
| 92 | + sort.Strings(stripped) |
| 93 | + return stripped, nil |
| 94 | +} |
| 95 | + |
| 96 | +// stripDeclaredNulls walks a value alongside its structural schema, deleting |
| 97 | +// every null whose key the schema declares as a property *and* lists as |
| 98 | +// required. Dispatch is on the shape of the value rather than on schema.Type, |
| 99 | +// which is empty for a schema that carries only properties or only items. |
| 100 | +func stripDeclaredNulls(v any, s *structuralschema.Structural, path string, stripped *[]string) { |
| 101 | + if s == nil || v == nil { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + switch value := v.(type) { |
| 106 | + case map[string]any: |
| 107 | + required := requiredNames(s) |
| 108 | + // Keys are collected up front: the loop deletes from the map it walks, |
| 109 | + // and a sorted walk keeps the reported paths stable across calls. |
| 110 | + for _, name := range sortedKeys(value) { |
| 111 | + child := value[name] |
| 112 | + if prop, declared := s.Properties[name]; declared { |
| 113 | + if child == nil { |
| 114 | + if _, mustBeSet := required[name]; mustBeSet { |
| 115 | + delete(value, name) |
| 116 | + *stripped = append(*stripped, joinFieldPath(path, name)) |
| 117 | + } |
| 118 | + continue |
| 119 | + } |
| 120 | + stripDeclaredNulls(child, &prop, joinFieldPath(path, name), stripped) |
| 121 | + continue |
| 122 | + } |
| 123 | + if s.AdditionalProperties != nil && s.AdditionalProperties.Structural != nil { |
| 124 | + stripDeclaredNulls(child, s.AdditionalProperties.Structural, joinFieldPath(path, name), stripped) |
| 125 | + } |
| 126 | + } |
| 127 | + case []any: |
| 128 | + if s.Items == nil { |
| 129 | + return |
| 130 | + } |
| 131 | + for i := range value { |
| 132 | + stripDeclaredNulls(value[i], s.Items, fmt.Sprintf("%s[%d]", path, i), stripped) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// requiredNames returns the property names the schema marks required. The |
| 138 | +// validation half of a structural schema is an optional pointer, absent for |
| 139 | +// every schema that constrains nothing. |
| 140 | +func requiredNames(s *structuralschema.Structural) map[string]struct{} { |
| 141 | + if s.ValueValidation == nil || len(s.ValueValidation.Required) == 0 { |
| 142 | + return nil |
| 143 | + } |
| 144 | + names := make(map[string]struct{}, len(s.ValueValidation.Required)) |
| 145 | + for _, name := range s.ValueValidation.Required { |
| 146 | + names[name] = struct{}{} |
| 147 | + } |
| 148 | + return names |
| 149 | +} |
| 150 | + |
| 151 | +func sortedKeys(m map[string]any) []string { |
| 152 | + keys := make([]string, 0, len(m)) |
| 153 | + for k := range m { |
| 154 | + keys = append(keys, k) |
| 155 | + } |
| 156 | + sort.Strings(keys) |
| 157 | + return keys |
| 158 | +} |
| 159 | + |
| 160 | +func joinFieldPath(path, name string) string { |
| 161 | + if path == "" { |
| 162 | + return name |
| 163 | + } |
| 164 | + return path + "." + name |
| 165 | +} |
| 166 | + |
| 167 | +// emptyFieldsWarning renders the client-facing warning for stripped nulls. The |
| 168 | +// normalization is deliberately visible: an empty key is a typo often enough |
| 169 | +// that silently resolving it would trade one confusing outcome for another. |
| 170 | +func emptyFieldsWarning(paths []string) string { |
| 171 | + shown := paths |
| 172 | + suffix := "" |
| 173 | + if len(shown) > maxWarnedNullPaths { |
| 174 | + shown = shown[:maxWarnedNullPaths] |
| 175 | + suffix = fmt.Sprintf(" (and %d more)", len(paths)-maxWarnedNullPaths) |
| 176 | + } |
| 177 | + return fmt.Sprintf("spec: %s left empty%s; treated as unset, the chart default applies", strings.Join(shown, ", "), suffix) |
| 178 | +} |
0 commit comments