Skip to content

Commit 5f3ad9c

Browse files
fix(api): declare OpenAPIModelName for core and sdn types
The aggregated apiserver publishes the core.cozystack.io and sdn.cozystack.io models under their Go import path, so a $ref to any of them does not resolve and client-side validation fails on every resource of those groups, not only the one named in the error: error validating data: SchemaError(…core/v1alpha1.Option.spec): unknown model in reference: "github.com~1cozystack~1…v1alpha1.OptionSpec" Since Kubernetes 0.35 the apiserver's DefinitionNamer.GetDefinitionName returns the model name it is handed verbatim; it no longer converts the Go import-path form into the "friendly" reversed-path form. Whatever name the generated openapi map uses therefore becomes both the published definition key and, JSON pointer escaped, the $ref pointing at it. A name containing "/" ships as a definition keyed on the raw path while every reference to it spells each slash "~1", and clients resolve a $ref by trimming "#/definitions/" without unescaping (kube-openapi pkg/util/proto/document.go). The two spellings never meet, the reference dangles, and validation of the whole document fails. Mirror b916d74, which fixed the same disagreement for apps: add the +k8s:openapi-model-package marker to each doc.go and declare OpenAPIModelName for all 11 core and 9 sdn types, so GetCanonicalTypeName, Scheme.ToOpenAPIDefinitionName and the generated map key all agree on a slash-free dotted name. That takes the number of slash-bearing definition names in the published document from 20 to 0. The methods are hand-written rather than emitted by openapi-gen's --output-model-name-file because that flag also rewrites zz_generated.model_name.go inside the read-only apimachinery and apiextensions module-cache packages the shared gen_openapi helper always passes as inputs, which fails on any consumer including CI that vendors deps from the cache. With the marker in place openapi-gen emits Type{}.OpenAPIModelName() for every type it references, so a future type without a method fails the build instead of silently reintroducing a Go-path name. The generated openapi is the output of the root `make generate`; nothing else in the generated tree moved. kubectl apply --validate has been broken against any cozystack-api built after 2026-07-21 on every resource, and this shipped in v1.6.0 and v1.6.1. In e2e it surfaces as the platform install hanging on cozy-backup-controller/backupstrategy-controller, which takes tenant-root and everything behind cozystack-basics with it. Refs: cozystack#3806 Assisted-By: Claude <[email protected]> Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 09dc7f4 commit 5f3ad9c

5 files changed

Lines changed: 331 additions & 135 deletions

File tree

‎pkg/apis/core/v1alpha1/doc.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
// +k8s:openapi-gen=true
18+
// +k8s:openapi-model-package=com.github.cozystack.cozystack.pkg.apis.core.v1alpha1
1819
// +k8s:deepcopy-gen=package
1920
// +k8s:conversion-gen=github.com/cozystack/cozystack/pkg/apis/core
2021
// +k8s:conversion-gen=k8s.io/apiextensions-apiserver/pkg/apis/apiextensions
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 v1alpha1
18+
19+
// OpenAPIModelName pins the OpenAPI model name for the core.cozystack.io types.
20+
// This is hand-written (not generated) on purpose.
21+
//
22+
// Without it, openapi-gen keys the generated definition map on the Go import
23+
// path ("github.com/cozystack/cozystack/pkg/apis/core/v1alpha1.Option"), and
24+
// since Kubernetes 0.35 the apiserver's DefinitionNamer.GetDefinitionName
25+
// returns whatever name it is given verbatim instead of converting it to the
26+
// "friendly" reversed-path form. The published document then keys the definition
27+
// on the raw path while every $ref to it is JSON pointer escaped
28+
// ("#/definitions/github.com~1cozystack~1…v1alpha1.OptionSpec", ~1 being a
29+
// slash), and clients resolve a $ref by trimming "#/definitions/" without
30+
// unescaping. The two spellings never meet, so the reference dangles and
31+
// client-side validation fails on every resource in the group, not only the one
32+
// named in the error:
33+
//
34+
// error validating data: SchemaError(…core/v1alpha1.Option.spec):
35+
// unknown model in reference: "github.com~1cozystack~1…v1alpha1.OptionSpec"
36+
//
37+
// Declaring the dotted name here makes GetCanonicalTypeName,
38+
// Scheme.ToOpenAPIDefinitionName and the generated openapi map key all agree on
39+
// a name with no slash in it, so nothing needs escaping and every $ref resolves.
40+
// The same agreement is what lets DefinitionNamer attach the
41+
// x-kubernetes-group-version-kind extension, which server-side apply needs to
42+
// resolve a kind (see the apps package, where its absence broke SSA instead).
43+
//
44+
// code-generator's openapi-gen could emit these via --output-model-name-file,
45+
// but that also rewrites zz_generated.model_name.go in the read-only apimachinery
46+
// / apiextensions module-cache packages the shared gen_openapi helper always
47+
// passes as inputs, which fails on any consumer (including CI) that vendors deps
48+
// from the module cache. Keeping the methods here sidesteps that.
49+
//
50+
// The +k8s:openapi-model-package marker in doc.go makes openapi-gen emit
51+
// Type{}.OpenAPIModelName() for every type in this package it generates a schema
52+
// or a reference for, so a new core.cozystack.io type without a method here
53+
// fails the build rather than silently reintroducing a Go-path name. The
54+
// returned string must be the dotted form Scheme.ToOpenAPIDefinitionName derives
55+
// from the type's Go package path, which is the marker's value plus the type
56+
// name. See https://github.com/cozystack/cozystack/issues/3806.
57+
58+
func (in Option) OpenAPIModelName() string {
59+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.Option"
60+
}
61+
62+
func (in OptionItem) OpenAPIModelName() string {
63+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.OptionItem"
64+
}
65+
66+
func (in OptionList) OpenAPIModelName() string {
67+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.OptionList"
68+
}
69+
70+
func (in OptionSpec) OpenAPIModelName() string {
71+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.OptionSpec"
72+
}
73+
74+
func (in TenantModule) OpenAPIModelName() string {
75+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantModule"
76+
}
77+
78+
func (in TenantModuleList) OpenAPIModelName() string {
79+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantModuleList"
80+
}
81+
82+
func (in TenantModuleStatus) OpenAPIModelName() string {
83+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantModuleStatus"
84+
}
85+
86+
func (in TenantNamespace) OpenAPIModelName() string {
87+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantNamespace"
88+
}
89+
90+
func (in TenantNamespaceList) OpenAPIModelName() string {
91+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantNamespaceList"
92+
}
93+
94+
func (in TenantSecret) OpenAPIModelName() string {
95+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantSecret"
96+
}
97+
98+
func (in TenantSecretList) OpenAPIModelName() string {
99+
return "com.github.cozystack.cozystack.pkg.apis.core.v1alpha1.TenantSecretList"
100+
}

‎pkg/apis/sdn/v1alpha1/doc.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
// +k8s:openapi-gen=true
18+
// +k8s:openapi-model-package=com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1
1819
// +k8s:deepcopy-gen=package
1920
// +k8s:conversion-gen=github.com/cozystack/cozystack/pkg/apis/sdn
2021
// +k8s:conversion-gen=k8s.io/apiextensions-apiserver/pkg/apis/apiextensions
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 v1alpha1
18+
19+
// OpenAPIModelName pins the OpenAPI model name for the sdn.cozystack.io types.
20+
// This is hand-written (not generated) on purpose.
21+
//
22+
// Without it, openapi-gen keys the generated definition map on the Go import
23+
// path ("github.com/cozystack/cozystack/pkg/apis/sdn/v1alpha1.SecurityGroup"),
24+
// and since Kubernetes 0.35 the apiserver's DefinitionNamer.GetDefinitionName
25+
// returns whatever name it is given verbatim instead of converting it to the
26+
// "friendly" reversed-path form. The published document then keys the definition
27+
// on the raw path while every $ref to it is JSON pointer escaped
28+
// ("#/definitions/github.com~1cozystack~1…v1alpha1.SecurityGroupSpec", ~1 being
29+
// a slash), and clients resolve a $ref by trimming "#/definitions/" without
30+
// unescaping. The two spellings never meet, so the reference dangles and
31+
// client-side validation fails on every resource in the group, not only the one
32+
// named in the error:
33+
//
34+
// error validating data: SchemaError(…sdn/v1alpha1.SecurityGroup.spec):
35+
// unknown model in reference: "github.com~1cozystack~1…v1alpha1.SecurityGroupSpec"
36+
//
37+
// Declaring the dotted name here makes GetCanonicalTypeName,
38+
// Scheme.ToOpenAPIDefinitionName and the generated openapi map key all agree on
39+
// a name with no slash in it, so nothing needs escaping and every $ref resolves.
40+
// The same agreement is what lets DefinitionNamer attach the
41+
// x-kubernetes-group-version-kind extension, which server-side apply needs to
42+
// resolve a kind (see the apps package, where its absence broke SSA instead).
43+
//
44+
// code-generator's openapi-gen could emit these via --output-model-name-file,
45+
// but that also rewrites zz_generated.model_name.go in the read-only apimachinery
46+
// / apiextensions module-cache packages the shared gen_openapi helper always
47+
// passes as inputs, which fails on any consumer (including CI) that vendors deps
48+
// from the module cache. Keeping the methods here sidesteps that.
49+
//
50+
// The +k8s:openapi-model-package marker in doc.go makes openapi-gen emit
51+
// Type{}.OpenAPIModelName() for every type in this package it generates a schema
52+
// or a reference for, so a new sdn.cozystack.io type without a method here fails
53+
// the build rather than silently reintroducing a Go-path name. The returned
54+
// string must be the dotted form Scheme.ToOpenAPIDefinitionName derives from the
55+
// type's Go package path, which is the marker's value plus the type name. See
56+
// https://github.com/cozystack/cozystack/issues/3806.
57+
58+
func (in ApplicationReference) OpenAPIModelName() string {
59+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.ApplicationReference"
60+
}
61+
62+
func (in EgressRule) OpenAPIModelName() string {
63+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.EgressRule"
64+
}
65+
66+
func (in FQDNSelector) OpenAPIModelName() string {
67+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.FQDNSelector"
68+
}
69+
70+
func (in IngressRule) OpenAPIModelName() string {
71+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.IngressRule"
72+
}
73+
74+
func (in PortProtocol) OpenAPIModelName() string {
75+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.PortProtocol"
76+
}
77+
78+
func (in PortRule) OpenAPIModelName() string {
79+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.PortRule"
80+
}
81+
82+
func (in SecurityGroup) OpenAPIModelName() string {
83+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.SecurityGroup"
84+
}
85+
86+
func (in SecurityGroupList) OpenAPIModelName() string {
87+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.SecurityGroupList"
88+
}
89+
90+
func (in SecurityGroupSpec) OpenAPIModelName() string {
91+
return "com.github.cozystack.cozystack.pkg.apis.sdn.v1alpha1.SecurityGroupSpec"
92+
}

0 commit comments

Comments
 (0)