Skip to content

Commit a5e6a28

Browse files
committed
refactor(generators)!: rename overloaded crds arg across CRD accessors
`crds` meant two different things: a list of CRD YAML files (src-based fromCRD/fromCRDModule/crdObjects) and a kind filter (chart-based fromChartCRD/fromChartCRDModule/crdObjectsFromChart). Split into two self-describing names: - src-based: crds -> crdFiles - chart-based: crds -> kindFilter (same role as fromCRD's kindFilter) Every function keeps `crds` as a deprecated alias (new arg wins; falling back to `crds` emits a lib.warn pointing at the replacement) via a shared `renamedArg` helper, so existing callers keep working. The crd2jsonschema JSON contract is unchanged (still keyed `crds`). Tests cover both new names and the deprecated alias (9/9).
1 parent 613ef22 commit a5e6a28

2 files changed

Lines changed: 132 additions & 33 deletions

File tree

pkgs/generators/default.nix

Lines changed: 107 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,47 @@ let
184184
# pre-process the CRD with a crude python script to flatten it before running
185185
# the generator. See: crd2jsonschema.py
186186
#
187+
# Resolve a renamed argument: prefer the new name, fall back to the
188+
# deprecated `crds` alias (emitting a warning that points at the new name),
189+
# else the supplied default. `default` is only forced when neither is given,
190+
# so passing a `throw` makes the new argument effectively required.
191+
renamedArg =
192+
{
193+
fn,
194+
old ? "crds",
195+
new,
196+
newVal,
197+
oldVal,
198+
default,
199+
}:
200+
if newVal != null then
201+
newVal
202+
else if oldVal != null then
203+
lib.warn "${fn}: argument `${old}` is deprecated, use `${new}` instead" oldVal
204+
else
205+
default;
206+
187207
# This Python parse is the one unavoidable IFD; both the file generator
188208
# (`fromCRD`) and the native module generator (`fromCRDModule`) share it.
189209
crdSchema =
190210
{
191211
name,
192212
src,
193-
crds,
213+
crdFiles,
194214
namePrefix ? "",
195215
attrNameOverrides ? { },
196216
# Optional list of CRD `kind` names to generate. When empty (the
197-
# default) every CustomResourceDefinition found in `crds` is generated.
198-
# Useful when `crds` points at a multi-document stream (e.g. raw
199-
# `helm template` output) containing more kinds than you want.
217+
# default) every CustomResourceDefinition found in `crdFiles` is
218+
# generated. Useful when `crdFiles` points at a multi-document stream
219+
# (e.g. raw `helm template` output) containing more kinds than you want.
200220
kindFilter ? [ ],
201221
}:
202222
let
203223
options = pkgs.writeText "${name}-crd2jsonschema-options.json" (
204224
builtins.toJSON {
225+
# crd2jsonschema.py reads this under the JSON key `crds`.
226+
crds = crdFiles;
205227
inherit
206-
crds
207228
namePrefix
208229
attrNameOverrides
209230
kindFilter
@@ -236,7 +257,10 @@ let
236257
{
237258
name,
238259
src,
239-
crds,
260+
# List of CRD YAML files (relative to `src`) to generate types from.
261+
crdFiles ? null,
262+
# Deprecated alias for `crdFiles`.
263+
crds ? null,
240264
namePrefix ? "",
241265
attrNameOverrides ? { },
242266
skipCoerceToList ? { },
@@ -254,11 +278,17 @@ let
254278
inherit
255279
name
256280
src
257-
crds
258281
namePrefix
259282
attrNameOverrides
260283
kindFilter
261284
;
285+
crdFiles = renamedArg {
286+
fn = "fromCRD";
287+
new = "crdFiles";
288+
newVal = crdFiles;
289+
oldVal = crds;
290+
default = throw "fromCRD: `crdFiles` is required";
291+
};
262292
};
263293
};
264294

@@ -272,7 +302,10 @@ let
272302
{
273303
name,
274304
src,
275-
crds,
305+
# List of CRD YAML files (relative to `src`) to generate types from.
306+
crdFiles ? null,
307+
# Deprecated alias for `crdFiles`.
308+
crds ? null,
276309
namePrefix ? "",
277310
attrNameOverrides ? { },
278311
skipCoerceToList ? { },
@@ -291,30 +324,47 @@ let
291324
inherit
292325
name
293326
src
294-
crds
295327
namePrefix
296328
attrNameOverrides
297329
kindFilter
298330
;
331+
crdFiles = renamedArg {
332+
fn = "fromCRDModule";
333+
new = "crdFiles";
334+
newVal = crdFiles;
335+
oldVal = crds;
336+
default = throw "fromCRDModule: `crdFiles` is required";
337+
};
299338
};
300339
};
301340

302341
# Extract the raw CustomResourceDefinition objects from a set of CRD YAML
303-
# files. The objects counterpart to `fromCRD`: same `src`/`crds` inputs, but
304-
# returns the CRD manifests as values (e.g. to apply them to a cluster)
342+
# files. The objects counterpart to `fromCRD`: same `src`/`crdFiles` inputs,
343+
# but returns the CRD manifests as values (e.g. to apply them to a cluster)
305344
# instead of generating resource option modules. Deployment-agnostic — what
306345
# you do with the objects is up to you.
307346
#
308347
# `kindFilter`, when non-empty, keeps only CRDs whose `spec.names.kind` is in
309-
# the list (mirrors `fromCRD`'s `kindFilter` and `fromChartCRD`'s `crds`).
348+
# the list (mirrors `fromCRD`'s and `fromChartCRD`'s `kindFilter`).
310349
crdObjects =
311350
{
312351
src,
313-
crds,
352+
# List of CRD YAML files (relative to `src`) to read.
353+
crdFiles ? null,
354+
# Deprecated alias for `crdFiles`.
355+
crds ? null,
314356
kindFilter ? [ ],
315357
}:
316358
let
317-
objects = lib.concatMap (f: klib.fromYAML (builtins.readFile "${src}/${f}")) crds;
359+
files = renamedArg {
360+
fn = "crdObjects";
361+
new = "crdFiles";
362+
newVal = crdFiles;
363+
oldVal = crds;
364+
default = throw "crdObjects: `crdFiles` is required";
365+
};
366+
367+
objects = lib.concatMap (f: klib.fromYAML (builtins.readFile "${src}/${f}")) files;
318368

319369
isWanted =
320370
obj:
@@ -331,7 +381,10 @@ let
331381
chartAttrs ? { },
332382
chart ? null,
333383
values ? { },
334-
crds ? [ ],
384+
# Optional list of CRD `kind` names to keep. Empty/unset = every CRD.
385+
kindFilter ? null,
386+
# Deprecated alias for `kindFilter`.
387+
crds ? null,
335388
namePrefix ? "",
336389
attrNameOverrides ? { },
337390
skipCoerceToList ? { },
@@ -342,6 +395,14 @@ let
342395
kubeVersion ? "v${pkgs.kubernetes.version}",
343396
}:
344397
let
398+
kindFilter' = renamedArg {
399+
fn = "fromChartCRD";
400+
new = "kindFilter";
401+
newVal = kindFilter;
402+
oldVal = crds;
403+
default = [ ];
404+
};
405+
345406
_chart = if chart != null then chart else klib.downloadHelmChart chartAttrs;
346407

347408
objects = klib.fromHelm {
@@ -359,7 +420,7 @@ let
359420
obj:
360421
obj ? kind
361422
&& obj.kind == "CustomResourceDefinition"
362-
&& (crds == [ ] || (lib.any (x: obj.spec.names.kind == x) crds));
423+
&& (kindFilter' == [ ] || (lib.any (x: obj.spec.names.kind == x) kindFilter'));
363424

364425
filtered = lib.filter isWanted objects;
365426

@@ -383,7 +444,7 @@ let
383444
skipCoerceToList
384445
;
385446

386-
crds = [
447+
crdFiles = [
387448
"crds.yaml"
388449
];
389450
};
@@ -427,15 +488,18 @@ let
427488
'';
428489

429490
# Chart counterpart to `fromCRDModule`: template a chart's CRDs and return a
430-
# module value (resource type options). `crds`, when non-empty, narrows the
431-
# generated types to those CRD kinds (mirrors `fromChartCRD`).
491+
# module value (resource type options). `kindFilter`, when non-empty, narrows
492+
# the generated types to those CRD kinds (mirrors `fromChartCRD`).
432493
fromChartCRDModule =
433494
{
434495
name,
435496
chart ? null,
436497
chartAttrs ? { },
437498
values ? { },
438-
crds ? [ ],
499+
# Optional list of CRD `kind` names to keep. Empty/unset = every CRD.
500+
kindFilter ? null,
501+
# Deprecated alias for `kindFilter`.
502+
crds ? null,
439503
extraOpts ? [ ],
440504
kubeVersion ? "v${pkgs.kubernetes.version}",
441505
namePrefix ? "",
@@ -459,19 +523,29 @@ let
459523
kubeVersion
460524
;
461525
};
462-
crds = [ "crds.yaml" ];
463-
kindFilter = crds;
526+
crdFiles = [ "crds.yaml" ];
527+
kindFilter = renamedArg {
528+
fn = "fromChartCRDModule";
529+
new = "kindFilter";
530+
newVal = kindFilter;
531+
oldVal = crds;
532+
default = [ ];
533+
};
464534
};
465535

466536
# Chart counterpart to `crdObjects`: template a chart's CRDs and return the
467-
# raw CustomResourceDefinition manifests as values. `crds` empty = every CRD.
537+
# raw CustomResourceDefinition manifests as values. `kindFilter` empty = every
538+
# CRD.
468539
crdObjectsFromChart =
469540
{
470541
name,
471542
chart ? null,
472543
chartAttrs ? { },
473544
values ? { },
474-
crds ? [ ],
545+
# Optional list of CRD `kind` names to keep. Empty/unset = every CRD.
546+
kindFilter ? null,
547+
# Deprecated alias for `kindFilter`.
548+
crds ? null,
475549
extraOpts ? [ ],
476550
kubeVersion ? "v${pkgs.kubernetes.version}",
477551
}:
@@ -486,8 +560,14 @@ let
486560
kubeVersion
487561
;
488562
};
489-
crds = [ "crds.yaml" ];
490-
kindFilter = crds;
563+
crdFiles = [ "crds.yaml" ];
564+
kindFilter = renamedArg {
565+
fn = "crdObjectsFromChart";
566+
new = "kindFilter";
567+
newVal = kindFilter;
568+
oldVal = crds;
569+
default = [ ];
570+
};
491571
};
492572
in
493573
{
@@ -509,7 +589,7 @@ in
509589
rev = "v3.0.0";
510590
hash = "sha256-g401mpNEhCNe8H6lk2HToAEZlZa16Py8ozK2z5/UozA=";
511591
};
512-
crds = [
592+
crdFiles = [
513593
"manifests/crds/application-crd.yaml"
514594
"manifests/crds/applicationset-crd.yaml"
515595
"manifests/crds/appproject-crd.yaml"

pkgs/generators/equiv-test.nix

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,23 @@ let
155155
generators.fromCRD {
156156
name = "foobar";
157157
inherit src skipCoerceToList;
158-
crds = [ "foobar.yaml" ];
158+
crdFiles = [ "foobar.yaml" ];
159159
}
160160
);
161161
nativeMod = generators.fromCRDModule {
162162
name = "foobar";
163163
inherit src skipCoerceToList;
164-
crds = [ "foobar.yaml" ];
164+
crdFiles = [ "foobar.yaml" ];
165165
};
166166
chartMod = generators.fromChartCRDModule {
167167
name = "foobar";
168168
inherit chart skipCoerceToList;
169-
crds = [ "FooBar" ];
169+
kindFilter = [ "FooBar" ];
170170
};
171171

172172
srcObjs = generators.crdObjects {
173173
inherit src;
174-
crds = [ "foobar.yaml" ];
174+
crdFiles = [ "foobar.yaml" ];
175175
};
176176
chartObjs = generators.crdObjectsFromChart {
177177
name = "foobar";
@@ -236,18 +236,37 @@ let
236236
lib.length (
237237
generators.crdObjects {
238238
inherit src;
239-
crds = [ "foobar.yaml" ];
239+
crdFiles = [ "foobar.yaml" ];
240240
kindFilter = [ "FooBar" ];
241241
}
242242
) == 1;
243243

244244
"crdObjects kindFilter miss" =
245245
generators.crdObjects {
246246
inherit src;
247-
crds = [ "foobar.yaml" ];
247+
crdFiles = [ "foobar.yaml" ];
248248
kindFilter = [ "Nope" ];
249249
} == [ ];
250250

251+
# Deprecated `crds` alias still resolves (src-based → crdFiles).
252+
"crdObjects deprecated `crds` alias == `crdFiles`" =
253+
generators.crdObjects {
254+
inherit src;
255+
crds = [ "foobar.yaml" ];
256+
} == srcObjs;
257+
258+
# Deprecated `crds` alias still resolves (chart-based → kindFilter).
259+
"crdObjectsFromChart deprecated `crds` alias == `kindFilter`" =
260+
generators.crdObjectsFromChart {
261+
name = "foobar";
262+
inherit chart;
263+
crds = [ "FooBar" ];
264+
} == generators.crdObjectsFromChart {
265+
name = "foobar";
266+
inherit chart;
267+
kindFilter = [ "FooBar" ];
268+
};
269+
251270
"crdObjectsFromChart == crdObjects" = chartObjs == srcObjs;
252271

253272
"crdObjectsFromChart honors kubeVersion" =

0 commit comments

Comments
 (0)