Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eslint max depth warnings with passing E2E tests #1366

Merged
merged 11 commits into from
Nov 4, 2024
239 changes: 120 additions & 119 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
["admin", "scoped"],
),
)
.action(async opts => {

Check warning on line 73 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has too many statements (54). Maximum allowed is 20

Check warning on line 73 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has a complexity of 18. Maximum allowed is 10
// assign custom output directory if provided
if (opts.outputDir) {
outputDir = opts.outputDir;
Expand All @@ -81,115 +81,119 @@
}

// 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 @@ -243,7 +247,7 @@
};
}

export async function buildModule(reloader?: Reloader, entryPoint = peprTS, embed = true) {

Check warning on line 250 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async function 'buildModule' has too many statements (35). Maximum allowed is 20
try {
const { cfg, modulePath, path, uuid } = await loadModule(entryPoint);

Expand Down Expand Up @@ -332,41 +336,38 @@
} 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 @@ -23,7 +23,7 @@
.option("--docker-email <email>", "Email for Docker registry")
.option("--docker-password <password>", "Password for Docker registry")
.option("--force", "Force deploy the module, override manager field")
.action(async opts => {

Check warning on line 26 in src/cli/deploy.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has too many statements (27). Maximum allowed is 20

Check warning on line 26 in src/cli/deploy.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has a complexity of 17. Maximum allowed is 10
let imagePullSecret: ImagePullSecret | undefined;

if (
Expand Down Expand Up @@ -72,34 +72,37 @@
}

// 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 @@ -28,7 +28,7 @@
* @param validateOnly
* @returns success
*/
export async function peprFormat(validateOnly: boolean) {

Check warning on line 31 in src/cli/format.ts

View workflow job for this annotation

GitHub Actions / format

Async function 'peprFormat' has too many statements (23). Maximum allowed is 20
{
try {
const eslint = new ESLint();
Expand Down Expand Up @@ -63,13 +63,10 @@
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
Loading