mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-09-13 18:59:25 +00:00
✨ feat: topology, and improves handling
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export function getErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
if (typeof error === "string") {
|
||||
return error;
|
||||
}
|
||||
return String(error);
|
||||
}
|
||||
|
||||
/** Returns age string like "3d", "12h", "5m" */
|
||||
export function getAge(timestamp: Date | string | undefined): string {
|
||||
if (!timestamp) return "unknown";
|
||||
|
||||
const now = new Date();
|
||||
const created = new Date(timestamp);
|
||||
const diff = now.getTime() - created.getTime();
|
||||
|
||||
const seconds = Math.floor(diff / 1000);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
|
||||
if (days > 0) return `${days}d`;
|
||||
if (hours > 0) return `${hours}h`;
|
||||
if (minutes > 0) return `${minutes}m`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./errors";
|
||||
export * from "./kube-auth";
|
||||
export * from "./logger";
|
||||
@@ -0,0 +1,24 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { KubeConfig } from "@kubernetes/client-node";
|
||||
import { createLogger } from "./logger";
|
||||
|
||||
const logger = createLogger("kube-auth");
|
||||
|
||||
export function buildKubeConfig(): KubeConfig {
|
||||
const kc = new KubeConfig();
|
||||
|
||||
const isInCluster =
|
||||
process.env.KUBERNETES_SERVICE_HOST &&
|
||||
existsSync("/var/run/secrets/kubernetes.io/serviceaccount/token");
|
||||
|
||||
if (isInCluster) {
|
||||
logger.info("Running in-cluster, loading from service account");
|
||||
kc.loadFromCluster();
|
||||
} else {
|
||||
logger.info("Running locally, loading from kubeconfig");
|
||||
kc.loadFromDefault();
|
||||
logger.info({ context: kc.getCurrentContext() }, "Using context");
|
||||
}
|
||||
|
||||
return kc;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import pino from "pino";
|
||||
|
||||
export function createLogger(component: string): pino.Logger;
|
||||
export function createLogger(
|
||||
context: Record<string, string | number>,
|
||||
): pino.Logger;
|
||||
export function createLogger(
|
||||
componentOrContext: string | Record<string, string | number>,
|
||||
): pino.Logger {
|
||||
const isString = typeof componentOrContext === "string";
|
||||
const baseContext = isString
|
||||
? { component: componentOrContext }
|
||||
: componentOrContext;
|
||||
|
||||
return pino({
|
||||
level: process.env.LOG_LEVEL || "info",
|
||||
transport:
|
||||
process.env.NODE_ENV !== "production"
|
||||
? {
|
||||
target: "pino-pretty",
|
||||
options: {
|
||||
colorize: true,
|
||||
translateTime: "HH:MM:ss.l",
|
||||
ignore: "pid,hostname",
|
||||
singleLine: false,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
base: baseContext,
|
||||
});
|
||||
}
|
||||
|
||||
export const logger = createLogger("shared");
|
||||
Reference in New Issue
Block a user