@nitra/cfr 0.1.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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/bin/cli.mjs +137 -0
  4. package/package.json +37 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nitra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @nitra/cfr
2
+
3
+ Kustomize's `resources:` field is an **explicit list**, not a glob. Add a
4
+ YAML manifest to a directory managed by a [Flux](https://fluxcd.io)
5
+ `Kustomization` without listing it in `resources:`, and
6
+ `kustomize-controller` silently skips it — no error, no warning, the
7
+ object just never reaches the cluster.
8
+
9
+ This CLI catches that drift before it ships: it compares every
10
+ `*.yaml`/`*.yml` file physically present in a directory against the
11
+ `resources:` list in its `kustomization.yaml`, in both directions.
12
+
13
+ ## Usage
14
+
15
+ ```sh
16
+ npx @nitra/cfr [dir-or-kustomization.yaml ...]
17
+ ```
18
+
19
+ No arguments checks `.`. Point it at one or more directories (or direct
20
+ paths to a `kustomization.yaml`/`kustomization.yml`):
21
+
22
+ ```sh
23
+ npx @nitra/cfr flux/clusters/production
24
+ ```
25
+
26
+ ```
27
+ ✗ flux/clusters/production/kustomization.yaml
28
+ on disk but missing from resources: (Flux will not apply them):
29
+ - new-app.yaml
30
+ ```
31
+
32
+ Exits `0` when every target is consistent, `1` otherwise — wire it into CI
33
+ on any path that touches a Kustomize directory with an explicit
34
+ `resources:` list:
35
+
36
+ ```yaml
37
+ # .github/workflows/cfr.yml
38
+ on:
39
+ push:
40
+ paths: ['flux/clusters/production/**']
41
+ pull_request:
42
+ jobs:
43
+ check:
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: actions/checkout@v6
47
+ - run: npx @nitra/cfr flux/clusters/production
48
+ ```
49
+
50
+ ## What it checks
51
+
52
+ For each target directory:
53
+
54
+ - every `*.yaml`/`*.yml` file in the directory (except the
55
+ `kustomization.yaml` itself) must appear in `resources:`
56
+ - every `resources:` entry that is a plain local filename (no `/`, no URL
57
+ scheme) must exist on disk
58
+
59
+ Entries containing `/` (subdirectories, components) or a URL scheme
60
+ (remote bases) are out of scope and skipped — this tool only guards
61
+ against the specific footgun of a loose file sitting next to
62
+ `kustomization.yaml` that nobody remembered to list.
63
+
64
+ ## Why this exists
65
+
66
+ A real incident: a PR added two manifests to a Flux cluster directory but
67
+ missed adding them to `resources:`. The PR merged clean, CI was green,
68
+ `git log` showed the files — and Flux applied nothing. No error surfaced
69
+ anywhere; the only symptom was the feature silently not existing in the
70
+ cluster. This tool turns that into a CI failure at PR time instead.
71
+
72
+ ## License
73
+
74
+ MIT
package/bin/cli.mjs ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env node
2
+ // Kustomize's `resources:` field is an explicit list, not a glob. Add a
3
+ // YAML file to a Kustomize directory without listing it there and
4
+ // kustomize-controller silently skips it — no error, no warning, the
5
+ // resource just never reaches the cluster. This CLI catches that drift
6
+ // before it ships.
7
+ import { readFileSync, readdirSync, statSync } from 'node:fs';
8
+ import { basename, dirname, join, resolve } from 'node:path';
9
+
10
+ const HELP = `cfr (@nitra/cfr) — verify kustomization.yaml resources: match the directory
11
+
12
+ Usage:
13
+ npx @nitra/cfr [dir-or-kustomization.yaml ...]
14
+
15
+ Each argument is either a directory containing a kustomization.yaml (or
16
+ kustomization.yml), or a direct path to one. Defaults to "." when no
17
+ argument is given.
18
+
19
+ For each target, compares:
20
+ - every *.yaml/*.yml file physically present in the directory
21
+ - every plain-filename entry under the top-level "resources:" list
22
+
23
+ and reports both directions: files on disk missing from resources: (Flux
24
+ will never apply them), and resources: entries with no matching file
25
+ (dead reference). Entries containing "/" or a URL scheme (subdirectories,
26
+ components, remote bases) are out of scope and skipped.
27
+
28
+ Exits 0 when every target is consistent, 1 otherwise.
29
+
30
+ Options:
31
+ -h, --help Show this help and exit.
32
+ `;
33
+
34
+ function findKustomization(target) {
35
+ const abs = resolve(target);
36
+ let stat;
37
+ try {
38
+ stat = statSync(abs);
39
+ } catch {
40
+ return { error: `${target}: no such file or directory` };
41
+ }
42
+ if (stat.isFile()) {
43
+ return { dir: dirname(abs), file: abs };
44
+ }
45
+ for (const name of ['kustomization.yaml', 'kustomization.yml']) {
46
+ const candidate = join(abs, name);
47
+ try {
48
+ if (statSync(candidate).isFile()) return { dir: abs, file: candidate };
49
+ } catch {
50
+ // try next
51
+ }
52
+ }
53
+ return { error: `${target}: no kustomization.yaml (or .yml) found` };
54
+ }
55
+
56
+ function extractResources(text) {
57
+ const lines = text.split('\n');
58
+ const entries = [];
59
+ let inBlock = false;
60
+ for (const line of lines) {
61
+ if (!inBlock) {
62
+ if (/^resources:\s*(#.*)?$/.test(line)) inBlock = true;
63
+ continue;
64
+ }
65
+ if (line.trim() === '') continue;
66
+ const item = line.match(/^\s+-\s*(.+?)\s*(#.*)?$/);
67
+ if (!item) break; // dedented past the list — block is over
68
+ let value = item[1].trim();
69
+ if (
70
+ (value.startsWith('"') && value.endsWith('"')) ||
71
+ (value.startsWith("'") && value.endsWith("'"))
72
+ ) {
73
+ value = value.slice(1, -1);
74
+ }
75
+ entries.push(value);
76
+ }
77
+ return entries;
78
+ }
79
+
80
+ function isLocalYamlFilename(entry) {
81
+ return /^[^/]+\.ya?ml$/i.test(entry) && !/^[a-z]+:\/\//i.test(entry);
82
+ }
83
+
84
+ function checkTarget(target) {
85
+ const found = findKustomization(target);
86
+ if (found.error) return { target, ok: false, error: found.error };
87
+
88
+ const { dir, file } = found;
89
+ const onDisk = new Set(
90
+ readdirSync(dir).filter((name) => /\.ya?ml$/i.test(name) && join(dir, name) !== file),
91
+ );
92
+
93
+ const listedAll = extractResources(readFileSync(file, 'utf8'));
94
+ const listedLocal = new Set(listedAll.filter(isLocalYamlFilename));
95
+
96
+ const missing = [...onDisk].filter((name) => !listedLocal.has(name)).sort();
97
+ const dangling = [...listedLocal].filter((name) => !onDisk.has(name)).sort();
98
+
99
+ return { target, ok: missing.length === 0 && dangling.length === 0, file, missing, dangling };
100
+ }
101
+
102
+ function main(argv) {
103
+ if (argv.includes('-h') || argv.includes('--help')) {
104
+ process.stdout.write(HELP);
105
+ return 0;
106
+ }
107
+
108
+ const targets = argv.length > 0 ? argv : ['.'];
109
+ let exitCode = 0;
110
+
111
+ for (const target of targets) {
112
+ const result = checkTarget(target);
113
+ if (result.error) {
114
+ console.error(`✗ ${result.target}: ${result.error}`);
115
+ exitCode = 1;
116
+ continue;
117
+ }
118
+ if (result.ok) {
119
+ console.log(`✓ ${result.file}`);
120
+ continue;
121
+ }
122
+ exitCode = 1;
123
+ console.error(`✗ ${result.file}`);
124
+ if (result.missing.length > 0) {
125
+ console.error(' on disk but missing from resources: (Flux will not apply them):');
126
+ for (const name of result.missing) console.error(` - ${name}`);
127
+ }
128
+ if (result.dangling.length > 0) {
129
+ console.error(' listed in resources: but missing on disk:');
130
+ for (const name of result.dangling) console.error(` - ${name}`);
131
+ }
132
+ }
133
+
134
+ return exitCode;
135
+ }
136
+
137
+ process.exit(main(process.argv.slice(2)));
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@nitra/cfr",
3
+ "version": "0.1.0",
4
+ "description": "Verify that every YAML file in a Kustomize directory is listed in kustomization.yaml's explicit resources: (and vice versa) — catches files Flux silently ignores.",
5
+ "keywords": [
6
+ "kustomize",
7
+ "flux",
8
+ "fluxcd",
9
+ "kubernetes",
10
+ "gitops",
11
+ "lint"
12
+ ],
13
+ "homepage": "https://github.com/nitra/cfr#readme",
14
+ "bugs": "https://github.com/nitra/cfr/issues",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/nitra/cfr.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Nitra <info@nitralabs.com> (https://nitra.ai)",
21
+ "type": "module",
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "bin": {
26
+ "cfr": "./bin/cli.mjs"
27
+ },
28
+ "files": [
29
+ "bin"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "test": "node --test test/cli.test.mjs"
36
+ }
37
+ }