wishforge-ide/apps/provisioner/workspaces.mjs
eust-w e89f11b396 feat: run the cloud IDE on the local k3s cluster
The local cluster deploys the IDE gateway, and the provisioner creates a
Theia workspace with an OCT sidecar per project in the tenant namespace.
`build` adds the ide, ide-gateway and oct-workspace images; `web`
forwards the workbench (3100) and the gateway (3101).

- On a local development cluster (WF_ALLOW_INSECURE_HTTP), the
  provisioner accepts the in-cluster HTTP platform Git and the localhost
  gateway for workspaces.
- Builds use a proxy from the host environment, at the address the VM
  sees the host at.
- `web` refuses ports already taken on the host, where kubectl would
  otherwise listen on ::1 only. Its defaults stay clear of the local
  development stack (3000, 4400).
- Pods reading the settings or secrets carry a digest of them, so a
  redeploy with changed settings rolls them.
- The IDE build context leaves out every local node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 23:24:04 +08:00

291 lines
11 KiB
JavaScript

import { createHash } from 'node:crypto';
import { spawn } from 'node:child_process';
import { runKubectl } from './kubernetes.mjs';
const safe = /^[a-zA-Z0-9_-]{1,128}$/;
/** HTTPS, or plain HTTP on a local development cluster (WF_ALLOW_INSECURE_HTTP) inside the cluster. */
const secureEnough = (config, url) =>
url.protocol === 'https:' || (url.protocol === 'http:' && config.insecureHttp === true);
/** The project's platform repository; the workspace accepts credentials for it alone. */
function workspaceRepository(config, input) {
if (input.repositoryUrl === undefined) return undefined;
let url;
try {
url = new URL(input.repositoryUrl);
} catch {
throw new Error('INVALID_WORKSPACE_REPOSITORY');
}
if (
!secureEnough(config, url) ||
url.username ||
url.password ||
url.search ||
url.hash ||
url.pathname !== `/git/${input.tenantId}/${input.projectId}.git`
)
throw new Error('INVALID_WORKSPACE_REPOSITORY');
return url.href;
}
export function renderWorkspaceResources(config, input, image, collaboration) {
if (!image || !/^[^\s]+@sha256:[a-f0-9]{64}$/.test(image)) throw new Error('UNCONFIGURED_THEIA_IMAGE');
if (
!input ||
input.tenantId !== config.tenantId ||
!safe.test(input.projectId ?? '') ||
!safe.test(input.workspaceId ?? '') ||
Object.keys(input).some(k => !['tenantId', 'projectId', 'workspaceId', 'repositoryUrl'].includes(k))
)
throw new Error('INVALID_WORKSPACE_REQUEST');
const repository = workspaceRepository(config, input);
const name = `wf-ide-${createHash('sha256').update(`${input.tenantId}/${input.workspaceId}`).digest('hex').slice(0, 24)}`;
const labels = {
'wishforge.io/component': 'ide',
'wishforge.io/workspace': name,
'app.kubernetes.io/managed-by': 'wishforge-provisioner',
};
const metadata = n => ({ name: n, namespace: config.namespace, labels });
const resources = [
{
apiVersion: 'v1',
kind: 'PersistentVolumeClaim',
metadata: metadata(name),
spec: {
storageClassName: config.storageClass,
accessModes: ['ReadWriteOnce'],
resources: { requests: { storage: config.storageSize } },
},
},
{
apiVersion: 'networking.k8s.io/v1',
kind: 'NetworkPolicy',
metadata: metadata(`${name}-isolation`),
spec: {
podSelector: { matchLabels: { 'wishforge.io/workspace': name } },
policyTypes: ['Ingress', 'Egress'],
ingress: [
{
from: [
{
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } },
podSelector: { matchLabels: { 'wishforge.io/component': 'workspace-gateway' } },
},
],
ports: [{ protocol: 'TCP', port: 3000 }],
},
],
egress: [
{
to: [
{
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' } },
podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } },
},
],
ports: [
{ protocol: 'UDP', port: 53 },
{ protocol: 'TCP', port: 53 },
],
},
],
},
},
{
apiVersion: 'apps/v1',
kind: 'Deployment',
metadata: metadata(name),
spec: {
replicas: 1,
strategy: { type: 'Recreate' },
selector: { matchLabels: { 'wishforge.io/workspace': name } },
template: {
metadata: { labels },
spec: {
...(config.runtimeClass ? { runtimeClassName: config.runtimeClass } : {}),
automountServiceAccountToken: false,
securityContext: {
runAsNonRoot: true,
runAsUser: 1000,
runAsGroup: 1000,
fsGroup: 1000,
seccompProfile: { type: 'RuntimeDefault' },
},
initContainers: [
{
name: 'workspace-directories',
image,
command: [
'node',
'-e',
"const fs=require('fs');for(const dir of ['workspace','config'])fs.mkdirSync('/home/theia/data/'+dir,{recursive:true});",
],
securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } },
volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }],
},
],
containers: [
{
name: 'theia',
image,
ports: [{ containerPort: 3000 }],
env: [
{ name: 'NODE_ENV', value: 'production' },
{ name: 'WISHFORGE_GATEWAY_ENFORCED', value: 'true' },
{ name: 'WISHFORGE_WORKSPACE_PATH', value: '/home/theia/data/workspace' },
{ name: 'THEIA_CONFIG_DIR', value: '/home/theia/data/config' },
{ name: 'WISHFORGE_IDE_HOST', value: '0.0.0.0' },
{ name: 'WISHFORGE_IDE_PORT', value: '3000' },
{ name: 'WISHFORGE_COLLABORATION_ENABLED', value: 'false' },
{ name: 'COLLABORATION_SERVER_URL', value: 'https://disabled.invalid/' },
],
resources: { requests: { cpu: '500m', memory: '1Gi' }, limits: { cpu: '2', memory: '4Gi' } },
securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } },
readinessProbe: { httpGet: { path: '/', port: 3000 }, initialDelaySeconds: 10, periodSeconds: 5 },
volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }],
},
],
volumes: [{ name: 'workspace', persistentVolumeClaim: { claimName: name } }],
},
},
},
},
{
apiVersion: 'v1',
kind: 'Service',
metadata: metadata(name),
spec: {
selector: { 'wishforge.io/workspace': name },
type: 'ClusterIP',
ports: [{ port: 3000, targetPort: 3000 }],
},
},
];
const workspacePod = resources.find(r => r.kind === 'Deployment').spec.template.spec;
workspacePod.containers[0].securityContext.readOnlyRootFilesystem = true;
workspacePod.containers[0].env.push(
{ name: 'HOME', value: '/home/theia/data' },
{ name: 'WF_TENANT_ID', value: input.tenantId },
{ name: 'WF_WORKSPACE_ID', value: input.workspaceId },
);
workspacePod.volumes.push({ name: 'temporary', emptyDir: { sizeLimit: '2Gi' } });
workspacePod.containers[0].volumeMounts.push({ name: 'temporary', mountPath: '/tmp' });
if (repository) {
// The gateway delivers a short-lived token while the IDE is open; it is kept in memory only.
workspacePod.containers[0].env.push(
{ name: 'WISHFORGE_GIT_REMOTE', value: repository },
{ name: 'WISHFORGE_GIT_CREDENTIAL_DIR', value: '/run/wishforge/git' },
);
workspacePod.volumes.push({ name: 'git-credentials', emptyDir: { medium: 'Memory', sizeLimit: '1Mi' } });
workspacePod.containers[0].volumeMounts.push({ name: 'git-credentials', mountPath: '/run/wishforge/git' });
resources
.find(r => r.kind === 'NetworkPolicy')
.spec.egress.push({
to: [
{
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } },
podSelector: { matchLabels: { 'wishforge.io/component': 'git-storage' } },
},
],
ports: [{ protocol: 'TCP', port: config.gitServicePort ?? 4504 }],
});
}
let collaborationUpstreamUrl;
if (collaboration) {
if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(collaboration.image ?? ''))
throw new Error('UNCONFIGURED_OCT_WORKSPACE_IMAGE');
const gateway = new URL(collaboration.gatewayBase);
if (
!secureEnough(config, gateway) ||
gateway.username ||
gateway.password ||
gateway.pathname !== '/' ||
gateway.search ||
gateway.hash
)
throw new Error('INVALID_PUBLIC_WORKSPACE_GATEWAY');
const pod = resources.find(r => r.kind === 'Deployment').spec.template.spec;
pod.containers[0].env.push({
name: 'WF_COLLABORATION_GATEWAY_URL',
value: `${gateway.origin}/workspaces/${input.workspaceId}/collaboration/`,
});
pod.containers.push({
name: 'collaboration',
image: collaboration.image,
env: [
{ name: 'WF_TENANT_ID', value: input.tenantId },
{ name: 'WF_WORKSPACE_ID', value: input.workspaceId },
{ name: 'WISHFORGE_GATEWAY_ENFORCED', value: 'true' },
],
ports: [{ containerPort: 8100 }],
resources: { requests: { cpu: '100m', memory: '128Mi' }, limits: { cpu: '500m', memory: '512Mi' } },
securityContext: {
allowPrivilegeEscalation: false,
readOnlyRootFilesystem: true,
capabilities: { drop: ['ALL'] },
},
readinessProbe: { httpGet: { path: '/api/meta', port: 8100 }, initialDelaySeconds: 3, periodSeconds: 5 },
});
resources.find(r => r.kind === 'Service').spec.ports = [
{ name: 'theia', port: 3000, targetPort: 3000 },
{ name: 'collaboration', port: 8100, targetPort: 8100 },
];
resources.find(r => r.kind === 'NetworkPolicy').spec.ingress[0].ports.push({ protocol: 'TCP', port: 8100 });
collaborationUpstreamUrl = `http://${name}.${config.namespace}.svc:8100`;
}
return { name, resources, upstreamUrl: `http://${name}.${config.namespace}.svc:3000`, collaborationUpstreamUrl };
}
async function observe(config, name) {
return new Promise((resolve, reject) => {
const child = spawn(
'kubectl',
['--context', config.context, '--namespace', config.namespace, 'get', 'deployment', name, '-o', 'json'],
{ stdio: ['ignore', 'pipe', 'pipe'] },
);
let stdout = '';
const timer = setTimeout(() => {
child.kill('SIGTERM');
reject(new Error('KUBERNETES_OBSERVE_TIMEOUT'));
}, 10000);
child.stderr.resume();
child.stdout.on('data', b => {
stdout += b;
if (stdout.length > 1000000) child.kill('SIGTERM');
});
child.on('error', () => {
clearTimeout(timer);
reject(new Error('KUBERNETES_OBSERVE_FAILED'));
});
child.on('close', code => {
clearTimeout(timer);
try {
if (code !== 0) throw new Error();
const d = JSON.parse(stdout);
resolve(
d.status?.observedGeneration >= d.metadata.generation &&
d.status?.availableReplicas === 1 &&
d.status?.updatedReplicas === 1,
);
} catch {
reject(new Error('KUBERNETES_OBSERVE_FAILED'));
}
});
});
}
export async function provisionWorkspace(config, input, options = {}) {
const collaboration =
options.collaboration ??
(process.env.WF_OCT_WORKSPACE_IMAGE
? { image: process.env.WF_OCT_WORKSPACE_IMAGE, gatewayBase: process.env.WF_IDE_PUBLIC_URL }
: undefined);
const rendered = renderWorkspaceResources(config, input, options.image ?? process.env.WF_THEIA_IMAGE, collaboration);
await (options.apply ?? runKubectl)(config, rendered.resources);
const ready = await (options.observe ?? observe)(config, rendered.name);
return {
status: ready ? 'ready' : 'pending',
workspaceId: input.workspaceId,
upstreamUrl: rendered.upstreamUrl,
collaborationUpstreamUrl: rendered.collaborationUpstreamUrl,
};
}