@@ -4,6 +4,7 @@ import { KubeConfig, CoreV1Api } from "@kubernetes/client-node";
44import * as k8s from "@kubernetes/client-node" ;
55import { getRandomName } from "./names" ;
66import cors from 'cors' ;
7+ import { Writable } from 'stream' ;
78import { addNewPod , addProjectToPod , redisClient , removePod } from './redis' ;
89import { getAllPods } from './redis' ;
910import { DOMAIN } from './config' ;
@@ -34,7 +35,7 @@ async function listPods(): Promise<string[]> {
3435 return res . items . filter ( pod => pod . status ?. phase === 'Running' || pod . status ?. phase === 'Pending' ) . filter ( pod => pod . metadata ?. name ) . map ( ( pod ) => pod . metadata ?. name as string ) ;
3536}
3637
37- async function createPod ( projectType : "NEXTJS" | "REACT_NATIVE" ) {
38+ async function createPod ( ) {
3839 const name = getRandomName ( ) ;
3940
4041 await k8sApi . createNamespacedPod ( { namespace : 'user-apps' , body : {
@@ -45,16 +46,30 @@ async function createPod(projectType: "NEXTJS" | "REACT_NATIVE") {
4546 } ,
4647 } ,
4748 spec : {
48- initContainers : [ {
49- name : 'init' ,
50- image : '100xdevs/code-server-base:latest' ,
51- command : [ "/bin/sh" , "-c" ] ,
52- args : [ `cp -r ${ PROJECT_TYPE_TO_BASE_FOLDER [ projectType ] } /app` ]
53- } ] ,
5449 containers : [ {
55- name,
56- image : '100xdevs/code-server-base:latest ' ,
50+ name : "code-server" ,
51+ image : '100xdevs/code-server-base:v2 ' ,
5752 ports : [ { containerPort : 8080 } , { containerPort : 8081 } ] ,
53+ } , {
54+ name : "ws-relayer" ,
55+ image : "100xdevs/ws-relayer:latest" ,
56+ ports : [ { containerPort : 9093 } ] ,
57+ } , {
58+ name : "worker" ,
59+ image : "100xdevs/worker:latest" ,
60+ ports : [ { containerPort : 9091 } ] ,
61+ env : [ {
62+ name : "WS_RELAYER_URL" ,
63+ value : `ws://localhost:9091` ,
64+ } , {
65+ name : "ANTHROPIC_API_KEY" ,
66+ valueFrom : {
67+ secretKeyRef : {
68+ name : "worker-secret" ,
69+ key : "ANTHROPIC_API_KEY" ,
70+ }
71+ }
72+ } ]
5873 } ] ,
5974 } ,
6075 } } ) ;
@@ -82,15 +97,59 @@ async function createPod(projectType: "NEXTJS" | "REACT_NATIVE") {
8297 ports : [ { port : 8080 , targetPort : 8081 , protocol : 'TCP' , name : 'preview' } ] ,
8398 } ,
8499 } } ) ;
100+
101+ await k8sApi . createNamespacedService ( { namespace : 'user-apps' , body : {
102+ metadata : {
103+ name : `worker-${ name } ` ,
104+ } ,
105+ spec : {
106+ selector : {
107+ app : name ,
108+ } ,
109+ ports : [ { port : 8080 , targetPort : 9091 , protocol : 'TCP' , name : 'preview' } ] ,
110+ } ,
111+ } } ) ;
85112}
86113
87- async function assignPodToProject ( projectId : string , projectType : "NEXTJS" | "REACT_NATIVE" ) {
114+ async function assignPodToProject ( projectId : string , projectType : "NEXTJS" | "REACT_NATIVE" | "REACT" ) {
88115 const pods = await getAllPods ( ) ;
89- const pod = Object . keys ( pods ) . find ( ( pod ) => pods [ pod ] === "empty" ) ;
116+ const pod = Object . keys ( pods ) . find ( ( pod ) => pods [ pod ] === "empty" ) ;
90117 if ( ! pod ) {
91- await createPod ( projectType ) ;
118+ await createPod ( ) ;
92119 return assignPodToProject ( projectId , projectType ) ;
93120 }
121+
122+ const exec = new k8s . Exec ( kc ) ;
123+ console . log ( pod ) ;
124+ let stdout = "" ;
125+ let stderr = "" ;
126+ console . log ( "executing" )
127+ exec . exec ( "user-apps" , pod , "code-server" , [ "/bin/sh" , "-c" , `mv ${ PROJECT_TYPE_TO_BASE_FOLDER [ projectType ] } /* /app` ] ,
128+ new Writable ( {
129+ write : ( chunk : Buffer , encoding : BufferEncoding , callback : ( ) => void ) => {
130+ stdout += chunk ;
131+ callback ( ) ;
132+ }
133+ } ) ,
134+ new Writable ( {
135+ write : ( chunk : Buffer , encoding : BufferEncoding , callback : ( ) => void ) => {
136+ stderr += chunk ;
137+ callback ( ) ;
138+ }
139+ } ) ,
140+ null ,
141+ false ,
142+ ( status ) => {
143+ console . log ( status ) ;
144+ console . log ( stdout ) ;
145+ console . log ( stderr ) ;
146+ }
147+ ) ;
148+
149+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
150+ console . log ( stdout ) ;
151+ console . log ( stderr ) ;
152+
94153 addProjectToPod ( projectId , pod ) ;
95154 console . log ( `Assigned project ${ projectId } to pod ${ pod } ` ) ;
96155 return pod ;
@@ -105,12 +164,17 @@ app.get("/worker/:projectId", async (req, res) => {
105164 } ) ;
106165
107166 if ( ! project ) {
108- return res . status ( 404 ) . json ( { error : "Project not found" } ) ;
167+ res . status ( 404 ) . json ( { error : "Project not found" } ) ;
168+ return ;
109169 }
110170
111- const pod = await assignPodToProject ( projectId , project . type ) ;
171+ const pod = await assignPodToProject ( projectId , "REACT" ) ; // project.type);
112172
113- res . json ( { workerUrl : `https://session-${ pod } .${ DOMAIN } ` , previewUrl : `https://preview-${ pod } .${ DOMAIN } ` } ) ;
173+ res . json ( {
174+ sessionUrl : `https://session-${ pod } .${ DOMAIN } ` ,
175+ previewUrl : `https://preview-${ pod } .${ DOMAIN } ` ,
176+ workerUrl : `http://worker-${ pod } .${ DOMAIN } `
177+ } ) ;
114178} ) ;
115179
116180app . listen ( 7001 , ( ) => {
0 commit comments