Skip to content

Commit f568fc2

Browse files
authored
fix(api): repair an empty required field instead of failing the release (#3956)
## What this PR does Application spec goes to flux as HelmRelease `.spec.values` as is, and helm value coalescing deletes null key together with chart default under it, before values are validated against `values.schema.json`. So a field that was written but left empty: ```yaml apiVersion: apps.cozystack.io/v1alpha1 kind: Kubernetes spec: talos: registryMirrors: ``` fails whole release with `at '/talos': missing property 'registryMirrors'`, naming property that spec does contain, and application never installs. Writing `registryMirrors: {}` works, and nothing in the error points at difference between the two. Such null is now removed on write path (`Create` and `Update` in cozystack-api), so chart default applies to it, and client gets a warning naming every field that was dropped. Writing schema default into the spec instead would pin current value into stored HelmRelease and freeze application on it, so a later bump of that default in the chart would never reach it. Only fields that schema marks required are repaired. For those null cant be load-bearing, because today it fails render entirely. Where schema allows field to be absent, null keeps meaning it has now (chart default suppressed, key never reaches template), and that one is worth keeping: 167 optional fields in catalogue carry non-trivial default, placeholder credentials and example s3 buckets among them, so resolving user null to those is worse than leaving key out. Free-form subtrees, null map entries under user chosen key (node group, registry host) and array elements are left as written too, there null may be the payload. Not covered by this PR: `talos:` with whole section left empty. Root schema of kubernetes app has no required fields at all, so it still dies at render with `nil pointer evaluating interface {}.version`. That one is loud and visible in HelmRelease status, and it belongs to templates. Tests are on the real shipped schema from `packages/system/kubernetes-rd/cozyrds/kubernetes.yaml`, and cover both sides of the boundary: required field repaired, optional field left alone. ### Related Same class as #3705, where `tls:` written empty deletes the chart default and panics the render in four charts. This PR covers the half where schema marks the field required, so validation fails instead of a nil deref. Template half of #3705 (guard the map, not the value) is not here, and that one also covers `talos:` with whole section left empty. #3046 was the same failure through the typed CR, closed by dropping `required` from one field in #3121, and #3949 was a second attempt of the same per field approach. That loop is what this PR is meant to end. ### Downstream repositories Walked the trigger map against the diff. Go code in api layer only, no package touched, no values.yaml, no schema field, no default, no version enum, no `kind` or `plural`, README unchanged. terraform-provider models spec fields itself and none of them changed here. - [x] No downstream repository is affected by this change ### Release note ```release-note fix(api): a field written with no value in an application spec (`registryMirrors:` with nothing under it) no longer fails the whole HelmRelease when the schema marks that field required. It is treated as unset, so the chart default applies to it. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Required application fields set to `null` now use their chart defaults during creation and updates. * Admission validation checks the normalized specification, helping prevent validation and stored values from diverging. * Invalid specifications return a bad-request response. * Creation and update responses warn when null fields are repaired. * Optional fields, free-form data, user-defined map entries, and array positions retain their existing `null` values. * Processing no longer modifies the incoming application object. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 6addef4 + 0474613 commit f568fc2

5 files changed

Lines changed: 765 additions & 9 deletions

File tree

‎pkg/registry/apps/application/rest.go‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
184184
return nil, fmt.Errorf("expected *appsv1alpha1.Application object, got %T", obj)
185185
}
186186

187+
app = app.DeepCopy()
188+
187189
// Validate Application name format (DNS-1035 plus any kind-specific rules)
188190
if errs := r.validateNameFormat(app.Name); len(errs) > 0 {
189191
return nil, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, errs)
@@ -215,6 +217,16 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
215217
return nil, apierrors.NewBadRequest(err.Error())
216218
}
217219

220+
// Omission permits a chart default; explicit null suppresses it in Helm.
221+
// Normalize only repairable fields before admission and persistence.
222+
emptied, err := r.normalizeSpecNulls(app)
223+
if err != nil {
224+
return nil, apierrors.NewBadRequest(fmt.Sprintf("failed to normalize spec: %v", err))
225+
}
226+
if len(emptied) > 0 {
227+
warning.AddWarning(ctx, "", emptyFieldsWarning(emptied))
228+
}
229+
218230
r.warnLegacyPresets(app)
219231
r.warnRemovedKubernetesFields(ctx, app)
220232
r.warnRemovedUserPasswords(ctx, app)
@@ -228,7 +240,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
228240
// this hook explicitly — unlike genericregistry.Store, which
229241
// wires it automatically.
230242
if createValidation != nil {
231-
if err := createValidation(ctx, obj); err != nil {
243+
if err := createValidation(ctx, app); err != nil {
232244
return nil, err
233245
}
234246
}
@@ -533,21 +545,15 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
533545
return nil, false, err
534546
}
535547

536-
// Validate the update if a validation function is provided
537-
if updateValidation != nil {
538-
if err := updateValidation(ctx, newObj, oldObj); err != nil {
539-
klog.Errorf("Update validation failed for Application %s: %v", name, err)
540-
return nil, false, err
541-
}
542-
}
543-
544548
// Assert the new object is of type Application
545549
app, ok := newObj.(*appsv1alpha1.Application)
546550
if !ok {
547551
klog.Errorf("expected *appsv1alpha1.Application object, got %T", newObj)
548552
return nil, false, fmt.Errorf("expected *appsv1alpha1.Application object, got %T", newObj)
549553
}
550554

555+
app = app.DeepCopy()
556+
551557
// Note: name validation (DNS-1035 format + length) is intentionally skipped on
552558
// Update because Kubernetes names are immutable. Validating here would block
553559
// updates to pre-existing resources whose names don't conform to the new rules.
@@ -557,6 +563,23 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
557563
return nil, false, apierrors.NewBadRequest(err.Error())
558564
}
559565

566+
// Admission must validate the same normalized values that will be stored.
567+
emptied, err := r.normalizeSpecNulls(app)
568+
if err != nil {
569+
return nil, false, apierrors.NewBadRequest(fmt.Sprintf("failed to normalize spec: %v", err))
570+
}
571+
if len(emptied) > 0 {
572+
warning.AddWarning(ctx, "", emptyFieldsWarning(emptied))
573+
}
574+
575+
// Validate the update if a validation function is provided
576+
if updateValidation != nil {
577+
if err := updateValidation(ctx, app, oldObj); err != nil {
578+
klog.Errorf("Update validation failed for Application %s: %v", name, err)
579+
return nil, false, err
580+
}
581+
}
582+
560583
// Enforce hierarchical quota allocation on quota changes too: raising a
561584
// child tenant's declared quota above its parent's remaining budget is
562585
// rejected the same way as on create.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
"bytes"
21+
"encoding/json"
22+
"fmt"
23+
"io"
24+
"sort"
25+
"strings"
26+
27+
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
28+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29+
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
30+
)
31+
32+
// maxWarnedNullPaths bounds the number of field paths named in the warning
33+
// handed back to the client, so a spec that nulls out dozens of fields does not
34+
// turn into a multi-kilobyte response header.
35+
const maxWarnedNullPaths = 8
36+
37+
// normalizeSpecNulls removes required null properties whose schema supplies a
38+
// non-null default. Helm deletes explicit nulls before validating values, whereas
39+
// omission lets the chart default apply without persisting its current value.
40+
// Optional/nullable properties retain their explicit-null meaning. User-keyed
41+
// maps and array elements do not imply a matching Helm values-default path,
42+
// so defaults in their item schemas alone cannot justify a repair.
43+
func (r *REST) normalizeSpecNulls(app *appsv1alpha1.Application) ([]string, error) {
44+
if r.specSchema == nil || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 {
45+
return nil, nil
46+
}
47+
var spec map[string]any
48+
decoder := json.NewDecoder(bytes.NewReader(app.Spec.Raw))
49+
decoder.UseNumber()
50+
if err := decoder.Decode(&spec); err != nil {
51+
return nil, err
52+
}
53+
if decoder.Decode(new(any)) != io.EOF {
54+
return nil, fmt.Errorf("spec must contain a single JSON object")
55+
}
56+
if spec == nil {
57+
return nil, nil
58+
}
59+
60+
var stripped []string
61+
stripDeclaredNulls(spec, r.specSchema, "", &stripped)
62+
if len(stripped) == 0 {
63+
return nil, nil
64+
}
65+
66+
raw, err := json.Marshal(spec)
67+
if err != nil {
68+
return nil, err
69+
}
70+
app.Spec = &apiextv1.JSON{Raw: raw}
71+
sort.Strings(stripped)
72+
return stripped, nil
73+
}
74+
75+
// Only declared property paths identify defaults independently of user keys.
76+
// In particular, a schema default inside an array item is not a
77+
// default for an element of a user-supplied replacement array.
78+
func stripDeclaredNulls(v any, s *structuralschema.Structural, path string, stripped *[]string) {
79+
if s == nil || v == nil {
80+
return
81+
}
82+
83+
switch value := v.(type) {
84+
case map[string]any:
85+
required := requiredNames(s)
86+
// Keys are collected up front: the loop deletes from the map it walks,
87+
// and a sorted walk keeps the reported paths stable across calls.
88+
for _, name := range sortedKeys(value) {
89+
child := value[name]
90+
if prop, declared := s.Properties[name]; declared {
91+
if child == nil {
92+
if _, mustBeSet := required[name]; mustBeSet && prop.Default.Object != nil && !prop.Nullable {
93+
delete(value, name)
94+
*stripped = append(*stripped, joinFieldPath(path, name))
95+
}
96+
continue
97+
}
98+
stripDeclaredNulls(child, &prop, joinFieldPath(path, name), stripped)
99+
continue
100+
}
101+
}
102+
}
103+
}
104+
105+
// requiredNames returns the property names the schema marks required. The
106+
// validation half of a structural schema is an optional pointer, absent for
107+
// every schema that constrains nothing.
108+
func requiredNames(s *structuralschema.Structural) map[string]struct{} {
109+
if s.ValueValidation == nil || len(s.ValueValidation.Required) == 0 {
110+
return nil
111+
}
112+
names := make(map[string]struct{}, len(s.ValueValidation.Required))
113+
for _, name := range s.ValueValidation.Required {
114+
names[name] = struct{}{}
115+
}
116+
return names
117+
}
118+
119+
func sortedKeys(m map[string]any) []string {
120+
keys := make([]string, 0, len(m))
121+
for k := range m {
122+
keys = append(keys, k)
123+
}
124+
sort.Strings(keys)
125+
return keys
126+
}
127+
128+
func joinFieldPath(path, name string) string {
129+
if path == "" {
130+
return name
131+
}
132+
return path + "." + name
133+
}
134+
135+
// emptyFieldsWarning renders the client-facing warning for stripped nulls. The
136+
// normalization is deliberately visible: an empty key is a typo often enough
137+
// that silently resolving it would trade one confusing outcome for another.
138+
func emptyFieldsWarning(paths []string) string {
139+
shown := paths
140+
suffix := ""
141+
if len(shown) > maxWarnedNullPaths {
142+
shown = shown[:maxWarnedNullPaths]
143+
suffix = fmt.Sprintf(" (and %d more)", len(paths)-maxWarnedNullPaths)
144+
}
145+
return fmt.Sprintf("spec: %s left empty%s; treated as unset, the chart default applies", strings.Join(shown, ", "), suffix)
146+
}

0 commit comments

Comments
 (0)