notebooksupervisor

package
v0.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package notebooksupervisor manages per-workspace Jupyter Server Deployments + Services in Kubernetes. EnsureRunning spawns on first call, returns a cached handle on subsequent calls. Touch refreshes lastActive so idle workspaces get reaped after Config.IdleTTL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDeployment

func BuildDeployment(k Key, c Config) (*appsv1.Deployment, error)

BuildDeployment produces a fresh Deployment spec for a notebook pod. Pure function — no I/O. Returns an error if the Key has an invalid workspace id.

func BuildService

func BuildService(k Key) (*corev1.Service, error)

BuildService returns a ClusterIP Service that fronts the Deployment.

func ServiceURL

func ServiceURL(k Key) string

ServiceURL returns the cluster-internal HTTP url for the Service.

Types

type Config

type Config struct {
	Image            string // e.g. "ghcr.io/agentserver/agentserver-notebook:dev"
	ImagePullPolicy  string // e.g. "Always" or "IfNotPresent"
	CPURequest       string // k8s quantity, e.g. "500m"
	CPULimit         string // e.g. "2"
	MemoryRequest    string // e.g. "1Gi"
	MemoryLimit      string // e.g. "4Gi"
	EphemeralStorage string // e.g. "5Gi"
	WorkspacePVCName string // mounted at /workspace inside the pod
	// ExtraEnvVars are injected into the notebook container env. Values
	// may contain `{workspace_id}` which BuildDeployment substitutes with
	// the Key.WorkspaceID. Keys are passed through as-is.
	ExtraEnvVars map[string]string
	ReadyTimeout time.Duration // how long EnsureRunning blocks waiting for ReadyReplicas >= 1
	IdleTTL      time.Duration // idle threshold for reaper
	ReapInterval time.Duration // how often the reaper loop ticks
}

Config tunes Supervisor lifetime + per-Deployment template.

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults returns a copy of c with zero-valued fields replaced by safe defaults. Always returns a Config; never errors.

type Handle

type Handle struct {
	ServiceURL string
}

Handle is what callers get back from EnsureRunning. ServiceURL is the cluster-internal HTTP url to reach the Jupyter Server.

type Key

type Key struct {
	WorkspaceID string
	Namespace   string
}

Key identifies one workspace's notebook Deployment. Namespace is the k8s namespace the Deployment + Service live in (typically per-workspace).

func (Key) DeploymentName

func (k Key) DeploymentName() string

DeploymentName returns the Deployment + Service name (they share one). Assumes the workspace id is already k8s-safe; use SafeDeploymentName when you need explicit validation.

func (Key) SafeDeploymentName

func (k Key) SafeDeploymentName() (string, error)

SafeDeploymentName validates the workspaceID against k8s DNS-1123 rules AND the 63-char Deployment name limit. Returns the prefixed name on success.

type Supervisor

type Supervisor struct {
	// contains filtered or unexported fields
}

Supervisor owns the per-workspace lifecycle map. Concurrent EnsureRunning calls for the same Key are serialised; a single in-memory map is the source of truth for lastActive.

func New

func New(k8s kubernetes.Interface, cfg Config, logger *slog.Logger) *Supervisor

New constructs a Supervisor. logger may be nil (defaults to slog.Default).

func (*Supervisor) EnsureRunning

func (s *Supervisor) EnsureRunning(ctx context.Context, k Key) (*Handle, error)

EnsureRunning creates the Deployment + Service if absent, then blocks up to Config.ReadyTimeout waiting for ReadyReplicas >= 1. Returns a cached Handle on subsequent calls.

func (*Supervisor) LastActive

func (s *Supervisor) LastActive(k Key) time.Time

LastActive returns the last activity timestamp for k, or zero time if the workspace isn't tracked.

func (*Supervisor) StartReaper

func (s *Supervisor) StartReaper(ctx context.Context)

StartReaper runs the idle reap loop. Returns when ctx is cancelled. Each tick: snapshot idle Keys (lastActive + IdleTTL < now), call Stop on each. Errors are logged, never propagated.

func (*Supervisor) Stop

func (s *Supervisor) Stop(ctx context.Context, k Key) error

Stop deletes the Deployment + Service. 404 is treated as success.

func (*Supervisor) Touch

func (s *Supervisor) Touch(k Key)

Touch updates lastActive for k. No-op if the workspace isn't tracked.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL