Skip to content

Commit

Permalink
Support duplicate resource detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nktpro committed Mar 8, 2024
1 parent 9907a05 commit 8444ab8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ install() {
local OUTPUT=${2:-$(mktemp -d)}

"$0" build "${VERSION}" "${OUTPUT}"
deno install --unstable -A -f "${OUTPUT}/helmet.js"
deno install -A -f "${OUTPUT}/helmet.js"
}

update_cache() {
Expand All @@ -63,7 +63,7 @@ update_lock() {
}

run() {
deno run --lock ./deno.lock --cached-only -A --check --unstable "${ENTRY_FILE}" "$@"
deno run --lock ./deno.lock --cached-only -A --check "${ENTRY_FILE}" "$@"
}

"$@"
5 changes: 5 additions & 0 deletions examples/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export default defineBundle({
name,
},
}),
createK8sNamespace({
metadata: {
name,
},
}),
],
crds: [],
}),
Expand Down
48 changes: 30 additions & 18 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
npmlock2nix = (import npmlock2nix { inherit pkgs; }).v2;
nodejs = pkgs.nodejs_20;
};
deno = hotPotPkgs.deno;
deno = hotPotPkgs.deno_1_41_x;
runtimeInputs = builtins.attrValues
{
inherit json2ts deno;
Expand Down
41 changes: 32 additions & 9 deletions src/actions/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,43 @@ export async function compile(
);
}

const duplicateDetectionMap = chartInstances.reduce((map, { name }) => {
const chartInstanceDuplicateDetectionMap = chartInstances.reduce((map, { name }) => {
const count = map.get(name) ?? 0;
map.set(name, count + 1);
return map;
}, new Map<string, number>());

Array
.from(duplicateDetectionMap.entries())
.filter(([_, count]) => count > 1)
.forEach(([name, count]) => {
throw new Error(
`There are ${count} instances with the same name of '${name}'`,
);
});
const resourceDuplicateDetectionMap = chartInstances.flatMap(({ crds, resources }) => [...crds, ...resources]).reduce(
(map, resource) => {
const kind = `${resource.kind}/${resource.apiVersion}`;
let byNameMap = map.get(kind);

if (!byNameMap) {
byNameMap = new Map<string, number>();
map.set(kind, byNameMap);
}

const name = resource.metadata.name;
const count = byNameMap.get(name) ?? 0;
byNameMap.set(name, count + 1);
return map;
},
new Map<string, Map<string, number>>(),
);

for (const [name, count] of chartInstanceDuplicateDetectionMap) {
if (count > 1) {
throw new Error(`There are ${count} instances with the same name of '${name}'`);
}
}

for (const [kind, group] of resourceDuplicateDetectionMap) {
for (const [name, count] of group) {
if (count > 1) {
throw new Error(`There are ${count} resources with the same name of '${name}' and kind of '${kind}'`);
}
}
}

console.error("Compiling", source, "to", destination);

Expand Down
2 changes: 1 addition & 1 deletion src/helmet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const program = new CliProgram()
try {
await program.run(Deno.args);
} catch (e) {
console.error(bold(red("[Error]")), JSON.stringify(e, null, 2));
console.error(bold(red("[Error]")), e.message);

if (Deno.env.get("HELMET_ENABLE_STACKTRACE") !== "0") {
throw e;
Expand Down

0 comments on commit 8444ab8

Please sign in to comment.