feat: add .kuberrc.ts configuration
This commit is contained in:
+21
-3
@@ -1,11 +1,14 @@
|
||||
import { defineCommand } from "citty";
|
||||
import { resolve } from "node:path";
|
||||
import { YAML } from "bun";
|
||||
import type { KuberResource } from "../types";
|
||||
import { sortResources } from "../lib/apply";
|
||||
import { ctx } from "../lib/context";
|
||||
import { renderResources } from "../lib/render";
|
||||
|
||||
function toDocumentYaml(resources: Parameters<typeof sortResources>[0]): string {
|
||||
function toDocumentYaml(
|
||||
resources: Parameters<typeof sortResources>[0],
|
||||
): string {
|
||||
return `${sortResources(resources)
|
||||
.map((resource) => YAML.stringify(resource, null, 2).trimEnd())
|
||||
.join("\n---\n")}\n`;
|
||||
@@ -24,8 +27,23 @@ export const exportCommand = defineCommand({
|
||||
},
|
||||
},
|
||||
async run({ args }) {
|
||||
const { project, compose, cwd } = ctx();
|
||||
const resources = await renderResources(project, await compose(), cwd);
|
||||
const {
|
||||
project,
|
||||
compose,
|
||||
cwd,
|
||||
config,
|
||||
hookContext: getHookContext,
|
||||
} = ctx();
|
||||
const resources = await renderResources(
|
||||
project,
|
||||
await compose(),
|
||||
cwd,
|
||||
config.registry,
|
||||
);
|
||||
await config.postRender?.(
|
||||
resources as KuberResource[],
|
||||
await getHookContext(),
|
||||
);
|
||||
const outputPath = resolve(cwd, args.output || `${project}.yaml`);
|
||||
|
||||
await Bun.write(outputPath, toDocumentYaml(resources));
|
||||
|
||||
+7
-1
@@ -14,9 +14,15 @@ import { up } from "./up";
|
||||
export const main = defineCommand({
|
||||
meta: {
|
||||
name: "kuber",
|
||||
version: "1.0.0",
|
||||
version: "1.1.0",
|
||||
description: "Docker Compose -> K8s translation layer",
|
||||
},
|
||||
args: {
|
||||
config: {
|
||||
type: "string",
|
||||
description: "Path to a kuber configuration file",
|
||||
},
|
||||
},
|
||||
subCommands: {
|
||||
db,
|
||||
down,
|
||||
|
||||
+34
-7
@@ -1,6 +1,7 @@
|
||||
import { defineCommand } from "citty";
|
||||
import { Listr } from "listr2";
|
||||
import type { ComposeSpecification } from "../schema/docker.d";
|
||||
import type { KuberResource } from "../types";
|
||||
import { ctx } from "../lib/context";
|
||||
import {
|
||||
assertManagedNamespace,
|
||||
@@ -45,7 +46,7 @@ export function getDeploymentNames(resources: KubernetesResource[]): string[] {
|
||||
}
|
||||
|
||||
export async function runUp(build: boolean) {
|
||||
const { project, compose, cwd } = ctx();
|
||||
const { project, compose, cwd, config, hookContext: getHookContext } = ctx();
|
||||
await assertManagedNamespace(project);
|
||||
|
||||
const taskCtx = await new Listr<UpContext>(
|
||||
@@ -54,6 +55,9 @@ export async function runUp(build: boolean) {
|
||||
title: "Read compose",
|
||||
task: async (taskCtx, task) => {
|
||||
taskCtx.compose = await compose();
|
||||
if (build) {
|
||||
await config.preBuild?.(taskCtx.compose, await getHookContext());
|
||||
}
|
||||
task.output = `${Object.keys(taskCtx.compose.services ?? {}).length} services`;
|
||||
},
|
||||
},
|
||||
@@ -69,13 +73,20 @@ export async function runUp(build: boolean) {
|
||||
(service) => service.build,
|
||||
),
|
||||
task: async (taskCtx, task) => {
|
||||
const result = await buildServices(project, taskCtx.compose!, cwd, {
|
||||
progress: (message) => {
|
||||
task.output = message;
|
||||
const result = await buildServices(
|
||||
project,
|
||||
taskCtx.compose!,
|
||||
cwd,
|
||||
{
|
||||
progress: (message) => {
|
||||
task.output = message;
|
||||
},
|
||||
stream: task.stdout(),
|
||||
},
|
||||
stream: task.stdout(),
|
||||
});
|
||||
config,
|
||||
);
|
||||
taskCtx.changedDeployments = result.changed;
|
||||
await config.postBuild?.(result, await getHookContext());
|
||||
task.output = `Built ${result.built.length} image${result.built.length === 1 ? "" : "s"}, ${result.changed.length} changed`;
|
||||
},
|
||||
},
|
||||
@@ -110,6 +121,11 @@ export async function runUp(build: boolean) {
|
||||
taskCtx.compose!,
|
||||
cwd,
|
||||
taskCtx.serviceEnv,
|
||||
config.registry,
|
||||
);
|
||||
await config.postRender?.(
|
||||
taskCtx.resources as KuberResource[],
|
||||
await getHookContext(),
|
||||
);
|
||||
task.output = `${taskCtx.resources.length} resources`;
|
||||
},
|
||||
@@ -139,6 +155,16 @@ export async function runUp(build: boolean) {
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: "Run post-apply hook",
|
||||
enabled: () => Boolean(config.postApply),
|
||||
task: async (taskCtx) => {
|
||||
await config.postApply?.(
|
||||
taskCtx.resources! as KuberResource[],
|
||||
await getHookContext(),
|
||||
);
|
||||
},
|
||||
},
|
||||
],
|
||||
{ rendererOptions: { collapseErrors: false } },
|
||||
).run();
|
||||
@@ -172,7 +198,8 @@ export async function runUp(build: boolean) {
|
||||
return task.newListr(
|
||||
deployments.map((name) => ({
|
||||
title: `Deployment ${name}`,
|
||||
task: () => waitForDeploymentRollout(name),
|
||||
task: () =>
|
||||
waitForDeploymentRollout(name, config.rolloutTimeoutMs),
|
||||
})),
|
||||
{ concurrent: false, exitOnError: true },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user