added createTraefikRedirectMiddlewareIfNotExist

This commit is contained in:
biersoeckli
2024-11-27 09:50:48 +00:00
parent 09e7c07af9
commit 4a9ec59fd0
5 changed files with 55 additions and 64 deletions

View File

@@ -1,23 +0,0 @@
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";
//import { Server } from 'socket.io'
// redirects to default route "general" for the app
export async function GET(request: NextRequest, response: NextResponse) {
/*
if (response.socket.server.io) {
console.log('Socket is already running')
} else {
console.log('Socket is initializing')
const io = new Server(res.socket.server)
res.socket.server.io = io
io.on('connection', socket => {
socket.on('input-change', msg => {
socket.broadcast.emit('update-input', msg)
})
})
}
res.end()*/
return redirect(`/project/app/overview?appId=${new URL(request.url).searchParams.get("appId")}`);
}

View File

@@ -22,7 +22,7 @@ export function CreateAppDialog({
}
const result = await Toast.fromAction(() => createApp(name, projectId));
if (result.status === "success") {
router.push(`/project/app?appId=${result.data.id}`);
router.push(`/project/app/${result.data.id}`);
return true;
}
return false;

View File

@@ -4,6 +4,7 @@ import { V1Ingress } from "@kubernetes/client-node";
import { KubeObjectNameUtils } from "../utils/kube-object-name.utils";
import { App, AppDomain } from "@prisma/client";
import { Constants } from "../../shared/utils/constants";
import ingressSetupService from "./setup-services/ingress-setup.service";
const traefikNamespace = 'kube-system';
@@ -52,7 +53,7 @@ class IngressService {
async createOrUpdateIngressForApp(app: AppExtendedModel) {
await this.createTraefikRedirectMiddlewareIfNotExist();
await ingressSetupService.createTraefikRedirectMiddlewareIfNotExist();
for (const domainObj of app.appDomains) {
await this.createIngress(app, domainObj);
@@ -122,45 +123,6 @@ class IngressService {
console.log(`Ingress ${ingressName} for domain ${hostname} successfully created.`);
}
}
async checkIfTraefikRedirectMiddlewareExists() {
const res = await k3s.customObjects.listNamespacedCustomObject(
'traefik.io', // group
'v1alpha1', // version
traefikNamespace, // namespace
'middlewares' // plural name of the custom resource
);
return (res.body as any) && (res.body as any)?.items && (res.body as any)?.items?.length > 0;
}
async createTraefikRedirectMiddlewareIfNotExist() {
if (await this.checkIfTraefikRedirectMiddlewareExists()) {
return;
}
const middlewareManifest = {
apiVersion: 'traefik.io/v1alpha1',
kind: 'Middleware',
metadata: {
name: 'redirect-to-https',
namespace: traefikNamespace,
},
spec: {
redirectScheme: {
scheme: 'https',
permanent: true,
}
},
};
await k3s.customObjects.createNamespacedCustomObject(
'traefik.io', // group
'v1alpha1', // version
traefikNamespace, // namespace
'middlewares', // plural name of the custom resource
middlewareManifest // object manifest
);
}
}
const ingressService = new IngressService();

View File

@@ -5,6 +5,7 @@ import { KubeObjectNameUtils } from "../utils/kube-object-name.utils";
import crypto from "crypto";
import { FancyConsoleUtils } from "../../shared/utils/fancy-console.utils";
import setupPodService from "./setup-services/setup-pod.service";
import ingressSetupService from "./setup-services/ingress-setup.service";
class QuickStackService {
@@ -57,6 +58,9 @@ class QuickStackService {
}
async createOrUpdateIngress(hostname: string) {
await ingressSetupService.createTraefikRedirectMiddlewareIfNotExist();
const ingressName = KubeObjectNameUtils.getIngressName(this.QUICKSTACK_NAMESPACE);
const existingIngresses = await k3s.network.listNamespacedIngress(this.QUICKSTACK_NAMESPACE);
const existingIngress = existingIngresses.body.items.find((item) => item.metadata?.name === ingressName);

View File

@@ -0,0 +1,48 @@
import k3s from "../../adapter/kubernetes-api.adapter";
const traefikNamespace = 'kube-system';
class IngressSetupService {
async checkIfTraefikRedirectMiddlewareExists() {
const res = await k3s.customObjects.listNamespacedCustomObject(
'traefik.io', // group
'v1alpha1', // version
traefikNamespace, // namespace
'middlewares' // plural name of the custom resource
);
return (res.body as any) && (res.body as any)?.items && (res.body as any)?.items?.length > 0;
}
async createTraefikRedirectMiddlewareIfNotExist() {
if (await this.checkIfTraefikRedirectMiddlewareExists()) {
return;
}
const middlewareManifest = {
apiVersion: 'traefik.io/v1alpha1',
kind: 'Middleware',
metadata: {
name: 'redirect-to-https',
namespace: traefikNamespace,
},
spec: {
redirectScheme: {
scheme: 'https',
permanent: true,
}
},
};
await k3s.customObjects.createNamespacedCustomObject(
'traefik.io', // group
'v1alpha1', // version
traefikNamespace, // namespace
'middlewares', // plural name of the custom resource
middlewareManifest // object manifest
);
}
}
const ingressSetupService = new IngressSetupService();
export default ingressSetupService;