@intentius/chant-lexicon-k8s 0.100.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.
Files changed (261) hide show
  1. package/dist/actions/apps.d.ts +4 -4
  2. package/dist/actions/apps.d.ts.map +1 -1
  3. package/dist/actions/core.d.ts +5 -5
  4. package/dist/actions/core.d.ts.map +1 -1
  5. package/dist/actions/networking.d.ts.map +1 -1
  6. package/dist/codegen/generate-typescript.d.ts +2 -2
  7. package/dist/codegen/generate-typescript.d.ts.map +1 -1
  8. package/dist/codegen/generate.d.ts.map +1 -1
  9. package/dist/codegen/naming.d.ts.map +1 -1
  10. package/dist/codegen/versions.d.ts.map +1 -1
  11. package/dist/components/capability-plugin.d.ts.map +1 -1
  12. package/dist/composites/catalog.d.ts.map +1 -1
  13. package/dist/composites/cockroachdb-cluster.d.ts +2 -2
  14. package/dist/composites/gke-otel-collector.d.ts.map +1 -1
  15. package/dist/composites/index.d.ts +1 -0
  16. package/dist/composites/index.d.ts.map +1 -1
  17. package/dist/composites/otel-collector-agent.d.ts +24 -2
  18. package/dist/composites/otel-collector-agent.d.ts.map +1 -1
  19. package/dist/composites/otel-collector-gateway.d.ts +37 -52
  20. package/dist/composites/otel-collector-gateway.d.ts.map +1 -1
  21. package/dist/composites/otel-collector-node.d.ts +88 -0
  22. package/dist/composites/otel-collector-node.d.ts.map +1 -0
  23. package/dist/composites/otel-collector-rbac.d.ts +74 -0
  24. package/dist/composites/otel-collector-rbac.d.ts.map +1 -0
  25. package/dist/composites/otel-collector-shape.d.ts +7 -0
  26. package/dist/composites/otel-collector-shape.d.ts.map +1 -1
  27. package/dist/composites/otel-collector.d.ts +17 -21
  28. package/dist/composites/otel-collector.d.ts.map +1 -1
  29. package/dist/composites/ray-cluster.d.ts +6 -6
  30. package/dist/composites/ray-job.d.ts +5 -5
  31. package/dist/composites/ray-service.d.ts +6 -6
  32. package/dist/crd/crd-sources.d.ts.map +1 -1
  33. package/dist/crd/loader.d.ts.map +1 -1
  34. package/dist/crd/oci-chart.d.ts +50 -0
  35. package/dist/crd/oci-chart.d.ts.map +1 -0
  36. package/dist/crd/types.d.ts +5 -0
  37. package/dist/crd/types.d.ts.map +1 -1
  38. package/dist/effect-receipt-row.d.ts.map +1 -1
  39. package/dist/generated/index.d.ts +752 -729
  40. package/dist/generated/index.d.ts.map +1 -1
  41. package/dist/import/embedded.d.ts +4 -2
  42. package/dist/import/embedded.d.ts.map +1 -1
  43. package/dist/import/generator.d.ts.map +1 -1
  44. package/dist/import/live-export.d.ts +5 -0
  45. package/dist/import/live-export.d.ts.map +1 -1
  46. package/dist/import/parser.d.ts.map +1 -1
  47. package/dist/import/testdata/embedded/fixtures.d.ts +6 -0
  48. package/dist/import/testdata/embedded/fixtures.d.ts.map +1 -1
  49. package/dist/integrity.json +8 -6
  50. package/dist/lint/audit-catalog.d.ts.map +1 -1
  51. package/dist/lint/post-synth/index.d.ts.map +1 -1
  52. package/dist/lint/post-synth/otel-placement-helpers.d.ts +5 -1
  53. package/dist/lint/post-synth/otel-placement-helpers.d.ts.map +1 -1
  54. package/dist/lint/post-synth/wk8604.d.ts +19 -0
  55. package/dist/lint/post-synth/wk8604.d.ts.map +1 -0
  56. package/dist/lint/post-synth/wk8605.d.ts +23 -0
  57. package/dist/lint/post-synth/wk8605.d.ts.map +1 -0
  58. package/dist/manifest.json +1 -1
  59. package/dist/meta.json +156 -17
  60. package/dist/okf/index.md +5 -0
  61. package/dist/okf/rules/WK8604.md +11 -0
  62. package/dist/okf/rules/WK8605.md +11 -0
  63. package/dist/okf/types/APIGroup.md +3 -3
  64. package/dist/okf/types/APIGroupList.md +1 -1
  65. package/dist/okf/types/APIResourceList.md +1 -1
  66. package/dist/okf/types/APIService.md +1 -1
  67. package/dist/okf/types/APIServiceList.md +2 -2
  68. package/dist/okf/types/APIVersions.md +1 -1
  69. package/dist/okf/types/BatchJob.md +1 -1
  70. package/dist/okf/types/CSIDriver.md +1 -1
  71. package/dist/okf/types/CSIDriverList.md +2 -2
  72. package/dist/okf/types/CSINode.md +1 -1
  73. package/dist/okf/types/CSINodeList.md +2 -2
  74. package/dist/okf/types/CSIStorageCapacity.md +2 -2
  75. package/dist/okf/types/CSIStorageCapacityList.md +2 -2
  76. package/dist/okf/types/CertificateSigningRequest.md +1 -1
  77. package/dist/okf/types/CertificateSigningRequestList.md +2 -2
  78. package/dist/okf/types/ClusterRole.md +1 -1
  79. package/dist/okf/types/ClusterRoleBindingList.md +2 -2
  80. package/dist/okf/types/ClusterRoleList.md +2 -2
  81. package/dist/okf/types/ClusterTrustBundle.md +1 -1
  82. package/dist/okf/types/ClusterTrustBundleList.md +2 -2
  83. package/dist/okf/types/ComponentStatus.md +1 -1
  84. package/dist/okf/types/ComponentStatusList.md +2 -2
  85. package/dist/okf/types/ConfigMapList.md +2 -2
  86. package/dist/okf/types/ControllerRevisionList.md +2 -2
  87. package/dist/okf/types/CoreEventList.md +2 -2
  88. package/dist/okf/types/CronJob.md +1 -1
  89. package/dist/okf/types/CronJobList.md +2 -2
  90. package/dist/okf/types/CustomResourceDefinition.md +1 -1
  91. package/dist/okf/types/CustomResourceDefinitionList.md +2 -2
  92. package/dist/okf/types/DaemonSet.md +1 -1
  93. package/dist/okf/types/DaemonSetList.md +2 -2
  94. package/dist/okf/types/DeleteOptions.md +1 -1
  95. package/dist/okf/types/Deploy.md +1 -1
  96. package/dist/okf/types/DeploymentList.md +2 -2
  97. package/dist/okf/types/DeviceClass.md +1 -1
  98. package/dist/okf/types/DeviceClassList.md +2 -2
  99. package/dist/okf/types/DeviceTaintRule.md +1 -1
  100. package/dist/okf/types/DeviceTaintRuleList.md +2 -2
  101. package/dist/okf/types/EndpointSlice.md +2 -2
  102. package/dist/okf/types/EndpointSliceList.md +2 -2
  103. package/dist/okf/types/Endpoints.md +1 -1
  104. package/dist/okf/types/EndpointsList.md +2 -2
  105. package/dist/okf/types/Event.md +2 -2
  106. package/dist/okf/types/EventsEvent.md +2 -2
  107. package/dist/okf/types/EventsEventList.md +2 -2
  108. package/dist/okf/types/Eviction.md +1 -1
  109. package/dist/okf/types/FlowSchema.md +1 -1
  110. package/dist/okf/types/FlowSchemaList.md +2 -2
  111. package/dist/okf/types/GrafanaDashboard.md +14 -0
  112. package/dist/okf/types/GrafanaDatasource.md +14 -0
  113. package/dist/okf/types/GrafanaFolder.md +14 -0
  114. package/dist/okf/types/HorizontalPodAutoscaler.md +1 -1
  115. package/dist/okf/types/HorizontalPodAutoscalerList.md +2 -2
  116. package/dist/okf/types/IPAddress.md +1 -1
  117. package/dist/okf/types/IPAddressList.md +2 -2
  118. package/dist/okf/types/Ing.md +1 -1
  119. package/dist/okf/types/IngressClass.md +1 -1
  120. package/dist/okf/types/IngressClassList.md +2 -2
  121. package/dist/okf/types/IngressList.md +2 -2
  122. package/dist/okf/types/JobList.md +2 -2
  123. package/dist/okf/types/Lease.md +1 -1
  124. package/dist/okf/types/LeaseCandidate.md +1 -1
  125. package/dist/okf/types/LeaseCandidateList.md +2 -2
  126. package/dist/okf/types/LeaseList.md +2 -2
  127. package/dist/okf/types/LimitRange.md +1 -1
  128. package/dist/okf/types/LimitRangeList.md +2 -2
  129. package/dist/okf/types/LocalSubjectAccessReview.md +1 -1
  130. package/dist/okf/types/MutatingAdmissionPolicy.md +1 -1
  131. package/dist/okf/types/MutatingAdmissionPolicyBinding.md +1 -1
  132. package/dist/okf/types/MutatingAdmissionPolicyBindingList.md +2 -2
  133. package/dist/okf/types/MutatingAdmissionPolicyList.md +2 -2
  134. package/dist/okf/types/MutatingWebhookConfiguration.md +1 -1
  135. package/dist/okf/types/MutatingWebhookConfigurationList.md +2 -2
  136. package/dist/okf/types/Namespace.md +1 -1
  137. package/dist/okf/types/NamespaceList.md +2 -2
  138. package/dist/okf/types/NetPol.md +1 -1
  139. package/dist/okf/types/NetworkPolicyList.md +2 -2
  140. package/dist/okf/types/Node.md +1 -1
  141. package/dist/okf/types/NodeList.md +2 -2
  142. package/dist/okf/types/PersistentVolume.md +1 -1
  143. package/dist/okf/types/PersistentVolumeClaimList.md +2 -2
  144. package/dist/okf/types/PersistentVolumeList.md +2 -2
  145. package/dist/okf/types/PodCertificateRequest.md +1 -1
  146. package/dist/okf/types/PodCertificateRequestList.md +2 -2
  147. package/dist/okf/types/PodDisruptionBudgetList.md +2 -2
  148. package/dist/okf/types/PodList.md +2 -2
  149. package/dist/okf/types/PodTemplateList.md +2 -2
  150. package/dist/okf/types/PriorityClassList.md +2 -2
  151. package/dist/okf/types/PriorityLevelConfiguration.md +1 -1
  152. package/dist/okf/types/PriorityLevelConfigurationList.md +2 -2
  153. package/dist/okf/types/ReplicaSet.md +1 -1
  154. package/dist/okf/types/ReplicaSetList.md +2 -2
  155. package/dist/okf/types/ReplicationController.md +1 -1
  156. package/dist/okf/types/ReplicationControllerList.md +2 -2
  157. package/dist/okf/types/ResourceClaim.md +1 -1
  158. package/dist/okf/types/ResourceClaimList.md +2 -2
  159. package/dist/okf/types/ResourceClaimTemplate.md +1 -1
  160. package/dist/okf/types/ResourceClaimTemplateList.md +2 -2
  161. package/dist/okf/types/ResourceQuota.md +1 -1
  162. package/dist/okf/types/ResourceQuotaList.md +2 -2
  163. package/dist/okf/types/ResourceSlice.md +1 -1
  164. package/dist/okf/types/ResourceSliceList.md +2 -2
  165. package/dist/okf/types/RoleBindingList.md +2 -2
  166. package/dist/okf/types/RoleList.md +2 -2
  167. package/dist/okf/types/RuntimeClass.md +2 -2
  168. package/dist/okf/types/RuntimeClassList.md +2 -2
  169. package/dist/okf/types/Scale.md +1 -1
  170. package/dist/okf/types/SecretList.md +2 -2
  171. package/dist/okf/types/SelfSubjectAccessReview.md +1 -1
  172. package/dist/okf/types/SelfSubjectRulesReview.md +1 -1
  173. package/dist/okf/types/Service.md +1 -1
  174. package/dist/okf/types/ServiceAccountList.md +2 -2
  175. package/dist/okf/types/ServiceCIDR.md +1 -1
  176. package/dist/okf/types/ServiceCIDRList.md +2 -2
  177. package/dist/okf/types/ServiceList.md +2 -2
  178. package/dist/okf/types/StatefulSet.md +1 -1
  179. package/dist/okf/types/StatefulSetList.md +2 -2
  180. package/dist/okf/types/Status.md +2 -2
  181. package/dist/okf/types/StorageClass.md +1 -1
  182. package/dist/okf/types/StorageClassList.md +2 -2
  183. package/dist/okf/types/StorageVersionMigration.md +1 -1
  184. package/dist/okf/types/StorageVersionMigrationList.md +2 -2
  185. package/dist/okf/types/SubjectAccessReview.md +1 -1
  186. package/dist/okf/types/TokenRequest.md +1 -1
  187. package/dist/okf/types/TokenReview.md +1 -1
  188. package/dist/okf/types/ValidatingAdmissionPolicy.md +1 -1
  189. package/dist/okf/types/ValidatingAdmissionPolicyBinding.md +1 -1
  190. package/dist/okf/types/ValidatingAdmissionPolicyBindingList.md +2 -2
  191. package/dist/okf/types/ValidatingAdmissionPolicyList.md +2 -2
  192. package/dist/okf/types/ValidatingWebhookConfiguration.md +1 -1
  193. package/dist/okf/types/ValidatingWebhookConfigurationList.md +2 -2
  194. package/dist/okf/types/VolumeAttachment.md +1 -1
  195. package/dist/okf/types/VolumeAttachmentList.md +2 -2
  196. package/dist/okf/types/VolumeAttributesClassList.md +2 -2
  197. package/dist/op/activities/argo.d.ts.map +1 -1
  198. package/dist/op/activities/index.d.ts +3 -3
  199. package/dist/op/activities/index.d.ts.map +1 -1
  200. package/dist/op/activities/wait-for-ready.d.ts.map +1 -1
  201. package/dist/op/builders.d.ts.map +1 -1
  202. package/dist/rules/otel-placement-helpers.ts +7 -11
  203. package/dist/rules/wk8604.ts +29 -0
  204. package/dist/rules/wk8605.ts +131 -0
  205. package/dist/skills/chant-k8s.md +1 -1
  206. package/dist/spec/parse.d.ts +27 -0
  207. package/dist/spec/parse.d.ts.map +1 -1
  208. package/dist/types/index.d.ts +5360 -972
  209. package/dist/variables.d.ts.map +1 -1
  210. package/package.json +4 -4
  211. package/src/codegen/declarations.test.ts +118 -0
  212. package/src/codegen/generate-typescript.ts +69 -4
  213. package/src/codegen/generate.ts +15 -6
  214. package/src/composites/__snapshots__/gke-otel-collector.snapshot.test.ts.snap +2 -68
  215. package/src/composites/catalog.ts +12 -0
  216. package/src/composites/gke-otel-collector.snapshot.test.ts +3 -1
  217. package/src/composites/gke-otel-collector.ts +13 -2
  218. package/src/composites/index.ts +1 -0
  219. package/src/composites/otel-collector-agent.ts +45 -11
  220. package/src/composites/otel-collector-gateway.test.ts +102 -5
  221. package/src/composites/otel-collector-gateway.ts +96 -18
  222. package/src/composites/otel-collector-node.test.ts +174 -0
  223. package/src/composites/otel-collector-node.ts +213 -0
  224. package/src/composites/otel-collector-rbac.test.ts +114 -0
  225. package/src/composites/otel-collector-rbac.ts +144 -0
  226. package/src/composites/otel-collector-shape.ts +19 -0
  227. package/src/composites/otel-collector.ts +40 -35
  228. package/src/crd/crd-sources.ts +26 -0
  229. package/src/crd/loader.test.ts +9 -1
  230. package/src/crd/loader.ts +26 -3
  231. package/src/crd/oci-chart.test.ts +149 -0
  232. package/src/crd/oci-chart.ts +178 -0
  233. package/src/crd/types.ts +5 -0
  234. package/src/generated/index.d.ts +5360 -972
  235. package/src/generated/index.ts +23 -0
  236. package/src/generated/lexicon-k8s.json +156 -17
  237. package/src/generated/operations.json +48 -0
  238. package/src/import/embedded-live.test.ts +180 -0
  239. package/src/import/embedded-roundtrip.test.ts +73 -7
  240. package/src/import/embedded-types.e2e.test.ts +3 -1
  241. package/src/import/embedded.test.ts +64 -0
  242. package/src/import/embedded.ts +31 -2
  243. package/src/import/generator.test.ts +19 -0
  244. package/src/import/generator.ts +20 -2
  245. package/src/import/live-export.ts +10 -2
  246. package/src/import/testdata/embedded/README.md +4 -1
  247. package/src/import/testdata/embedded/alertmanager-configmap.yaml +91 -0
  248. package/src/import/testdata/embedded/fixtures.ts +18 -0
  249. package/src/import/testdata/embedded/grafana-operator-dashboard.yaml +363 -0
  250. package/src/import/testdata/embedded/grafana-v2-dashboard-configmap.yaml +851 -0
  251. package/src/lint/audit-catalog.ts +2 -0
  252. package/src/lint/post-synth/index.ts +4 -0
  253. package/src/lint/post-synth/otel-placement-helpers.ts +7 -11
  254. package/src/lint/post-synth/otel-placement.test.ts +35 -0
  255. package/src/lint/post-synth/post-synth.test.ts +3 -3
  256. package/src/lint/post-synth/wk8604.ts +29 -0
  257. package/src/lint/post-synth/wk8605.test.ts +141 -0
  258. package/src/lint/post-synth/wk8605.ts +131 -0
  259. package/src/skills/chant-k8s.md +1 -1
  260. package/src/spec/parse.test.ts +118 -0
  261. package/src/spec/parse.ts +250 -29
@@ -76,7 +76,15 @@ describe("helm CRD source", () => {
76
76
 
77
77
  test("says what to do when helm is missing or the pull fails", async () => {
78
78
  await expect(
79
- loadCRDs({ type: "helm", chart: "oci://example.invalid/nope", version: "0.0.1" }),
79
+ loadCRDs({ type: "helm", chart: "https://example.invalid/charts/nope", version: "0.0.1" }),
80
80
  ).rejects.toThrow(/helm (pull failed|not found on PATH)/);
81
81
  });
82
+
83
+ // An oci:// chart is pulled over HTTP, so a missing helm cannot change what
84
+ // generation produces; a failed pull names the chart instead.
85
+ test("an oci:// chart does not go through helm", async () => {
86
+ await expect(
87
+ loadCRDs({ type: "helm", chart: "oci://example.invalid/nope", version: "0.0.1" }),
88
+ ).rejects.toThrow(/could not pull chart oci:\/\/example\.invalid\/nope 0\.0\.1/);
89
+ });
82
90
  });
package/src/crd/loader.ts CHANGED
@@ -10,6 +10,7 @@ import { fetchWithRetry } from "@intentius/chant/codegen/fetch";
10
10
  import type { CRDSource, CRDSpec } from "./types";
11
11
  import type { K8sParseResult } from "../spec/parse";
12
12
  import { parseCRD } from "./parser";
13
+ import { chartYamlFiles, pullOciChart, readTarGz } from "./oci-chart";
13
14
 
14
15
  /**
15
16
  * Load CRDs from a source and return parsed K8sParseResult entries.
@@ -93,8 +94,11 @@ async function loadFromURL(source: CRDSource): Promise<string> {
93
94
  * unchanged — a chart carrying more CRDs than a consumer wants is the same
94
95
  * situation as Flux's install bundle.
95
96
  *
96
- * Needs the `helm` binary at generation time, the same shape of dependency
97
- * that type="cluster" has on `kubectl`.
97
+ * An `oci://` chart is pulled over the registry's HTTP API and unpacked in
98
+ * process, so generation does not depend on `helm` being on PATH. Before,
99
+ * a host without helm dropped the chart's kinds with only a warning and the
100
+ * generated surface changed with the machine. Only a classic `https://` chart
101
+ * repository still shells out to `helm pull`.
98
102
  */
99
103
  async function loadFromHelmChart(source: CRDSource): Promise<string> {
100
104
  if (!source.chart) {
@@ -107,6 +111,26 @@ async function loadFromHelmChart(source: CRDSource): Promise<string> {
107
111
  );
108
112
  }
109
113
 
114
+ const subdir = source.chartSubdir ?? "crds";
115
+
116
+ if (source.chart.startsWith("oci://")) {
117
+ let tgz: Buffer;
118
+ try {
119
+ tgz = await pullOciChart(source.chart, source.version, source.digest);
120
+ } catch (err) {
121
+ const msg = err instanceof Error ? err.message : String(err);
122
+ throw new Error(`could not pull chart ${source.chart} ${source.version}: ${msg}`);
123
+ }
124
+ const files = chartYamlFiles(readTarGz(tgz), subdir);
125
+ if (files.length === 0) {
126
+ throw new Error(
127
+ `chart ${source.chart} ${source.version} has no CRD YAML in '${subdir}'. ` +
128
+ "Charts that template their CRDs instead of shipping them in crds/ need 'chartSubdir'.",
129
+ );
130
+ }
131
+ return files.map(([, text]) => text).join("\n---\n");
132
+ }
133
+
110
134
  const { execFile } = await import("child_process");
111
135
  const { promisify } = await import("util");
112
136
  const { mkdtempSync, rmSync, readdirSync, readFileSync: read } = await import("fs");
@@ -142,7 +166,6 @@ async function loadFromHelmChart(source: CRDSource): Promise<string> {
142
166
  );
143
167
  }
144
168
 
145
- const subdir = source.chartSubdir ?? "crds";
146
169
  const crdDir = join(workdir, unpacked[0].name, subdir);
147
170
 
148
171
  let entries: string[];
@@ -0,0 +1,149 @@
1
+ import { describe, test, expect } from "vitest";
2
+ import { createHash } from "crypto";
3
+ import { gzipSync } from "zlib";
4
+ import {
5
+ HELM_CHART_LAYER,
6
+ chartYamlFiles,
7
+ parseOciRef,
8
+ pullOciChart,
9
+ readTarGz,
10
+ type OciHttp,
11
+ } from "./oci-chart";
12
+
13
+ /** A minimal ustar writer, enough to stand in for a Helm chart archive. */
14
+ function tarGz(entries: Array<{ name: string; body: string; type?: string }>): Buffer {
15
+ const blocks: Buffer[] = [];
16
+ for (const { name, body, type = "0" } of entries) {
17
+ const data = Buffer.from(body, "utf8");
18
+ const header = Buffer.alloc(512);
19
+ header.write(name.slice(0, 100), 0, "utf8");
20
+ header.write("0000644\0", 100);
21
+ header.write("0000000\0", 108);
22
+ header.write("0000000\0", 116);
23
+ header.write(data.length.toString(8).padStart(11, "0") + "\0", 124);
24
+ header.write("00000000000\0", 136);
25
+ header.write(" ", 148);
26
+ header.write(type, 156);
27
+ header.write("ustar\0", 257);
28
+ header.write("00", 263);
29
+ let sum = 0;
30
+ for (const b of header) sum += b;
31
+ header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148);
32
+ blocks.push(header, data, Buffer.alloc((512 - (data.length % 512)) % 512));
33
+ }
34
+ blocks.push(Buffer.alloc(1024));
35
+ return gzipSync(Buffer.concat(blocks));
36
+ }
37
+
38
+ const CHART = tarGz([
39
+ { name: "op/Chart.yaml", body: "name: op\n" },
40
+ { name: "op/templates/deployment.yaml", body: "kind: Deployment\n" },
41
+ { name: "op/crds/b.yml", body: "kind: B\n" },
42
+ { name: "op/crds/a.yaml", body: "kind: A\n" },
43
+ { name: "op/crds/README.md", body: "not yaml\n" },
44
+ { name: "op/crds/nested/c.yaml", body: "kind: C\n" },
45
+ ]);
46
+
47
+ describe("readTarGz / chartYamlFiles", () => {
48
+ test("reads regular files and keeps only YAML directly in the subdir, sorted", () => {
49
+ const files = readTarGz(CHART);
50
+ expect(files.get("op/Chart.yaml")?.toString()).toBe("name: op\n");
51
+ expect(chartYamlFiles(files, "crds")).toEqual([
52
+ ["op/crds/a.yaml", "kind: A\n"],
53
+ ["op/crds/b.yml", "kind: B\n"],
54
+ ]);
55
+ });
56
+
57
+ test("follows a GNU long-name record", () => {
58
+ const long = `op/crds/${"x".repeat(120)}.yaml`;
59
+ const files = readTarGz(
60
+ tarGz([
61
+ { name: "././@LongLink", body: long + "\0", type: "L" },
62
+ { name: "truncated", body: "kind: Long\n" },
63
+ ]),
64
+ );
65
+ expect(files.get(long)?.toString()).toBe("kind: Long\n");
66
+ });
67
+
68
+ test("follows a pax path record", () => {
69
+ const long = `op/crds/${"y".repeat(120)}.yaml`;
70
+ const record = `path=${long}\n`;
71
+ const len = String(record.length + 4).length + 1 + record.length;
72
+ const files = readTarGz(
73
+ tarGz([
74
+ { name: "PaxHeader", body: `${len} ${record}`, type: "x" },
75
+ { name: "truncated", body: "kind: Pax\n" },
76
+ ]),
77
+ );
78
+ expect(files.get(long)?.toString()).toBe("kind: Pax\n");
79
+ });
80
+ });
81
+
82
+ describe("parseOciRef", () => {
83
+ test("splits host and repository", () => {
84
+ expect(parseOciRef("oci://ghcr.io/org/helm/op")).toEqual({ host: "ghcr.io", repository: "org/helm/op" });
85
+ });
86
+
87
+ test("rejects a non-oci reference", () => {
88
+ expect(() => parseOciRef("https://charts.example.com/op")).toThrow(/not an oci:\/\//);
89
+ });
90
+ });
91
+
92
+ describe("pullOciChart", () => {
93
+ const digest = `sha256:${createHash("sha256").update(CHART).digest("hex")}`;
94
+
95
+ /** A registry that challenges, hands out a token, and serves one chart. */
96
+ function registry(layerDigest = digest, blob = CHART): { http: OciHttp; seen: string[] } {
97
+ const seen: string[] = [];
98
+ const json = (body: unknown) => new Response(JSON.stringify(body), { status: 200 });
99
+ const http: OciHttp = {
100
+ probe: async (url) => {
101
+ seen.push(url);
102
+ return new Response("", {
103
+ status: 401,
104
+ headers: { "www-authenticate": 'Bearer realm="https://reg.test/token",service="reg.test"' },
105
+ });
106
+ },
107
+ get: async (url, init) => {
108
+ seen.push(url);
109
+ if (url.startsWith("https://reg.test/token")) {
110
+ expect(new URL(url).searchParams.get("scope")).toBe("repository:org/op:pull");
111
+ return json({ token: "t0k" });
112
+ }
113
+ expect((init?.headers as Record<string, string>).Authorization).toBe("Bearer t0k");
114
+ if (url.endsWith("/manifests/1.2.3")) {
115
+ return json({ layers: [{ mediaType: HELM_CHART_LAYER, digest: layerDigest }] });
116
+ }
117
+ if (url.endsWith(`/blobs/${layerDigest}`)) return new Response(new Uint8Array(blob), { status: 200 });
118
+ throw new Error(`unexpected ${url}`);
119
+ },
120
+ };
121
+ return { http, seen };
122
+ }
123
+
124
+ test("pulls the chart layer with an anonymous token, without helm", async () => {
125
+ const { http, seen } = registry();
126
+ const blob = await pullOciChart("oci://reg.test/org/op", "1.2.3", digest, http);
127
+ expect(blob.equals(CHART)).toBe(true);
128
+ expect(seen).toEqual([
129
+ "https://reg.test/v2/",
130
+ "https://reg.test/token?service=reg.test&scope=repository%3Aorg%2Fop%3Apull",
131
+ "https://reg.test/v2/org/op/manifests/1.2.3",
132
+ `https://reg.test/v2/org/op/blobs/${digest}`,
133
+ ]);
134
+ });
135
+
136
+ test("refuses a re-pushed tag whose layer no longer matches the pin", async () => {
137
+ const { http } = registry();
138
+ await expect(pullOciChart("oci://reg.test/org/op", "1.2.3", "sha256:" + "0".repeat(64), http)).rejects.toThrow(
139
+ /re-pushed/,
140
+ );
141
+ });
142
+
143
+ test("refuses a blob whose bytes do not match its digest", async () => {
144
+ const { http } = registry(digest, gzipSync(Buffer.from("tampered")));
145
+ await expect(pullOciChart("oci://reg.test/org/op", "1.2.3", undefined, http)).rejects.toThrow(
146
+ /downloaded as sha256:/,
147
+ );
148
+ });
149
+ });
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Pull a Helm chart out of an OCI registry over plain HTTP, and read its files.
3
+ *
4
+ * Generation used to shell out to `helm pull` for these. A machine without
5
+ * helm on PATH (a fresh CI runner, a container) then dropped the chart's kinds
6
+ * with only a warning, and the generated surface depended on which tools the
7
+ * host happened to have. The registry API is three requests, and a chart is a
8
+ * small gzipped tarball, so neither needs the binary.
9
+ */
10
+
11
+ import { createHash } from "crypto";
12
+ import { gunzipSync } from "zlib";
13
+ import { fetchWithRetry } from "@intentius/chant/codegen/fetch";
14
+
15
+ /** The layer media type Helm pushes a chart's content under. */
16
+ export const HELM_CHART_LAYER = "application/vnd.cncf.helm.chart.content.v1.tar+gzip";
17
+
18
+ const MANIFEST_ACCEPT = [
19
+ "application/vnd.oci.image.manifest.v1+json",
20
+ "application/vnd.docker.distribution.manifest.v2+json",
21
+ ].join(", ");
22
+
23
+ type Fetch = (url: string, init?: RequestInit) => Promise<Response>;
24
+
25
+ /**
26
+ * The two kinds of request a pull makes. `probe` must return a 401 as a
27
+ * response (the registry's auth challenge); `get` throws on any failure and
28
+ * retries transient ones.
29
+ */
30
+ export interface OciHttp {
31
+ probe: Fetch;
32
+ get: Fetch;
33
+ }
34
+
35
+ const defaultHttp: OciHttp = {
36
+ probe: (url, init) => fetch(url, init),
37
+ get: (url, init) => fetchWithRetry(url, undefined, undefined, init),
38
+ };
39
+
40
+ export interface OciChartRef {
41
+ /** Registry host, e.g. "ghcr.io". */
42
+ host: string;
43
+ /** Repository path inside the registry, e.g. "codriverlabs/helm/kube-microvm-operator". */
44
+ repository: string;
45
+ }
46
+
47
+ /** Split an `oci://host/repo/path` chart reference. */
48
+ export function parseOciRef(chart: string): OciChartRef {
49
+ const m = /^oci:\/\/([^/]+)\/(.+)$/.exec(chart);
50
+ if (!m) throw new Error(`not an oci:// chart reference: ${chart}`);
51
+ return { host: m[1], repository: m[2].replace(/\/+$/, "") };
52
+ }
53
+
54
+ /**
55
+ * Get an anonymous pull token, following the registry's own challenge
56
+ * (`WWW-Authenticate: Bearer realm=...,service=...`). Returns undefined when
57
+ * the registry does not ask for one.
58
+ */
59
+ async function anonymousToken(ref: OciChartRef, http: OciHttp): Promise<string | undefined> {
60
+ const probe = await http.probe(`https://${ref.host}/v2/`);
61
+ if (probe.status !== 401) return undefined;
62
+ const challenge = probe.headers.get("www-authenticate") ?? "";
63
+ const params = Object.fromEntries(
64
+ [...challenge.matchAll(/(\w+)="([^"]*)"/g)].map((m) => [m[1], m[2]]),
65
+ );
66
+ if (!/^bearer/i.test(challenge) || !params.realm) {
67
+ throw new Error(`registry ${ref.host} asks for auth this loader does not speak: ${challenge || "(no challenge)"}`);
68
+ }
69
+ const url = new URL(params.realm);
70
+ if (params.service) url.searchParams.set("service", params.service);
71
+ url.searchParams.set("scope", `repository:${ref.repository}:pull`);
72
+ const res = await http.get(url.toString());
73
+ const body = (await res.json()) as { token?: string; access_token?: string };
74
+ const token = body.token ?? body.access_token;
75
+ if (!token) throw new Error(`registry ${ref.host} returned no token for ${ref.repository}`);
76
+ return token;
77
+ }
78
+
79
+ /**
80
+ * Download a chart's content layer (the .tgz `helm pull` would fetch).
81
+ *
82
+ * @param digest When given ("sha256:..."), the layer must have this digest.
83
+ * Pinning it makes the source content-addressed: a re-pushed
84
+ * tag fails loudly instead of changing generated output.
85
+ */
86
+ export async function pullOciChart(
87
+ chart: string,
88
+ version: string,
89
+ digest?: string,
90
+ http: OciHttp = defaultHttp,
91
+ ): Promise<Buffer> {
92
+ const ref = parseOciRef(chart);
93
+ const token = await anonymousToken(ref, http);
94
+ const auth: Record<string, string> = token ? { Authorization: `Bearer ${token}` } : {};
95
+ const base = `https://${ref.host}/v2/${ref.repository}`;
96
+
97
+ const manifestRes = await http.get(`${base}/manifests/${version}`, {
98
+ headers: { ...auth, Accept: MANIFEST_ACCEPT },
99
+ });
100
+ const manifest = (await manifestRes.json()) as { layers?: Array<{ mediaType: string; digest: string }> };
101
+ const layer = manifest.layers?.find((l) => l.mediaType === HELM_CHART_LAYER);
102
+ if (!layer) {
103
+ throw new Error(`${chart}:${version} has no layer of type ${HELM_CHART_LAYER}; is it a Helm chart?`);
104
+ }
105
+ if (digest && layer.digest !== digest) {
106
+ throw new Error(
107
+ `${chart}:${version} chart layer is ${layer.digest}, but the source pins ${digest}. ` +
108
+ "The tag was re-pushed; review the new chart and update the pin.",
109
+ );
110
+ }
111
+
112
+ const blobRes = await http.get(`${base}/blobs/${layer.digest}`, { headers: auth });
113
+ const blob = Buffer.from(await blobRes.arrayBuffer());
114
+ const actual = `sha256:${createHash("sha256").update(blob).digest("hex")}`;
115
+ if (actual !== layer.digest) {
116
+ throw new Error(`${chart}:${version} chart layer downloaded as ${actual}, expected ${layer.digest}`);
117
+ }
118
+ return blob;
119
+ }
120
+
121
+ /**
122
+ * Read the regular files out of a gzipped tarball, as path to content.
123
+ * Handles ustar, plus the pax (`x`) and GNU (`L`) long-name records Helm's
124
+ * Go archiver may write.
125
+ */
126
+ export function readTarGz(tgz: Buffer): Map<string, Buffer> {
127
+ const tar = gunzipSync(tgz);
128
+ const files = new Map<string, Buffer>();
129
+ let offset = 0;
130
+ let longName: string | undefined;
131
+
132
+ const field = (start: number, len: number): string => {
133
+ const raw = tar.subarray(offset + start, offset + start + len);
134
+ const nul = raw.indexOf(0);
135
+ return raw.subarray(0, nul === -1 ? raw.length : nul).toString("utf8");
136
+ };
137
+
138
+ while (offset + 512 <= tar.length) {
139
+ // Two zero blocks end the archive; one is enough to stop reading.
140
+ if (tar.subarray(offset, offset + 512).every((b) => b === 0)) break;
141
+
142
+ const size = parseInt(field(124, 12).trim() || "0", 8);
143
+ const type = field(156, 1) || "0";
144
+ const prefix = field(345, 155);
145
+ const name = longName ?? (prefix ? `${prefix}/${field(0, 100)}` : field(0, 100));
146
+ const data = tar.subarray(offset + 512, offset + 512 + size);
147
+ longName = undefined;
148
+
149
+ if (type === "x") {
150
+ const path = /(?:^|\n)\d+ path=([^\n]*)\n/.exec(data.toString("utf8"));
151
+ if (path) longName = path[1];
152
+ } else if (type === "L") {
153
+ longName = data.toString("utf8").replace(/\0+$/, "");
154
+ } else if (type === "0" || type === "\0") {
155
+ files.set(name, Buffer.from(data));
156
+ }
157
+ offset += 512 + Math.ceil(size / 512) * 512;
158
+ }
159
+ return files;
160
+ }
161
+
162
+ /**
163
+ * The YAML files directly inside `<chart dir>/<subdir>/`, sorted by path so
164
+ * the concatenation does not depend on archive order.
165
+ */
166
+ export function chartYamlFiles(files: Map<string, Buffer>, subdir: string): Array<[string, string]> {
167
+ const dir = subdir.replace(/^\/+|\/+$/g, "");
168
+ return [...files.entries()]
169
+ .filter(([path]) => {
170
+ // Strip the single top-level chart directory, whatever it is named.
171
+ const rest = path.split("/").slice(1).join("/");
172
+ if (!rest.startsWith(`${dir}/`)) return false;
173
+ const leaf = rest.slice(dir.length + 1);
174
+ return !leaf.includes("/") && (leaf.endsWith(".yaml") || leaf.endsWith(".yml"));
175
+ })
176
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
177
+ .map(([path, buf]) => [path, buf.toString("utf8")]);
178
+ }
package/src/crd/types.ts CHANGED
@@ -28,6 +28,11 @@ export interface CRDSource {
28
28
  chart?: string;
29
29
  /** Chart version for type="helm". Required: an unpinned source is not auditable. */
30
30
  version?: string;
31
+ /**
32
+ * Digest of an `oci://` chart's content layer ("sha256:..."). Optional; when
33
+ * set, a re-pushed tag fails generation instead of changing its output.
34
+ */
35
+ digest?: string;
31
36
  /**
32
37
  * Directory inside the chart holding the CRDs. Defaults to Helm's own
33
38
  * convention, "crds". Charts that instead template their CRDs need this