Skip to content

Commit

Permalink
chore: validate build arguments that are used together add docs (#1678)
Browse files Browse the repository at this point in the history
## Description

Controls which build arguments are used together and adds docs

Lets hold off on this one to see if we can get the commits signed on PR
#1651 first. CC @tamirazrab

## Related Issue

Fixes #1662 
<!-- or -->
Relates to #

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] 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

---------

Signed-off-by: Case Wylie <[email protected]>
Co-authored-by: Barrett <[email protected]>
  • Loading branch information
cmwylie19 and btlghrants authored Jan 21, 2025
1 parent 0286f93 commit 50af73b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 77 deletions.
4 changes: 2 additions & 2 deletions docs/030_user-guide/010_pepr-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ Create a [zarf.yaml](https://zarf.dev) and K8s manifest for the current module.

- `-e, --entry-point [file]` - Specify the entry point file to build with. (default: "pepr.ts")
- `-n, --no-embed` - Disables embedding of deployment files into output module. Useful when creating library modules intended solely for reuse/distribution via NPM
- `-r, --registry-info [<registry>/<username>]` - Registry Info: Image registry and username. Note: You must be signed into the registry
- `-r, --registry-info [<registry>/<username>]` - Provide the image registry and username for building and pushing a custom WASM container. Requires authentication. Builds and pushes 'registry/username/custom-pepr-controller:<current-version>'.
- `-o, --output-dir [output directory]` - Define where to place build output
- `--timeout [timeout]` - How long the API server should wait for a webhook to respond before treating the call as a failure
- `--rbac-mode [admin|scoped]` - Rbac Mode: admin, scoped (default: admin) (choices: "admin", "scoped", default: "admin")
- `-i, --custom-image [custom-image]` - Custom Image: Use custom image for Admission and Watcher Deployments.
- `-i, --custom-image [custom-image]` - Specify a custom image (including version) for Admission and Watch Deployments. Example: 'docker.io/username/custom-pepr-controller:v1.0.0'
- `--registry [GitHub, Iron Bank]` - Container registry: Choose container registry for deployment manifests.
- `-v, --version <version>. Example: '0.27.3'` - The version of the Pepr image to use in the deployment manifests.
- `--withPullSecret <imagePullSecret>` - Image Pull Secret: Use image pull secret for controller Deployment.
Expand Down
18 changes: 0 additions & 18 deletions src/cli/build.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,6 @@ export function validImagePullSecret(imagePullSecretName: string): void {
}
}

/**
* Constraint to majke sure customImage and registry are not both used
* @param customImage
* @param registry
* @returns
*/
export function handleCustomImage(customImage: string, registry: string): string {
let defaultImage = "";
if (customImage) {
if (registry) {
console.error(`Custom Image and registry cannot be used together.`);
process.exit(1);
}
defaultImage = customImage;
}
return defaultImage;
}

/**
* Creates and pushes a custom image for WASM or any other included files
* @param includedFiles
Expand Down
45 changes: 0 additions & 45 deletions src/cli/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
handleCustomImageBuild,
checkIronBankImage,
validImagePullSecret,
handleCustomImage,
} from "./build.helpers";
import { createDirectoryIfNotExists } from "../lib/filesystemService";
import { expect, describe, it, jest, beforeEach } from "@jest/globals";
Expand Down Expand Up @@ -133,50 +132,6 @@ describe("validImagePullSecret", () => {
expect(mockExit).toHaveBeenCalled();
});
});
describe("handleCustomImage", () => {
const mockExit = jest.spyOn(process, "exit").mockImplementation(() => {
return undefined as never;
});

const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {});

beforeEach(() => {
jest.clearAllMocks();
});

it("should return the customImage if no registry is provided", () => {
const customImage = "custom-image";
const registry = "";

const result = handleCustomImage(customImage, registry);

expect(result).toBe(customImage);
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(mockExit).not.toHaveBeenCalled();
});

it("should return an empty string if neither customImage nor registry is provided", () => {
const customImage = "";
const registry = "";

const result = handleCustomImage(customImage, registry);

expect(result).toBe("");
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(mockExit).not.toHaveBeenCalled();
});

it("should call process.exit with 1 and log an error if both customImage and registry are provided", () => {
const customImage = "custom-image";
const registry = "registry";

handleCustomImage(customImage, registry);
expect(consoleErrorSpy).toHaveBeenCalledWith(
"Custom Image and registry cannot be used together.",
);
expect(mockExit).toHaveBeenCalledWith(1);
});
});

describe("handleCustomImageBuild", () => {
const mockedExecSync = jest.mocked(execSync);
Expand Down
32 changes: 20 additions & 12 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
handleEmbedding,
handleCustomOutputDir,
handleValidCapabilityNames,
handleCustomImage,
handleCustomImageBuild,
checkIronBankImage,
validImagePullSecret,
Expand Down Expand Up @@ -76,23 +75,30 @@ export default function (program: RootCmd): void {
"-n, --no-embed",
"Disables embedding of deployment files into output module. Useful when creating library modules intended solely for reuse/distribution via NPM.",
)
.option(
"-i, --custom-image <custom-image>",
"Custom Image: Use custom image for Admission and Watch Deployments.",
.addOption(
new Option(
"-i, --custom-image <custom-image>",
"Specify a custom image (including version) for Admission and Watch Deployments. Example: 'docker.io/username/custom-pepr-controller:v1.0.0'",
).conflicts(["version", "registryInfo", "registry"]),
)
.option(
"-r, --registry-info [<registry>/<username>]",
"Registry Info: Image registry and username. Note: You must be signed into the registry",
.addOption(
new Option(
"-r, --registry-info [<registry>/<username>]",
"Provide the image registry and username for building and pushing a custom WASM container. Requires authentication. Builds and pushes 'registry/username/custom-pepr-controller:<current-version>'.",
).conflicts(["customImage", "version", "registry"]),
)

.option("-o, --output-dir <output directory>", "Define where to place build output")
.option(
"--timeout <timeout>",
"How long the API server should wait for a webhook to respond before treating the call as a failure",
parseTimeout,
)
.option(
"-v, --version <version>. Example: '0.27.3'",
"The version of the Pepr image to use in the deployment manifests.",
.addOption(
new Option(
"-v, --version <version>",
"The version of the Pepr image to use in the deployment manifests. Example: '0.27.3'.",
).conflicts(["customImage", "registryInfo"]),
)
.option(
"--withPullSecret <imagePullSecret>",
Expand All @@ -104,7 +110,9 @@ export default function (program: RootCmd): void {
new Option(
"--registry <GitHub|Iron Bank>",
"Container registry: Choose container registry for deployment manifests. Can't be used with --custom-image.",
).choices(["GitHub", "Iron Bank"]),
)
.conflicts(["customImage", "registryInfo"])
.choices(["GitHub", "Iron Bank"]),
)

.addOption(
Expand All @@ -131,7 +139,7 @@ export default function (program: RootCmd): void {
// Files to include in controller image for WASM support
const { includedFiles } = cfg.pepr;

let image = handleCustomImage(opts.customImage, opts.registry);
let image = opts.customImage || "";

// Check if there is a custom timeout defined
if (opts.timeout !== undefined) {
Expand Down

0 comments on commit 50af73b

Please sign in to comment.