Skip to content

Commit c161e1f

Browse files
committed
fix: Do not hoist identical version schemas for apiextensions.k8s.io/v1
The underlying k8s api that converts into the final CRD object is deduplicating versions. With rehydrating the schemas we fix a problem with possible API incompatibility and the generated specs. Signed-off-by: Richard Hillmann <[email protected]>
1 parent a7b2d06 commit c161e1f

File tree

8 files changed

+1092
-21
lines changed

8 files changed

+1092
-21
lines changed

pkg/kube_resource/generator/generator.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,41 @@ func CRDContainsValidation(crd *apiextensions.CustomResourceDefinition) bool {
241241
func buildSwagger(crd *apiextensions.CustomResourceDefinition) (*spec.Swagger, error) {
242242
var schemas spec.Definitions = map[string]spec.Schema{}
243243
group, kind := crd.Spec.Group, crd.Spec.Names.Kind
244-
if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
244+
245+
// The upstream v1→internal conversion hoists identical per-version schemas into Spec.Validation
246+
// and clears them on each version. Rehydrate them here so we still emit every version.
247+
if crd.Spec.Validation != nil {
248+
for i := range crd.Spec.Versions {
249+
if crd.Spec.Versions[i].Schema == nil {
250+
crd.Spec.Versions[i].Schema = crd.Spec.Validation
251+
}
252+
}
253+
}
254+
255+
if len(crd.Spec.Versions) > 0 {
256+
for _, version := range crd.Spec.Versions {
257+
if version.Schema == nil || version.Schema.OpenAPIV3Schema == nil {
258+
continue
259+
}
260+
var schema spec.Schema
261+
err := validation.ConvertJSONSchemaProps(version.Schema.OpenAPIV3Schema, &schema)
262+
if err != nil {
263+
return nil, err
264+
}
265+
setKubeNative(&schema, group, version.Name, kind)
266+
name := fmt.Sprintf("%s.%s.%s", group, version.Name, kind)
267+
schemas[name] = schema
268+
}
269+
} else if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
245270
var schema spec.Schema
246271
err := validation.ConvertJSONSchemaProps(crd.Spec.Validation.OpenAPIV3Schema, &schema)
247272
if err != nil {
248273
return nil, err
249274
}
250-
var version string
251-
if len(crd.Spec.Versions) >= 0 {
252-
version = crd.Spec.Versions[0].Name
253-
} else {
254-
version = crd.Spec.Version
255-
}
275+
version := crd.Spec.Version
256276
setKubeNative(&schema, group, version, kind)
257277
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
258278
schemas[name] = schema
259-
} else if len(crd.Spec.Versions) > 0 {
260-
for _, version := range crd.Spec.Versions {
261-
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
262-
var schema spec.Schema
263-
err := validation.ConvertJSONSchemaProps(version.Schema.OpenAPIV3Schema, &schema)
264-
if err != nil {
265-
return nil, err
266-
}
267-
version := version.Name
268-
setKubeNative(&schema, group, version, kind)
269-
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
270-
schemas[name] = schema
271-
}
272-
}
273279
}
274280

275281
// todo: set extensions, include kcl-type and user-defined extensions
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
"""
2+
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
3+
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
4+
"""
5+
import regex
6+
import k8s.apimachinery.pkg.apis.meta.v1
7+
_regex_match = regex.match
8+
9+
10+
schema GatewayClass:
11+
r"""
12+
GatewayClass describes a class of Gateways available to the user for creating Gateway resources.
13+
It is recommended that this resource be used as a template for Gateways. This means that a Gateway is based on the state of the GatewayClass at the time it was created and changes to the GatewayClass or associated parameters are not propagated down to existing Gateways. This recommendation is intended to limit the blast radius of changes to GatewayClass or associated parameters. If implementations choose to propagate GatewayClass changes to existing Gateways, that MUST be clearly documented by the implementation.
14+
Whenever one or more Gateways are using a GatewayClass, implementations SHOULD add the `gateway-exists-finalizer.gateway.networking.k8s.io` finalizer on the associated GatewayClass. This ensures that a GatewayClass associated with a Gateway is not deleted while in use.
15+
GatewayClass is a Cluster level resource.
16+
17+
Attributes
18+
----------
19+
apiVersion : str, default is "gateway.networking.k8s.io/v1beta1", required
20+
APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
21+
kind : str, default is "GatewayClass", required
22+
Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
23+
metadata : v1.ObjectMeta, default is Undefined, optional
24+
metadata
25+
spec : GatewayNetworkingK8sIoV1beta1GatewayClassSpec, default is Undefined, required
26+
spec
27+
status : GatewayNetworkingK8sIoV1beta1GatewayClassStatus, default is Undefined, optional
28+
status
29+
"""
30+
31+
32+
apiVersion: "gateway.networking.k8s.io/v1beta1" = "gateway.networking.k8s.io/v1beta1"
33+
34+
kind: "GatewayClass" = "GatewayClass"
35+
36+
metadata?: v1.ObjectMeta
37+
38+
spec: GatewayNetworkingK8sIoV1beta1GatewayClassSpec
39+
40+
status?: GatewayNetworkingK8sIoV1beta1GatewayClassStatus
41+
42+
43+
schema GatewayNetworkingK8sIoV1beta1GatewayClassSpec:
44+
r"""
45+
Spec defines the desired state of GatewayClass.
46+
47+
Attributes
48+
----------
49+
controllerName : str, default is Undefined, required
50+
ControllerName is the name of the controller that is managing Gateways of this class. The value of this field MUST be a domain prefixed path.
51+
Example: "example.net/gateway-controller".
52+
This field is not mutable and cannot be empty.
53+
Support: Core
54+
description : str, default is Undefined, optional
55+
Description helps describe a GatewayClass with more details.
56+
parametersRef : GatewayNetworkingK8sIoV1beta1GatewayClassSpecParametersRef, default is Undefined, optional
57+
parameters ref
58+
"""
59+
60+
61+
controllerName: str
62+
63+
description?: str
64+
65+
parametersRef?: GatewayNetworkingK8sIoV1beta1GatewayClassSpecParametersRef
66+
67+
68+
check:
69+
len(controllerName) <= 253
70+
len(controllerName) >= 1
71+
_regex_match(str(controllerName), r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$")
72+
len(description) <= 64 if description
73+
74+
75+
schema GatewayNetworkingK8sIoV1beta1GatewayClassSpecParametersRef:
76+
r"""
77+
ParametersRef is a reference to a resource that contains the configuration parameters corresponding to the GatewayClass. This is optional if the controller does not require any additional configuration.
78+
ParametersRef can reference a standard Kubernetes resource, i.e. ConfigMap, or an implementation-specific custom resource. The resource can be cluster-scoped or namespace-scoped.
79+
If the referent cannot be found, the GatewayClass's "InvalidParameters" status condition will be true.
80+
Support: Implementation-specific
81+
82+
Attributes
83+
----------
84+
group : str, default is Undefined, required
85+
Group is the group of the referent.
86+
kind : str, default is Undefined, required
87+
Kind is kind of the referent.
88+
name : str, default is Undefined, required
89+
Name is the name of the referent.
90+
namespace : str, default is Undefined, optional
91+
Namespace is the namespace of the referent. This field is required when referring to a Namespace-scoped resource and MUST be unset when referring to a Cluster-scoped resource.
92+
"""
93+
94+
95+
group: str
96+
97+
kind: str
98+
99+
name: str
100+
101+
namespace?: str
102+
103+
104+
check:
105+
len(group) <= 253
106+
_regex_match(str(group), r"^$|^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$")
107+
len(kind) <= 63
108+
len(kind) >= 1
109+
_regex_match(str(kind), r"^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$")
110+
len(name) <= 253
111+
len(name) >= 1
112+
len(namespace) <= 63 if namespace
113+
len(namespace) >= 1 if namespace
114+
_regex_match(str(namespace), r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$") if namespace
115+
116+
117+
schema GatewayNetworkingK8sIoV1beta1GatewayClassStatus:
118+
r"""
119+
Status defines the current state of GatewayClass.
120+
121+
Attributes
122+
----------
123+
conditions : [GatewayNetworkingK8sIoV1beta1GatewayClassStatusConditionsItems0], default is [{"lastTransitionTime": "1970-01-01T00:00:00Z", "message": "Waiting for controller", "reason": "Pending", "status": "Unknown", "type": "Accepted"}], optional
124+
Conditions is the current status from the controller for this GatewayClass.
125+
Controllers should prefer to publish conditions using values of GatewayClassConditionType for the type of each Condition.
126+
"""
127+
128+
129+
conditions?: [GatewayNetworkingK8sIoV1beta1GatewayClassStatusConditionsItems0] = [{"lastTransitionTime": "1970-01-01T00:00:00Z", "message": "Waiting for controller", "reason": "Pending", "status": "Unknown", "type": "Accepted"}]
130+
131+
132+
check:
133+
len(conditions) <= 8 if conditions
134+
135+
136+
schema GatewayNetworkingK8sIoV1beta1GatewayClassStatusConditionsItems0:
137+
r"""
138+
Condition contains details for one aspect of the current state of this API Resource. --- This struct is intended for direct use as an array at the field path .status.conditions. For example,
139+
type FooStatus struct{ // Represents the observations of a foo's current state. // Known .status.conditions.type are: "Available", "Progressing", and "Degraded" // +patchMergeKey=type // +patchStrategy=merge // +listType=map // +listMapKey=type Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
140+
// other fields }
141+
142+
Attributes
143+
----------
144+
lastTransitionTime : str, default is Undefined, required
145+
lastTransitionTime is the last time the condition transitioned from one status to another. This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
146+
message : str, default is Undefined, required
147+
message is a human readable message indicating details about the transition. This may be an empty string.
148+
observedGeneration : int, default is Undefined, optional
149+
observedGeneration represents the .metadata.generation that the condition was set based upon. For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date with respect to the current state of the instance.
150+
reason : str, default is Undefined, required
151+
reason contains a programmatic identifier indicating the reason for the condition's last transition. Producers of specific condition types may define expected values and meanings for this field, and whether the values are considered a guaranteed API. The value should be a CamelCase string. This field may not be empty.
152+
status : str, default is Undefined, required
153+
status of the condition, one of True, False, Unknown.
154+
$type : str, default is Undefined, required
155+
type of condition in CamelCase or in foo.example.com/CamelCase. --- Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be useful (see .node.status.conditions), the ability to deconflict is important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
156+
"""
157+
158+
159+
lastTransitionTime: str
160+
161+
message: str
162+
163+
observedGeneration?: int
164+
165+
reason: str
166+
167+
status: "True" | "False" | "Unknown"
168+
169+
$type: str
170+
171+
172+
check:
173+
len(message) <= 32768
174+
observedGeneration >= 0 if observedGeneration not in [None, Undefined]
175+
len(reason) <= 1024
176+
len(reason) >= 1
177+
_regex_match(str(reason), r"^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$")
178+
len($type) <= 316
179+
_regex_match(str($type), r"^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$")
180+
181+

0 commit comments

Comments
 (0)