Skip to content

Commit

Permalink
chore: knockout rest of complexity;
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Dec 10, 2024
1 parent eeff83f commit 6e64713
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 59 deletions.
9 changes: 0 additions & 9 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
"max-statements": ["warn", { "max": 20 }, { "ignoreTopLevelFunctions": true }],
"no-invalid-this": "warn"
},
"overrides": [
{
"files": ["*.ts"],
"excludedFiles": "*.test.ts",
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn"
}
}
],
"plugins": ["@typescript-eslint"],
"ignorePatterns": [
"src/templates",
Expand Down
30 changes: 30 additions & 0 deletions src/cli/build.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { CapabilityExport } from "../lib/types";
import { validateCapabilityNames } from "../lib/helpers";
import { peprFormat } from "./format";
import { BuildOptions, BuildResult, context, BuildContext } from "esbuild";
import { Assets } from "../lib/assets";
import { resolve } from "path";
import { promises as fs } from "fs";

export type Reloader = (opts: BuildResult<BuildOptions>) => void | Promise<void>;
/**
Expand Down Expand Up @@ -187,3 +190,30 @@ export async function watchForChanges(

return ctx;
}

export async function generateYamlAndWriteToDisk(obj: {
uuid: string;
imagePullSecret: string;
outputDir: string;
assets: Assets;
zarf: string;
}): Promise<void> {
const { uuid, imagePullSecret, outputDir, assets, zarf } = obj;
const yamlFile = `pepr-module-${uuid}.yaml`;
const chartPath = `${uuid}-chart`;
const yamlPath = resolve(outputDir, yamlFile);
const yaml = await assets.allYaml(imagePullSecret);
const zarfPath = resolve(outputDir, "zarf.yaml");

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

await assets.generateHelmChart(outputDir);
console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
}
62 changes: 14 additions & 48 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { execFileSync } from "child_process";
import { BuildOptions, BuildResult, analyzeMetafile, BuildContext } from "esbuild";
import { BuildOptions, BuildResult, analyzeMetafile } from "esbuild";
import { promises as fs } from "fs";
import { basename, dirname, extname, resolve } from "path";
import { Assets } from "../lib/assets";
import { dependencies, version } from "./init/templates";
import { RootCmd } from "./root";
import { Option } from "commander";
import { parseTimeout } from "../lib/helpers";
import { PackageJSON } from "../lib/module";

import {
checkFormat,
watchForChanges,
Expand All @@ -22,6 +22,7 @@ import {
handleCustomImageBuild,
checkIronBankImage,
validImagePullSecret,
generateYamlAndWriteToDisk,
} from "./build.helpers";

const peprTS = "pepr.ts";
Expand Down Expand Up @@ -112,15 +113,13 @@ export default function (program: RootCmd): void {
handleEmbedding(opts.embed, path);

// set the image version if provided
if (opts.version) {
cfg.pepr.peprVersion = opts.version;
}
opts.version ? (cfg.pepr.peprVersion = opts.version) : null;

// Generate a secret for the module
const assets = new Assets(
{
...cfg.pepr,
appVersion: version,
appVersion: cfg.version,
description: cfg.description,
// Can override the rbacMode with the CLI option
rbacMode: determineRbacMode(opts, cfg),
Expand All @@ -137,28 +136,14 @@ export default function (program: RootCmd): void {
// Ensure imagePullSecret is valid
validImagePullSecret(opts.withPullSecret);

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

handleValidCapabilityNames(assets.capabilities);

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);

await assets.generateHelmChart(outputDir);

console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
await generateYamlAndWriteToDisk({
uuid,
outputDir,
imagePullSecret: opts.withPullSecret,
zarf: opts.zarf,
assets,
});
}
});
}
Expand All @@ -172,14 +157,7 @@ externalLibs.push("pepr");
// Add the kubernetes client to the list of external libraries as it is pulled in by kubernetes-fluent-client
externalLibs.push("@kubernetes/client-node");

export async function loadModule(entryPoint = peprTS): Promise<{
cfg: PackageJSON;
entryPointPath: string;
modulePath: string;
name: string;
path: string;
uuid: string;
}> {
export async function loadModule(entryPoint = peprTS) {
// Resolve path to the module / files
const entryPointPath = resolve(".", entryPoint);
const modulePath = dirname(entryPointPath);
Expand Down Expand Up @@ -220,19 +198,7 @@ export async function loadModule(entryPoint = peprTS): Promise<{
};
}

export async function buildModule(
reloader?: Reloader,
entryPoint = peprTS,
embed = true,
): Promise<
| {
ctx: BuildContext<BuildOptions>;
path: string;
cfg: PackageJSON;
uuid: string;
}
| undefined
> {
export async function buildModule(reloader?: Reloader, entryPoint = peprTS, embed = true) {
try {
const { cfg, modulePath, path, uuid } = await loadModule(entryPoint);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface CustomLabels {
/** Global configuration for the Pepr runtime. */
export type ModuleConfig = {
/** The Pepr version this module uses */
peprVersion: string;
peprVersion?: string;
/** The user-defined version of the module */
appVersion?: string;
/** A unique identifier for this Pepr module. This is automatically generated by Pepr. */
Expand All @@ -59,7 +59,7 @@ export type ModuleConfig = {
/** The RBAC mode; if "scoped", generates scoped rules, otherwise uses wildcard rules. */
rbacMode?: string;
/** The included files to be added to the container */
includedFiles: string[];
includedFiles?: string[];
};
export type ExtendedPackageJSON = PackageJSON & {
name: string;
Expand Down

0 comments on commit 6e64713

Please sign in to comment.