From 3e053c1ae7e8ee4a2b9a67ed84f41eb0c415ead1 Mon Sep 17 00:00:00 2001 From: biersoeckli Date: Thu, 7 Nov 2024 16:49:20 +0000 Subject: [PATCH] added redirect for ingress --- .../20241107155300_migration/migration.sql | 33 +++ prisma/schema.prisma | 13 +- setup.sh | 113 +++++++++ .../[tabName]/domains/domain-edit-overlay.tsx | 2 + .../project/app/[tabName]/domains/domains.tsx | 2 + .../app/[tabName]/environment/env-edit.tsx | 2 +- .../storage/storage-edit-overlay.tsx | 2 +- .../app/[tabName]/storage/storages.tsx | 2 +- src/model/domain-edit.model.ts | 1 + src/model/generated-zod/appdomain.ts | 1 + src/server/adapter/kubernetes-api.adapter.ts | 11 + src/server/services/deployment.service.ts | 21 +- src/server/services/ingress.service.ts | 233 +++++++++++++----- src/server/services/pvc.service.ts | 16 +- 14 files changed, 367 insertions(+), 85 deletions(-) create mode 100644 prisma/migrations/20241107155300_migration/migration.sql create mode 100644 setup.sh diff --git a/prisma/migrations/20241107155300_migration/migration.sql b/prisma/migrations/20241107155300_migration/migration.sql new file mode 100644 index 0000000..9e85f4d --- /dev/null +++ b/prisma/migrations/20241107155300_migration/migration.sql @@ -0,0 +1,33 @@ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_AppDomain" ( + "id" TEXT NOT NULL PRIMARY KEY, + "hostname" TEXT NOT NULL, + "port" INTEGER NOT NULL, + "useSsl" BOOLEAN NOT NULL DEFAULT true, + "redirectHttps" BOOLEAN NOT NULL DEFAULT true, + "appId" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + CONSTRAINT "AppDomain_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_AppDomain" ("appId", "createdAt", "hostname", "id", "port", "updatedAt", "useSsl") SELECT "appId", "createdAt", "hostname", "id", "port", "updatedAt", "useSsl" FROM "AppDomain"; +DROP TABLE "AppDomain"; +ALTER TABLE "new_AppDomain" RENAME TO "AppDomain"; +CREATE UNIQUE INDEX "AppDomain_hostname_key" ON "AppDomain"("hostname"); +CREATE TABLE "new_AppVolume" ( + "id" TEXT NOT NULL PRIMARY KEY, + "containerMountPath" TEXT NOT NULL, + "size" INTEGER NOT NULL, + "accessMode" TEXT NOT NULL DEFAULT 'rwo', + "appId" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + CONSTRAINT "AppVolume_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_AppVolume" ("accessMode", "appId", "containerMountPath", "createdAt", "id", "size", "updatedAt") SELECT "accessMode", "appId", "containerMountPath", "createdAt", "id", "size", "updatedAt" FROM "AppVolume"; +DROP TABLE "AppVolume"; +ALTER TABLE "new_AppVolume" RENAME TO "AppVolume"; +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4878eb9..8b1c781 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -155,12 +155,13 @@ model App { } model AppDomain { - id String @id @default(uuid()) - hostname String @unique - port Int - useSsl Boolean @default(true) - appId String - app App @relation(fields: [appId], references: [id]) + id String @id @default(uuid()) + hostname String @unique + port Int + useSsl Boolean @default(true) + redirectHttps Boolean @default(true) + appId String + app App @relation(fields: [appId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..7ab3832 --- /dev/null +++ b/setup.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +wait_until_all_pods_running() { + + # Waits another 5 seconds to make sure all pods are registered for the first time. + sleep 5 + + while true; do + OUTPUT=$(sudo k3s kubectl get pods -A --no-headers 2>&1) + + # Checks if there are no resources found --> Kubernetes ist still starting up + if echo "$OUTPUT" | grep -q "No resources found"; then + echo "Kubernetes is still starting up..." + else + # Extracts the STATUS column from the kubectl output and filters out the values "Running" and "Completed". + STATUS=$(echo "$OUTPUT" | awk '{print $4}' | grep -vE '^(Running|Completed)$') + + # If the STATUS variable is empty, all pods are running and the loop can be exited. + if [ -z "$STATUS" ]; then + echo "Pods started successfully." + break + else + echo "Waiting for all pods to come online..." + fi + fi + + # Waits for X seconds before checking the pod status again. + sleep 10 + done + + # Waits another 5 seconds to make sure all pods are ready. + sleep 5 + + sudo kubectl get node + sudo kubectl get pods -A +} + +# Installation of k3s +curl -sfL https://get.k3s.io | sh - +# Todo: Check for Ready node, takes ~30 seconds +sudo k3s kubectl get node + +echo "Waiting for Kubernetes to start..." +wait_until_all_pods_running + +# Installation of Longhorn +sudo kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.6.0/deploy/longhorn.yaml +echo "Waiting for Longhorn to start..." +wait_until_all_pods_running + +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +# THIS MUST BE INSTALLED ON ALL NODES --> https://longhorn.io/docs/1.7.2/deploy/install/#installing-nfsv4-client +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +echo "Installing nfs-common..." +kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.7.2/deploy/prerequisite/longhorn-nfs-installation.yaml +wait_until_all_pods_running + +# Installation of Cert-Manager +sudo kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.1/cert-manager.yaml +echo "Waiting for Cert-Manager to start..." +wait_until_all_pods_running +sudo kubectl -n cert-manager get pod + +# add Cluster Issuer +cat < cluster-issuer.yaml +# Staging ClusterIssuer +apiVersion: cert-manager.io/v1 +kind: ClusterIssuer +metadata: + name: letsencrypt-staging + namespace: default +spec: + acme: + server: https://acme-staging-v02.api.letsencrypt.org/directory + email: test@ost.ch + privateKeySecretRef: + name: letsencrypt-staging + solvers: + - selector: {} + http01: + ingress: + class: traefik +--- +# Production ClusterIssuer +apiVersion: cert-manager.io/v1 +kind: ClusterIssuer +metadata: + name: letsencrypt-production + namespace: default +spec: + acme: + server: https://acme-v02.api.letsencrypt.org/directory + email: test@ost.ch + privateKeySecretRef: + name: letsencrypt-production + solvers: + - selector: {} + http01: + ingress: + class: traefik +EOF +sudo kubectl apply -f cluster-issuer.yaml +sudo kubectl get clusterissuer -o wide +rm cluster-issuer.yaml + +sudo kubectl get nodes + +# evaluate url to add node to cluster +joinTokenForOtherNodes=$(sudo cat /var/lib/rancher/k3s/server/node-token) +echo "To add a worker node to the cluster, run the following command on the worker node:" +echo "------------------------------------------------------------" +echo "curl -sfL https://get.k3s.io | K3S_URL=https://:6443 K3S_TOKEN=$joinTokenForOtherNodes sh -" +echo "------------------------------------------------------------" \ No newline at end of file diff --git a/src/app/project/app/[tabName]/domains/domain-edit-overlay.tsx b/src/app/project/app/[tabName]/domains/domain-edit-overlay.tsx index 5f16dfc..8d30a8c 100644 --- a/src/app/project/app/[tabName]/domains/domain-edit-overlay.tsx +++ b/src/app/project/app/[tabName]/domains/domain-edit-overlay.tsx @@ -54,6 +54,7 @@ export default function DialogEditDialog({ children, domain, appId }: { children FormUtils.mapValidationErrorsToForm(state, form); }, [state]); + const values = form.watch(); return ( <> @@ -102,6 +103,7 @@ export default function DialogEditDialog({ children, domain, appId }: { children /> + {values.useSsl && }

{state.message}

Save diff --git a/src/app/project/app/[tabName]/domains/domains.tsx b/src/app/project/app/[tabName]/domains/domains.tsx index 963c96c..a6eb57a 100644 --- a/src/app/project/app/[tabName]/domains/domains.tsx +++ b/src/app/project/app/[tabName]/domains/domains.tsx @@ -51,6 +51,7 @@ export default function DomainsList({ app }: { Name Port SSL + Redirect HTTP to HTTPS Action @@ -60,6 +61,7 @@ export default function DomainsList({ app }: { {domain.hostname} {domain.port} {domain.useSsl ? : } + {domain.useSsl && domain.redirectHttps ? : } diff --git a/src/app/project/app/[tabName]/environment/env-edit.tsx b/src/app/project/app/[tabName]/environment/env-edit.tsx index 8ac5846..07f6c3e 100644 --- a/src/app/project/app/[tabName]/environment/env-edit.tsx +++ b/src/app/project/app/[tabName]/environment/env-edit.tsx @@ -56,7 +56,7 @@ export default function EnvEdit({ app }: { Env Variables -