From aef8105bb0ec28c398e15754fb9bfb75f8cf135a Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Mon, 13 May 2024 13:22:11 -0700 Subject: [PATCH] working orchestrator --- backend/orchestrator/service.yaml | 20 +++++---- backend/orchestrator/src/index.ts | 63 ++++++++++++++------------- frontend/app/(app)/code/[id]/page.tsx | 4 ++ frontend/components/editor/index.tsx | 1 + 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/backend/orchestrator/service.yaml b/backend/orchestrator/service.yaml index 1fe5792..4357e8e 100644 --- a/backend/orchestrator/service.yaml +++ b/backend/orchestrator/service.yaml @@ -15,17 +15,17 @@ spec: app: spec: volumes: - - name: workspace-volume + - name: projects-volume emptyDir: {} containers: - - name: runner + - name: sandbox image: ishaan1013/sandbox:latest ports: - containerPort: 4000 - - containerPort: 8000 + - containerPort: 3000 volumeMounts: - - name: workspace-volume - mountPath: /workspace + - name: projects-volume + mountPath: /projects resources: requests: cpu: "1" @@ -53,14 +53,16 @@ spec: port: 4000 targetPort: 4000 - protocol: TCP - name: s - port: 8000 - targetPort: 8000 + name: user + port: 3000 + targetPort: 3000 --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: + annotations: + kubernetes.io/ingress.class: "nginx" spec: ingressClassName: nginx rules: @@ -83,4 +85,4 @@ spec: service: name: port: - number: 8000 + number: 3000 diff --git a/backend/orchestrator/src/index.ts b/backend/orchestrator/src/index.ts index d25e6cf..e3c41c2 100644 --- a/backend/orchestrator/src/index.ts +++ b/backend/orchestrator/src/index.ts @@ -18,11 +18,11 @@ const port = process.env.PORT || 4001 app.use(express.json()) dotenv.config() -const corsOptions = { - origin: ['http://localhost:3000', 'https://s.ishaand.com', 'http://localhost:4000', /\.ws\.ishaand\.com$/], -} - +// const corsOptions = { +// origin: ['http://localhost:3000', 'https://s.ishaand.com', 'http://localhost:4000', /\.ws\.ishaand\.com$/], +// } // app.use(cors(corsOptions)) + app.use(cors()) const kubeconfig = new KubeConfig() @@ -35,14 +35,12 @@ if (process.env.NODE_ENV === "production") { caData: process.env.GKE_CLUSTER_CA_DATA, } ], - users: [ + users: [ { name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster', exec: { apiVersion: 'client.authentication.k8s.io/v1beta1', command: 'gke-gcloud-auth-plugin', - args: [], - env: null, installHint: 'Install gke-gcloud-auth-plugin for use with kubectl by following https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#install_plugin', interactiveMode: 'IfAvailable', provideClusterInfo: true @@ -51,17 +49,12 @@ if (process.env.NODE_ENV === "production") { ], contexts: [ { - name: 'docker-desktop', - cluster: 'docker-desktop', - user: 'docker-desktop' - }, - { - name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox', - cluster: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox', - user: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox' + name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster', + cluster: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster', + user: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster' } ], - currentContext: "gke_sylvan-epoch-422219-f9_us-central1_sandbox", + currentContext: "gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster" }); } kubeconfig.loadFromDefault() @@ -134,24 +127,32 @@ app.post("/start", async (req, res) => { } } + const createResource = async (api: any, method: string, manifest: any) => { + const { kind, metadata: { name } } = manifest; + if (!(await resourceExists(api, 'readNamespaced' + kind, name))) { + await api['createNamespaced' + kind](namespace, manifest); + console.log(`Created ${kind.toLowerCase()}`, name); + } else { + console.log(`${kind} ${name} already exists.`); + } + }; + const promises = kubeManifests.map(async (manifest) => { const { kind, metadata: { name } } = manifest - if (kind === "Deployment") - if (!(await resourceExists(appsV1Api, 'readNamespacedDeployment', name))) { - await appsV1Api.createNamespacedDeployment(namespace, manifest) - console.log("Made deploymnet") - } - else if (kind === "Service") - if (!(await resourceExists(coreV1Api, 'readNamespacedService', name))) { - await coreV1Api.createNamespacedService(namespace, manifest) - console.log("Made service") - } - else if (kind === "Ingress") - if (!(await resourceExists(networkingV1Api, 'readNamespacedIngress', name))) { - await networkingV1Api.createNamespacedIngress(namespace, manifest) - console.log("Made ingress") - } + console.log("Kind:", kind) + + switch (manifest.kind) { + case 'Deployment': + return createResource(appsV1Api, 'Deployment', manifest); + case 'Service': + return createResource(coreV1Api, 'Service', manifest); + case 'Ingress': + return createResource(networkingV1Api, 'Ingress', manifest); + default: + console.error("Unsupported kind:", manifest.kind); + return Promise.reject("Unsupported kind: " + manifest.kind); + } }) await Promise.all(promises) diff --git a/frontend/app/(app)/code/[id]/page.tsx b/frontend/app/(app)/code/[id]/page.tsx index 4c7c705..7a1e236 100644 --- a/frontend/app/(app)/code/[id]/page.tsx +++ b/frontend/app/(app)/code/[id]/page.tsx @@ -22,6 +22,10 @@ const getSandboxData = async (id: string) => { }; const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => { + if (!usersToSandboxes) { + return []; + } + const shared = await Promise.all( usersToSandboxes.map(async (user) => { const userRes = await fetch( diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index df99094..87ba45b 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -19,6 +19,7 @@ export default function Editor({ userData: User; sandboxData: Sandbox; }) { + console.log("userData", userData); const [isServerRunning, setIsServerRunning] = useState(false); const [didFail, setDidFail] = useState(false);