mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-01-31 14:57:49 +00:00
🎨 style: auto format
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
{
|
||||
"name": "Minikura Development",
|
||||
"dockerComposeFile": "./docker-compose.yml",
|
||||
"service": "devcontainer",
|
||||
"workspaceFolder": "/workspace",
|
||||
"remoteUser": "dev",
|
||||
"name": "Minikura Development",
|
||||
"dockerComposeFile": "./docker-compose.yml",
|
||||
"service": "devcontainer",
|
||||
"workspaceFolder": "/workspace",
|
||||
"remoteUser": "dev",
|
||||
|
||||
"postCreateCommand": "bash /workspace/.devcontainer/post-create.sh",
|
||||
"postCreateCommand": "bash /workspace/.devcontainer/post-create.sh",
|
||||
|
||||
"forwardPorts": [3000, 3001, 5432, 6443, 25565, 25577],
|
||||
"forwardPorts": [3000, 3001, 5432, 6443, 25565, 25577],
|
||||
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"biomejs.biome",
|
||||
"Prisma.prisma",
|
||||
"ms-kubernetes-tools.vscode-kubernetes-tools",
|
||||
"ms-azuretools.vscode-docker",
|
||||
"bradlc.vscode-tailwindcss",
|
||||
"redhat.vscode-yaml"
|
||||
],
|
||||
"settings": {
|
||||
"editor.defaultFormatter": "biomejs.biome",
|
||||
"editor.formatOnSave": true,
|
||||
"[prisma]": {
|
||||
"editor.defaultFormatter": "Prisma.prisma"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"biomejs.biome",
|
||||
"Prisma.prisma",
|
||||
"ms-kubernetes-tools.vscode-kubernetes-tools",
|
||||
"ms-azuretools.vscode-docker",
|
||||
"bradlc.vscode-tailwindcss",
|
||||
"redhat.vscode-yaml"
|
||||
],
|
||||
"settings": {
|
||||
"editor.defaultFormatter": "biomejs.biome",
|
||||
"editor.formatOnSave": true,
|
||||
"[prisma]": {
|
||||
"editor.defaultFormatter": "Prisma.prisma"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
.vscode/settings.json
vendored
8
.vscode/settings.json
vendored
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"editor.defaultFormatter": "biomejs.biome",
|
||||
"[prisma]": {
|
||||
"editor.defaultFormatter": "Prisma.prisma"
|
||||
}
|
||||
"editor.defaultFormatter": "biomejs.biome",
|
||||
"[prisma]": {
|
||||
"editor.defaultFormatter": "Prisma.prisma"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
import { dotenvLoad } from "dotenv-mono";
|
||||
const dotenv = dotenvLoad();
|
||||
|
||||
export const API_GROUP = 'minikura.kirameki.cafe';
|
||||
export const API_VERSION = 'v1alpha1';
|
||||
export const API_GROUP = "minikura.kirameki.cafe";
|
||||
export const API_VERSION = "v1alpha1";
|
||||
|
||||
export const KUBERNETES_NAMESPACE_ENV = process.env.KUBERNETES_NAMESPACE;
|
||||
export const NAMESPACE = process.env.KUBERNETES_NAMESPACE || 'minikura';
|
||||
export const NAMESPACE = process.env.KUBERNETES_NAMESPACE || "minikura";
|
||||
|
||||
export const ENABLE_CRD_REFLECTION = process.env.ENABLE_CRD_REFLECTION === 'true';
|
||||
export const SKIP_TLS_VERIFY = process.env.KUBERNETES_SKIP_TLS_VERIFY === 'true';
|
||||
export const ENABLE_CRD_REFLECTION = process.env.ENABLE_CRD_REFLECTION === "true";
|
||||
export const SKIP_TLS_VERIFY = process.env.KUBERNETES_SKIP_TLS_VERIFY === "true";
|
||||
|
||||
if (SKIP_TLS_VERIFY) {
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||
}
|
||||
|
||||
// Resource types
|
||||
export const RESOURCE_TYPES = {
|
||||
MINECRAFT_SERVER: {
|
||||
kind: 'MinecraftServer',
|
||||
plural: 'minecraftservers',
|
||||
singular: 'minecraftserver',
|
||||
shortNames: ['mcs'],
|
||||
kind: "MinecraftServer",
|
||||
plural: "minecraftservers",
|
||||
singular: "minecraftserver",
|
||||
shortNames: ["mcs"],
|
||||
},
|
||||
REVERSE_PROXY_SERVER: {
|
||||
kind: 'ReverseProxyServer',
|
||||
plural: 'reverseproxyservers',
|
||||
singular: 'reverseproxyserver',
|
||||
shortNames: ['rps'],
|
||||
kind: "ReverseProxyServer",
|
||||
plural: "reverseproxyservers",
|
||||
singular: "reverseproxyserver",
|
||||
shortNames: ["rps"],
|
||||
},
|
||||
};
|
||||
|
||||
// Kubernetes resource label prefixes
|
||||
export const LABEL_PREFIX = 'minikura.kirameki.cafe';
|
||||
export const LABEL_PREFIX = "minikura.kirameki.cafe";
|
||||
|
||||
// Polling intervals (in milliseconds)
|
||||
export const SYNC_INTERVAL = 30 * 1000; // 30 seconds
|
||||
|
||||
export const IMAGES = {
|
||||
MINECRAFT: 'itzg/minecraft-server',
|
||||
REVERSE_PROXY: 'itzg/minecraft-server',
|
||||
MINECRAFT: "itzg/minecraft-server",
|
||||
REVERSE_PROXY: "itzg/minecraft-server",
|
||||
};
|
||||
|
||||
export const DEFAULTS = {
|
||||
MEMORY: '1G',
|
||||
CPU_REQUEST: '250m',
|
||||
CPU_LIMIT: '1000m',
|
||||
STORAGE_SIZE: '1Gi',
|
||||
MEMORY: "1G",
|
||||
CPU_REQUEST: "250m",
|
||||
CPU_LIMIT: "1000m",
|
||||
STORAGE_SIZE: "1Gi",
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PrismaClient } from '@minikura/db';
|
||||
import { KubernetesClient } from '../utils/k8s-client';
|
||||
import { SYNC_INTERVAL } from '../config/constants';
|
||||
import type { PrismaClient } from "@minikura/db";
|
||||
import { KubernetesClient } from "../utils/k8s-client";
|
||||
import { SYNC_INTERVAL } from "../config/constants";
|
||||
|
||||
export abstract class BaseController {
|
||||
protected prisma: PrismaClient;
|
||||
@@ -21,14 +21,14 @@ export abstract class BaseController {
|
||||
console.log(`Starting to watch for changes in ${this.getControllerName()}...`);
|
||||
|
||||
// Initial sync
|
||||
this.syncResources().catch(err => {
|
||||
this.syncResources().catch((err) => {
|
||||
console.error(`Error during initial sync of ${this.getControllerName()}:`, err);
|
||||
});
|
||||
|
||||
// Polling interval for changes
|
||||
// TODO: Maybe there's a better way to do this
|
||||
this.intervalId = setInterval(() => {
|
||||
this.syncResources().catch(err => {
|
||||
this.syncResources().catch((err) => {
|
||||
console.error(`Error syncing ${this.getControllerName()}:`, err);
|
||||
});
|
||||
}, SYNC_INTERVAL);
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { 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';
|
||||
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";
|
||||
|
||||
type ReverseProxyWithEnvVars = ReverseProxyServer & {
|
||||
env_variables: CustomEnvironmentVariable[];
|
||||
@@ -16,7 +19,7 @@ export class ReverseProxyController extends BaseController {
|
||||
}
|
||||
|
||||
protected getControllerName(): string {
|
||||
return 'ReverseProxyController';
|
||||
return "ReverseProxyController";
|
||||
}
|
||||
|
||||
protected async syncResources(): Promise<void> {
|
||||
@@ -25,18 +28,20 @@ export class ReverseProxyController extends BaseController {
|
||||
const coreApi = this.k8sClient.getCoreApi();
|
||||
const networkingApi = this.k8sClient.getNetworkingApi();
|
||||
|
||||
const proxies = await this.prisma.reverseProxyServer.findMany({
|
||||
const proxies = (await this.prisma.reverseProxyServer.findMany({
|
||||
include: {
|
||||
env_variables: true,
|
||||
}
|
||||
}) as ReverseProxyWithEnvVars[];
|
||||
},
|
||||
})) as ReverseProxyWithEnvVars[];
|
||||
|
||||
const currentProxyIds = new Set(proxies.map(proxy => proxy.id));
|
||||
const currentProxyIds = new Set(proxies.map((proxy) => proxy.id));
|
||||
|
||||
// Delete reverse proxy servers that are no longer in the database
|
||||
for (const [proxyId, proxy] of this.deployedProxies.entries()) {
|
||||
if (!currentProxyIds.has(proxyId)) {
|
||||
console.log(`Reverse proxy server ${proxy.id} (${proxyId}) has been removed from the database, deleting from Kubernetes...`);
|
||||
console.log(
|
||||
`Reverse proxy server ${proxy.id} (${proxyId}) has been removed from the database, deleting from Kubernetes...`
|
||||
);
|
||||
await deleteReverseProxyServer(proxy.id, proxy.type, appsApi, coreApi, this.namespace);
|
||||
this.deployedProxies.delete(proxyId);
|
||||
}
|
||||
@@ -48,7 +53,9 @@ export class ReverseProxyController extends BaseController {
|
||||
|
||||
// If proxy doesn't exist yet or has been updated
|
||||
if (!deployedProxy || this.hasProxyChanged(deployedProxy, proxy)) {
|
||||
console.log(`${!deployedProxy ? 'Creating' : 'Updating'} reverse proxy server ${proxy.id} (${proxy.id}) in Kubernetes...`);
|
||||
console.log(
|
||||
`${!deployedProxy ? "Creating" : "Updating"} reverse proxy server ${proxy.id} (${proxy.id}) in Kubernetes...`
|
||||
);
|
||||
|
||||
const proxyConfig: ReverseProxyConfig = {
|
||||
id: proxy.id,
|
||||
@@ -59,10 +66,10 @@ export class ReverseProxyController extends BaseController {
|
||||
apiKey: proxy.api_key,
|
||||
type: proxy.type,
|
||||
memory: proxy.memory,
|
||||
env_variables: proxy.env_variables?.map(ev => ({
|
||||
env_variables: proxy.env_variables?.map((ev) => ({
|
||||
key: ev.key,
|
||||
value: ev.value
|
||||
}))
|
||||
value: ev.value,
|
||||
})),
|
||||
};
|
||||
|
||||
await createReverseProxyServer(
|
||||
@@ -78,7 +85,7 @@ export class ReverseProxyController extends BaseController {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error syncing reverse proxy servers:', error);
|
||||
console.error("Error syncing reverse proxy servers:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -102,7 +109,7 @@ export class ReverseProxyController extends BaseController {
|
||||
|
||||
if (oldEnvVars.length !== newEnvVars.length) return true;
|
||||
for (const newEnv of newEnvVars) {
|
||||
const oldEnv = oldEnvVars.find(e => e.key === newEnv.key);
|
||||
const oldEnv = oldEnvVars.find((e) => e.key === newEnv.key);
|
||||
if (!oldEnv || oldEnv.value !== newEnv.value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { PrismaClient, ServerType } from '@minikura/db';
|
||||
import type { Server, CustomEnvironmentVariable } from '@minikura/db';
|
||||
import { BaseController } from './base-controller';
|
||||
import type { ServerConfig } from '../types';
|
||||
import { createServer, deleteServer } from '../resources/server';
|
||||
import { type PrismaClient, ServerType } from "@minikura/db";
|
||||
import type { Server, CustomEnvironmentVariable } from "@minikura/db";
|
||||
import { BaseController } from "./base-controller";
|
||||
import type { ServerConfig } from "../types";
|
||||
import { createServer, deleteServer } from "../resources/server";
|
||||
|
||||
type ServerWithEnvVars = Server & {
|
||||
env_variables: CustomEnvironmentVariable[];
|
||||
@@ -16,7 +16,7 @@ export class ServerController extends BaseController {
|
||||
}
|
||||
|
||||
protected getControllerName(): string {
|
||||
return 'ServerController';
|
||||
return "ServerController";
|
||||
}
|
||||
|
||||
protected async syncResources(): Promise<void> {
|
||||
@@ -25,18 +25,20 @@ export class ServerController extends BaseController {
|
||||
const coreApi = this.k8sClient.getCoreApi();
|
||||
const networkingApi = this.k8sClient.getNetworkingApi();
|
||||
|
||||
const servers = await this.prisma.server.findMany({
|
||||
const servers = (await this.prisma.server.findMany({
|
||||
include: {
|
||||
env_variables: true,
|
||||
}
|
||||
}) as ServerWithEnvVars[];
|
||||
},
|
||||
})) as ServerWithEnvVars[];
|
||||
|
||||
const currentServerIds = new Set(servers.map(server => server.id));
|
||||
const currentServerIds = new Set(servers.map((server) => server.id));
|
||||
|
||||
// Delete servers that are no longer in the database
|
||||
for (const [serverId, server] of this.deployedServers.entries()) {
|
||||
if (!currentServerIds.has(serverId)) {
|
||||
console.log(`Server ${server.id} (${serverId}) has been removed from the database, deleting from Kubernetes...`);
|
||||
console.log(
|
||||
`Server ${server.id} (${serverId}) has been removed from the database, deleting from Kubernetes...`
|
||||
);
|
||||
await deleteServer(serverId, server.id, appsApi, coreApi, this.namespace);
|
||||
this.deployedServers.delete(serverId);
|
||||
}
|
||||
@@ -48,7 +50,9 @@ export class ServerController extends BaseController {
|
||||
|
||||
// If server doesn't exist yet or has been updated
|
||||
if (!deployedServer || this.hasServerChanged(deployedServer, server)) {
|
||||
console.log(`${!deployedServer ? 'Creating' : 'Updating'} server ${server.id} (${server.id}) in Kubernetes...`);
|
||||
console.log(
|
||||
`${!deployedServer ? "Creating" : "Updating"} server ${server.id} (${server.id}) in Kubernetes...`
|
||||
);
|
||||
|
||||
const serverConfig: ServerConfig = {
|
||||
id: server.id,
|
||||
@@ -57,34 +61,25 @@ export class ServerController extends BaseController {
|
||||
description: server.description,
|
||||
listen_port: server.listen_port,
|
||||
memory: server.memory,
|
||||
env_variables: server.env_variables?.map(ev => ({
|
||||
env_variables: server.env_variables?.map((ev) => ({
|
||||
key: ev.key,
|
||||
value: ev.value
|
||||
}))
|
||||
value: ev.value,
|
||||
})),
|
||||
};
|
||||
|
||||
await createServer(
|
||||
serverConfig,
|
||||
appsApi,
|
||||
coreApi,
|
||||
networkingApi,
|
||||
this.namespace
|
||||
);
|
||||
await createServer(serverConfig, appsApi, coreApi, networkingApi, this.namespace);
|
||||
|
||||
// Update cache
|
||||
this.deployedServers.set(server.id, { ...server });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error syncing servers:', error);
|
||||
console.error("Error syncing servers:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private hasServerChanged(
|
||||
oldServer: ServerWithEnvVars,
|
||||
newServer: ServerWithEnvVars
|
||||
): boolean {
|
||||
private hasServerChanged(oldServer: ServerWithEnvVars, newServer: ServerWithEnvVars): boolean {
|
||||
// Check basic properties
|
||||
const basicPropsChanged =
|
||||
oldServer.type !== newServer.type ||
|
||||
@@ -102,7 +97,7 @@ export class ServerController extends BaseController {
|
||||
|
||||
// Check if any of the existing env vars have changed
|
||||
for (const newEnv of newEnvVars) {
|
||||
const oldEnv = oldEnvVars.find(e => e.key === newEnv.key);
|
||||
const oldEnv = oldEnvVars.find((e) => e.key === newEnv.key);
|
||||
if (!oldEnv || oldEnv.value !== newEnv.value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { NAMESPACE } from '../config/constants';
|
||||
import { NAMESPACE } from "../config/constants";
|
||||
|
||||
/**
|
||||
* Namespace definition
|
||||
*/
|
||||
export const minikuraNamespace = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'Namespace',
|
||||
apiVersion: "v1",
|
||||
kind: "Namespace",
|
||||
metadata: {
|
||||
name: NAMESPACE,
|
||||
},
|
||||
@@ -15,10 +15,10 @@ export const minikuraNamespace = {
|
||||
* Service account
|
||||
*/
|
||||
export const minikuraServiceAccount = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'ServiceAccount',
|
||||
apiVersion: "v1",
|
||||
kind: "ServiceAccount",
|
||||
metadata: {
|
||||
name: 'minikura-operator',
|
||||
name: "minikura-operator",
|
||||
namespace: NAMESPACE,
|
||||
},
|
||||
};
|
||||
@@ -27,41 +27,41 @@ export const minikuraServiceAccount = {
|
||||
* Cluster role
|
||||
*/
|
||||
export const minikuraClusterRole = {
|
||||
apiVersion: 'rbac.authorization.k8s.io/v1',
|
||||
kind: 'ClusterRole',
|
||||
apiVersion: "rbac.authorization.k8s.io/v1",
|
||||
kind: "ClusterRole",
|
||||
metadata: {
|
||||
name: 'minikura-operator-role',
|
||||
name: "minikura-operator-role",
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
apiGroups: [''],
|
||||
resources: ['configmaps', 'services', 'secrets'],
|
||||
verbs: ['get', 'list', 'watch', 'create', 'update', 'patch', 'delete'],
|
||||
apiGroups: [""],
|
||||
resources: ["configmaps", "services", "secrets"],
|
||||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"],
|
||||
},
|
||||
{
|
||||
apiGroups: ['apps'],
|
||||
resources: ['deployments', 'statefulsets'],
|
||||
verbs: ['get', 'list', 'watch', 'create', 'update', 'patch', 'delete'],
|
||||
apiGroups: ["apps"],
|
||||
resources: ["deployments", "statefulsets"],
|
||||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"],
|
||||
},
|
||||
{
|
||||
apiGroups: ['networking.k8s.io'],
|
||||
resources: ['ingresses'],
|
||||
verbs: ['get', 'list', 'watch', 'create', 'update', 'patch', 'delete'],
|
||||
apiGroups: ["networking.k8s.io"],
|
||||
resources: ["ingresses"],
|
||||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"],
|
||||
},
|
||||
{
|
||||
apiGroups: ['apiextensions.k8s.io'],
|
||||
resources: ['customresourcedefinitions'],
|
||||
verbs: ['get', 'list', 'watch', 'create', 'update', 'patch', 'delete'],
|
||||
apiGroups: ["apiextensions.k8s.io"],
|
||||
resources: ["customresourcedefinitions"],
|
||||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"],
|
||||
},
|
||||
{
|
||||
apiGroups: ['minikura.kirameki.cafe'],
|
||||
resources: ['minecraftservers', 'velocityproxies'],
|
||||
verbs: ['get', 'list', 'watch', 'create', 'update', 'patch', 'delete'],
|
||||
apiGroups: ["minikura.kirameki.cafe"],
|
||||
resources: ["minecraftservers", "velocityproxies"],
|
||||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"],
|
||||
},
|
||||
{
|
||||
apiGroups: ['minikura.kirameki.cafe'],
|
||||
resources: ['minecraftservers/status', 'velocityproxies/status'],
|
||||
verbs: ['get', 'update', 'patch'],
|
||||
apiGroups: ["minikura.kirameki.cafe"],
|
||||
resources: ["minecraftservers/status", "velocityproxies/status"],
|
||||
verbs: ["get", "update", "patch"],
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -70,22 +70,22 @@ export const minikuraClusterRole = {
|
||||
* Cluster role binding
|
||||
*/
|
||||
export const minikuraClusterRoleBinding = {
|
||||
apiVersion: 'rbac.authorization.k8s.io/v1',
|
||||
kind: 'ClusterRoleBinding',
|
||||
apiVersion: "rbac.authorization.k8s.io/v1",
|
||||
kind: "ClusterRoleBinding",
|
||||
metadata: {
|
||||
name: 'minikura-operator-role-binding',
|
||||
name: "minikura-operator-role-binding",
|
||||
},
|
||||
subjects: [
|
||||
{
|
||||
kind: 'ServiceAccount',
|
||||
name: 'minikura-operator',
|
||||
kind: "ServiceAccount",
|
||||
name: "minikura-operator",
|
||||
namespace: NAMESPACE,
|
||||
},
|
||||
],
|
||||
roleRef: {
|
||||
kind: 'ClusterRole',
|
||||
name: 'minikura-operator-role',
|
||||
apiGroup: 'rbac.authorization.k8s.io',
|
||||
kind: "ClusterRole",
|
||||
name: "minikura-operator-role",
|
||||
apiGroup: "rbac.authorization.k8s.io",
|
||||
},
|
||||
};
|
||||
|
||||
@@ -93,70 +93,70 @@ export const minikuraClusterRoleBinding = {
|
||||
* Deployment for the Minikura operator
|
||||
*/
|
||||
export const minikuraOperatorDeployment = {
|
||||
apiVersion: 'apps/v1',
|
||||
kind: 'Deployment',
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
metadata: {
|
||||
name: 'minikura-operator',
|
||||
name: "minikura-operator",
|
||||
namespace: NAMESPACE,
|
||||
},
|
||||
spec: {
|
||||
replicas: 1,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: 'minikura-operator',
|
||||
app: "minikura-operator",
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
app: 'minikura-operator',
|
||||
app: "minikura-operator",
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
serviceAccountName: 'minikura-operator',
|
||||
serviceAccountName: "minikura-operator",
|
||||
containers: [
|
||||
{
|
||||
name: 'operator',
|
||||
image: '${REGISTRY_URL}/minikura-operator:latest',
|
||||
name: "operator",
|
||||
image: "${REGISTRY_URL}/minikura-operator:latest",
|
||||
env: [
|
||||
{
|
||||
name: 'DATABASE_URL',
|
||||
name: "DATABASE_URL",
|
||||
valueFrom: {
|
||||
secretKeyRef: {
|
||||
name: 'minikura-operator-secrets',
|
||||
key: 'DATABASE_URL',
|
||||
name: "minikura-operator-secrets",
|
||||
key: "DATABASE_URL",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'KUBERNETES_NAMESPACE',
|
||||
name: "KUBERNETES_NAMESPACE",
|
||||
value: NAMESPACE,
|
||||
},
|
||||
{
|
||||
name: 'USE_CRDS',
|
||||
value: 'true',
|
||||
name: "USE_CRDS",
|
||||
value: "true",
|
||||
},
|
||||
],
|
||||
resources: {
|
||||
requests: {
|
||||
memory: '256Mi',
|
||||
cpu: '200m',
|
||||
memory: "256Mi",
|
||||
cpu: "200m",
|
||||
},
|
||||
limits: {
|
||||
memory: '512Mi',
|
||||
cpu: '500m',
|
||||
memory: "512Mi",
|
||||
cpu: "500m",
|
||||
},
|
||||
},
|
||||
livenessProbe: {
|
||||
exec: {
|
||||
command: ['bun', '-e', "console.log('Health check')"],
|
||||
command: ["bun", "-e", "console.log('Health check')"],
|
||||
},
|
||||
initialDelaySeconds: 30,
|
||||
periodSeconds: 30,
|
||||
},
|
||||
readinessProbe: {
|
||||
exec: {
|
||||
command: ['bun', '-e', "console.log('Ready check')"],
|
||||
command: ["bun", "-e", "console.log('Ready check')"],
|
||||
},
|
||||
initialDelaySeconds: 5,
|
||||
periodSeconds: 10,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { API_GROUP, API_VERSION, RESOURCE_TYPES } from '../config/constants';
|
||||
import { API_GROUP, API_VERSION, RESOURCE_TYPES } from "../config/constants";
|
||||
|
||||
export const REVERSE_PROXY_SERVER_CRD = {
|
||||
apiVersion: 'apiextensions.k8s.io/v1',
|
||||
kind: 'CustomResourceDefinition',
|
||||
apiVersion: "apiextensions.k8s.io/v1",
|
||||
kind: "CustomResourceDefinition",
|
||||
metadata: {
|
||||
name: `${RESOURCE_TYPES.REVERSE_PROXY_SERVER.plural}.${API_GROUP}`,
|
||||
},
|
||||
@@ -15,67 +15,67 @@ export const REVERSE_PROXY_SERVER_CRD = {
|
||||
storage: true,
|
||||
schema: {
|
||||
openAPIV3Schema: {
|
||||
type: 'object',
|
||||
type: "object",
|
||||
properties: {
|
||||
spec: {
|
||||
type: 'object',
|
||||
required: ['id', 'external_address', 'external_port'],
|
||||
type: "object",
|
||||
required: ["id", "external_address", "external_port"],
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
pattern: '^[a-zA-Z0-9-_]+$',
|
||||
description: 'ID of the reverse proxy server',
|
||||
type: "string",
|
||||
pattern: "^[a-zA-Z0-9-_]+$",
|
||||
description: "ID of the reverse proxy server",
|
||||
},
|
||||
description: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Optional description of the server',
|
||||
description: "Optional description of the server",
|
||||
},
|
||||
external_address: {
|
||||
type: 'string',
|
||||
description: 'External address of the proxy server',
|
||||
type: "string",
|
||||
description: "External address of the proxy server",
|
||||
},
|
||||
external_port: {
|
||||
type: 'integer',
|
||||
type: "integer",
|
||||
minimum: 1,
|
||||
maximum: 65535,
|
||||
description: 'External port of the proxy server',
|
||||
description: "External port of the proxy server",
|
||||
},
|
||||
listen_port: {
|
||||
type: 'integer',
|
||||
type: "integer",
|
||||
minimum: 1,
|
||||
maximum: 65535,
|
||||
default: 25565,
|
||||
nullable: true,
|
||||
description: 'Port the proxy server listens on internally',
|
||||
description: "Port the proxy server listens on internally",
|
||||
},
|
||||
type: {
|
||||
type: 'string',
|
||||
enum: ['VELOCITY', 'BUNGEECORD'],
|
||||
default: 'VELOCITY',
|
||||
type: "string",
|
||||
enum: ["VELOCITY", "BUNGEECORD"],
|
||||
default: "VELOCITY",
|
||||
nullable: true,
|
||||
description: 'Type of the reverse proxy server',
|
||||
description: "Type of the reverse proxy server",
|
||||
},
|
||||
memory: {
|
||||
type: 'string',
|
||||
default: '512M',
|
||||
type: "string",
|
||||
default: "512M",
|
||||
nullable: true,
|
||||
description: 'Memory allocation for the server',
|
||||
description: "Memory allocation for the server",
|
||||
},
|
||||
environmentVariables: {
|
||||
type: 'array',
|
||||
type: "array",
|
||||
nullable: true,
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['key', 'value'],
|
||||
type: "object",
|
||||
required: ["key", "value"],
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
description: 'Environment variable key',
|
||||
type: "string",
|
||||
description: "Environment variable key",
|
||||
},
|
||||
value: {
|
||||
type: 'string',
|
||||
description: 'Environment variable value',
|
||||
type: "string",
|
||||
description: "Environment variable value",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -83,33 +83,33 @@ export const REVERSE_PROXY_SERVER_CRD = {
|
||||
},
|
||||
},
|
||||
status: {
|
||||
type: 'object',
|
||||
type: "object",
|
||||
nullable: true,
|
||||
properties: {
|
||||
phase: {
|
||||
type: 'string',
|
||||
enum: ['Pending', 'Running', 'Failed'],
|
||||
description: 'Current phase of the server',
|
||||
type: "string",
|
||||
enum: ["Pending", "Running", "Failed"],
|
||||
description: "Current phase of the server",
|
||||
},
|
||||
message: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Detailed message about the current status',
|
||||
description: "Detailed message about the current status",
|
||||
},
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'API key for server communication',
|
||||
description: "API key for server communication",
|
||||
},
|
||||
internalId: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Internal ID assigned by Minikura',
|
||||
description: "Internal ID assigned by Minikura",
|
||||
},
|
||||
lastSyncedAt: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Last time the server was synced with Kubernetes',
|
||||
description: "Last time the server was synced with Kubernetes",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -118,34 +118,34 @@ export const REVERSE_PROXY_SERVER_CRD = {
|
||||
},
|
||||
additionalPrinterColumns: [
|
||||
{
|
||||
name: 'Type',
|
||||
type: 'string',
|
||||
jsonPath: '.spec.type',
|
||||
name: "Type",
|
||||
type: "string",
|
||||
jsonPath: ".spec.type",
|
||||
},
|
||||
{
|
||||
name: 'External Address',
|
||||
type: 'string',
|
||||
jsonPath: '.spec.external_address',
|
||||
name: "External Address",
|
||||
type: "string",
|
||||
jsonPath: ".spec.external_address",
|
||||
},
|
||||
{
|
||||
name: 'External Port',
|
||||
type: 'integer',
|
||||
jsonPath: '.spec.external_port',
|
||||
name: "External Port",
|
||||
type: "integer",
|
||||
jsonPath: ".spec.external_port",
|
||||
},
|
||||
{
|
||||
name: 'Status',
|
||||
type: 'string',
|
||||
jsonPath: '.status.phase',
|
||||
name: "Status",
|
||||
type: "string",
|
||||
jsonPath: ".status.phase",
|
||||
},
|
||||
{
|
||||
name: 'Age',
|
||||
type: 'date',
|
||||
jsonPath: '.metadata.creationTimestamp',
|
||||
name: "Age",
|
||||
type: "date",
|
||||
jsonPath: ".metadata.creationTimestamp",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
scope: 'Namespaced',
|
||||
scope: "Namespaced",
|
||||
names: {
|
||||
singular: RESOURCE_TYPES.REVERSE_PROXY_SERVER.singular,
|
||||
plural: RESOURCE_TYPES.REVERSE_PROXY_SERVER.plural,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { API_GROUP, API_VERSION, RESOURCE_TYPES } from '../config/constants';
|
||||
import { API_GROUP, API_VERSION, RESOURCE_TYPES } from "../config/constants";
|
||||
|
||||
export const MINECRAFT_SERVER_CRD = {
|
||||
apiVersion: 'apiextensions.k8s.io/v1',
|
||||
kind: 'CustomResourceDefinition',
|
||||
apiVersion: "apiextensions.k8s.io/v1",
|
||||
kind: "CustomResourceDefinition",
|
||||
metadata: {
|
||||
name: `${RESOURCE_TYPES.MINECRAFT_SERVER.plural}.${API_GROUP}`,
|
||||
},
|
||||
@@ -15,53 +15,53 @@ export const MINECRAFT_SERVER_CRD = {
|
||||
storage: true,
|
||||
schema: {
|
||||
openAPIV3Schema: {
|
||||
type: 'object',
|
||||
type: "object",
|
||||
properties: {
|
||||
spec: {
|
||||
type: 'object',
|
||||
required: ['id', 'type', 'listen_port'],
|
||||
type: "object",
|
||||
required: ["id", "type", "listen_port"],
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
pattern: '^[a-zA-Z0-9-_]+$',
|
||||
description: 'ID of the Minecraft server',
|
||||
type: "string",
|
||||
pattern: "^[a-zA-Z0-9-_]+$",
|
||||
description: "ID of the Minecraft server",
|
||||
},
|
||||
description: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Optional description of the server',
|
||||
description: "Optional description of the server",
|
||||
},
|
||||
listen_port: {
|
||||
type: 'integer',
|
||||
type: "integer",
|
||||
minimum: 1,
|
||||
maximum: 65535,
|
||||
description: 'Port the server listens on',
|
||||
description: "Port the server listens on",
|
||||
},
|
||||
type: {
|
||||
type: 'string',
|
||||
enum: ['STATEFUL', 'STATELESS'],
|
||||
description: 'Type of the server',
|
||||
type: "string",
|
||||
enum: ["STATEFUL", "STATELESS"],
|
||||
description: "Type of the server",
|
||||
},
|
||||
memory: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
default: '1G',
|
||||
description: 'Memory allocation for the server',
|
||||
default: "1G",
|
||||
description: "Memory allocation for the server",
|
||||
},
|
||||
environmentVariables: {
|
||||
type: 'array',
|
||||
type: "array",
|
||||
nullable: true,
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['key', 'value'],
|
||||
type: "object",
|
||||
required: ["key", "value"],
|
||||
properties: {
|
||||
key: {
|
||||
type: 'string',
|
||||
description: 'Environment variable key',
|
||||
type: "string",
|
||||
description: "Environment variable key",
|
||||
},
|
||||
value: {
|
||||
type: 'string',
|
||||
description: 'Environment variable value',
|
||||
type: "string",
|
||||
description: "Environment variable value",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -69,33 +69,33 @@ export const MINECRAFT_SERVER_CRD = {
|
||||
},
|
||||
},
|
||||
status: {
|
||||
type: 'object',
|
||||
type: "object",
|
||||
nullable: true,
|
||||
properties: {
|
||||
phase: {
|
||||
type: 'string',
|
||||
enum: ['Pending', 'Running', 'Failed'],
|
||||
description: 'Current phase of the server',
|
||||
type: "string",
|
||||
enum: ["Pending", "Running", "Failed"],
|
||||
description: "Current phase of the server",
|
||||
},
|
||||
message: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Detailed message about the current status',
|
||||
description: "Detailed message about the current status",
|
||||
},
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'API key for server communication',
|
||||
description: "API key for server communication",
|
||||
},
|
||||
internalId: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Internal ID assigned by Minikura',
|
||||
description: "Internal ID assigned by Minikura",
|
||||
},
|
||||
lastSyncedAt: {
|
||||
type: 'string',
|
||||
type: "string",
|
||||
nullable: true,
|
||||
description: 'Last time the server was synced with Kubernetes',
|
||||
description: "Last time the server was synced with Kubernetes",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -104,24 +104,24 @@ export const MINECRAFT_SERVER_CRD = {
|
||||
},
|
||||
additionalPrinterColumns: [
|
||||
{
|
||||
name: 'Type',
|
||||
type: 'string',
|
||||
jsonPath: '.spec.type',
|
||||
name: "Type",
|
||||
type: "string",
|
||||
jsonPath: ".spec.type",
|
||||
},
|
||||
{
|
||||
name: 'Status',
|
||||
type: 'string',
|
||||
jsonPath: '.status.phase',
|
||||
name: "Status",
|
||||
type: "string",
|
||||
jsonPath: ".status.phase",
|
||||
},
|
||||
{
|
||||
name: 'Age',
|
||||
type: 'date',
|
||||
jsonPath: '.metadata.creationTimestamp',
|
||||
name: "Age",
|
||||
type: "date",
|
||||
jsonPath: ".metadata.creationTimestamp",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
scope: 'Namespaced',
|
||||
scope: "Namespaced",
|
||||
names: {
|
||||
singular: RESOURCE_TYPES.MINECRAFT_SERVER.singular,
|
||||
plural: RESOURCE_TYPES.MINECRAFT_SERVER.plural,
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { dotenvLoad } from "dotenv-mono";
|
||||
const dotenv = dotenvLoad();
|
||||
|
||||
import { NAMESPACE, KUBERNETES_NAMESPACE_ENV, ENABLE_CRD_REFLECTION } from './config/constants';
|
||||
import { NAMESPACE, KUBERNETES_NAMESPACE_ENV, ENABLE_CRD_REFLECTION } from "./config/constants";
|
||||
import { prisma } from "@minikura/db";
|
||||
import { KubernetesClient } from './utils/k8s-client';
|
||||
import { ServerController } from './controllers/server-controller';
|
||||
import { ReverseProxyController } from './controllers/reverse-proxy-controller';
|
||||
import { setupCRDRegistration } from './utils/crd-registrar';
|
||||
import { KubernetesClient } from "./utils/k8s-client";
|
||||
import { ServerController } from "./controllers/server-controller";
|
||||
import { ReverseProxyController } from "./controllers/reverse-proxy-controller";
|
||||
import { setupCRDRegistration } from "./utils/crd-registrar";
|
||||
|
||||
async function main() {
|
||||
console.log('Starting Minikura Kubernetes Operator...');
|
||||
console.log("Starting Minikura Kubernetes Operator...");
|
||||
console.log(`Using namespace: ${NAMESPACE}`);
|
||||
|
||||
try {
|
||||
const k8sClient = KubernetesClient.getInstance();
|
||||
console.log('Connected to Kubernetes cluster');
|
||||
console.log("Connected to Kubernetes cluster");
|
||||
|
||||
const serverController = new ServerController(prisma, NAMESPACE);
|
||||
const reverseProxyController = new ReverseProxyController(prisma, NAMESPACE);
|
||||
@@ -23,7 +23,7 @@ async function main() {
|
||||
reverseProxyController.startWatching();
|
||||
|
||||
if (ENABLE_CRD_REFLECTION) {
|
||||
console.log('CRD reflection enabled - will create CRDs to reflect database state');
|
||||
console.log("CRD reflection enabled - will create CRDs to reflect database state");
|
||||
try {
|
||||
await setupCRDRegistration(prisma, k8sClient, NAMESPACE);
|
||||
} catch (error: any) {
|
||||
@@ -32,25 +32,26 @@ async function main() {
|
||||
console.error(`Response status: ${error.response.statusCode}`);
|
||||
console.error(`Response body: ${JSON.stringify(error.response.body)}`);
|
||||
}
|
||||
console.error('Continuing operation without CRD reflection');
|
||||
console.log('Kubernetes resources will still be created/updated, but CRD reflection is disabled');
|
||||
console.error("Continuing operation without CRD reflection");
|
||||
console.log(
|
||||
"Kubernetes resources will still be created/updated, but CRD reflection is disabled"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Minikura Kubernetes Operator is running');
|
||||
console.log("Minikura Kubernetes Operator is running");
|
||||
|
||||
process.on('SIGINT', gracefulShutdown);
|
||||
process.on('SIGTERM', gracefulShutdown);
|
||||
process.on("SIGINT", gracefulShutdown);
|
||||
process.on("SIGTERM", gracefulShutdown);
|
||||
|
||||
function gracefulShutdown() {
|
||||
console.log('Shutting down operator gracefully...');
|
||||
console.log("Shutting down operator gracefully...");
|
||||
serverController.stopWatching();
|
||||
reverseProxyController.stopWatching();
|
||||
prisma.$disconnect();
|
||||
console.log('Resources released, exiting...');
|
||||
console.log("Resources released, exiting...");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
console.error(`Failed to start Minikura Kubernetes Operator: ${error.message}`);
|
||||
if (error.response) {
|
||||
@@ -64,7 +65,7 @@ async function main() {
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error('Unhandled error:', error);
|
||||
main().catch((error) => {
|
||||
console.error("Unhandled error:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,8 +1,8 @@
|
||||
import * as k8s from '@kubernetes/client-node';
|
||||
import { ReverseProxyServerType } from '@minikura/db';
|
||||
import { LABEL_PREFIX } from '../config/constants';
|
||||
import { calculateJavaMemory, convertToK8sFormat } from '../utils/memory';
|
||||
import type { ReverseProxyConfig } from '../types';
|
||||
import type * as k8s from "@kubernetes/client-node";
|
||||
import type { ReverseProxyServerType } from "@minikura/db";
|
||||
import { LABEL_PREFIX } from "../config/constants";
|
||||
import { calculateJavaMemory, convertToK8sFormat } from "../utils/memory";
|
||||
import type { ReverseProxyConfig } from "../types";
|
||||
|
||||
export async function createReverseProxyServer(
|
||||
server: ReverseProxyConfig,
|
||||
@@ -17,8 +17,8 @@ export async function createReverseProxyServer(
|
||||
const serverName = `${serverType}-${server.id}`;
|
||||
|
||||
const configMap = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'ConfigMap',
|
||||
apiVersion: "v1",
|
||||
kind: "ConfigMap",
|
||||
metadata: {
|
||||
name: `${serverName}-config`,
|
||||
namespace: namespace,
|
||||
@@ -26,11 +26,11 @@ export async function createReverseProxyServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: serverType,
|
||||
[`${LABEL_PREFIX}/proxy-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
data: {
|
||||
'minikura-api-key': server.apiKey,
|
||||
}
|
||||
"minikura-api-key": server.apiKey,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -48,8 +48,8 @@ export async function createReverseProxyServer(
|
||||
|
||||
// Create Service for the reverse proxy - Always LoadBalancer for now
|
||||
const service = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'Service',
|
||||
apiVersion: "v1",
|
||||
kind: "Service",
|
||||
metadata: {
|
||||
name: serverName,
|
||||
namespace: namespace,
|
||||
@@ -57,7 +57,7 @@ export async function createReverseProxyServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: serverType,
|
||||
[`${LABEL_PREFIX}/proxy-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
selector: {
|
||||
@@ -67,12 +67,12 @@ export async function createReverseProxyServer(
|
||||
{
|
||||
port: server.external_port,
|
||||
targetPort: server.listen_port,
|
||||
protocol: 'TCP',
|
||||
name: 'minecraft',
|
||||
}
|
||||
protocol: "TCP",
|
||||
name: "minecraft",
|
||||
},
|
||||
],
|
||||
type: 'LoadBalancer',
|
||||
}
|
||||
type: "LoadBalancer",
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -90,8 +90,8 @@ export async function createReverseProxyServer(
|
||||
|
||||
// Create Deployment
|
||||
const deployment = {
|
||||
apiVersion: 'apps/v1',
|
||||
kind: 'Deployment',
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
metadata: {
|
||||
name: serverName,
|
||||
namespace: namespace,
|
||||
@@ -99,14 +99,14 @@ export async function createReverseProxyServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: serverType,
|
||||
[`${LABEL_PREFIX}/proxy-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
replicas: 1,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: serverName,
|
||||
}
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
@@ -114,33 +114,33 @@ export async function createReverseProxyServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: serverType,
|
||||
[`${LABEL_PREFIX}/proxy-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: serverType,
|
||||
image: 'itzg/mc-proxy:latest',
|
||||
image: "itzg/mc-proxy:latest",
|
||||
ports: [
|
||||
{
|
||||
containerPort: server.listen_port,
|
||||
name: 'minecraft',
|
||||
}
|
||||
name: "minecraft",
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: 'TYPE',
|
||||
name: "TYPE",
|
||||
value: server.type,
|
||||
},
|
||||
{
|
||||
name: 'NETWORKADDRESS_CACHE_TTL',
|
||||
value: '30',
|
||||
name: "NETWORKADDRESS_CACHE_TTL",
|
||||
value: "30",
|
||||
},
|
||||
{
|
||||
name: 'MEMORY',
|
||||
value: calculateJavaMemory(server.memory || '512M', 0.8),
|
||||
name: "MEMORY",
|
||||
value: calculateJavaMemory(server.memory || "512M", 0.8),
|
||||
},
|
||||
...(server.env_variables || []).map(ev => ({
|
||||
...(server.env_variables || []).map((ev) => ({
|
||||
name: ev.key,
|
||||
value: ev.value,
|
||||
})),
|
||||
@@ -160,13 +160,13 @@ export async function createReverseProxyServer(
|
||||
limits: {
|
||||
memory: convertToK8sFormat(server.memory || "512M"),
|
||||
cpu: "500m",
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import * as k8s from '@kubernetes/client-node';
|
||||
import { ServerType } from '@minikura/db';
|
||||
import { LABEL_PREFIX } from '../config/constants';
|
||||
import { calculateJavaMemory, convertToK8sFormat } from '../utils/memory';
|
||||
import type { ServerConfig } from '../types';
|
||||
import type * as k8s from "@kubernetes/client-node";
|
||||
import { ServerType } from "@minikura/db";
|
||||
import { LABEL_PREFIX } from "../config/constants";
|
||||
import { calculateJavaMemory, convertToK8sFormat } from "../utils/memory";
|
||||
import type { ServerConfig } from "../types";
|
||||
|
||||
export async function createServer(
|
||||
server: ServerConfig,
|
||||
@@ -14,8 +14,8 @@ export async function createServer(
|
||||
const serverName = `minecraft-${server.id}`;
|
||||
|
||||
const configMap = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'ConfigMap',
|
||||
apiVersion: "v1",
|
||||
kind: "ConfigMap",
|
||||
metadata: {
|
||||
name: `${serverName}-config`,
|
||||
namespace: namespace,
|
||||
@@ -23,12 +23,12 @@ export async function createServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: server.type.toLowerCase(),
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
data: {
|
||||
'server-type': server.type,
|
||||
'minikura-api-key': server.apiKey,
|
||||
}
|
||||
"server-type": server.type,
|
||||
"minikura-api-key": server.apiKey,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -45,8 +45,8 @@ export async function createServer(
|
||||
}
|
||||
|
||||
const service = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'Service',
|
||||
apiVersion: "v1",
|
||||
kind: "Service",
|
||||
metadata: {
|
||||
name: serverName,
|
||||
namespace: namespace,
|
||||
@@ -54,7 +54,7 @@ export async function createServer(
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: server.type.toLowerCase(),
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
selector: {
|
||||
@@ -64,12 +64,12 @@ export async function createServer(
|
||||
{
|
||||
port: server.listen_port,
|
||||
targetPort: 25565,
|
||||
protocol: 'TCP',
|
||||
name: 'minecraft',
|
||||
}
|
||||
protocol: "TCP",
|
||||
name: "minecraft",
|
||||
},
|
||||
],
|
||||
type: 'ClusterIP', // Always ClusterIP for regular servers
|
||||
}
|
||||
type: "ClusterIP", // Always ClusterIP for regular servers
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -99,78 +99,78 @@ async function createDeployment(
|
||||
namespace: string
|
||||
): Promise<void> {
|
||||
const deployment = {
|
||||
apiVersion: 'apps/v1',
|
||||
kind: 'Deployment',
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
metadata: {
|
||||
name: serverName,
|
||||
namespace: namespace,
|
||||
labels: {
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: 'stateless',
|
||||
[`${LABEL_PREFIX}/server-type`]: "stateless",
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
replicas: 1,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: serverName,
|
||||
}
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: 'stateless',
|
||||
[`${LABEL_PREFIX}/server-type`]: "stateless",
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: 'minecraft',
|
||||
image: 'itzg/minecraft-server',
|
||||
name: "minecraft",
|
||||
image: "itzg/minecraft-server",
|
||||
ports: [
|
||||
{
|
||||
containerPort: 25565,
|
||||
name: 'minecraft',
|
||||
}
|
||||
name: "minecraft",
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: 'EULA',
|
||||
value: 'TRUE',
|
||||
name: "EULA",
|
||||
value: "TRUE",
|
||||
},
|
||||
{
|
||||
name: 'TYPE',
|
||||
value: 'VANILLA',
|
||||
name: "TYPE",
|
||||
value: "VANILLA",
|
||||
},
|
||||
{
|
||||
name: 'MEMORY',
|
||||
value: calculateJavaMemory(server.memory || '1G', 0.8),
|
||||
name: "MEMORY",
|
||||
value: calculateJavaMemory(server.memory || "1G", 0.8),
|
||||
},
|
||||
{
|
||||
name: 'OPS',
|
||||
value: '',
|
||||
name: "OPS",
|
||||
value: "",
|
||||
},
|
||||
{
|
||||
name: 'OVERRIDE_SERVER_PROPERTIES',
|
||||
value: 'true',
|
||||
name: "OVERRIDE_SERVER_PROPERTIES",
|
||||
value: "true",
|
||||
},
|
||||
{
|
||||
name: 'ENABLE_RCON',
|
||||
value: 'false',
|
||||
name: "ENABLE_RCON",
|
||||
value: "false",
|
||||
},
|
||||
...(server.env_variables || []).map(ev => ({
|
||||
...(server.env_variables || []).map((ev) => ({
|
||||
name: ev.key,
|
||||
value: ev.value,
|
||||
})),
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: 'config',
|
||||
mountPath: '/config',
|
||||
}
|
||||
name: "config",
|
||||
mountPath: "/config",
|
||||
},
|
||||
],
|
||||
readinessProbe: {
|
||||
tcpSocket: {
|
||||
@@ -187,21 +187,21 @@ async function createDeployment(
|
||||
limits: {
|
||||
memory: convertToK8sFormat(server.memory || "1G"),
|
||||
cpu: "500m",
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'config',
|
||||
name: "config",
|
||||
configMap: {
|
||||
name: `${serverName}-config`,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -225,16 +225,16 @@ async function createStatefulSet(
|
||||
namespace: string
|
||||
): Promise<void> {
|
||||
const statefulSet = {
|
||||
apiVersion: 'apps/v1',
|
||||
kind: 'StatefulSet',
|
||||
apiVersion: "apps/v1",
|
||||
kind: "StatefulSet",
|
||||
metadata: {
|
||||
name: serverName,
|
||||
namespace: namespace,
|
||||
labels: {
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: 'stateful',
|
||||
[`${LABEL_PREFIX}/server-type`]: "stateful",
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
serviceName: serverName,
|
||||
@@ -242,66 +242,66 @@ async function createStatefulSet(
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: serverName,
|
||||
}
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
app: serverName,
|
||||
[`${LABEL_PREFIX}/server-type`]: 'stateful',
|
||||
[`${LABEL_PREFIX}/server-type`]: "stateful",
|
||||
[`${LABEL_PREFIX}/server-id`]: server.id,
|
||||
}
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: 'minecraft',
|
||||
image: 'itzg/minecraft-server',
|
||||
name: "minecraft",
|
||||
image: "itzg/minecraft-server",
|
||||
ports: [
|
||||
{
|
||||
containerPort: 25565,
|
||||
name: 'minecraft',
|
||||
}
|
||||
name: "minecraft",
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: 'EULA',
|
||||
value: 'TRUE',
|
||||
name: "EULA",
|
||||
value: "TRUE",
|
||||
},
|
||||
{
|
||||
name: 'TYPE',
|
||||
value: 'VANILLA',
|
||||
name: "TYPE",
|
||||
value: "VANILLA",
|
||||
},
|
||||
{
|
||||
name: 'MEMORY',
|
||||
value: calculateJavaMemory(server.memory || '1G', 0.8),
|
||||
name: "MEMORY",
|
||||
value: calculateJavaMemory(server.memory || "1G", 0.8),
|
||||
},
|
||||
{
|
||||
name: 'OPS',
|
||||
value: '',
|
||||
name: "OPS",
|
||||
value: "",
|
||||
},
|
||||
{
|
||||
name: 'OVERRIDE_SERVER_PROPERTIES',
|
||||
value: 'true',
|
||||
name: "OVERRIDE_SERVER_PROPERTIES",
|
||||
value: "true",
|
||||
},
|
||||
{
|
||||
name: 'ENABLE_RCON',
|
||||
value: 'false',
|
||||
name: "ENABLE_RCON",
|
||||
value: "false",
|
||||
},
|
||||
...(server.env_variables || []).map(ev => ({
|
||||
...(server.env_variables || []).map((ev) => ({
|
||||
name: ev.key,
|
||||
value: ev.value,
|
||||
})),
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: 'data',
|
||||
mountPath: '/data',
|
||||
name: "data",
|
||||
mountPath: "/data",
|
||||
},
|
||||
{
|
||||
name: 'config',
|
||||
mountPath: '/config',
|
||||
}
|
||||
name: "config",
|
||||
mountPath: "/config",
|
||||
},
|
||||
],
|
||||
readinessProbe: {
|
||||
tcpSocket: {
|
||||
@@ -318,36 +318,36 @@ async function createStatefulSet(
|
||||
limits: {
|
||||
memory: convertToK8sFormat(server.memory),
|
||||
cpu: "500m",
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'config',
|
||||
name: "config",
|
||||
configMap: {
|
||||
name: `${serverName}-config`,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
volumeClaimTemplates: [
|
||||
{
|
||||
metadata: {
|
||||
name: 'data',
|
||||
name: "data",
|
||||
},
|
||||
spec: {
|
||||
accessModes: ['ReadWriteOnce'],
|
||||
accessModes: ["ReadWriteOnce"],
|
||||
resources: {
|
||||
requests: {
|
||||
storage: '1Gi',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
storage: "1Gi",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { KubernetesClient } from '../utils/k8s-client';
|
||||
import { registerRBACResources } from '../utils/rbac-registrar';
|
||||
import { setupCRDRegistration } from '../utils/crd-registrar';
|
||||
import { NAMESPACE } from '../config/constants';
|
||||
import { PrismaClient } from '@minikura/db';
|
||||
import { dotenvLoad } from 'dotenv-mono';
|
||||
import { KubernetesClient } from "../utils/k8s-client";
|
||||
import { registerRBACResources } from "../utils/rbac-registrar";
|
||||
import { setupCRDRegistration } from "../utils/crd-registrar";
|
||||
import { NAMESPACE } from "../config/constants";
|
||||
import { PrismaClient } from "@minikura/db";
|
||||
import { dotenvLoad } from "dotenv-mono";
|
||||
|
||||
dotenvLoad();
|
||||
|
||||
async function main() {
|
||||
console.log('Starting to apply TypeScript-defined CRDs to Kubernetes cluster...');
|
||||
console.log("Starting to apply TypeScript-defined CRDs to Kubernetes cluster...");
|
||||
|
||||
try {
|
||||
const k8sClient = KubernetesClient.getInstance();
|
||||
@@ -16,14 +16,14 @@ async function main() {
|
||||
|
||||
await registerRBACResources(k8sClient);
|
||||
|
||||
console.log('Registering Custom Resource Definitions...');
|
||||
console.log("Registering Custom Resource Definitions...");
|
||||
const prisma = new PrismaClient();
|
||||
await setupCRDRegistration(prisma, k8sClient, NAMESPACE);
|
||||
|
||||
console.log('Successfully applied all resources to Kubernetes cluster');
|
||||
console.log("Successfully applied all resources to Kubernetes cluster");
|
||||
process.exit(0);
|
||||
} catch (error: any) {
|
||||
console.error('Failed to apply resources:', error.message);
|
||||
console.error("Failed to apply resources:", error.message);
|
||||
if (error.stack) {
|
||||
console.error(error.stack);
|
||||
}
|
||||
@@ -31,7 +31,7 @@ async function main() {
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error('Unhandled error:', error);
|
||||
main().catch((error) => {
|
||||
console.error("Unhandled error:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -3,8 +3,8 @@ import type {
|
||||
ReverseProxyServerType,
|
||||
Server as PrismaServer,
|
||||
ReverseProxyServer as PrismaReverseProxyServer,
|
||||
CustomEnvironmentVariable
|
||||
} from '@minikura/db';
|
||||
CustomEnvironmentVariable,
|
||||
} from "@minikura/db";
|
||||
|
||||
// Base interface
|
||||
export interface CustomResource {
|
||||
@@ -19,17 +19,23 @@ export interface CustomResource {
|
||||
};
|
||||
}
|
||||
|
||||
export type ServerConfig = Pick<PrismaServer, 'id' | 'description' | 'type' | 'listen_port' | 'memory'> & {
|
||||
export type ServerConfig = Pick<
|
||||
PrismaServer,
|
||||
"id" | "description" | "type" | "listen_port" | "memory"
|
||||
> & {
|
||||
apiKey: string;
|
||||
env_variables?: Array<Pick<CustomEnvironmentVariable, 'key' | 'value'>>;
|
||||
env_variables?: Array<Pick<CustomEnvironmentVariable, "key" | "value">>;
|
||||
};
|
||||
|
||||
export type MinecraftServerSpec = Pick<PrismaServer, 'id' | 'description' | 'type' | 'listen_port' | 'memory'> & {
|
||||
environmentVariables?: Array<Pick<CustomEnvironmentVariable, 'key' | 'value'>>;
|
||||
export type MinecraftServerSpec = Pick<
|
||||
PrismaServer,
|
||||
"id" | "description" | "type" | "listen_port" | "memory"
|
||||
> & {
|
||||
environmentVariables?: Array<Pick<CustomEnvironmentVariable, "key" | "value">>;
|
||||
};
|
||||
|
||||
export interface MinecraftServerStatus {
|
||||
phase: 'Pending' | 'Running' | 'Failed';
|
||||
phase: "Pending" | "Running" | "Failed";
|
||||
message?: string;
|
||||
apiKey?: string;
|
||||
internalId?: string;
|
||||
@@ -45,23 +51,26 @@ export interface MinecraftServerCRD extends CustomResource {
|
||||
|
||||
export type ReverseProxyConfig = Pick<
|
||||
PrismaReverseProxyServer,
|
||||
'id' | 'description' | 'external_address' | 'external_port' | 'listen_port' | 'type' | 'memory'
|
||||
"id" | "description" | "external_address" | "external_port" | "listen_port" | "type" | "memory"
|
||||
> & {
|
||||
apiKey: string;
|
||||
env_variables?: Array<Pick<CustomEnvironmentVariable, 'key' | 'value'>>;
|
||||
env_variables?: Array<Pick<CustomEnvironmentVariable, "key" | "value">>;
|
||||
};
|
||||
|
||||
export type ReverseProxyServerSpec = Partial<
|
||||
Pick<PrismaReverseProxyServer, 'id' | 'description' | 'external_address' | 'external_port' | 'listen_port' | 'type' | 'memory'>
|
||||
Pick<
|
||||
PrismaReverseProxyServer,
|
||||
"id" | "description" | "external_address" | "external_port" | "listen_port" | "type" | "memory"
|
||||
>
|
||||
> & {
|
||||
id: string;
|
||||
external_address: string;
|
||||
external_port: number;
|
||||
environmentVariables?: Array<Pick<CustomEnvironmentVariable, 'key' | 'value'>>;
|
||||
environmentVariables?: Array<Pick<CustomEnvironmentVariable, "key" | "value">>;
|
||||
};
|
||||
|
||||
export interface ReverseProxyServerStatus {
|
||||
phase: 'Pending' | 'Running' | 'Failed';
|
||||
phase: "Pending" | "Running" | "Failed";
|
||||
message?: string;
|
||||
apiKey?: string;
|
||||
internalId?: string;
|
||||
@@ -73,4 +82,4 @@ export interface ReverseProxyServerCRD extends CustomResource {
|
||||
status?: ReverseProxyServerStatus;
|
||||
}
|
||||
|
||||
export type EnvironmentVariable = Pick<CustomEnvironmentVariable, 'key' | 'value'>;
|
||||
export type EnvironmentVariable = Pick<CustomEnvironmentVariable, "key" | "value">;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { PrismaClient } from '@minikura/db';
|
||||
import type { Server, ReverseProxyServer, CustomEnvironmentVariable } from '@minikura/db';
|
||||
import { KubernetesClient } from './k8s-client';
|
||||
import { API_GROUP, API_VERSION, LABEL_PREFIX } from '../config/constants';
|
||||
import { MINECRAFT_SERVER_CRD } from '../crds/server';
|
||||
import { REVERSE_PROXY_SERVER_CRD } from '../crds/reverseProxy';
|
||||
import type { PrismaClient } from "@minikura/db";
|
||||
import type { Server, ReverseProxyServer, CustomEnvironmentVariable } from "@minikura/db";
|
||||
import type { KubernetesClient } from "./k8s-client";
|
||||
import { API_GROUP, API_VERSION, LABEL_PREFIX } from "../config/constants";
|
||||
import { MINECRAFT_SERVER_CRD } from "../crds/server";
|
||||
import { REVERSE_PROXY_SERVER_CRD } from "../crds/reverseProxy";
|
||||
|
||||
/**
|
||||
* Sets up the CRD registration and starts a reflector to sync database state to CRDs
|
||||
@@ -24,7 +24,7 @@ async function registerCRDs(k8sClient: KubernetesClient): Promise<void> {
|
||||
try {
|
||||
const apiExtensionsClient = k8sClient.getApiExtensionsApi();
|
||||
|
||||
console.log('Registering CRDs...');
|
||||
console.log("Registering CRDs...");
|
||||
|
||||
try {
|
||||
await apiExtensionsClient.createCustomResourceDefinition(MINECRAFT_SERVER_CRD);
|
||||
@@ -32,9 +32,9 @@ async function registerCRDs(k8sClient: KubernetesClient): Promise<void> {
|
||||
} catch (error: any) {
|
||||
if (error.response?.statusCode === 409) {
|
||||
// TODO: Handle conflict
|
||||
console.log('MinecraftServer CRD already exists');
|
||||
console.log("MinecraftServer CRD already exists");
|
||||
} else {
|
||||
console.error('Error creating MinecraftServer CRD:', error);
|
||||
console.error("Error creating MinecraftServer CRD:", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,14 +43,14 @@ async function registerCRDs(k8sClient: KubernetesClient): Promise<void> {
|
||||
console.log(`ReverseProxyServer CRD created successfully (${API_GROUP}/${API_VERSION})`);
|
||||
} catch (error: any) {
|
||||
if (error.response?.statusCode === 409) {
|
||||
// TODO: Handle conflict
|
||||
console.log('ReverseProxyServer CRD already exists');
|
||||
// TODO: Handle conflict
|
||||
console.log("ReverseProxyServer CRD already exists");
|
||||
} else {
|
||||
console.error('Error creating ReverseProxyServer CRD:', error);
|
||||
console.error("Error creating ReverseProxyServer CRD:", error);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error registering CRDs:', error);
|
||||
console.error("Error registering CRDs:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -69,17 +69,27 @@ async function startCRDReflector(
|
||||
const reflectedMinecraftServers = new Map<string, string>(); // DB ID -> CR name
|
||||
const reflectedReverseProxyServers = new Map<string, string>(); // DB ID -> CR name
|
||||
|
||||
console.log('Starting CRD reflector...');
|
||||
console.log("Starting CRD reflector...");
|
||||
|
||||
// Initial sync to create CRs that reflect the DB state
|
||||
await syncDBtoCRDs(prisma, customObjectsApi, namespace,
|
||||
reflectedMinecraftServers, reflectedReverseProxyServers);
|
||||
await syncDBtoCRDs(
|
||||
prisma,
|
||||
customObjectsApi,
|
||||
namespace,
|
||||
reflectedMinecraftServers,
|
||||
reflectedReverseProxyServers
|
||||
);
|
||||
|
||||
// Polling interval to check for changes in the DB
|
||||
// TODO: Make this listener instead
|
||||
setInterval(async () => {
|
||||
await syncDBtoCRDs(prisma, customObjectsApi, namespace,
|
||||
reflectedMinecraftServers, reflectedReverseProxyServers);
|
||||
await syncDBtoCRDs(
|
||||
prisma,
|
||||
customObjectsApi,
|
||||
namespace,
|
||||
reflectedMinecraftServers,
|
||||
reflectedReverseProxyServers
|
||||
);
|
||||
}, 30 * 1000);
|
||||
}
|
||||
|
||||
@@ -96,7 +106,12 @@ async function syncDBtoCRDs(
|
||||
try {
|
||||
console.log(`[${new Date().toISOString()}] Starting CRD sync operation...`);
|
||||
await syncMinecraftServers(prisma, customObjectsApi, namespace, reflectedMinecraftServers);
|
||||
await syncReverseProxyServers(prisma, customObjectsApi, namespace, reflectedReverseProxyServers);
|
||||
await syncReverseProxyServers(
|
||||
prisma,
|
||||
customObjectsApi,
|
||||
namespace,
|
||||
reflectedReverseProxyServers
|
||||
);
|
||||
console.log(`[${new Date().toISOString()}] CRD sync operation completed`);
|
||||
} catch (error) {
|
||||
console.error(`[${new Date().toISOString()}] Error syncing database to CRDs:`, error);
|
||||
@@ -121,11 +136,11 @@ async function syncMinecraftServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'minecraftservers'
|
||||
"minecraftservers"
|
||||
);
|
||||
existingCRs = (response.body as any).items || [];
|
||||
} catch (error) {
|
||||
console.error('Error listing MinecraftServer CRs:', error);
|
||||
console.error("Error listing MinecraftServer CRs:", error);
|
||||
// TODO: Potentially better error handling here
|
||||
// For now, continue anyway - it might just be that none exist yet
|
||||
}
|
||||
@@ -155,41 +170,41 @@ async function syncMinecraftServers(
|
||||
|
||||
// Build the CR object
|
||||
const serverCR: {
|
||||
apiVersion: string,
|
||||
kind: string,
|
||||
apiVersion: string;
|
||||
kind: string;
|
||||
metadata: {
|
||||
name: string,
|
||||
namespace: string,
|
||||
annotations: Record<string, string>,
|
||||
resourceVersion?: string
|
||||
},
|
||||
spec: any,
|
||||
status: any
|
||||
name: string;
|
||||
namespace: string;
|
||||
annotations: Record<string, string>;
|
||||
resourceVersion?: string;
|
||||
};
|
||||
spec: any;
|
||||
status: any;
|
||||
} = {
|
||||
apiVersion: `${API_GROUP}/${API_VERSION}`,
|
||||
kind: 'MinecraftServer',
|
||||
kind: "MinecraftServer",
|
||||
metadata: {
|
||||
name: crName,
|
||||
namespace: namespace,
|
||||
annotations: {
|
||||
[`${LABEL_PREFIX}/database-managed`]: 'true',
|
||||
[`${LABEL_PREFIX}/last-synced`]: new Date().toISOString()
|
||||
}
|
||||
[`${LABEL_PREFIX}/database-managed`]: "true",
|
||||
[`${LABEL_PREFIX}/last-synced`]: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
id: server.id,
|
||||
description: server.description,
|
||||
listen_port: server.listen_port,
|
||||
type: server.type,
|
||||
memory: server.memory
|
||||
memory: server.memory,
|
||||
},
|
||||
status: {
|
||||
phase: 'Running',
|
||||
message: 'Managed by database',
|
||||
phase: "Running",
|
||||
message: "Managed by database",
|
||||
internalId: server.id,
|
||||
apiKey: '[REDACTED]', // Don't expose actual API key
|
||||
lastSyncedAt: new Date().toISOString()
|
||||
}
|
||||
apiKey: "[REDACTED]", // Don't expose actual API key
|
||||
lastSyncedAt: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -205,7 +220,7 @@ async function syncMinecraftServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'minecraftservers',
|
||||
"minecraftservers",
|
||||
crName
|
||||
);
|
||||
|
||||
@@ -220,7 +235,7 @@ async function syncMinecraftServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'minecraftservers',
|
||||
"minecraftservers",
|
||||
crName,
|
||||
serverCR
|
||||
);
|
||||
@@ -234,7 +249,7 @@ async function syncMinecraftServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'minecraftservers',
|
||||
"minecraftservers",
|
||||
serverCR
|
||||
);
|
||||
console.log(`Created MinecraftServer CR ${crName} for server ${server.id}`);
|
||||
@@ -249,13 +264,13 @@ async function syncMinecraftServers(
|
||||
|
||||
// Delete CRs for servers that no longer exist
|
||||
for (const [dbId, crName] of existingCRMap.entries()) {
|
||||
if (!servers.some(s => s.id === dbId)) {
|
||||
if (!servers.some((s) => s.id === dbId)) {
|
||||
try {
|
||||
await customObjectsApi.deleteNamespacedCustomObject(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'minecraftservers',
|
||||
"minecraftservers",
|
||||
crName
|
||||
);
|
||||
console.log(`Deleted MinecraftServer CR ${crName} for removed server ID ${dbId}`);
|
||||
@@ -265,7 +280,7 @@ async function syncMinecraftServers(
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error syncing Minecraft servers to CRDs:', error);
|
||||
console.error("Error syncing Minecraft servers to CRDs:", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,8 +296,8 @@ async function syncReverseProxyServers(
|
||||
try {
|
||||
const proxies = await prisma.reverseProxyServer.findMany({
|
||||
include: {
|
||||
env_variables: true
|
||||
}
|
||||
env_variables: true,
|
||||
},
|
||||
});
|
||||
|
||||
let existingCRs: any[] = [];
|
||||
@@ -291,11 +306,11 @@ async function syncReverseProxyServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'reverseproxyservers'
|
||||
"reverseproxyservers"
|
||||
);
|
||||
existingCRs = (response.body as any).items || [];
|
||||
} catch (error) {
|
||||
console.error('Error listing ReverseProxyServer CRs:', error);
|
||||
console.error("Error listing ReverseProxyServer CRs:", error);
|
||||
// TODO: Potentially better error handling here
|
||||
// For now, continue anyway - it might just be that none exist yet
|
||||
}
|
||||
@@ -325,26 +340,26 @@ async function syncReverseProxyServers(
|
||||
|
||||
// Build the CR object
|
||||
const proxyCR: {
|
||||
apiVersion: string,
|
||||
kind: string,
|
||||
apiVersion: string;
|
||||
kind: string;
|
||||
metadata: {
|
||||
name: string,
|
||||
namespace: string,
|
||||
annotations: Record<string, string>,
|
||||
resourceVersion?: string
|
||||
},
|
||||
spec: any,
|
||||
status: any
|
||||
name: string;
|
||||
namespace: string;
|
||||
annotations: Record<string, string>;
|
||||
resourceVersion?: string;
|
||||
};
|
||||
spec: any;
|
||||
status: any;
|
||||
} = {
|
||||
apiVersion: `${API_GROUP}/${API_VERSION}`,
|
||||
kind: 'ReverseProxyServer',
|
||||
kind: "ReverseProxyServer",
|
||||
metadata: {
|
||||
name: crName,
|
||||
namespace: namespace,
|
||||
annotations: {
|
||||
[`${LABEL_PREFIX}/database-managed`]: 'true',
|
||||
[`${LABEL_PREFIX}/last-synced`]: new Date().toISOString()
|
||||
}
|
||||
[`${LABEL_PREFIX}/database-managed`]: "true",
|
||||
[`${LABEL_PREFIX}/last-synced`]: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
id: proxy.id,
|
||||
@@ -354,18 +369,18 @@ async function syncReverseProxyServers(
|
||||
listen_port: proxy.listen_port,
|
||||
type: proxy.type,
|
||||
memory: proxy.memory,
|
||||
environmentVariables: proxy.env_variables?.map(ev => ({
|
||||
environmentVariables: proxy.env_variables?.map((ev) => ({
|
||||
key: ev.key,
|
||||
value: ev.value
|
||||
}))
|
||||
value: ev.value,
|
||||
})),
|
||||
},
|
||||
status: {
|
||||
phase: 'Running',
|
||||
message: 'Managed by database',
|
||||
phase: "Running",
|
||||
message: "Managed by database",
|
||||
internalId: proxy.id,
|
||||
apiKey: '[REDACTED]', // Don't expose actual API key
|
||||
lastSyncedAt: new Date().toISOString()
|
||||
}
|
||||
apiKey: "[REDACTED]", // Don't expose actual API key
|
||||
lastSyncedAt: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -381,7 +396,7 @@ async function syncReverseProxyServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'reverseproxyservers',
|
||||
"reverseproxyservers",
|
||||
crName
|
||||
);
|
||||
|
||||
@@ -396,7 +411,7 @@ async function syncReverseProxyServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'reverseproxyservers',
|
||||
"reverseproxyservers",
|
||||
crName,
|
||||
proxyCR
|
||||
);
|
||||
@@ -410,7 +425,7 @@ async function syncReverseProxyServers(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'reverseproxyservers',
|
||||
"reverseproxyservers",
|
||||
proxyCR
|
||||
);
|
||||
console.log(`Created ReverseProxyServer CR ${crName} for proxy ${proxy.id}`);
|
||||
@@ -425,13 +440,13 @@ async function syncReverseProxyServers(
|
||||
|
||||
// Delete CRs for proxies that no longer exist
|
||||
for (const [dbId, crName] of existingCRMap.entries()) {
|
||||
if (!proxies.some(p => p.id === dbId)) {
|
||||
if (!proxies.some((p) => p.id === dbId)) {
|
||||
try {
|
||||
await customObjectsApi.deleteNamespacedCustomObject(
|
||||
API_GROUP,
|
||||
API_VERSION,
|
||||
namespace,
|
||||
'reverseproxyservers',
|
||||
"reverseproxyservers",
|
||||
crName
|
||||
);
|
||||
console.log(`Deleted ReverseProxyServer CR ${crName} for removed proxy ID ${dbId}`);
|
||||
@@ -441,6 +456,6 @@ async function syncReverseProxyServers(
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error syncing Reverse Proxy servers to CRDs:', error);
|
||||
console.error("Error syncing Reverse Proxy servers to CRDs:", error);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as k8s from '@kubernetes/client-node';
|
||||
import { SKIP_TLS_VERIFY, NAMESPACE } from '../config/constants';
|
||||
import * as k8s from "@kubernetes/client-node";
|
||||
import { SKIP_TLS_VERIFY, NAMESPACE } from "../config/constants";
|
||||
|
||||
export class KubernetesClient {
|
||||
private static instance: KubernetesClient;
|
||||
@@ -12,8 +12,8 @@ export class KubernetesClient {
|
||||
|
||||
private constructor() {
|
||||
if (SKIP_TLS_VERIFY) {
|
||||
console.log('Disabling TLS certificate validation');
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||
console.log("Disabling TLS certificate validation");
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||
}
|
||||
|
||||
this.kc = new k8s.KubeConfig();
|
||||
@@ -31,23 +31,23 @@ export class KubernetesClient {
|
||||
private setupConfig(): void {
|
||||
try {
|
||||
this.kc.loadFromDefault();
|
||||
console.log('Loaded Kubernetes config from default location');
|
||||
console.log("Loaded Kubernetes config from default location");
|
||||
} catch (err) {
|
||||
console.warn('Failed to load Kubernetes config from default location:', err);
|
||||
console.warn("Failed to load Kubernetes config from default location:", err);
|
||||
}
|
||||
|
||||
// Running in a cluster, try to load in-cluster config
|
||||
if (!this.kc.getCurrentContext()) {
|
||||
try {
|
||||
this.kc.loadFromCluster();
|
||||
console.log('Loaded Kubernetes config from cluster');
|
||||
console.log("Loaded Kubernetes config from cluster");
|
||||
} catch (err) {
|
||||
console.warn('Failed to load Kubernetes config from cluster:', err);
|
||||
console.warn("Failed to load Kubernetes config from cluster:", err);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.kc.getCurrentContext()) {
|
||||
throw new Error('Failed to setup Kubernetes client - no valid configuration found');
|
||||
throw new Error("Failed to setup Kubernetes client - no valid configuration found");
|
||||
}
|
||||
|
||||
const currentCluster = this.kc.getCurrentCluster();
|
||||
|
||||
@@ -30,7 +30,7 @@ export function convertToK8sFormat(memoryString: string): string {
|
||||
|
||||
const [, valueStr, unit] = match;
|
||||
|
||||
if (unit.toUpperCase() === 'G') {
|
||||
if (unit.toUpperCase() === "G") {
|
||||
return `${valueStr}Gi`;
|
||||
} else {
|
||||
return `${valueStr}Mi`;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { KubernetesClient } from './k8s-client';
|
||||
import type { KubernetesClient } from "./k8s-client";
|
||||
import {
|
||||
minikuraNamespace,
|
||||
minikuraServiceAccount,
|
||||
minikuraClusterRole,
|
||||
minikuraClusterRoleBinding,
|
||||
minikuraOperatorDeployment
|
||||
} from '../crds/rbac';
|
||||
import fetch from 'node-fetch';
|
||||
minikuraOperatorDeployment,
|
||||
} from "../crds/rbac";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
/**
|
||||
* Registers all RBAC resources required
|
||||
@@ -14,16 +14,16 @@ import fetch from 'node-fetch';
|
||||
*/
|
||||
export async function registerRBACResources(k8sClient: KubernetesClient): Promise<void> {
|
||||
try {
|
||||
console.log('Starting RBAC resources registration...');
|
||||
console.log("Starting RBAC resources registration...");
|
||||
|
||||
await registerNamespace(k8sClient);
|
||||
await registerServiceAccount(k8sClient);
|
||||
await registerClusterRole(k8sClient);
|
||||
await registerClusterRoleBinding(k8sClient);
|
||||
|
||||
console.log('RBAC resources registration completed successfully');
|
||||
console.log("RBAC resources registration completed successfully");
|
||||
} catch (error: any) {
|
||||
console.error('Error registering RBAC resources:', error.message);
|
||||
console.error("Error registering RBAC resources:", error.message);
|
||||
if (error.response) {
|
||||
console.error(`Response status: ${error.response.statusCode}`);
|
||||
console.error(`Response body: ${JSON.stringify(error.response.body)}`);
|
||||
@@ -82,19 +82,22 @@ async function registerClusterRole(k8sClient: KubernetesClient): Promise<void> {
|
||||
// Get cluster URL
|
||||
const cluster = kc.getCurrentCluster();
|
||||
if (!cluster) {
|
||||
throw new Error('No active cluster found in KubeConfig');
|
||||
throw new Error("No active cluster found in KubeConfig");
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${cluster.server}/apis/rbac.authorization.k8s.io/v1/clusterroles`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(opts as any).headers
|
||||
},
|
||||
body: JSON.stringify(minikuraClusterRole),
|
||||
agent: (opts as any).agent
|
||||
});
|
||||
const response = await fetch(
|
||||
`${cluster.server}/apis/rbac.authorization.k8s.io/v1/clusterroles`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(opts as any).headers,
|
||||
},
|
||||
body: JSON.stringify(minikuraClusterRole),
|
||||
agent: (opts as any).agent,
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
console.log(`Created cluster role ${minikuraClusterRole.metadata.name}`);
|
||||
@@ -102,11 +105,13 @@ async function registerClusterRole(k8sClient: KubernetesClient): Promise<void> {
|
||||
console.log(`Cluster role ${minikuraClusterRole.metadata.name} already exists`);
|
||||
} else {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to create cluster role: ${response.status} ${response.statusText} - ${text}`);
|
||||
throw new Error(
|
||||
`Failed to create cluster role: ${response.status} ${response.statusText} - ${text}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
// If the error message contains "already exists", that's OK
|
||||
if (error.message?.includes('already exists') || error.message?.includes('409')) {
|
||||
if (error.message?.includes("already exists") || error.message?.includes("409")) {
|
||||
console.log(`Cluster role ${minikuraClusterRole.metadata.name} already exists`);
|
||||
} else {
|
||||
throw error;
|
||||
@@ -131,36 +136,45 @@ async function registerClusterRoleBinding(k8sClient: KubernetesClient): Promise<
|
||||
// Get cluster URL
|
||||
const cluster = kc.getCurrentCluster();
|
||||
if (!cluster) {
|
||||
throw new Error('No active cluster found in KubeConfig');
|
||||
throw new Error("No active cluster found in KubeConfig");
|
||||
}
|
||||
|
||||
// Create the cluster role binding
|
||||
const { default: fetch } = await import('node-fetch');
|
||||
const { default: fetch } = await import("node-fetch");
|
||||
|
||||
try {
|
||||
const response = await fetch(`${cluster.server}/apis/rbac.authorization.k8s.io/v1/clusterrolebindings`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(opts as any).headers
|
||||
},
|
||||
body: JSON.stringify(minikuraClusterRoleBinding),
|
||||
agent: (opts as any).agent
|
||||
});
|
||||
const response = await fetch(
|
||||
`${cluster.server}/apis/rbac.authorization.k8s.io/v1/clusterrolebindings`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(opts as any).headers,
|
||||
},
|
||||
body: JSON.stringify(minikuraClusterRoleBinding),
|
||||
agent: (opts as any).agent,
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
console.log(`Created cluster role binding ${minikuraClusterRoleBinding.metadata.name}`);
|
||||
} else if (response.status === 409) {
|
||||
console.log(`Cluster role binding ${minikuraClusterRoleBinding.metadata.name} already exists`);
|
||||
console.log(
|
||||
`Cluster role binding ${minikuraClusterRoleBinding.metadata.name} already exists`
|
||||
);
|
||||
} else {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to create cluster role binding: ${response.status} ${response.statusText} - ${text}`);
|
||||
throw new Error(
|
||||
`Failed to create cluster role binding: ${response.status} ${response.statusText} - ${text}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
// If the error message contains "already exists"
|
||||
// TODO: Potentially better error handling here
|
||||
if (error.message?.includes('already exists') || error.message?.includes('409')) {
|
||||
console.log(`Cluster role binding ${minikuraClusterRoleBinding.metadata.name} already exists`);
|
||||
if (error.message?.includes("already exists") || error.message?.includes("409")) {
|
||||
console.log(
|
||||
`Cluster role binding ${minikuraClusterRoleBinding.metadata.name} already exists`
|
||||
);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
@@ -182,28 +196,27 @@ export async function registerOperatorDeployment(
|
||||
try {
|
||||
// Replace the registry URL placeholder, for future use
|
||||
const deployment = JSON.parse(
|
||||
JSON.stringify(minikuraOperatorDeployment).replace('${REGISTRY_URL}', registryUrl)
|
||||
JSON.stringify(minikuraOperatorDeployment).replace("${REGISTRY_URL}", registryUrl)
|
||||
);
|
||||
|
||||
const appsApi = k8sClient.getAppsApi();
|
||||
await appsApi.createNamespacedDeployment(
|
||||
deployment.metadata.namespace,
|
||||
deployment
|
||||
);
|
||||
await appsApi.createNamespacedDeployment(deployment.metadata.namespace, deployment);
|
||||
console.log(`Created deployment ${deployment.metadata.name}`);
|
||||
} catch (error: any) {
|
||||
if (error.response?.statusCode === 409) {
|
||||
console.log(`Deployment ${minikuraOperatorDeployment.metadata.name} already exists`);
|
||||
// Update the deployment if it already exists
|
||||
const deployment = JSON.parse(
|
||||
JSON.stringify(minikuraOperatorDeployment).replace('${REGISTRY_URL}', registryUrl)
|
||||
JSON.stringify(minikuraOperatorDeployment).replace("${REGISTRY_URL}", registryUrl)
|
||||
);
|
||||
|
||||
await k8sClient.getAppsApi().replaceNamespacedDeployment(
|
||||
deployment.metadata.name,
|
||||
deployment.metadata.namespace,
|
||||
deployment
|
||||
);
|
||||
await k8sClient
|
||||
.getAppsApi()
|
||||
.replaceNamespacedDeployment(
|
||||
deployment.metadata.name,
|
||||
deployment.metadata.namespace,
|
||||
deployment
|
||||
);
|
||||
console.log(`Updated deployment ${deployment.metadata.name}`);
|
||||
} else {
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user