Skip to content

Commit

Permalink
fix: eslint max depth warnings with passing E2E tests (#1366)
Browse files Browse the repository at this point in the history
This reverts commit 9dbf9ab.

## Description

#1249, but with attention paid to passing E2E tests that were missed in
#1255. Two instances of `max-depth` violations remain in the codebase
(`validate-processor.ts` & `mutate-processor.ts`), but that's a lot
better than where things were.

End to End Test:  
(See [Pepr Excellent
Examples](https://github.com/defenseunicorns/pepr-excellent-examples))

## Related Issue

Relates to #1249 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Other (security config, docs update, etc)

## Checklist before merging
- [x] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed

---------

Co-authored-by: Barrett <81570928+btlghrants@users.noreply.github.com>
  • Loading branch information
samayer12 and btlghrants authored Nov 4, 2024
1 parent cedad18 commit 73341c6
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 256 deletions.
239 changes: 120 additions & 119 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,115 +81,119 @@ export default function (program: RootCmd) {
}

// Build the module
const { cfg, path, uuid } = await buildModule(undefined, opts.entryPoint, opts.embed);

// Files to include in controller image for WASM support
const { includedFiles } = cfg.pepr;

let image: string = "";
const buildModuleResult = await buildModule(undefined, opts.entryPoint, opts.embed);
if (buildModuleResult?.cfg && buildModuleResult.path && buildModuleResult.uuid) {
const { cfg, path, uuid } = buildModuleResult;
// Files to include in controller image for WASM support
const { includedFiles } = cfg.pepr;

let image: string = "";

// Build Kubernetes manifests with custom image
if (opts.customImage) {
if (opts.registry) {
console.error(`Custom Image and registry cannot be used together.`);
process.exit(1);
}
image = opts.customImage;
}

// Build Kubernetes manifests with custom image
if (opts.customImage) {
if (opts.registry) {
console.error(`Custom Image and registry cannot be used together.`);
process.exit(1);
// Check if there is a custom timeout defined
if (opts.timeout !== undefined) {
cfg.pepr.webhookTimeout = opts.timeout;
}
image = opts.customImage;
}

// Check if there is a custom timeout defined
if (opts.timeout !== undefined) {
cfg.pepr.webhookTimeout = opts.timeout;
}
if (opts.registryInfo !== undefined) {
console.info(`Including ${includedFiles.length} files in controller image.`);

if (opts.registryInfo !== undefined) {
console.info(`Including ${includedFiles.length} files in controller image.`);
// for journey test to make sure the image is built
image = `${opts.registryInfo}/custom-pepr-controller:${cfg.pepr.peprVersion}`;

// for journey test to make sure the image is built
image = `${opts.registryInfo}/custom-pepr-controller:${cfg.pepr.peprVersion}`;
// only actually build/push if there are files to include
if (includedFiles.length > 0) {
await createDockerfile(cfg.pepr.peprVersion, cfg.description, includedFiles);
execSync(`docker build --tag ${image} -f Dockerfile.controller .`, {
stdio: "inherit",
});
execSync(`docker push ${image}`, { stdio: "inherit" });
}
}

// only actually build/push if there are files to include
if (includedFiles.length > 0) {
await createDockerfile(cfg.pepr.peprVersion, cfg.description, includedFiles);
execSync(`docker build --tag ${image} -f Dockerfile.controller .`, { stdio: "inherit" });
execSync(`docker push ${image}`, { stdio: "inherit" });
// If building without embedding, exit after building
if (!opts.embed) {
console.info(`✅ Module built successfully at ${path}`);
return;
}
}

// If building without embedding, exit after building
if (!opts.embed) {
console.info(`✅ Module built successfully at ${path}`);
return;
}
// set the image version if provided
if (opts.version) {
cfg.pepr.peprVersion = opts.version;
}

// set the image version if provided
if (opts.version) {
cfg.pepr.peprVersion = opts.version;
}
// Generate a secret for the module
const assets = new Assets(
{
...cfg.pepr,
appVersion: cfg.version,
description: cfg.description,
// Can override the rbacMode with the CLI option
rbacMode: determineRbacMode(opts, cfg),
},
path,
);

// Generate a secret for the module
const assets = new Assets(
{
...cfg.pepr,
appVersion: cfg.version,
description: cfg.description,
// Can override the rbacMode with the CLI option
rbacMode: determineRbacMode(opts, cfg),
},
path,
);
// If registry is set to Iron Bank, use Iron Bank image
if (opts?.registry === "Iron Bank") {
console.info(
`\n\tThis command assumes the latest release. Pepr's Iron Bank image release cycle is dictated by renovate and is typically released a few days after the GitHub release.\n\tAs an alternative you may consider custom --custom-image to target a specific image and version.`,
);
image = `registry1.dso.mil/ironbank/opensource/defenseunicorns/pepr/controller:v${cfg.pepr.peprVersion}`;
}

// If registry is set to Iron Bank, use Iron Bank image
if (opts?.registry === "Iron Bank") {
console.info(
`\n\tThis command assumes the latest release. Pepr's Iron Bank image release cycle is dictated by renovate and is typically released a few days after the GitHub release.\n\tAs an alternative you may consider custom --custom-image to target a specific image and version.`,
);
image = `registry1.dso.mil/ironbank/opensource/defenseunicorns/pepr/controller:v${cfg.pepr.peprVersion}`;
}
// if image is a custom image, use that instead of the default
if (image !== "") {
assets.image = image;
}

// if image is a custom image, use that instead of the default
if (image !== "") {
assets.image = image;
}
// Ensure imagePullSecret is valid
if (opts.withPullSecret) {
if (sanitizeResourceName(opts.withPullSecret) !== opts.withPullSecret) {
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
console.error(
"Invalid imagePullSecret. Please provide a valid name as defined in RFC 1123.",
);
process.exit(1);
}
}

// Ensure imagePullSecret is valid
if (opts.withPullSecret) {
if (sanitizeResourceName(opts.withPullSecret) !== opts.withPullSecret) {
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
console.error(
"Invalid imagePullSecret. Please provide a valid name as defined in RFC 1123.",
);
const yamlFile = `pepr-module-${uuid}.yaml`;
const chartPath = `${uuid}-chart`;
const yamlPath = resolve(outputDir, yamlFile);
const yaml = await assets.allYaml(opts.withPullSecret);

try {
// wait for capabilities to be loaded and test names
validateCapabilityNames(assets.capabilities);
} catch (e) {
console.error(`Error loading capability:`, e);
process.exit(1);
}
}

const yamlFile = `pepr-module-${uuid}.yaml`;
const chartPath = `${uuid}-chart`;
const yamlPath = resolve(outputDir, yamlFile);
const yaml = await assets.allYaml(opts.withPullSecret);

try {
// wait for capabilities to be loaded and test names
validateCapabilityNames(assets.capabilities);
} catch (e) {
console.error(`Error loading capability:`, e);
process.exit(1);
}

const zarfPath = resolve(outputDir, "zarf.yaml");
const zarfPath = resolve(outputDir, "zarf.yaml");

let zarf = "";
if (opts.zarf === "chart") {
zarf = assets.zarfYamlChart(chartPath);
} else {
zarf = assets.zarfYaml(yamlFile);
}
await fs.writeFile(yamlPath, yaml);
await fs.writeFile(zarfPath, zarf);
let zarf = "";
if (opts.zarf === "chart") {
zarf = assets.zarfYamlChart(chartPath);
} else {
zarf = assets.zarfYaml(yamlFile);
}
await fs.writeFile(yamlPath, yaml);
await fs.writeFile(zarfPath, zarf);

await assets.generateHelmChart(outputDir);
await assets.generateHelmChart(outputDir);

console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
}
});
}

Expand Down Expand Up @@ -332,41 +336,38 @@ export async function buildModule(reloader?: Reloader, entryPoint = peprTS, embe
} catch (e) {
console.error(`Error building module:`, e);

if (e.stdout) {
const out = e.stdout.toString() as string;
const err = e.stderr.toString();
if (!e.stdout) process.exit(1); // Exit with a non-zero exit code on any other error

console.log(out);
console.error(err);
const out = e.stdout.toString() as string;
const err = e.stderr.toString();

// Check for version conflicts
if (out.includes("Types have separate declarations of a private property '_name'.")) {
// Try to find the conflicting package
const pgkErrMatch = /error TS2322: .*? 'import\("\/.*?\/node_modules\/(.*?)\/node_modules/g;
out.matchAll(pgkErrMatch);
console.log(out);
console.error(err);

// Look for package conflict errors
const conflicts = [...out.matchAll(pgkErrMatch)];
// Check for version conflicts
if (out.includes("Types have separate declarations of a private property '_name'.")) {
// Try to find the conflicting package
const pgkErrMatch = /error TS2322: .*? 'import\("\/.*?\/node_modules\/(.*?)\/node_modules/g;
out.matchAll(pgkErrMatch);

// If the regex didn't match, leave a generic error
if (conflicts.length < 1) {
console.info(
`\n\tOne or more imported Pepr Capabilities seem to be using an incompatible version of Pepr.\n\tTry updating your Pepr Capabilities to their latest versions.`,
"Version Conflict",
);
}
// Look for package conflict errors
const conflicts = [...out.matchAll(pgkErrMatch)];

// Otherwise, loop through each conflicting package and print an error
conflicts.forEach(match => {
console.info(
`\n\tPackage '${match[1]}' seems to be incompatible with your current version of Pepr.\n\tTry updating to the latest version.`,
"Version Conflict",
);
});
// If the regex didn't match, leave a generic error
if (conflicts.length < 1) {
console.info(
`\n\tOne or more imported Pepr Capabilities seem to be using an incompatible version of Pepr.\n\tTry updating your Pepr Capabilities to their latest versions.`,
"Version Conflict",
);
}
}

// On any other error, exit with a non-zero exit code
process.exit(1);
// Otherwise, loop through each conflicting package and print an error
conflicts.forEach(match => {
console.info(
`\n\tPackage '${match[1]}' seems to be incompatible with your current version of Pepr.\n\tTry updating to the latest version.`,
"Version Conflict",
);
});
}
}
}
51 changes: 27 additions & 24 deletions src/cli/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,37 @@ export default function (program: RootCmd) {
}

// Build the module
const { cfg, path } = await buildModule();
const buildModuleResult = await buildModule();
if (buildModuleResult?.cfg && buildModuleResult?.path) {
const { cfg, path } = buildModuleResult;

// Generate a secret for the module
const webhook = new Assets(
{
...cfg.pepr,
description: cfg.description,
},
path,
);
// Generate a secret for the module
const webhook = new Assets(
{
...cfg.pepr,
description: cfg.description,
},
path,
);

if (opts.image) {
webhook.image = opts.image;
}
if (opts.image) {
webhook.image = opts.image;
}

// Identify conf'd webhookTimeout to give to deploy call
const timeout = cfg.pepr.webhookTimeout ? cfg.pepr.webhookTimeout : 10;
// Identify conf'd webhookTimeout to give to deploy call
const timeout = cfg.pepr.webhookTimeout ? cfg.pepr.webhookTimeout : 10;

try {
await webhook.deploy(opts.force, timeout);
// wait for capabilities to be loaded and test names
validateCapabilityNames(webhook.capabilities);
// Wait for the pepr-system resources to be fully up
await namespaceDeploymentsReady();
console.info(`✅ Module deployed successfully`);
} catch (e) {
console.error(`Error deploying module:`, e);
process.exit(1);
try {
await webhook.deploy(opts.force, timeout);
// wait for capabilities to be loaded and test names
validateCapabilityNames(webhook.capabilities);
// Wait for the pepr-system resources to be fully up
await namespaceDeploymentsReady();
console.info(`✅ Module deployed successfully`);
} catch (e) {
console.error(`Error deploying module:`, e);
process.exit(1);
}
}
});
}
9 changes: 3 additions & 6 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ export async function peprFormat(validateOnly: boolean) {
const formatted = await format(content, { filepath: filePath, ...cfg });

// If in validate-only mode, check if the file is formatted correctly
if (validateOnly) {
if (formatted !== content) {
hasFailure = true;
console.error(`File ${filePath} is not formatted correctly`);
}
if (validateOnly && formatted !== content) {
hasFailure = true;
console.error(`File ${filePath} is not formatted correctly`);
} else {
// Otherwise, write the formatted file
await fs.writeFile(filePath, formatted);
}
}
Expand Down
Loading

0 comments on commit 73341c6

Please sign in to comment.