working orchestrator

This commit is contained in:
Ishaan Dey 2024-05-13 13:22:11 -07:00
parent 6a6ddf5363
commit aef8105bb0
4 changed files with 48 additions and 40 deletions

View File

@ -15,17 +15,17 @@ spec:
app: <SANDBOX> app: <SANDBOX>
spec: spec:
volumes: volumes:
- name: workspace-volume - name: projects-volume
emptyDir: {} emptyDir: {}
containers: containers:
- name: runner - name: sandbox
image: ishaan1013/sandbox:latest image: ishaan1013/sandbox:latest
ports: ports:
- containerPort: 4000 - containerPort: 4000
- containerPort: 8000 - containerPort: 3000
volumeMounts: volumeMounts:
- name: workspace-volume - name: projects-volume
mountPath: /workspace mountPath: /projects
resources: resources:
requests: requests:
cpu: "1" cpu: "1"
@ -53,14 +53,16 @@ spec:
port: 4000 port: 4000
targetPort: 4000 targetPort: 4000
- protocol: TCP - protocol: TCP
name: s name: user
port: 8000 port: 3000
targetPort: 8000 targetPort: 3000
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: <SANDBOX> name: <SANDBOX>
annotations:
kubernetes.io/ingress.class: "nginx"
spec: spec:
ingressClassName: nginx ingressClassName: nginx
rules: rules:
@ -83,4 +85,4 @@ spec:
service: service:
name: <SANDBOX> name: <SANDBOX>
port: port:
number: 8000 number: 3000

View File

@ -18,11 +18,11 @@ const port = process.env.PORT || 4001
app.use(express.json()) app.use(express.json())
dotenv.config() dotenv.config()
const corsOptions = { // const corsOptions = {
origin: ['http://localhost:3000', 'https://s.ishaand.com', 'http://localhost:4000', /\.ws\.ishaand\.com$/], // origin: ['http://localhost:3000', 'https://s.ishaand.com', 'http://localhost:4000', /\.ws\.ishaand\.com$/],
} // }
// app.use(cors(corsOptions)) // app.use(cors(corsOptions))
app.use(cors()) app.use(cors())
const kubeconfig = new KubeConfig() const kubeconfig = new KubeConfig()
@ -35,14 +35,12 @@ if (process.env.NODE_ENV === "production") {
caData: process.env.GKE_CLUSTER_CA_DATA, caData: process.env.GKE_CLUSTER_CA_DATA,
} }
], ],
users: [ users: [
{ {
name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster', name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster',
exec: { exec: {
apiVersion: 'client.authentication.k8s.io/v1beta1', apiVersion: 'client.authentication.k8s.io/v1beta1',
command: 'gke-gcloud-auth-plugin', 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', 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', interactiveMode: 'IfAvailable',
provideClusterInfo: true provideClusterInfo: true
@ -51,17 +49,12 @@ if (process.env.NODE_ENV === "production") {
], ],
contexts: [ contexts: [
{ {
name: 'docker-desktop', name: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster',
cluster: 'docker-desktop', cluster: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster',
user: 'docker-desktop' user: 'gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster'
},
{
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'
} }
], ],
currentContext: "gke_sylvan-epoch-422219-f9_us-central1_sandbox", currentContext: "gke_sylvan-epoch-422219-f9_us-central1_sandbox-cluster"
}); });
} }
kubeconfig.loadFromDefault() 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 promises = kubeManifests.map(async (manifest) => {
const { kind, metadata: { name } } = manifest const { kind, metadata: { name } } = manifest
if (kind === "Deployment") console.log("Kind:", kind)
if (!(await resourceExists(appsV1Api, 'readNamespacedDeployment', name))) {
await appsV1Api.createNamespacedDeployment(namespace, manifest) switch (manifest.kind) {
console.log("Made deploymnet") case 'Deployment':
} return createResource(appsV1Api, 'Deployment', manifest);
else if (kind === "Service") case 'Service':
if (!(await resourceExists(coreV1Api, 'readNamespacedService', name))) { return createResource(coreV1Api, 'Service', manifest);
await coreV1Api.createNamespacedService(namespace, manifest) case 'Ingress':
console.log("Made service") return createResource(networkingV1Api, 'Ingress', manifest);
} default:
else if (kind === "Ingress") console.error("Unsupported kind:", manifest.kind);
if (!(await resourceExists(networkingV1Api, 'readNamespacedIngress', name))) { return Promise.reject("Unsupported kind: " + manifest.kind);
await networkingV1Api.createNamespacedIngress(namespace, manifest) }
console.log("Made ingress")
}
}) })
await Promise.all(promises) await Promise.all(promises)

View File

@ -22,6 +22,10 @@ const getSandboxData = async (id: string) => {
}; };
const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => { const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => {
if (!usersToSandboxes) {
return [];
}
const shared = await Promise.all( const shared = await Promise.all(
usersToSandboxes.map(async (user) => { usersToSandboxes.map(async (user) => {
const userRes = await fetch( const userRes = await fetch(

View File

@ -19,6 +19,7 @@ export default function Editor({
userData: User; userData: User;
sandboxData: Sandbox; sandboxData: Sandbox;
}) { }) {
console.log("userData", userData);
const [isServerRunning, setIsServerRunning] = useState(false); const [isServerRunning, setIsServerRunning] = useState(false);
const [didFail, setDidFail] = useState(false); const [didFail, setDidFail] = useState(false);