Overview
The K8s Scheduler server is a Go HTTP server that provides the backend API for deployment management, authentication, authorization, billing, and Kubernetes integration. It uses session-based authentication, role-based access control (RBAC), and integrates with Kubernetes via the operator pattern.Architecture
Server Entry Point
The server is initialized incmd/server/main.go with dependency injection:
cmd/server/main.go:37-56
cmd/server/main.go:37-62
Key Initialization Steps
- Load configuration from environment variables
- Run migrations (if
--migrate-onlyflag orAUTO_MIGRATE=true) - Initialize OAuth provider (Google)
- Open database connection with connection pooling
- Initialize session store (memory, PostgreSQL, or Redis)
- Create Kubernetes clients (if enabled)
- Initialize secrets store (Vault or AWS Secrets Manager)
- Setup billing (Stripe integration)
- Start background workers (template sync, image watcher, status syncers)
HTTP Handler
The main HTTP handler is defined ininternal/server/server.go:
internal/server/server.go:100-128
internal/server/server.go:100-128
Routing
Routes are registered in theroutes() method:
internal/server/server.go:211-310
internal/server/server.go:211-310
Route Categories
Health & Config
Health & Config
GET /health- Health check (liveness probe)GET /readyz- Readiness check (readiness probe)GET /api/config- Public configuration (feature flags)
Authentication
Authentication
GET /login- Initiate OAuth flowGET /oauth2/callback- OAuth callback handlerPOST /logout- Logout and clear sessionGET /dev/login- Dev mode login (bypass OAuth)
Deployments
Deployments
GET /api/deployments- List user deploymentsPOST /api/deployments- Create deploymentPOST /api/deployments/delete- Delete deploymentGET /api/deployments/status- Get deployment statusGET/PUT /api/deployments/config- Deployment configurationPOST /api/deployments/restart- Restart deploymentPOST /api/deployments/deploy-image- Deploy custom image
Templates
Templates
GET /api/templates- List templatesPOST /api/templates- Create template (admin)PUT /api/templates/:id- Update template (admin)DELETE /api/templates/:id- Delete template (admin)POST /api/templates/generate- AI-generated templates
Secrets
Secrets
GET /api/secrets- List user secretsPOST /api/secrets- Create secretPUT /api/secrets/:id- Update secretDELETE /api/secrets/:id- Delete secretGET /api/org/secrets- Org-wide secrets
Tasks & Workflows
Tasks & Workflows
GET /api/tasks- List tasksPOST /api/tasks- Create ephemeral taskGET /api/tasks/:id- Get task statusGET /api/workflows- List workflowsPOST /api/workflows- Create workflowGET /api/workflows/:id- Get workflow status
Sandboxes
Sandboxes
GET /api/sandboxes- List sandboxesPOST /api/sandboxes- Create sandboxGET /api/sandboxes/:id- Get sandbox detailsDELETE /api/sandboxes/:id- Delete sandbox
RBAC & Teams
RBAC & Teams
GET /api/me- Current user infoGET /api/orgs- List user organizationsPOST /api/orgs- Create organizationGET /api/teams- List teamsPOST /api/teams- Create teamPOST /api/teams/:id/invite- Invite team member
Billing
Billing
GET /api/billing/session- Create Stripe checkout sessionPOST /api/billing/webhook- Stripe webhookGET /api/public/plans- Available subscription plans
Admin
Admin
GET /api/admin/users- List all users (platform admin)GET /api/admin/metrics- Platform metricsGET /api/clients- White-label client management
Middleware Stack
The server uses a composable middleware stack defined ininternal/middleware/:
Core Middleware
internal/middleware/ directory
Middleware Application
internal/server/server.go:379-396
internal/server/server.go:379-396
Request Flow
- Request Logger - Logs all requests with status and duration
- Metrics - Records Prometheus metrics
- Security Headers - Adds CSP, HSTS, X-Frame-Options, etc.
- Recovery - Catches panics and returns 500
- Request ID - Generates/extracts correlation ID
- Rate Limiter - Token bucket rate limiting
- Authentication - Session or API key validation
- Authorization - RBAC permission checks
- Handler - Business logic
Authentication
Session-Based Auth
The server supports multiple session backends:cmd/server/main.go:98-140
cmd/server/main.go:98-140
OAuth Flow
- Login (
/login) - Generates state token and redirects to Google OAuth - Callback (
/oauth2/callback) - Validates state, exchanges code for token, fetches profile - User Creation - Creates user record, assigns tier, creates org/team
- Session Creation - Creates session and sets secure cookie
- Redirect - Redirects to dashboard or invite page
internal/server/server.go:412-859
API Key Auth
API endpoints support both session and API key authentication:Authorization
RBAC System
The server implements a hierarchical RBAC system:Permission Scopes
internal/server/server.go:240-243
Database Layer
The database layer is ininternal/db/:
internal/db/ directory structure
Database Operations
- User management
- Organization/team CRUD
- Deployment tracking
- Session storage (PostgreSQL backend)
- Billing records
- Audit logs
Kubernetes Integration
The server integrates with Kubernetes using two client types:Controller-Runtime Client
For operator pattern (CRDs):cmd/server/main.go:160-186
cmd/server/main.go:160-186
Standard Clientset
For pod logs and exec:cmd/server/main.go:206-209
Background Workers
The server runs several background workers:Template Syncer
Syncs templates from database to Kubernetes ConfigMap:cmd/server/main.go:295-307
cmd/server/main.go:295-307
Image Watcher
Watches ECR for image updates and auto-updates deployments:cmd/server/main.go:309-311
cmd/server/main.go:309-311
Task Status Syncer
Syncs AgentTask CR status to database:cmd/server/main.go:313-320
cmd/server/main.go:313-320
Workflow Status Syncer
Syncs Workflow CR status to database:cmd/server/main.go:322-329
cmd/server/main.go:322-329
Sandbox Status Syncer
Syncs SandboxClaim CR status to database:cmd/server/main.go:331-344
cmd/server/main.go:331-344
Secrets Management
The server supports multiple secrets backends:Vault Integration
cmd/server/main.go:232-254
cmd/server/main.go:232-266
Secret Injection
Secrets are injected into deployments via External Secrets Operator:- Org secrets: Injected into all deployments in the org
- Deployment secrets: Injected into all services in the deployment
- Service secrets: Injected only into the specified service
Billing Integration
The server integrates with Stripe for subscription management:cmd/server/main.go:288-292
cmd/server/main.go:288-292
Billing Features
- Stripe Checkout for subscription creation
- Webhook handling for payment events
- Usage-based billing tracking
- Tier limits enforcement
Observability
Metrics Server
Prometheus metrics are exposed on a separate port:cmd/server/main.go:356-379
cmd/server/main.go:356-379
Request Logging
All requests are logged with:- Request ID (correlation)
- User ID and email
- HTTP method and path
- Status code
- Duration
- Client IP
Graceful Shutdown
cmd/server/main.go:381-400
cmd/server/main.go:381-400
Configuration
Server configuration is loaded from environment variables:DATABASE_DSN- PostgreSQL connection stringGOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET- OAuth credentialsSESSION_BACKEND- Session store backend (memory/postgres/redis)KUBERNETES_ENABLED- Enable K8s integrationVAULT_ADDR/VAULT_TOKEN- Vault secrets integrationSTRIPE_KEY/STRIPE_WEBHOOK_SECRET- Billing integrationDEPLOYMENT_DOMAIN- Domain for ingresses
internal/config/config.go for full configuration options.
Related Documentation
Frontend Architecture
React 19 frontend with TypeScript and TanStack Query
Operator
Kubernetes operator for deployment reconciliation