Files
kuber/lib/k8s.ts
T
dmgnr 8f331d4cc7 feat: add commands for managing deployments and resources
- Implemented `down` command to delete managed resources, with an option to delete ingress and PVCs.
- Added `exec` command to execute commands inside deployment pods.
- Created `logs` command to show deployment logs with an option to follow the output.
- Introduced `start` command to start deployments without rebuilding images.
- Developed `stop` command to scale managed deployments to zero.

refactor: enhance resource management and Kubernetes interactions

- Refactored resource handling in `lib/apply.ts` to support sorting and applying resources.
- Improved logging functionality with a new logger in `lib/logger.ts`.
- Enhanced build process in `lib/build.ts` to support remote builds and syncing with Git repositories.
- Added functions to manage deployments and pods in `lib/shared.ts`.

chore: update dependencies and TypeScript configuration

- Updated package.json to include `listr2` for task management.
- Modified tsconfig.json to exclude build artifacts and fixtures.
2026-08-09 00:18:14 +07:00

94 lines
2.9 KiB
TypeScript

import {
AppsV1Api,
CoreV1Api,
Exec,
KubeConfig,
KubernetesObjectApi,
Log,
} from "@kubernetes/client-node";
import { ResponseContext } from "@kubernetes/client-node/dist/gen/http/http.js";
import { from } from "@kubernetes/client-node/dist/gen/rxjsStub.js";
import http from "node:http";
import https from "node:https";
const kc = new KubeConfig();
kc.loadFromDefault();
const bunHttpLibrary = {
send(request: {
getUrl(): string;
getHttpMethod(): { toString(): string };
getBody(): unknown;
getHeaders(): Record<string, string>;
getSignal(): AbortSignal | undefined;
getAgent(): http.Agent | https.Agent | undefined;
}) {
const result = new Promise<ResponseContext>((resolve, reject) => {
const url = new URL(request.getUrl());
const transport = url.protocol === "http:" ? http : https;
const req = transport.request(
url,
{
method: request.getHttpMethod().toString(),
headers: request.getHeaders(),
agent: request.getAgent(),
},
(response) => {
const chunks: Buffer[] = [];
response.on("data", (chunk) => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
response.on("end", () => {
const buffer = Buffer.concat(chunks);
const headers: Record<string, string> = {};
for (const [key, value] of Object.entries(response.headers)) {
if (Array.isArray(value)) headers[key] = value.join(", ");
else if (value !== undefined) headers[key] = String(value);
}
resolve(
new ResponseContext(response.statusCode ?? 0, headers, {
text: async () => buffer.toString("utf8"),
binary: async () => buffer,
}),
);
});
},
);
request.getSignal()?.addEventListener("abort", () => {
req.destroy(new Error("Request aborted"));
});
req.on("error", reject);
const body = request.getBody();
if (body !== undefined && body !== null) req.write(body as string | Uint8Array);
req.end();
});
return from(result);
},
};
const makeApiClient = kc.makeApiClient.bind(kc);
kc.makeApiClient = ((apiClientType) => {
const client = makeApiClient(apiClientType) as unknown as {
api?: { configuration?: { httpApi?: typeof bunHttpLibrary } };
configuration?: { httpApi?: typeof bunHttpLibrary };
};
if (client.api?.configuration) client.api.configuration.httpApi = bunHttpLibrary;
if (client.configuration) client.configuration.httpApi = bunHttpLibrary;
return client;
}) as typeof kc.makeApiClient;
export { kc };
export const core = kc.makeApiClient(CoreV1Api);
export const apps = kc.makeApiClient(AppsV1Api);
export const objectApi = KubernetesObjectApi.makeApiClient(kc);
export const logClient = new Log(kc);
export const execClient = new Exec(kc);