@intentius/chant-lexicon-k8s 0.101.0 → 0.102.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/apps.d.ts +4 -4
- package/dist/actions/apps.d.ts.map +1 -1
- package/dist/actions/core.d.ts +5 -5
- package/dist/actions/core.d.ts.map +1 -1
- package/dist/actions/networking.d.ts.map +1 -1
- package/dist/codegen/generate-typescript.d.ts +2 -2
- package/dist/codegen/generate-typescript.d.ts.map +1 -1
- package/dist/codegen/generate.d.ts.map +1 -1
- package/dist/codegen/naming.d.ts.map +1 -1
- package/dist/codegen/versions.d.ts.map +1 -1
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/composites/catalog.d.ts.map +1 -1
- package/dist/composites/cockroachdb-cluster.d.ts +2 -2
- package/dist/composites/gke-otel-collector.d.ts.map +1 -1
- package/dist/composites/index.d.ts +1 -0
- package/dist/composites/index.d.ts.map +1 -1
- package/dist/composites/otel-collector-agent.d.ts +19 -1
- package/dist/composites/otel-collector-agent.d.ts.map +1 -1
- package/dist/composites/otel-collector-node.d.ts +88 -0
- package/dist/composites/otel-collector-node.d.ts.map +1 -0
- package/dist/composites/otel-collector-shape.d.ts.map +1 -1
- package/dist/composites/otel-collector.d.ts +9 -1
- package/dist/composites/otel-collector.d.ts.map +1 -1
- package/dist/composites/ray-cluster.d.ts +6 -6
- package/dist/composites/ray-job.d.ts +5 -5
- package/dist/composites/ray-service.d.ts +6 -6
- package/dist/crd/crd-sources.d.ts.map +1 -1
- package/dist/crd/loader.d.ts.map +1 -1
- package/dist/crd/oci-chart.d.ts +50 -0
- package/dist/crd/oci-chart.d.ts.map +1 -0
- package/dist/crd/types.d.ts +5 -0
- package/dist/crd/types.d.ts.map +1 -1
- package/dist/effect-receipt-row.d.ts.map +1 -1
- package/dist/generated/index.d.ts +752 -729
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/import/embedded.d.ts +4 -2
- package/dist/import/embedded.d.ts.map +1 -1
- package/dist/import/generator.d.ts.map +1 -1
- package/dist/import/parser.d.ts.map +1 -1
- package/dist/import/testdata/embedded/fixtures.d.ts +6 -0
- package/dist/import/testdata/embedded/fixtures.d.ts.map +1 -1
- package/dist/integrity.json +6 -5
- package/dist/lint/audit-catalog.d.ts.map +1 -1
- package/dist/lint/post-synth/index.d.ts.map +1 -1
- package/dist/lint/post-synth/otel-placement-helpers.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8605.d.ts +23 -0
- package/dist/lint/post-synth/wk8605.d.ts.map +1 -0
- package/dist/manifest.json +1 -1
- package/dist/meta.json +156 -17
- package/dist/okf/index.md +4 -0
- package/dist/okf/rules/WK8605.md +11 -0
- package/dist/okf/types/APIGroup.md +3 -3
- package/dist/okf/types/APIGroupList.md +1 -1
- package/dist/okf/types/APIResourceList.md +1 -1
- package/dist/okf/types/APIService.md +1 -1
- package/dist/okf/types/APIServiceList.md +2 -2
- package/dist/okf/types/APIVersions.md +1 -1
- package/dist/okf/types/BatchJob.md +1 -1
- package/dist/okf/types/CSIDriver.md +1 -1
- package/dist/okf/types/CSIDriverList.md +2 -2
- package/dist/okf/types/CSINode.md +1 -1
- package/dist/okf/types/CSINodeList.md +2 -2
- package/dist/okf/types/CSIStorageCapacity.md +2 -2
- package/dist/okf/types/CSIStorageCapacityList.md +2 -2
- package/dist/okf/types/CertificateSigningRequest.md +1 -1
- package/dist/okf/types/CertificateSigningRequestList.md +2 -2
- package/dist/okf/types/ClusterRole.md +1 -1
- package/dist/okf/types/ClusterRoleBindingList.md +2 -2
- package/dist/okf/types/ClusterRoleList.md +2 -2
- package/dist/okf/types/ClusterTrustBundle.md +1 -1
- package/dist/okf/types/ClusterTrustBundleList.md +2 -2
- package/dist/okf/types/ComponentStatus.md +1 -1
- package/dist/okf/types/ComponentStatusList.md +2 -2
- package/dist/okf/types/ConfigMapList.md +2 -2
- package/dist/okf/types/ControllerRevisionList.md +2 -2
- package/dist/okf/types/CoreEventList.md +2 -2
- package/dist/okf/types/CronJob.md +1 -1
- package/dist/okf/types/CronJobList.md +2 -2
- package/dist/okf/types/CustomResourceDefinition.md +1 -1
- package/dist/okf/types/CustomResourceDefinitionList.md +2 -2
- package/dist/okf/types/DaemonSet.md +1 -1
- package/dist/okf/types/DaemonSetList.md +2 -2
- package/dist/okf/types/DeleteOptions.md +1 -1
- package/dist/okf/types/Deploy.md +1 -1
- package/dist/okf/types/DeploymentList.md +2 -2
- package/dist/okf/types/DeviceClass.md +1 -1
- package/dist/okf/types/DeviceClassList.md +2 -2
- package/dist/okf/types/DeviceTaintRule.md +1 -1
- package/dist/okf/types/DeviceTaintRuleList.md +2 -2
- package/dist/okf/types/EndpointSlice.md +2 -2
- package/dist/okf/types/EndpointSliceList.md +2 -2
- package/dist/okf/types/Endpoints.md +1 -1
- package/dist/okf/types/EndpointsList.md +2 -2
- package/dist/okf/types/Event.md +2 -2
- package/dist/okf/types/EventsEvent.md +2 -2
- package/dist/okf/types/EventsEventList.md +2 -2
- package/dist/okf/types/Eviction.md +1 -1
- package/dist/okf/types/FlowSchema.md +1 -1
- package/dist/okf/types/FlowSchemaList.md +2 -2
- package/dist/okf/types/GrafanaDashboard.md +14 -0
- package/dist/okf/types/GrafanaDatasource.md +14 -0
- package/dist/okf/types/GrafanaFolder.md +14 -0
- package/dist/okf/types/HorizontalPodAutoscaler.md +1 -1
- package/dist/okf/types/HorizontalPodAutoscalerList.md +2 -2
- package/dist/okf/types/IPAddress.md +1 -1
- package/dist/okf/types/IPAddressList.md +2 -2
- package/dist/okf/types/Ing.md +1 -1
- package/dist/okf/types/IngressClass.md +1 -1
- package/dist/okf/types/IngressClassList.md +2 -2
- package/dist/okf/types/IngressList.md +2 -2
- package/dist/okf/types/JobList.md +2 -2
- package/dist/okf/types/Lease.md +1 -1
- package/dist/okf/types/LeaseCandidate.md +1 -1
- package/dist/okf/types/LeaseCandidateList.md +2 -2
- package/dist/okf/types/LeaseList.md +2 -2
- package/dist/okf/types/LimitRange.md +1 -1
- package/dist/okf/types/LimitRangeList.md +2 -2
- package/dist/okf/types/LocalSubjectAccessReview.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicy.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicyBinding.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicyBindingList.md +2 -2
- package/dist/okf/types/MutatingAdmissionPolicyList.md +2 -2
- package/dist/okf/types/MutatingWebhookConfiguration.md +1 -1
- package/dist/okf/types/MutatingWebhookConfigurationList.md +2 -2
- package/dist/okf/types/Namespace.md +1 -1
- package/dist/okf/types/NamespaceList.md +2 -2
- package/dist/okf/types/NetPol.md +1 -1
- package/dist/okf/types/NetworkPolicyList.md +2 -2
- package/dist/okf/types/Node.md +1 -1
- package/dist/okf/types/NodeList.md +2 -2
- package/dist/okf/types/PersistentVolume.md +1 -1
- package/dist/okf/types/PersistentVolumeClaimList.md +2 -2
- package/dist/okf/types/PersistentVolumeList.md +2 -2
- package/dist/okf/types/PodCertificateRequest.md +1 -1
- package/dist/okf/types/PodCertificateRequestList.md +2 -2
- package/dist/okf/types/PodDisruptionBudgetList.md +2 -2
- package/dist/okf/types/PodList.md +2 -2
- package/dist/okf/types/PodTemplateList.md +2 -2
- package/dist/okf/types/PriorityClassList.md +2 -2
- package/dist/okf/types/PriorityLevelConfiguration.md +1 -1
- package/dist/okf/types/PriorityLevelConfigurationList.md +2 -2
- package/dist/okf/types/ReplicaSet.md +1 -1
- package/dist/okf/types/ReplicaSetList.md +2 -2
- package/dist/okf/types/ReplicationController.md +1 -1
- package/dist/okf/types/ReplicationControllerList.md +2 -2
- package/dist/okf/types/ResourceClaim.md +1 -1
- package/dist/okf/types/ResourceClaimList.md +2 -2
- package/dist/okf/types/ResourceClaimTemplate.md +1 -1
- package/dist/okf/types/ResourceClaimTemplateList.md +2 -2
- package/dist/okf/types/ResourceQuota.md +1 -1
- package/dist/okf/types/ResourceQuotaList.md +2 -2
- package/dist/okf/types/ResourceSlice.md +1 -1
- package/dist/okf/types/ResourceSliceList.md +2 -2
- package/dist/okf/types/RoleBindingList.md +2 -2
- package/dist/okf/types/RoleList.md +2 -2
- package/dist/okf/types/RuntimeClass.md +2 -2
- package/dist/okf/types/RuntimeClassList.md +2 -2
- package/dist/okf/types/Scale.md +1 -1
- package/dist/okf/types/SecretList.md +2 -2
- package/dist/okf/types/SelfSubjectAccessReview.md +1 -1
- package/dist/okf/types/SelfSubjectRulesReview.md +1 -1
- package/dist/okf/types/Service.md +1 -1
- package/dist/okf/types/ServiceAccountList.md +2 -2
- package/dist/okf/types/ServiceCIDR.md +1 -1
- package/dist/okf/types/ServiceCIDRList.md +2 -2
- package/dist/okf/types/ServiceList.md +2 -2
- package/dist/okf/types/StatefulSet.md +1 -1
- package/dist/okf/types/StatefulSetList.md +2 -2
- package/dist/okf/types/Status.md +2 -2
- package/dist/okf/types/StorageClass.md +1 -1
- package/dist/okf/types/StorageClassList.md +2 -2
- package/dist/okf/types/StorageVersionMigration.md +1 -1
- package/dist/okf/types/StorageVersionMigrationList.md +2 -2
- package/dist/okf/types/SubjectAccessReview.md +1 -1
- package/dist/okf/types/TokenRequest.md +1 -1
- package/dist/okf/types/TokenReview.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicy.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicyBinding.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicyBindingList.md +2 -2
- package/dist/okf/types/ValidatingAdmissionPolicyList.md +2 -2
- package/dist/okf/types/ValidatingWebhookConfiguration.md +1 -1
- package/dist/okf/types/ValidatingWebhookConfigurationList.md +2 -2
- package/dist/okf/types/VolumeAttachment.md +1 -1
- package/dist/okf/types/VolumeAttachmentList.md +2 -2
- package/dist/okf/types/VolumeAttributesClassList.md +2 -2
- package/dist/op/activities/argo.d.ts.map +1 -1
- package/dist/op/activities/index.d.ts +3 -3
- package/dist/op/activities/index.d.ts.map +1 -1
- package/dist/op/activities/wait-for-ready.d.ts.map +1 -1
- package/dist/op/builders.d.ts.map +1 -1
- package/dist/rules/wk8605.ts +131 -0
- package/dist/skills/chant-k8s.md +1 -1
- package/dist/spec/parse.d.ts +27 -0
- package/dist/spec/parse.d.ts.map +1 -1
- package/dist/types/index.d.ts +5360 -972
- package/dist/variables.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/codegen/declarations.test.ts +118 -0
- package/src/codegen/generate-typescript.ts +69 -4
- package/src/codegen/generate.ts +15 -6
- package/src/composites/catalog.ts +6 -0
- package/src/composites/gke-otel-collector.ts +6 -1
- package/src/composites/index.ts +1 -0
- package/src/composites/otel-collector-agent.ts +39 -2
- package/src/composites/otel-collector-node.test.ts +174 -0
- package/src/composites/otel-collector-node.ts +213 -0
- package/src/composites/otel-collector.ts +15 -1
- package/src/crd/crd-sources.ts +26 -0
- package/src/crd/loader.test.ts +9 -1
- package/src/crd/loader.ts +26 -3
- package/src/crd/oci-chart.test.ts +149 -0
- package/src/crd/oci-chart.ts +178 -0
- package/src/crd/types.ts +5 -0
- package/src/generated/index.d.ts +5360 -972
- package/src/generated/index.ts +23 -0
- package/src/generated/lexicon-k8s.json +156 -17
- package/src/generated/operations.json +48 -0
- package/src/import/embedded-live.test.ts +46 -4
- package/src/import/embedded-roundtrip.test.ts +73 -7
- package/src/import/embedded-types.e2e.test.ts +3 -1
- package/src/import/embedded.test.ts +64 -0
- package/src/import/embedded.ts +31 -2
- package/src/import/generator.test.ts +19 -0
- package/src/import/generator.ts +20 -2
- package/src/import/testdata/embedded/README.md +4 -1
- package/src/import/testdata/embedded/alertmanager-configmap.yaml +91 -0
- package/src/import/testdata/embedded/fixtures.ts +18 -0
- package/src/import/testdata/embedded/grafana-operator-dashboard.yaml +363 -0
- package/src/import/testdata/embedded/grafana-v2-dashboard-configmap.yaml +851 -0
- package/src/lint/audit-catalog.ts +1 -0
- package/src/lint/post-synth/index.ts +2 -0
- package/src/lint/post-synth/post-synth.test.ts +3 -3
- package/src/lint/post-synth/wk8605.test.ts +141 -0
- package/src/lint/post-synth/wk8605.ts +131 -0
- package/src/skills/chant-k8s.md +1 -1
- package/src/spec/parse.test.ts +118 -0
- package/src/spec/parse.ts +250 -29
package/src/spec/parse.ts
CHANGED
|
@@ -39,6 +39,29 @@ export interface ParsedPropertyType {
|
|
|
39
39
|
properties: ParsedProperty[];
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A named OpenAPI object definition emitted as a declaration-only interface
|
|
44
|
+
* in the `.d.ts` (chant #3093): `DeploymentSpec`, `ServiceSpec`, `JobSpec`
|
|
45
|
+
* and every other object definition a resource reaches. Unlike the
|
|
46
|
+
* {@link PROPERTY_TYPE_DEFS} property types it has no runtime constructor and
|
|
47
|
+
* no registry entry; authors write it as an object literal.
|
|
48
|
+
*/
|
|
49
|
+
export interface ParsedDefinitionType {
|
|
50
|
+
/** TypeScript name, e.g. `DeploymentSpec` or `FlowcontrolSubject`. */
|
|
51
|
+
name: string;
|
|
52
|
+
/** The definition key, e.g. `io.k8s.api.apps.v1.DeploymentSpec`. */
|
|
53
|
+
defKey: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
properties: ParsedProperty[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Everything one swagger document yields: resources and property types, plus the declaration-only interfaces. */
|
|
59
|
+
export interface K8sSwaggerParse {
|
|
60
|
+
results: K8sParseResult[];
|
|
61
|
+
/** Sorted by name. */
|
|
62
|
+
definitionTypes: ParsedDefinitionType[];
|
|
63
|
+
}
|
|
64
|
+
|
|
42
65
|
export interface ParsedEnum {
|
|
43
66
|
name: string;
|
|
44
67
|
values: string[];
|
|
@@ -237,12 +260,22 @@ const PROPERTY_TYPE_DEFS: Record<string, { typeName: string; description: string
|
|
|
237
260
|
* Returns one result per top-level resource identified by x-kubernetes-group-version-kind.
|
|
238
261
|
*/
|
|
239
262
|
export function parseK8sSwagger(data: string | Buffer): K8sParseResult[] {
|
|
263
|
+
return parseK8sSwaggerTypes(data).results;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Parse the swagger document into resource and property-type results, and the
|
|
268
|
+
* declaration-only interfaces for every other object definition a resource
|
|
269
|
+
* reaches (chant #3093).
|
|
270
|
+
*/
|
|
271
|
+
export function parseK8sSwaggerTypes(data: string | Buffer): K8sSwaggerParse {
|
|
240
272
|
const spec: SwaggerSpec = JSON.parse(typeof data === "string" ? data : data.toString("utf-8"));
|
|
241
273
|
const definitions = spec.definitions ?? {};
|
|
242
274
|
const results: K8sParseResult[] = [];
|
|
243
275
|
const operations = parseOperations(spec.paths);
|
|
244
276
|
|
|
245
|
-
//
|
|
277
|
+
// Which definitions become resources: those with a GVK, preferred version only.
|
|
278
|
+
const resourceDefs: Array<{ defKey: string; def: SwaggerDefinition; gvk: GroupVersionKind }> = [];
|
|
246
279
|
for (const [defKey, def] of Object.entries(definitions)) {
|
|
247
280
|
const gvks = def["x-kubernetes-group-version-kind"];
|
|
248
281
|
if (!gvks || gvks.length === 0) continue;
|
|
@@ -252,9 +285,22 @@ export function parseK8sSwagger(data: string | Buffer): K8sParseResult[] {
|
|
|
252
285
|
|
|
253
286
|
// Skip internal/legacy API versions — only take the preferred version
|
|
254
287
|
if (!isPreferredVersion(defKey, gvk, definitions)) continue;
|
|
288
|
+
resourceDefs.push({ defKey, def, gvk });
|
|
289
|
+
}
|
|
255
290
|
|
|
291
|
+
// Name every object definition a resource reaches before any property is
|
|
292
|
+
// typed, so a `$ref` resolves to its interface instead of a Record.
|
|
293
|
+
const reachable = reachableDefinitions(resourceDefs.map((r) => r.def), definitions);
|
|
294
|
+
const ctx: TypeContext = {
|
|
295
|
+
definitions,
|
|
296
|
+
names: nameDefinitions(reachable, definitions),
|
|
297
|
+
resources: new Set(resourceDefs.map((r) => r.defKey)),
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// Phase 1: Extract top-level resources (definitions with GVK)
|
|
301
|
+
for (const { defKey, def, gvk } of resourceDefs) {
|
|
256
302
|
const typeName = gvkToTypeName(gvk);
|
|
257
|
-
const result = extractResource(defKey, def, typeName, gvk,
|
|
303
|
+
const result = extractResource(defKey, def, typeName, gvk, ctx);
|
|
258
304
|
if (result) {
|
|
259
305
|
const operation = operations.get(gvkKey(gvk));
|
|
260
306
|
if (operation) result.operation = operation;
|
|
@@ -267,11 +313,189 @@ export function parseK8sSwagger(data: string | Buffer): K8sParseResult[] {
|
|
|
267
313
|
const def = definitions[defKey];
|
|
268
314
|
if (!def) continue;
|
|
269
315
|
|
|
270
|
-
const result = extractPropertyType(defKey, def, config.typeName, config.description,
|
|
316
|
+
const result = extractPropertyType(defKey, def, config.typeName, config.description, ctx);
|
|
271
317
|
if (result) results.push(result);
|
|
272
318
|
}
|
|
273
319
|
|
|
274
|
-
|
|
320
|
+
// Phase 3: Every other reachable object definition, as an interface.
|
|
321
|
+
const definitionTypes: ParsedDefinitionType[] = [];
|
|
322
|
+
for (const defKey of reachable) {
|
|
323
|
+
if (PROPERTY_TYPE_DEFS[defKey]) continue;
|
|
324
|
+
const def = definitions[defKey];
|
|
325
|
+
definitionTypes.push({
|
|
326
|
+
name: ctx.names.get(defKey)!,
|
|
327
|
+
defKey,
|
|
328
|
+
description: def.description,
|
|
329
|
+
properties: parseProperties(def.properties ?? {}, new Set(def.required ?? []), ctx),
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
definitionTypes.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
333
|
+
|
|
334
|
+
return { results, definitionTypes };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ── Definition interfaces (chant #3093) ────────────────────────────
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Definitions that stay open however they are reached: `RawExtension` holds an
|
|
341
|
+
* arbitrary embedded object, and the rest are scalars on the wire. A Quantity
|
|
342
|
+
* is a string in the schema, but the API server also decodes a number
|
|
343
|
+
* (`cpu: 1`), and manifests write it both ways.
|
|
344
|
+
*/
|
|
345
|
+
const OPEN_OR_SCALAR_DEFS: Record<string, string> = {
|
|
346
|
+
"io.k8s.apimachinery.pkg.util.intstr.IntOrString": "string | number",
|
|
347
|
+
"io.k8s.apimachinery.pkg.api.resource.Quantity": "string | number",
|
|
348
|
+
"io.k8s.apimachinery.pkg.apis.meta.v1.Time": "string",
|
|
349
|
+
"io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime": "string",
|
|
350
|
+
"io.k8s.apimachinery.pkg.runtime.RawExtension": "Record<string, any>",
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
interface TypeContext {
|
|
354
|
+
definitions: Record<string, SwaggerDefinition>;
|
|
355
|
+
/** Definition key → TypeScript name, for every definition typed as a class or interface. */
|
|
356
|
+
names: Map<string, string>;
|
|
357
|
+
/** Emitted resource definitions, by key. */
|
|
358
|
+
resources: Set<string>;
|
|
359
|
+
/** Resource definitions being inlined, to stop a cycle. */
|
|
360
|
+
inlining?: Set<string>;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* A resource embedded in another object (`StatefulSetSpec.volumeClaimTemplates`
|
|
365
|
+
* holds PersistentVolumeClaims, a List holds its items) is typed inline with
|
|
366
|
+
* the props its class takes. A name would have to avoid the class's own.
|
|
367
|
+
*/
|
|
368
|
+
function inlineResourceType(defKey: string, ctx: TypeContext): string {
|
|
369
|
+
const inlining = ctx.inlining ?? new Set<string>();
|
|
370
|
+
if (inlining.has(defKey)) return "Record<string, any>";
|
|
371
|
+
const def = ctx.definitions[defKey];
|
|
372
|
+
const inner: TypeContext = { ...ctx, inlining: new Set([...inlining, defKey]) };
|
|
373
|
+
const required = new Set(def.required ?? []);
|
|
374
|
+
const members = Object.entries(def.properties ?? {})
|
|
375
|
+
.filter(([name]) => name !== "apiVersion" && name !== "kind" && name !== "status")
|
|
376
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
377
|
+
.map(([name, prop]) => `${name}${required.has(name) ? "" : "?"}: ${resolvePropertyType(prop, inner)}`);
|
|
378
|
+
return members.length === 0 ? "Record<string, any>" : `{ ${members.join("; ")} }`;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Whether a definition becomes a named type: an object with properties that
|
|
383
|
+
* is not itself a resource. A `$ref` to a resource is typed inline instead
|
|
384
|
+
* ({@link inlineResourceType}).
|
|
385
|
+
*/
|
|
386
|
+
function isNamedObjectDef(defKey: string, def: SwaggerDefinition | undefined): def is SwaggerDefinition {
|
|
387
|
+
if (!def?.properties || defKey in OPEN_OR_SCALAR_DEFS) return false;
|
|
388
|
+
return !def["x-kubernetes-group-version-kind"]?.length;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The object definitions reachable from the resources' authored properties
|
|
393
|
+
* (everything but `apiVersion`, `kind` and `status`), plus the well-known
|
|
394
|
+
* property types. Sorted by key. A recursive definition such as
|
|
395
|
+
* `JSONSchemaProps` is visited once and refers to itself by name, so no depth
|
|
396
|
+
* bound is needed.
|
|
397
|
+
*/
|
|
398
|
+
function reachableDefinitions(
|
|
399
|
+
resources: SwaggerDefinition[],
|
|
400
|
+
definitions: Record<string, SwaggerDefinition>,
|
|
401
|
+
): string[] {
|
|
402
|
+
const seen = new Set<string>();
|
|
403
|
+
const queue: string[] = [];
|
|
404
|
+
const visitRef = (defKey: string) => {
|
|
405
|
+
if (seen.has(defKey) || !isNamedObjectDef(defKey, definitions[defKey])) return;
|
|
406
|
+
seen.add(defKey);
|
|
407
|
+
queue.push(defKey);
|
|
408
|
+
};
|
|
409
|
+
const walk = (prop: SwaggerProperty | undefined) => {
|
|
410
|
+
if (!prop) return;
|
|
411
|
+
if (prop.$ref) {
|
|
412
|
+
if (prop.$ref.startsWith("#/definitions/")) visitRef(prop.$ref.slice("#/definitions/".length));
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
walk(prop.items);
|
|
416
|
+
if (prop.additionalProperties && typeof prop.additionalProperties === "object") walk(prop.additionalProperties);
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
for (const def of resources) {
|
|
420
|
+
for (const [name, prop] of Object.entries(def.properties ?? {})) {
|
|
421
|
+
if (name !== "apiVersion" && name !== "kind" && name !== "status") walk(prop);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
for (const defKey of Object.keys(PROPERTY_TYPE_DEFS)) visitRef(defKey);
|
|
425
|
+
while (queue.length > 0) {
|
|
426
|
+
const def = definitions[queue.shift()!];
|
|
427
|
+
for (const prop of Object.values(def.properties ?? {})) walk(prop);
|
|
428
|
+
}
|
|
429
|
+
return [...seen].sort();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const VERSION_SEGMENT = /^v\d+((alpha|beta)\d+)?$/;
|
|
433
|
+
|
|
434
|
+
/** `io.k8s.api.flowcontrol.v1.Subject` → `{ group: "Flowcontrol", version: "V1", kind: "Subject" }`. */
|
|
435
|
+
function definitionKeyParts(defKey: string): { group: string; version: string; kind: string } {
|
|
436
|
+
const parts = defKey.split(".");
|
|
437
|
+
const kind = parts[parts.length - 1];
|
|
438
|
+
const vIdx = parts.length - 2;
|
|
439
|
+
const version = VERSION_SEGMENT.test(parts[vIdx] ?? "") ? parts[vIdx] : "";
|
|
440
|
+
const group = (version ? parts[vIdx - 1] : parts[vIdx]) ?? "";
|
|
441
|
+
const pascal = (s: string) => s.split(/[^A-Za-z0-9]+/).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join("");
|
|
442
|
+
return { group: pascal(group), version: pascal(version), kind };
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Give every reachable definition a TypeScript name.
|
|
447
|
+
*
|
|
448
|
+
* The well-known property types keep their friendly names. Every other
|
|
449
|
+
* definition takes the last segment of its key (`DeploymentSpec`) when no
|
|
450
|
+
* other reachable definition shares that segment and no resource kind or
|
|
451
|
+
* friendly name already uses it. Otherwise each contender is qualified by its
|
|
452
|
+
* API group (`FlowcontrolSubject`, `CoreResourceClaim`), and by group and
|
|
453
|
+
* version when one group has several (`ResourceV1beta2Device`). The rule
|
|
454
|
+
* reads only the set of keys, so the result does not depend on the order of
|
|
455
|
+
* the document.
|
|
456
|
+
*/
|
|
457
|
+
function nameDefinitions(reachable: string[], definitions: Record<string, SwaggerDefinition>): Map<string, string> {
|
|
458
|
+
const names = new Map<string, string>();
|
|
459
|
+
const taken = new Set<string>();
|
|
460
|
+
for (const def of Object.values(definitions)) {
|
|
461
|
+
for (const gvk of def["x-kubernetes-group-version-kind"] ?? []) taken.add(gvk.kind);
|
|
462
|
+
}
|
|
463
|
+
for (const [defKey, config] of Object.entries(PROPERTY_TYPE_DEFS)) {
|
|
464
|
+
const name = k8sShortName(config.typeName);
|
|
465
|
+
names.set(defKey, name);
|
|
466
|
+
taken.add(name);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const byKind = new Map<string, string[]>();
|
|
470
|
+
for (const defKey of reachable) {
|
|
471
|
+
if (names.has(defKey)) continue;
|
|
472
|
+
const { kind } = definitionKeyParts(defKey);
|
|
473
|
+
byKind.set(kind, [...(byKind.get(kind) ?? []), defKey]);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
for (const [kind, keys] of byKind) {
|
|
477
|
+
if (keys.length === 1 && !taken.has(kind)) {
|
|
478
|
+
names.set(keys[0], kind);
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
const groupCount = new Map<string, number>();
|
|
482
|
+
for (const k of keys) {
|
|
483
|
+
const { group } = definitionKeyParts(k);
|
|
484
|
+
groupCount.set(group, (groupCount.get(group) ?? 0) + 1);
|
|
485
|
+
}
|
|
486
|
+
for (const k of keys) {
|
|
487
|
+
const { group, version } = definitionKeyParts(k);
|
|
488
|
+
names.set(k, groupCount.get(group)! > 1 ? `${group}${version}${kind}` : `${group}${kind}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const owner = new Map<string, string>();
|
|
493
|
+
for (const [defKey, name] of names) {
|
|
494
|
+
const prev = owner.get(name);
|
|
495
|
+
if (prev) throw new Error(`k8s codegen: definitions ${prev} and ${defKey} both map to the type name ${name}`);
|
|
496
|
+
owner.set(name, defKey);
|
|
497
|
+
}
|
|
498
|
+
return names;
|
|
275
499
|
}
|
|
276
500
|
|
|
277
501
|
/** Stable key for a GVK, used to join the `paths` pass onto the `definitions` pass. */
|
|
@@ -429,7 +653,7 @@ function extractResource(
|
|
|
429
653
|
def: SwaggerDefinition,
|
|
430
654
|
typeName: string,
|
|
431
655
|
gvk: GroupVersionKind,
|
|
432
|
-
|
|
656
|
+
ctx: TypeContext,
|
|
433
657
|
): K8sParseResult | null {
|
|
434
658
|
if (!def.properties) return null;
|
|
435
659
|
|
|
@@ -445,7 +669,7 @@ function extractResource(
|
|
|
445
669
|
}
|
|
446
670
|
}
|
|
447
671
|
|
|
448
|
-
const properties = parseProperties(filteredProps, requiredSet,
|
|
672
|
+
const properties = parseProperties(filteredProps, requiredSet, ctx);
|
|
449
673
|
|
|
450
674
|
return {
|
|
451
675
|
resource: {
|
|
@@ -473,12 +697,12 @@ function extractPropertyType(
|
|
|
473
697
|
def: SwaggerDefinition,
|
|
474
698
|
typeName: string,
|
|
475
699
|
description: string,
|
|
476
|
-
|
|
700
|
+
ctx: TypeContext,
|
|
477
701
|
): K8sParseResult | null {
|
|
478
702
|
if (!def.properties) return null;
|
|
479
703
|
|
|
480
704
|
const requiredSet = new Set<string>(def.required ?? []);
|
|
481
|
-
const properties = parseProperties(def.properties, requiredSet,
|
|
705
|
+
const properties = parseProperties(def.properties, requiredSet, ctx);
|
|
482
706
|
|
|
483
707
|
const gvkParts = typeName.split("::");
|
|
484
708
|
return {
|
|
@@ -502,12 +726,12 @@ function extractPropertyType(
|
|
|
502
726
|
function parseProperties(
|
|
503
727
|
properties: Record<string, SwaggerProperty>,
|
|
504
728
|
requiredSet: Set<string>,
|
|
505
|
-
|
|
729
|
+
ctx: TypeContext,
|
|
506
730
|
): ParsedProperty[] {
|
|
507
731
|
const result: ParsedProperty[] = [];
|
|
508
732
|
|
|
509
733
|
for (const [name, prop] of Object.entries(properties)) {
|
|
510
|
-
const tsType = resolvePropertyType(prop,
|
|
734
|
+
const tsType = resolvePropertyType(prop, ctx);
|
|
511
735
|
const parsed: ParsedProperty = {
|
|
512
736
|
name,
|
|
513
737
|
tsType,
|
|
@@ -532,12 +756,12 @@ function parseProperties(
|
|
|
532
756
|
/**
|
|
533
757
|
* Resolve a swagger property to its TypeScript type string.
|
|
534
758
|
*/
|
|
535
|
-
function resolvePropertyType(prop: SwaggerProperty,
|
|
759
|
+
function resolvePropertyType(prop: SwaggerProperty, ctx: TypeContext): string {
|
|
536
760
|
if (!prop) return "any";
|
|
537
761
|
|
|
538
762
|
// Handle $ref
|
|
539
763
|
if (prop.$ref) {
|
|
540
|
-
return resolveRefType(prop.$ref,
|
|
764
|
+
return resolveRefType(prop.$ref, ctx);
|
|
541
765
|
}
|
|
542
766
|
|
|
543
767
|
// x-kubernetes-int-or-string
|
|
@@ -561,14 +785,14 @@ function resolvePropertyType(prop: SwaggerProperty, definitions: Record<string,
|
|
|
561
785
|
return "boolean";
|
|
562
786
|
case "array":
|
|
563
787
|
if (prop.items) {
|
|
564
|
-
const itemType = resolvePropertyType(prop.items,
|
|
788
|
+
const itemType = resolvePropertyType(prop.items, ctx);
|
|
565
789
|
if (itemType.includes(" | ")) return `(${itemType})[]`;
|
|
566
790
|
return `${itemType}[]`;
|
|
567
791
|
}
|
|
568
792
|
return "any[]";
|
|
569
793
|
case "object":
|
|
570
794
|
if (prop.additionalProperties && typeof prop.additionalProperties === "object") {
|
|
571
|
-
const valueType = resolvePropertyType(prop.additionalProperties,
|
|
795
|
+
const valueType = resolvePropertyType(prop.additionalProperties, ctx);
|
|
572
796
|
return `Record<string, ${valueType}>`;
|
|
573
797
|
}
|
|
574
798
|
return "Record<string, any>";
|
|
@@ -580,27 +804,23 @@ function resolvePropertyType(prop: SwaggerProperty, definitions: Record<string,
|
|
|
580
804
|
/**
|
|
581
805
|
* Resolve a $ref to a TypeScript type.
|
|
582
806
|
*/
|
|
583
|
-
function resolveRefType(ref: string,
|
|
807
|
+
function resolveRefType(ref: string, ctx: TypeContext): string {
|
|
584
808
|
const prefix = "#/definitions/";
|
|
585
809
|
if (!ref.startsWith(prefix)) return "any";
|
|
586
810
|
|
|
587
811
|
const defKey = ref.slice(prefix.length);
|
|
588
812
|
|
|
589
|
-
// Well-known
|
|
590
|
-
|
|
591
|
-
if (
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
const ptConfig = PROPERTY_TYPE_DEFS[defKey];
|
|
598
|
-
if (ptConfig) {
|
|
599
|
-
return k8sShortName(ptConfig.typeName);
|
|
600
|
-
}
|
|
813
|
+
// Well-known scalars, and RawExtension, which stays open.
|
|
814
|
+
const fixed = OPEN_OR_SCALAR_DEFS[defKey];
|
|
815
|
+
if (fixed) return fixed;
|
|
816
|
+
|
|
817
|
+
// A property class or a definition interface (chant #3093).
|
|
818
|
+
const named = ctx.names.get(defKey);
|
|
819
|
+
if (named) return named;
|
|
820
|
+
if (ctx.resources.has(defKey)) return inlineResourceType(defKey, ctx);
|
|
601
821
|
|
|
602
822
|
// Resolve the definition
|
|
603
|
-
const def = definitions[defKey];
|
|
823
|
+
const def = ctx.definitions[defKey];
|
|
604
824
|
if (!def) return "any";
|
|
605
825
|
|
|
606
826
|
// Enum
|
|
@@ -620,7 +840,8 @@ function resolveRefType(ref: string, definitions: Record<string, SwaggerDefiniti
|
|
|
620
840
|
}
|
|
621
841
|
}
|
|
622
842
|
|
|
623
|
-
//
|
|
843
|
+
// An object this pass does not name: a resource of a non-preferred
|
|
844
|
+
// version, or a definition with no properties (`JSON`, `FieldsV1`).
|
|
624
845
|
if (def.properties) return "Record<string, any>";
|
|
625
846
|
|
|
626
847
|
return "any";
|