Files
Minikura/packages/k8s-operator/src/controllers/reverse-proxy-controller.ts
T

121 lines
3.9 KiB
TypeScript
Raw Normal View History

2026-01-16 12:32:36 +07:00
import type { PrismaClient } from "@minikura/db";
import type { ReverseProxyServer, CustomEnvironmentVariable } from "@minikura/db";
import { BaseController } from "./base-controller";
import type { ReverseProxyConfig } from "../types";
import {
createReverseProxyServer,
deleteReverseProxyServer,
} from "../resources/reverseProxyServer";
2025-05-12 12:34:09 +07:00
type ReverseProxyWithEnvVars = ReverseProxyServer & {
env_variables: CustomEnvironmentVariable[];
};
export class ReverseProxyController extends BaseController {
private deployedProxies = new Map<string, ReverseProxyWithEnvVars>();
constructor(prisma: PrismaClient, namespace: string) {
super(prisma, namespace);
}
protected getControllerName(): string {
2026-01-16 12:32:36 +07:00
return "ReverseProxyController";
2025-05-12 12:34:09 +07:00
}
protected async syncResources(): Promise<void> {
try {
const appsApi = this.k8sClient.getAppsApi();
const coreApi = this.k8sClient.getCoreApi();
const networkingApi = this.k8sClient.getNetworkingApi();
2026-01-16 12:32:36 +07:00
const proxies = (await this.prisma.reverseProxyServer.findMany({
2025-05-12 12:34:09 +07:00
include: {
env_variables: true,
2026-01-16 12:32:36 +07:00
},
})) as ReverseProxyWithEnvVars[];
const currentProxyIds = new Set(proxies.map((proxy) => proxy.id));
2025-05-12 12:34:09 +07:00
// Delete reverse proxy servers that are no longer in the database
for (const [proxyId, proxy] of this.deployedProxies.entries()) {
if (!currentProxyIds.has(proxyId)) {
2026-01-16 12:32:36 +07:00
console.log(
`Reverse proxy server ${proxy.id} (${proxyId}) has been removed from the database, deleting from Kubernetes...`
);
2025-05-12 12:34:09 +07:00
await deleteReverseProxyServer(proxy.id, proxy.type, appsApi, coreApi, this.namespace);
this.deployedProxies.delete(proxyId);
}
}
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
// Create or update reverse proxy servers that are in the database
for (const proxy of proxies) {
const deployedProxy = this.deployedProxies.get(proxy.id);
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
// If proxy doesn't exist yet or has been updated
if (!deployedProxy || this.hasProxyChanged(deployedProxy, proxy)) {
2026-01-16 12:32:36 +07:00
console.log(
`${!deployedProxy ? "Creating" : "Updating"} reverse proxy server ${proxy.id} (${proxy.id}) in Kubernetes...`
);
2025-05-12 12:34:09 +07:00
const proxyConfig: ReverseProxyConfig = {
id: proxy.id,
external_address: proxy.external_address,
external_port: proxy.external_port,
listen_port: proxy.listen_port,
description: proxy.description,
apiKey: proxy.api_key,
type: proxy.type,
memory: proxy.memory,
2026-01-16 12:32:36 +07:00
env_variables: proxy.env_variables?.map((ev) => ({
2025-05-12 12:34:09 +07:00
key: ev.key,
2026-01-16 12:32:36 +07:00
value: ev.value,
})),
2025-05-12 12:34:09 +07:00
};
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
await createReverseProxyServer(
2026-01-16 12:32:36 +07:00
proxyConfig,
appsApi,
coreApi,
networkingApi,
2025-05-12 12:34:09 +07:00
this.namespace
);
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
// Update cache
this.deployedProxies.set(proxy.id, { ...proxy });
}
}
} catch (error) {
2026-01-16 12:32:36 +07:00
console.error("Error syncing reverse proxy servers:", error);
2025-05-12 12:34:09 +07:00
throw error;
}
}
private hasProxyChanged(
2026-01-16 12:32:36 +07:00
oldProxy: ReverseProxyWithEnvVars,
2025-05-12 12:34:09 +07:00
newProxy: ReverseProxyWithEnvVars
): boolean {
// Check basic properties
2026-01-16 12:32:36 +07:00
const basicPropsChanged =
2025-05-12 12:34:09 +07:00
oldProxy.external_address !== newProxy.external_address ||
oldProxy.external_port !== newProxy.external_port ||
oldProxy.listen_port !== newProxy.listen_port ||
oldProxy.description !== newProxy.description;
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
if (basicPropsChanged) return true;
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
// Check if environment variables have changed
const oldEnvVars = oldProxy.env_variables || [];
const newEnvVars = newProxy.env_variables || [];
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
if (oldEnvVars.length !== newEnvVars.length) return true;
for (const newEnv of newEnvVars) {
2026-01-16 12:32:36 +07:00
const oldEnv = oldEnvVars.find((e) => e.key === newEnv.key);
2025-05-12 12:34:09 +07:00
if (!oldEnv || oldEnv.value !== newEnv.value) {
return true;
}
}
2026-01-16 12:32:36 +07:00
2025-05-12 12:34:09 +07:00
return false;
}
2026-01-16 12:32:36 +07:00
}