Skip to main content

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 in cmd/server/main.go with dependency injection:
cmd/server/main.go:37-56
Source: cmd/server/main.go:37-62

Key Initialization Steps

  1. Load configuration from environment variables
  2. Run migrations (if --migrate-only flag or AUTO_MIGRATE=true)
  3. Initialize OAuth provider (Google)
  4. Open database connection with connection pooling
  5. Initialize session store (memory, PostgreSQL, or Redis)
  6. Create Kubernetes clients (if enabled)
  7. Initialize secrets store (Vault or AWS Secrets Manager)
  8. Setup billing (Stripe integration)
  9. Start background workers (template sync, image watcher, status syncers)

HTTP Handler

The main HTTP handler is defined in internal/server/server.go:
internal/server/server.go:100-128
Source: internal/server/server.go:100-128

Routing

Routes are registered in the routes() method:
internal/server/server.go:211-310
Source: internal/server/server.go:211-310

Route Categories

  • GET /health - Health check (liveness probe)
  • GET /readyz - Readiness check (readiness probe)
  • GET /api/config - Public configuration (feature flags)
  • GET /login - Initiate OAuth flow
  • GET /oauth2/callback - OAuth callback handler
  • POST /logout - Logout and clear session
  • GET /dev/login - Dev mode login (bypass OAuth)
  • GET /api/deployments - List user deployments
  • POST /api/deployments - Create deployment
  • POST /api/deployments/delete - Delete deployment
  • GET /api/deployments/status - Get deployment status
  • GET/PUT /api/deployments/config - Deployment configuration
  • POST /api/deployments/restart - Restart deployment
  • POST /api/deployments/deploy-image - Deploy custom image
  • GET /api/templates - List templates
  • POST /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
  • GET /api/secrets - List user secrets
  • POST /api/secrets - Create secret
  • PUT /api/secrets/:id - Update secret
  • DELETE /api/secrets/:id - Delete secret
  • GET /api/org/secrets - Org-wide secrets
  • GET /api/tasks - List tasks
  • POST /api/tasks - Create ephemeral task
  • GET /api/tasks/:id - Get task status
  • GET /api/workflows - List workflows
  • POST /api/workflows - Create workflow
  • GET /api/workflows/:id - Get workflow status
  • GET /api/sandboxes - List sandboxes
  • POST /api/sandboxes - Create sandbox
  • GET /api/sandboxes/:id - Get sandbox details
  • DELETE /api/sandboxes/:id - Delete sandbox
  • GET /api/me - Current user info
  • GET /api/orgs - List user organizations
  • POST /api/orgs - Create organization
  • GET /api/teams - List teams
  • POST /api/teams - Create team
  • POST /api/teams/:id/invite - Invite team member
  • GET /api/billing/session - Create Stripe checkout session
  • POST /api/billing/webhook - Stripe webhook
  • GET /api/public/plans - Available subscription plans
  • GET /api/admin/users - List all users (platform admin)
  • GET /api/admin/metrics - Platform metrics
  • GET /api/clients - White-label client management

Middleware Stack

The server uses a composable middleware stack defined in internal/middleware/:

Core Middleware

Source: internal/middleware/ directory

Middleware Application

internal/server/server.go:379-396
Source: internal/server/server.go:379-396

Request Flow

  1. Request Logger - Logs all requests with status and duration
  2. Metrics - Records Prometheus metrics
  3. Security Headers - Adds CSP, HSTS, X-Frame-Options, etc.
  4. Recovery - Catches panics and returns 500
  5. Request ID - Generates/extracts correlation ID
  6. Rate Limiter - Token bucket rate limiting
  7. Authentication - Session or API key validation
  8. Authorization - RBAC permission checks
  9. Handler - Business logic

Authentication

Session-Based Auth

The server supports multiple session backends:
cmd/server/main.go:98-140
Source: cmd/server/main.go:98-140

OAuth Flow

  1. Login (/login) - Generates state token and redirects to Google OAuth
  2. Callback (/oauth2/callback) - Validates state, exchanges code for token, fetches profile
  3. User Creation - Creates user record, assigns tier, creates org/team
  4. Session Creation - Creates session and sets secure cookie
  5. Redirect - Redirects to dashboard or invite page
Source: internal/server/server.go:412-859

API Key Auth

API endpoints support both session and API key authentication:
This allows programmatic access via API keys while maintaining session-based auth for the UI.

Authorization

RBAC System

The server implements a hierarchical RBAC system:

Permission Scopes

Source: internal/server/server.go:240-243

Database Layer

The database layer is in internal/db/:
Source: 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
Source: cmd/server/main.go:160-186

Standard Clientset

For pod logs and exec:
Source: 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
Source: cmd/server/main.go:295-307

Image Watcher

Watches ECR for image updates and auto-updates deployments:
cmd/server/main.go:309-311
Source: cmd/server/main.go:309-311

Task Status Syncer

Syncs AgentTask CR status to database:
cmd/server/main.go:313-320
Source: cmd/server/main.go:313-320

Workflow Status Syncer

Syncs Workflow CR status to database:
cmd/server/main.go:322-329
Source: cmd/server/main.go:322-329

Sandbox Status Syncer

Syncs SandboxClaim CR status to database:
cmd/server/main.go:331-344
Source: cmd/server/main.go:331-344

Secrets Management

The server supports multiple secrets backends:

Vault Integration

cmd/server/main.go:232-254
Source: 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
Source: 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
Source: 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
Source: cmd/server/main.go:381-400

Configuration

Server configuration is loaded from environment variables:
  • DATABASE_DSN - PostgreSQL connection string
  • GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET - OAuth credentials
  • SESSION_BACKEND - Session store backend (memory/postgres/redis)
  • KUBERNETES_ENABLED - Enable K8s integration
  • VAULT_ADDR / VAULT_TOKEN - Vault secrets integration
  • STRIPE_KEY / STRIPE_WEBHOOK_SECRET - Billing integration
  • DEPLOYMENT_DOMAIN - Domain for ingresses
See internal/config/config.go for full configuration options.

Frontend Architecture

React 19 frontend with TypeScript and TanStack Query

Operator

Kubernetes operator for deployment reconciliation