Skip to content

Commit 8f2a6bc

Browse files
committed
ci(api-gate): report tightened Application name limits
An application declares the limits on its own metadata.name at the root of its schema, beside properties, where the gate's walk over spec never looked. A chart lowering its name cap or adding a pattern rejects names its previous release accepted, which is the same break as tightening a spec field, so the gate reports it the same way. Assisted-by: LLM Signed-off-by: Timofei Larkin <[email protected]>
1 parent a391181 commit 8f2a6bc

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎internal/apigate/classify.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ func diffResource(base, head Resource) []string {
157157
for _, c := range diffSchema("spec", base.Versions[v], headSchema) {
158158
out = append(out, fmt.Sprintf("[%s] %s", v, c))
159159
}
160+
for _, c := range diffNameSchema(base.Versions[v], headSchema) {
161+
out = append(out, fmt.Sprintf("[%s] %s", v, c))
162+
}
160163
}
161164
return out
162165
}

‎internal/apigate/classify_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,40 @@ func TestClassify(t *testing.T) {
126126
}
127127
})
128128
}
129+
130+
// TestClassifyNameDeclaration covers the constraints an application declares
131+
// on its own metadata.name at the schema root: tightening one rejects names
132+
// the base accepted, loosening or dropping one does not.
133+
func TestClassifyNameDeclaration(t *testing.T) {
134+
kube := func(declaration map[string]any) Resource {
135+
s := Schema{"type": "object", "properties": map[string]any{}}
136+
if declaration != nil {
137+
s["x-cozystack-name"] = declaration
138+
}
139+
return res("apps.cozystack.io", "Kubernetes", "kuberneteses", SourceCozyRD, s)
140+
}
141+
capAt := func(n float64) map[string]any { return map[string]any{"type": "string", "maxLength": n} }
142+
143+
for _, tc := range []struct {
144+
name string
145+
base, head Resource
146+
breaking bool
147+
}{
148+
{"cap added", kube(nil), kube(capAt(32)), true},
149+
{"cap lowered", kube(capAt(32)), kube(capAt(30)), true},
150+
{"pattern added", kube(capAt(32)), kube(map[string]any{"type": "string", "maxLength": 32.0, "pattern": "^[a-z]+$"}), true},
151+
{"cap raised", kube(capAt(32)), kube(capAt(40)), false},
152+
{"cap dropped", kube(capAt(32)), kube(nil), false},
153+
{"unchanged", kube(capAt(32)), kube(capAt(32)), false},
154+
} {
155+
t.Run(tc.name, func(t *testing.T) {
156+
got := countCategory(Classify(snap(tc.base), snap(tc.head)), Breaking)
157+
if tc.breaking && got != 1 {
158+
t.Fatalf("expected 1 breaking finding, got %d", got)
159+
}
160+
if !tc.breaking && got != 0 {
161+
t.Fatalf("expected no breaking finding, got %d", got)
162+
}
163+
})
164+
}
165+
}

‎internal/apigate/schemadiff.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121
"sort"
2222
"strings"
23+
24+
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
2325
)
2426

2527
// diffSchema walks a base and head OpenAPIv3Schema node in lockstep and
@@ -39,6 +41,32 @@ func diffSchema(path string, base, head Schema) []string {
3941
return out
4042
}
4143

44+
// diffNameSchema reports a tightened constraint on metadata.name. An
45+
// application declares those beside "properties" at the schema root, where the
46+
// walk over spec never looks. The type is left out: a name is a string whether
47+
// or not the declaration says so.
48+
func diffNameSchema(base, head Schema) []string {
49+
headName := nameDeclaration(head)
50+
if headName == nil {
51+
return nil
52+
}
53+
return diffSchema("metadata.name", nameDeclaration(base), headName)
54+
}
55+
56+
func nameDeclaration(s Schema) Schema {
57+
declared, ok := s[appsv1alpha1.NameSchemaExtension].(map[string]any)
58+
if !ok {
59+
return nil
60+
}
61+
out := Schema{}
62+
for k, v := range declared {
63+
if k != "type" {
64+
out[k] = v
65+
}
66+
}
67+
return out
68+
}
69+
4270
func diffNode(path string, base, head Schema, out *[]string) {
4371
// A nil head means the schema at this path was removed entirely. Dropping a
4472
// constraint is a widening, never a break, so there is nothing to report.

0 commit comments

Comments
 (0)